-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgit_rails
executable file
·164 lines (147 loc) · 4.02 KB
/
git_rails
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/bash
function help {
echo -e "\nUsage:"
echo -e "\tgit-rails [-d] [-v] [-y] [src_ref] [dest_ref]"
echo -e "\tgit-rails -h"
echo "Flags:"
echo -e "\t-d\tdry-run: display pending changes and exit"
echo -e "\t-v\tverbose: display pending changes, prompt to continue, display command output"
echo -e "\t-y\tauto-confirm: in verbose mode, apply pending changes without prompting"
echo
echo -e "\tsrc_ref\t\tBranch/ref you're coming from; defaults to ORIG_HEAD"
echo
echo -e "\tdest_ref\tBranch/ref you're ending at; defaults to HEAD"
echo -e "\t\t\tUse with care; see README for more details"
echo
echo -e "Web: https://github.com/brysgo/git-rails"
echo
exit 0
}
dry_run=0
verbose=0
confirm=0
out_target="/dev/null"
optspec=":vdyh-:"
while getopts "$optspec" optchar; do
case "${optchar}" in
-)
case "${OPTARG}" in
dry-run | dry_run)
dry_run=1
verbose=1
;;
verbose)
verbose=1
confirm=1
out_target="/dev/stdout"
;;
auto-confirm | auto_confirm)
confirm_opt=0
;;
help)
help
;;
esac;;
d)
dry_run=1
verbose=1
;;
v)
verbose=1
confirm=1
out_target="/dev/stdout"
;;
y)
confirm_opt=0
;;
h)
help
;;
esac
done
if [ "$verbose" -eq 1 -a ! -z "$confirm_opt" ]; then
confirm=$confirm_opt
fi
shift $((OPTIND -1))
old_ref=$1
new_ref=$2
if [ -z "$old_ref" ]; then
old_ref=`git rev-parse ORIG_HEAD`
fi
if [ -z "$new_ref" ]; then
new_ref=`git rev-parse HEAD`
fi
# CHECK IF WE NEED TO DO A BUNDLE
bundle_changed=`git diff $old_ref $new_ref -- Gemfile.lock`
# CHECK IF WE NEED TO SPIN SOME YARN
yarn_changed=`git diff $old_ref $new_ref -- yarn.lock`
migrations=`git diff --name-status $old_ref $new_ref -- db/migrate | grep '^[AD]'`
if [ "$verbose" -eq 1 ]; then
if [ ! -z "$bundle_changed" -o ! -z "$yarn_changed" ]; then
echo -e "\nPrep needed:"
if [ ! -z "$bundle_changed" ]; then
echo -e "\t- bundle"
fi
if [ ! -z "$yarn_changed" ]; then
echo -e "\t- yarn"
fi
fi
if [ ! -z "$migrations" ]; then
echo -e "\nMigrations needed:\n${migrations}"
fi
if [ -z "$bundle_changed" -a -z "$yarn_changed" -a -z "$migrations" ]; then
echo -e "\nNo changes necessary\n"
exit 0
fi
echo
if [ "$dry_run" -eq 1 ]; then
exit 0
elif [ "$confirm" -eq 1 ]; then
read -p "Continue? [Yn]: " confirm
[[ $confirm =~ ^[Nn] ]] && echo && exit 0
fi
fi
# okay, now actually run whatever's needed
if [ ! -z "$bundle_changed" ]; then
echo "Your Gemfile.lock has changed, running bundle"
bundle
fi
if [ ! -z "$yarn_changed" ]; then
echo "Your yarn.lock has changed, running yarn"
yarn
fi
if [ ! -z "$migrations" ]; then
echo "Running migrations!"
for migration in $migrations
do
# CHECK THE MIGRATION TYPE AND CONTINUE TO THE FILENAME
if [ $migration == "D" ]; then
migration_type="down"
continue
elif [ $migration == "A" ]; then
migration_type="up"
continue
fi
# BUILD THE MIGRATION COMMAND FROM THE VERSION AND TYPE
version=`echo "$migration" | grep -o 'db/migrate/.*' | cut -d'_' -f1 | cut -d'/' -f3`
migrate_command="ActiveRecord::Migrator.run(:$migration_type, 'db/migrate', $version) rescue nil"
# APPEND OR PREPREND TO THE COMMAND LIST DEPENDING ON MIGRATION TYPE
if [[ $migration_type == "down" ]]; then
# CHECKOUT DOWN MIGRATION AND SAVE PATH FOR CLEANUP
git checkout "$old_ref" -- ":/$migration"
migration_cleanup="$migration_cleanup $(git ls-files :/$migration)"
migrate_commands="$migrate_command;$migrate_commands"
else
migrate_commands="$migrate_commands;$migrate_command"
fi
done
# RUN THE MIGRATIONS (AND TEST PREPARE)
{
echo "$migrate_commands" | bundle exec rails runner /dev/stdin
} > $out_target
# CLEAN UP DOWN MIGRATIONS
if [ ! -z "$migration_cleanup" ]; then
git reset $new_ref -- $migration_cleanup
rm $migration_cleanup
fi
fi