Skip to content

Commit 96be0aa

Browse files
committed
Initial commit
0 parents  commit 96be0aa

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Git Rails
2+
=========
3+
4+
Manage bundle and migrations automatically while you use git!
5+
6+
For example:
7+
8+
- If you pull down code from your remote and there are new or updated gems
9+
* it will run `bundle` for you
10+
- 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
12+
- If you rebase and there are new gems and migrations
13+
* it will run `bundle` and `rake db:migrate test:prepare`
14+
- 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
16+
17+
### Install
18+
19+
Copy or symlink:
20+
21+
1. `bin/git_rails` into your executable `$PATH`
22+
2. `hooks/*` into your `.git/hooks/`

bin/git_rails

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
old_ref=$1
3+
new_ref=$2
4+
5+
if [ -z "$old_ref" ]; then
6+
old_ref=`git rev-parse ORIG_HEAD`
7+
fi
8+
9+
if [ -z "$new_ref" ]; then
10+
new_ref=`git rev-parse HEAD`
11+
fi
12+
13+
files_changed=`git diff $old_ref $new_ref --name-status`
14+
migrations_removed=`echo "$files_changed" | egrep 'D\tdb/migrate/([0-9]+)' | sort -r`
15+
migrations_added=`echo "$files_changed" | egrep 'A\tdb/migrate/([0-9]+)'`
16+
17+
# CHECK IF WE NEED TO DO A BUNDLE
18+
bundle_changed=`echo "$files_changed" | egrep 'M\tGemfile.lock'`
19+
if [ ! -z "$bundle_changed" ]; then
20+
echo "Your Gemfile.lock has changed, running bundle"
21+
bundle
22+
fi
23+
24+
if [ ! -z "$migrations_removed" ]; then
25+
echo "Rolling back missing migrations"
26+
for migration in $migrations_removed
27+
do
28+
if [ $migration == "D" ]; then
29+
continue
30+
fi
31+
git checkout "$old_ref" -- "$migration"
32+
version=`echo "$migration" | cut -d'_' -f1 | cut -d'/' -f3`
33+
bundle exec rake db:migrate:down VERSION="$version"
34+
git reset
35+
rm "$migration"
36+
done
37+
bundle exec rake db:test:prepare
38+
git checkout $new_ref -- db/schema.rb
39+
fi
40+
41+
if [ ! -z "$old_ref" ]; then
42+
echo "New migrations have been added running migrations"
43+
bundle exec rake db:migrate db:test:prepare
44+
git checkout $new_ref -- db/schema.rb
45+
fi
46+
47+
# CHECK IF ENVIRONMENT HAS CHANGED
48+
env_erb_changed=`echo "$files_changed" | egrep 'M\t(.env.erb|bin/setup)'`
49+
if [ ! -z "$env_erb_changed" ]; then
50+
echo 'Environment stuff has changed, you should run ./bin/setup!!'
51+
fi

hooks/post-checkout

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
checking_out_branch=$3
3+
old_ref=$1
4+
new_ref=$2
5+
6+
if [ $checking_out_branch -eq 1 ] && b=$(git symbolic-ref --short -q HEAD); then
7+
pwd
8+
git_rails.sh $old_ref $new_ref
9+
fi

hooks/post-merge

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
git_rails

hooks/post-rewrite

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
command_used=$1
3+
4+
if [ $command_used == 'rebase' ]; then
5+
old_ref=`git rev-parse ORIG_HEAD`
6+
new_ref=`git rev-parse HEAD`
7+
git_rails $old_ref $new_ref
8+
fi

0 commit comments

Comments
 (0)