Skip to content

Make it work when Rails project is not in repository root #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,15 @@ The following command line options are supported:
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.

Also, the hooks are optional, you can use `git_rails` without them!

#### Monorepo usage

If the Rails project is not in the root of the Git repository, execute `git_rails` and hooks in the Rails directory.

For example, to make the post-checkout hook run in a particular subdirectory, create the following `.git/hooks/post-checkout` file:

```sh
#!/bin/sh
cd path/to/rails/project
exec /path/to/git_rails/hooks/post-checkout "$@"
```
12 changes: 5 additions & 7 deletions bin/git_rails
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,11 @@ if [ -z "$new_ref" ]; then
new_ref=`git rev-parse HEAD`
fi

files_changed=`git diff $old_ref $new_ref --name-status`

# CHECK IF WE NEED TO DO A BUNDLE
bundle_changed=`echo "$files_changed" | grep $'M\tGemfile.lock'`
bundle_changed=`git diff $old_ref $new_ref -- Gemfile.lock`

# CHECK IF WE NEED TO SPIN SOME YARN
yarn_changed=`echo "$files_changed" | grep $'M\tyarn.lock'`
yarn_changed=`git diff $old_ref $new_ref -- yarn.lock`

migrations=`git diff --name-status $old_ref $new_ref -- db/migrate | grep '^[AD]'`

Expand Down Expand Up @@ -139,14 +137,14 @@ if [ ! -z "$migrations" ]; then
fi

# BUILD THE MIGRATION COMMAND FROM THE VERSION AND TYPE
version=`echo "$migration" | cut -d'_' -f1 | cut -d'/' -f3`
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 $migration"
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"
Expand Down