diff --git a/git-p4.py b/git-p4.py
index fd5ca52..6aa8957 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -49,6 +49,13 @@ defaultLabelRegexp = r'[a-zA-Z0-9_\-.]+$'
# Grab changes in blocks of this many revisions, unless otherwise requested
defaultBlockSize = 512
+def gitdir():
+ d = read_pipe("git rev-parse --git-dir").strip()
+ if not d or len(d) == 0:
+ return None
+ else:
+ return d
+
def p4_build_cmd(cmd):
"""Build a suitable p4 command line.
@@ -562,12 +569,6 @@ def currentGitBranch():
else:
return read_pipe(["git", "name-rev", "HEAD"]).split(" ")[1].strip()
-def isValidGitDir(path):
- if (os.path.exists(path + "/HEAD")
- and os.path.exists(path + "/refs") and os.path.exists(path + "/objects")):
- return True;
- return False
-
def parseRevision(ref):
return read_pipe("git rev-parse %s" % ref).strip()
@@ -3462,7 +3463,7 @@ class P4Sync(Command, P4UserMap):
if self.tempBranches != []:
for branch in self.tempBranches:
read_pipe("git update-ref -d %s" % branch)
- os.rmdir(os.path.join(os.environ.get("GIT_DIR", ".git"), self.tempBranchLocation))
+ os.rmdir(os.path.join(gitdir(), self.tempBranchLocation))
# Create a symbolic ref p4/HEAD pointing to p4/<branch> to allow
# a convenient shortcut refname "p4".@@ -3678,23 +3679,27 @@ def main():
(cmd, args) = parser.parse_args(sys.argv[2:], cmd);
global verbose
verbose = cmd.verbose
+
if cmd.needsGit:
- if cmd.gitdir == None:
- cmd.gitdir = os.path.abspath(".git")
- if not isValidGitDir(cmd.gitdir):
- cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
- if os.path.exists(cmd.gitdir):
- cdup = read_pipe("git rev-parse --show-cdup").strip()
- if len(cdup) > 0:
- chdir(cdup);
-
- if not isValidGitDir(cmd.gitdir):
- if isValidGitDir(cmd.gitdir + "/.git"):
- cmd.gitdir += "/.git"
- else:
+ if cmd.gitdir:
+ os.environ["GIT_DIR"] = cmd.gitdir
+
+ # did we get a valid git dir on the command line or via $GIT_DIR?
+ if not gitdir():
die("fatal: cannot locate git repository at %s" % cmd.gitdir)
- os.environ["GIT_DIR"] = cmd.gitdir
+ else:
+ # already in a git directory?
+ if not gitdir():
+ die("fatal: not in a valid git repository")
+
+ cdup = read_pipe("git rev-parse --show-cdup").strip()
+ if len(cdup) > 0:
+ chdir(cdup);
+
+ # ensure subshells spawned in the p4 repo directory
+ # get the correct GIT_DIR
+ os.environ["GIT_DIR"] = os.path.abspath(gitdir())
if not cmd.run(args):
parser.print_help()diff --git a/t/t9800-git-p4-basic.sh b/t/t9800-git-p4-basic.sh
index 0730f18..093e9bd 100755
--- a/t/t9800-git-p4-basic.sh
+++ b/t/t9800-git-p4-basic.sh
@@ -257,6 +257,26 @@ test_expect_success 'submit from detached head' '
)
'
+test_expect_success 'submit from worktree' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git worktree add ../worktree-test
+ ) &&
+ (
+ cd "$git/../worktree-test" &&
+ test_commit "worktree-commit" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ test_path_is_file worktree-commit.t
+ )
+'
+
test_expect_success 'kill p4d' '
kill_p4d
'
--
2.8.2.703.g78b384c.dirty