Skip to content

Commit 3170c5c

Browse files
authored
Merge pull request #9 from jfouse/cmd-line-options
Command line options for dry run and verbose output
2 parents 5524b43 + 7d681ef commit 3170c5c

File tree

2 files changed

+126
-6
lines changed

2 files changed

+126
-6
lines changed

README.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ Manage bundle and migrations automatically while you use git!
66
For example:
77

88
- If you pull down code from your remote and there are new or updated gems
9-
* it will run `bundle` for you
9+
* it will run `bundle` for you
1010
- If you checkout a branch that uses a different set of migrations
11-
* it will roll back the old set and apply the new ones
11+
* it will roll back the old set and apply the new ones
1212
- If you rebase and there are new gems and migrations
13-
* it will run `bundle` and `rake db:migrate test:prepare`
13+
* it will run `bundle` and `rake db:migrate test:prepare`
1414
- If you don't like automagical git hooks
15-
* run `git_rails` and it will try and find out where you came from and run the same check
15+
* run `git_rails` and it will try and find out where you came from and run the same check
1616

1717
### Install
1818

@@ -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+
* Use in conjunction with verbose mode to apply pending changes without prompting
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: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,72 @@
11
#!/bin/bash
2+
3+
function help {
4+
echo -e "\nUsage:"
5+
echo -e "\tgit-rails [-d] [-v] [-y] [src_ref] [dest_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, apply pending changes without prompting"
11+
echo
12+
echo -e "\tsrc_ref\t\tBranch/ref you're coming from; defaults to ORIG_HEAD"
13+
echo
14+
echo -e "\tdest_ref\tBranch/ref you're ending at; defaults to HEAD"
15+
echo -e "\t\t\tUse with care; see README for more details"
16+
echo
17+
echo -e "Web: https://github.com/brysgo/git-rails"
18+
echo
19+
exit 0
20+
}
21+
22+
dry_run=0
23+
verbose=0
24+
confirm=0
25+
out_target="/dev/null"
26+
27+
optspec=":vdyh-:"
28+
while getopts "$optspec" optchar; do
29+
case "${optchar}" in
30+
-)
31+
case "${OPTARG}" in
32+
dry-run | dry_run)
33+
dry_run=1
34+
verbose=1
35+
;;
36+
verbose)
37+
verbose=1
38+
confirm=1
39+
out_target="/dev/stdout"
40+
;;
41+
auto-confirm | auto_confirm)
42+
confirm_opt=0
43+
;;
44+
help)
45+
help
46+
;;
47+
esac;;
48+
d)
49+
dry_run=1
50+
verbose=1
51+
;;
52+
v)
53+
verbose=1
54+
confirm=1
55+
out_target="/dev/stdout"
56+
;;
57+
y)
58+
confirm_opt=0
59+
;;
60+
h)
61+
help
62+
;;
63+
esac
64+
done
65+
if [ "$verbose" -eq 1 -a ! -z "$confirm_opt" ]; then
66+
confirm=$confirm_opt
67+
fi
68+
shift $((OPTIND -1))
69+
270
old_ref=$1
371
new_ref=$2
472

@@ -14,12 +82,49 @@ files_changed=`git diff $old_ref $new_ref --name-status`
1482

1583
# CHECK IF WE NEED TO DO A BUNDLE
1684
bundle_changed=`echo "$files_changed" | grep $'M\tGemfile.lock'`
85+
86+
# CHECK IF WE NEED TO SPIN SOME YARN
87+
yarn_changed=`echo "$files_changed" | grep $'M\tyarn.lock'`
88+
89+
migrations=`git diff --name-status $old_ref $new_ref -- db/migrate | grep '^[AD]'`
90+
91+
if [ "$verbose" -eq 1 ]; then
92+
if [ ! -z "$bundle_changed" -o ! -z "$yarn_changed" ]; then
93+
echo -e "\nPrep needed:"
94+
if [ ! -z "$bundle_changed" ]; then
95+
echo -e "\t- bundle"
96+
fi
97+
if [ ! -z "$yarn_changed" ]; then
98+
echo -e "\t- yarn"
99+
fi
100+
fi
101+
if [ ! -z "$migrations" ]; then
102+
echo -e "\nMigrations needed:\n${migrations}"
103+
fi
104+
if [ -z "$bundle_changed" -a -z "$yarn_changed" -a -z "$migrations" ]; then
105+
echo -e "\nNo changes necessary\n"
106+
exit 0
107+
fi
108+
echo
109+
if [ "$dry_run" -eq 1 ]; then
110+
exit 0
111+
elif [ "$confirm" -eq 1 ]; then
112+
read -p "Continue? [Yn]: " confirm
113+
[[ $confirm =~ ^[Nn] ]] && echo && exit 0
114+
fi
115+
fi
116+
117+
# okay, now actually run whatever's needed
17118
if [ ! -z "$bundle_changed" ]; then
18119
echo "Your Gemfile.lock has changed, running bundle"
19120
bundle
20121
fi
21122

22-
migrations=`git diff --name-status $old_ref $new_ref -- db/migrate | grep '^[AD]'`
123+
if [ ! -z "$yarn_changed" ]; then
124+
echo "Your yarn.lock has changed, running yarn"
125+
yarn
126+
fi
127+
23128
if [ ! -z "$migrations" ]; then
24129
echo "Running migrations!"
25130
for migration in $migrations
@@ -49,7 +154,9 @@ if [ ! -z "$migrations" ]; then
49154
done
50155

51156
# RUN THE MIGRATIONS (AND TEST PREPARE)
52-
echo "$migrate_commands" | bundle exec rails c > /dev/null
157+
{
158+
echo "$migrate_commands" | bundle exec rails c
159+
} > $out_target
53160

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

0 commit comments

Comments
 (0)