Skip to content

Commit aafd757

Browse files
committed
Add cmd-line options
-v: verbose -y: confirm -d: dry run -h: usage
1 parent 5524b43 commit aafd757

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,19 @@ Or if you really know what you are doing and you want to pretend like you are on
4343

4444
Migrations that were added between `REF_YOU_CAME_FROM` and `REF_YOU_ARE_GOING_TO` that do not exist on your current `HEAD` will not be applied because they don't exist!!
4545

46+
#### Options
47+
48+
The following command line options are supported:
49+
50+
* -v, --verbose
51+
* Display pending changes, prompt to continue, display command output
52+
* -y, --auto_confirm
53+
* Variant of verbose mode; display pending changes but do not prompt before applying them
54+
* -d, --dry_run
55+
* Display pending changes and exit
56+
* -h, --help
57+
* Display usage and exit
58+
4659
#### Hooks
4760

4861
You shouldn't have to think about the hooks once they are installed. Just pull, checkout, and rebase as normal and they should work fine. If you find that `git_rails` hasn't fired when it should follow the command instructions to run it manually.

bin/git_rails

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,65 @@
11
#!/bin/bash
2+
3+
function help {
4+
echo -e "\nUsage:"
5+
echo -e "\tgit-rails [-d] [-v] [-y] [old_ref] [new_ref]"
6+
echo -e "\tgit-rails -h"
7+
echo "Flags:"
8+
echo -e "\t-d\tdry-run: display pending changes and exit"
9+
echo -e "\t-v\tverbose: display pending changes, prompt to continue, display command output"
10+
echo -e "\t-y\tauto-confirm: in verbose mode, do not prompt after displaying pending changes"
11+
echo
12+
exit 0
13+
}
14+
15+
dry_run=0
16+
verbose=0
17+
confirm=0
18+
out_target="/dev/null"
19+
20+
optspec=":vdyh-:"
21+
while getopts "$optspec" optchar; do
22+
case "${optchar}" in
23+
-)
24+
case "${OPTARG}" in
25+
dry-run | dry_run)
26+
dry_run=1
27+
verbose=1
28+
;;
29+
verbose)
30+
verbose=1
31+
confirm=1
32+
out_target="/dev/stdout"
33+
;;
34+
auto-confirm | auto_confirm)
35+
confirm_opt=0
36+
;;
37+
help)
38+
help
39+
;;
40+
esac;;
41+
d)
42+
dry_run=1
43+
verbose=1
44+
;;
45+
v)
46+
verbose=1
47+
confirm=1
48+
out_target="/dev/stdout"
49+
;;
50+
y)
51+
confirm_opt=0
52+
;;
53+
h)
54+
help
55+
;;
56+
esac
57+
done
58+
if [ "$verbose" -eq 1 -a ! -z "$confirm_opt" ]; then
59+
confirm=$confirm_opt
60+
fi
61+
shift $((OPTIND -1))
62+
263
old_ref=$1
364
new_ref=$2
465

@@ -14,12 +75,36 @@ files_changed=`git diff $old_ref $new_ref --name-status`
1475

1576
# CHECK IF WE NEED TO DO A BUNDLE
1677
bundle_changed=`echo "$files_changed" | grep $'M\tGemfile.lock'`
78+
79+
migrations=`git diff --name-status $old_ref $new_ref -- db/migrate | grep '^[AD]'`
80+
81+
if [ "$verbose" -eq 1 ]; then
82+
if [ ! -z "$bundle_changed" ]; then
83+
echo -e "\nPrep needed:"
84+
echo -e "\t- bundle"
85+
fi
86+
if [ ! -z "$migrations" ]; then
87+
echo -e "\nMigrations needed:\n${migrations}"
88+
fi
89+
if [ -z "$bundle_changed" -a -z "$migrations" ]; then
90+
echo -e "\nNo changes necessary\n"
91+
exit 0
92+
fi
93+
echo
94+
if [ "$dry_run" -eq 1 ]; then
95+
exit 0
96+
elif [ "$confirm" -eq 1 ]; then
97+
read -p "Continue? [Yn]: " confirm
98+
[[ $confirm =~ ^[Nn] ]] && echo && exit 0
99+
fi
100+
fi
101+
102+
# okay, now actually run whatever's needed
17103
if [ ! -z "$bundle_changed" ]; then
18104
echo "Your Gemfile.lock has changed, running bundle"
19105
bundle
20106
fi
21107

22-
migrations=`git diff --name-status $old_ref $new_ref -- db/migrate | grep '^[AD]'`
23108
if [ ! -z "$migrations" ]; then
24109
echo "Running migrations!"
25110
for migration in $migrations
@@ -49,7 +134,9 @@ if [ ! -z "$migrations" ]; then
49134
done
50135

51136
# RUN THE MIGRATIONS (AND TEST PREPARE)
52-
echo "$migrate_commands" | bundle exec rails c > /dev/null
137+
{
138+
echo "$migrate_commands" | bundle exec rails c
139+
} > $out_target
53140

54141
# CLEAN UP DOWN MIGRATIONS
55142
if [ ! -z "$migration_cleanup" ]; then

0 commit comments

Comments
 (0)