stevenharman / git_tracker

Some simple tricks that make working with Pivotal Tracker even better... and easier... um, besier!
https://github.com/stevenharman/git_tracker
MIT License
170 stars 11 forks source link

Submodule support #19

Open RobotCaleb opened 8 years ago

RobotCaleb commented 8 years ago

This doesn't behave well when trying to init in a repository that is a submodule.

I had to git clone my submodule repo, then init in there and copy the created hook to the proper location in ../parent/.git/modules/repo/hooks/

$ git tracker init
/var/lib/gems/2.1.0/gems/git_tracker-1.6.3/lib/git_tracker/hook.rb:20:in `initialize': Not a directory @ rb_sysopen - /home/user/parent/repo/.git/hooks/prepare-commit-msg (Errno::ENO
TDIR)
        from /var/lib/gems/2.1.0/gems/git_tracker-1.6.3/lib/git_tracker/hook.rb:20:in `open'
        from /var/lib/gems/2.1.0/gems/git_tracker-1.6.3/lib/git_tracker/hook.rb:20:in `write'
        from /var/lib/gems/2.1.0/gems/git_tracker-1.6.3/lib/git_tracker/hook.rb:12:in `init_at'
        from /var/lib/gems/2.1.0/gems/git_tracker-1.6.3/lib/git_tracker/hook.rb:8:in `init'
        from /var/lib/gems/2.1.0/gems/git_tracker-1.6.3/lib/git_tracker/runner.rb:19:in `init'
        from /var/lib/gems/2.1.0/gems/git_tracker-1.6.3/lib/git_tracker/runner.rb:11:in `execute'
        from /var/lib/gems/2.1.0/gems/git_tracker-1.6.3/bin/git-tracker:4:in `<top (required)>'
        from /usr/local/bin/git-tracker:23:in `load'
        from /usr/local/bin/git-tracker:23:in `<main>'
stevenharman commented 8 years ago

Hello @RobotCaleb,

Sorry for the trouble, but thanks for the report. Are you saying that you had a repo (/home/users/alice/parent-repo/) which also had a submodule it in (/home/users/alice/parent-repo/foo/submodule-repo/)? And you were trying to run git tracker init from the parent repo - e.g., from /home/users/alice/parent-repo/?

Can you lay out, step by step, instructions to recreate the issue?

RobotCaleb commented 8 years ago

Sure!

First, the commands to run. Then I'll show the output I get.

Commands:

mkdir parent sub
cd sub/
git init
touch s; git add s; git commit -m "commit s to sub"
cd ../parent
git init
touch p; git add p; git commit -m "commit p to parent"
git submodule add ../sub
cd sub
git tracker init

Output:

  ~$ mkdir parent sub
  ~$ cd sub/
  ~/sub$ git init
Initialized empty Git repository in /home/rc/sub/.git/
  ~/sub$ touch s; git add s; git commit -m "commit s to sub"
[master (root-commit) c4233d7] commit s to sub
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 s
  ~/sub$ cd ../parent
  ~/parent$ git init
Initialized empty Git repository in /home/rc/parent/.git/
  ~/parent$ touch p; git add p; git commit -m "commit p to parent"
[master (root-commit) 86b9fff] commit p to parent
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 p
  ~/parent$ git submodule add ../sub
Cloning into 'sub'...
done.
  ~/parent$ cd sub
  ~/parent/sub$ cat .git
gitdir: ../.git/modules/sub
  ~/parent/sub$ git tracker init
/var/lib/gems/1.9.1/gems/git_tracker-1.6.3/lib/git_tracker/hook.rb:20:in `initialize': Not a directory - /home/rc/parent/sub/.git/hooks/prepare-commit-msg (Errno::ENOTDIR)
        from /var/lib/gems/1.9.1/gems/git_tracker-1.6.3/lib/git_tracker/hook.rb:20:in `open'
        from /var/lib/gems/1.9.1/gems/git_tracker-1.6.3/lib/git_tracker/hook.rb:20:in `write'
        from /var/lib/gems/1.9.1/gems/git_tracker-1.6.3/lib/git_tracker/hook.rb:12:in `init_at'
        from /var/lib/gems/1.9.1/gems/git_tracker-1.6.3/lib/git_tracker/hook.rb:8:in `init'
        from /var/lib/gems/1.9.1/gems/git_tracker-1.6.3/lib/git_tracker/runner.rb:19:in `init'
        from /var/lib/gems/1.9.1/gems/git_tracker-1.6.3/lib/git_tracker/runner.rb:11:in `execute'
        from /var/lib/gems/1.9.1/gems/git_tracker-1.6.3/bin/git-tracker:4:in `<top (required)>'
        from /usr/local/bin/git-tracker:23:in `load'
        from /usr/local/bin/git-tracker:23:in `<main>'

Notice that .git in /parent/sub/ is a file, not a directory. Run cat on it for more info.

~/parent/sub$ cat .git 
gitdir: ../.git/modules/sub

I hope that helps. My workaround was to go to ~/sub/ and init tracker there. Then copy the newly created hook to ~/parent/.git/modules/sub/hooks/

  ~$ cd ~/sub
  ~/sub$ git tracker init
  ~/sub$ cp .git/hooks/prepare-commit-msg ~/parent/.git/modules/sub/hooks/
stevenharman commented 8 years ago

OK, I see now that the error happens when trying to git tracker init from within a submodule. In Git > 1.7.8, Git does not leave repo directories (e.g., .git/) in the working directory, but rather centralizes them in the parent's .git/modules directory. As you said, in the submodule, .git is a file, not a directory.

As of now, GitTracker assumes that the root directory (as reported by git rev-parse --show-toplevel) is the root, and it contains a directory structure of .git/hooks. It then attempts to blindly write a prepare-commit-msg file there. Instead we should check if .git is a file, and if so parse out the gitdir path and use it to write the hook file.

Any chance you have bandwidth to try making such a change?

RobotCaleb commented 8 years ago

Unfortunately, I don't. I wouldn't know where to start with ruby (right? Gem is a ruby thing?), and I have a workaround that worked for the one instance where I need it. Sorry!

stevenharman commented 8 years ago

No problem. I'll try to get a fix in at some point. For myself, or anyone wanting to fix this, something to consider would be using git rev-parse --git-dir and the related options, rather than git rev-parse --show-toplevel.