tpope/hookup — pull request #12 by mal (2012-12-12 18:03:52+00:00)
Title: Ensure down migrations work with GIT_WORK_TREE

**tl;dr:** this patch enables hooks to run with a `pwd` within, but deeper than the root of the containing git repo.

---

So I found this at the same time as the `GIT_DIR` thing, but it was seemingly unrelated hence a separate PR.

**The problem**
... started with changing the `pwd` inside the hook, such that it was no longer the repository root. In order for the git commands inside hookup to still work `GIT_WORK_TREE` had to be exported as the repository root.

The difficulty is that `git diff -- migrations/*` returns output that's relative to the repository root, rather than `pwd`.

```sh
~/repo/subdir$ git diff --name-status 53ddb9... beb5097... -- migrations/*
M subdir/migrations/mig1.rb
```

When it comes time to checkout that future migration so it can be reverted, something like this happens:

```sh
~/repo/subdir$ git co 53ddb9... -- subdir/migrations/mig1.rb
error: pathspec 'subdir/migrations/mig1.rb' did not match any file(s) known to git.
```

**The solution**
... is to join the repository root to the diff output to create an absolute path, and then convert that path relative to `pwd`

```rb
absPath = File.join("/path/to/repo", "subdir/migrations/mig1.rb") #=> "/path/to/repo/subdir/migrations/mig1.rb"
path = Pathname.new(absPath).relative_path_from( ... pwd ... ) #=> "migrations/mig1.rb"
```

This new relative path can then be used to checkout the future migration and revert it.