Bug 1247838 - Fix running git mozreview from a subdirectory. r=gps
Bug 1246998 broke it, and the push code in cinnabar assumes it's running
from the toplevel directory, which is a fine assumption in the
git-remote-hg helper, but not in the git cinnabar command. Until
cinnabar itself is fixed, work around it.
While here, add a test case for worktrees.
--- a/git/commands/git-mozreview
+++ b/git/commands/git-mozreview
@@ -59,16 +59,24 @@ MAX_REVIEW_COMMITS = 100
git_dir = os.environ.get('GIT_COMMON_DIR')
if not git_dir:
git_dir = os.environ.get('GIT_DIR')
if not git_dir:
git_dir = subprocess.check_output(
['git', 'rev-parse', '--no-flags', '--git-common-dir']).rstrip('\n')
+ # Up to version 2.8, git rev-parse --git-common-dir returns '.git' when in
+ # a subdirectory of a non worktree.
+ # In this case, we can just fall through to --git-dir.
+ # git rev-parse --show-cdup will return an empty string if we are at the
+ # top-level, and '../' * depth otherwise.
+ if git_dir == '.git' and subprocess.check_output(
+ ['git', 'rev-parse', '--show-cdup']).strip():
+ git_dir = None
# git versions before 2.5 don't support --git-common-dir, so fallback to
# --git-dir
if not git_dir:
git_dir = subprocess.check_output(
['git', 'rev-parse', '--git-dir']).rstrip('\n')
def gethgui():
@@ -158,19 +166,20 @@ def sendauthenticatedhttprequest(git_con
if not session:
session = requests.Session()
req = requests.Request(url=url, auth=BugzillaAPIKeyAuth(git_config),
**kwargs)
return session.send(req.prepare())
-def get_output(args):
+def get_output(args, cwd=None):
try:
- output = subprocess.check_output(args, stderr=subprocess.STDOUT)
+ output = subprocess.check_output(args, stderr=subprocess.STDOUT,
+ cwd=cwd)
return 0, output
except subprocess.CalledProcessError as e:
return e.returncode, e.output
def get_git_config():
res, output = get_output(['git', 'config', '--list', '-z'])
if res:
@@ -353,17 +362,24 @@ def push_command(args):
'cinnabar',
'python',
os.path.join(ROOT, 'git', 'cinnabar-debug-push.py'),
'--config-file', path,
url_s,
push_commit,
]
- res, output = get_output(push_args)
+ # git doesn't invoke git-remote-hg and git-cinnabar the same way.
+ # Notably, it changes the current directory before running
+ # git-remote-hg, and the push code in cinnabar doesn't work
+ # properly from another directory as of 0.3.1.
+ cdup = subprocess.check_output(
+ ['git', 'rev-parse', '--show-cdup']).strip() or None
+
+ res, output = get_output(push_args, cwd=cdup)
finally:
os.unlink(path)
if res:
raise AbortError('error performing cinnabar push; '
'please report this bug: %s' % output)
output = json.loads(output, encoding='utf-8')
--- a/git/tests/test-mozreview-push-basic.t
+++ b/git/tests/test-mozreview-push-basic.t
@@ -186,16 +186,62 @@ Reviews should be published and Bugzilla
component: TestComponent
depends_on: []
platform: All
product: TestProduct
resolution: ''
status: NEW
summary: bug1
+Pushing from a subdirectory works
+
+ $ mkdir subdir
+ $ cd subdir && git mozreview push
+ remote: adding changesets
+ remote: adding manifests
+ remote: adding file changes
+ remote: added 0 changesets with 0 changes to 1 files
+
+ submitting 2 commits for review
+
+ commit: 4ba654c Bug 1 - Foo 1
+ review: http://$DOCKER_HOSTNAME:$HGPORT1/r/2
+
+ commit: f6c6fd8 Bug 1 - Foo 2
+ review: http://$DOCKER_HOSTNAME:$HGPORT1/r/3
+
+ (review requests lack reviewers; visit review url to assign reviewers)
+
+ publish these review requests now (Yn)? y
+ (published review request 1)
+
+Pushing from a worktree works
+
+ $ git worktree add -b foo ../worktree
+ Preparing subdir/../worktree (identifier worktree)
+ HEAD is now at f6c6fd8 Bug 1 - Foo 2
+ $ cd ../worktree && git mozreview push
+ remote: adding changesets
+ remote: adding manifests
+ remote: adding file changes
+ remote: added 0 changesets with 0 changes to 1 files
+
+ submitting 2 commits for review
+
+ commit: 4ba654c Bug 1 - Foo 1
+ review: http://$DOCKER_HOSTNAME:$HGPORT1/r/2
+
+ commit: f6c6fd8 Bug 1 - Foo 2
+ review: http://$DOCKER_HOSTNAME:$HGPORT1/r/3
+
+ (review requests lack reviewers; visit review url to assign reviewers)
+
+ publish these review requests now (Yn)? y
+ (published review request 1)
+
hg:// URLs work
$ git config mozreview.remote hg://${DOCKER_HOSTNAME}:${HGPORT}:http/test-repo
$ git mozreview push
remote: adding changesets
remote: adding manifests
remote: adding file changes
remote: added 0 changesets with 0 changes to 1 files