2526: post-review fails with new git submodules

nicolas.morey*************@gmai***** (Google Code) (Is this you? Claim this profile.)
chipx86
chipx86
March 16, 2012
What version are you running?
Confirmed on fresh clone of rbtools



What steps will reproduce the problem?
1. Clone a repository with submodules (git >= 1.7.9)
2. Run post-review from a submodule

What is the expected output? What do you see instead?
$ post-review --guess-summary --guess-description --parent=HEAD^
fatal: This operation must be run in a work tree

Please provide any additional information below.
On recent version of git, .git is not stored in the worktree of the submodule anymore but as as a subdirectory of the top project
$ git rev-parse --git-dir
/home/nmorey/workspace/git/SigmaCToolchain/.git/modules/scdiag
As post-review tries to reach the top directory by chdir to dirname(git_dit)
we end-up into the git directory of the top project so the diff fails.
The idea is to use the --show-toplevel option of git-rev-parse instead (when it exists).
I quickly wrote a patch that fixes the issue for me.
diff --git a/rbtools/clients/git.py b/rbtools/clients/git.py
index f7a728a..c2e5223 100644
--- a/rbtools/clients/git.py
+++ b/rbtools/clients/git.py
@@ -46,7 +46,14 @@ class GitClient(SCMClient):
         # post-review in directories other than the top level of
         # of a work-tree would result in broken diffs on the server
         if not self.bare:
-            os.chdir(os.path.dirname(os.path.abspath(git_dir)))
+            git_top = execute([self.git, "rev-parse", "--show-toplevel"],
+                          ignore_errors=True).rstrip("\n")
+
+            # top level might not work on old git version se we use git dir to find it
+            if git_top.startswith("fatal:") or not os.path.isdir(git_dir):
+                git_top = git_dir
+
+            os.chdir(os.path.abspath(git_top))
 
         self.head_ref = execute([self.git, 'symbolic-ref', '-q',
                                  'HEAD']).strip()
#1 nicolas.morey*************@gmai***** (Google Code) (Is this you? Claim this profile.)
Note: A pull-request has been sent on github
chipx86
#2 chipx86
Fixed on master (8a254f0)
  • +Fixed
  • +Component-RBTools
    +Milestone-RBTools-Release1.0
  • +chipx86