pre-upload: fix running in a worktree

The rev-parse --git-dir shows the worktree's internal .git/ dir which
isn't what we really want here -- we're trying to find the root of
this git project checkout.

BUG=chromium:1104812
TEST=unittests pass in a git worktree

Change-Id: I2ae7650235da124235c274ac45b4b29021357df3
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/repohooks/+/2551820
Reviewed-by: Chirantan Ekbote <chirantan@chromium.org>
Commit-Queue: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/pre-upload.py b/pre-upload.py
index 00f36c1..d37c7b4 100755
--- a/pre-upload.py
+++ b/pre-upload.py
@@ -2471,11 +2471,11 @@
   # Check/normlaize git dir; if unspecified, we'll use the root of the git
   # project from CWD
   if opts.dir is None:
-    git_dir = _run_command(['git', 'rev-parse', '--git-dir'],
+    git_dir = _run_command(['git', 'rev-parse', '--show-toplevel'],
                            stderr=True).strip()
     if not git_dir:
       raise BadInvocation('The current directory is not part of a git project.')
-    opts.dir = os.path.dirname(os.path.abspath(git_dir))
+    opts.dir = git_dir
   elif not os.path.isdir(opts.dir):
     raise BadInvocation('Invalid dir: %s' % opts.dir)
   elif not os.path.isdir(os.path.join(opts.dir, '.git')):
diff --git a/pre-upload_unittest.py b/pre-upload_unittest.py
index e742711..b5902aa 100755
--- a/pre-upload_unittest.py
+++ b/pre-upload_unittest.py
@@ -1773,8 +1773,8 @@
     ret = pre_upload.direct_main(['--project', 'foooooooooo'])
     self.assertEqual(ret, 0)
     self.hooks_mock.assert_called_once_with(
-        'foooooooooo', proj_dir=os.getcwd(), commit_list=[],
-        presubmit=mock.ANY, config_file=mock.ANY)
+        'foooooooooo', proj_dir=os.path.dirname(os.path.abspath(__file__)),
+        commit_list=[], presubmit=mock.ANY, config_file=mock.ANY)
 
   def testNoGitDir(self):
     """We should die when run on a non-git dir."""