Skip to content

Commit 101f3dc

Browse files
committed
Merge branch 'ld/p4-worktree'
"git p4" didn't interact with the internal of .git directory correctly in the modern "git-worktree"-enabled world. * ld/p4-worktree: git-p4: support git worktrees
2 parents 09b4fdb + 378f7be commit 101f3dc

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

git-p4.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ def p4_build_cmd(cmd):
9090
real_cmd += cmd
9191
return real_cmd
9292

93+
def git_dir(path):
94+
""" Return TRUE if the given path is a git directory (/path/to/dir/.git).
95+
This won't automatically add ".git" to a directory.
96+
"""
97+
d = read_pipe(["git", "--git-dir", path, "rev-parse", "--git-dir"], True).strip()
98+
if not d or len(d) == 0:
99+
return None
100+
else:
101+
return d
102+
93103
def chdir(path, is_client_path=False):
94104
"""Do chdir to the given path, and set the PWD environment
95105
variable for use by P4. It does not look at getcwd() output.
@@ -572,10 +582,7 @@ def currentGitBranch():
572582
return read_pipe(["git", "name-rev", "HEAD"]).split(" ")[1].strip()
573583

574584
def isValidGitDir(path):
575-
if (os.path.exists(path + "/HEAD")
576-
and os.path.exists(path + "/refs") and os.path.exists(path + "/objects")):
577-
return True;
578-
return False
585+
return git_dir(path) != None
579586

580587
def parseRevision(ref):
581588
return read_pipe("git rev-parse %s" % ref).strip()
@@ -3725,6 +3732,7 @@ def main():
37253732
if cmd.gitdir == None:
37263733
cmd.gitdir = os.path.abspath(".git")
37273734
if not isValidGitDir(cmd.gitdir):
3735+
# "rev-parse --git-dir" without arguments will try $PWD/.git
37283736
cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
37293737
if os.path.exists(cmd.gitdir):
37303738
cdup = read_pipe("git rev-parse --show-cdup").strip()
@@ -3737,6 +3745,7 @@ def main():
37373745
else:
37383746
die("fatal: cannot locate git repository at %s" % cmd.gitdir)
37393747

3748+
# so git commands invoked from the P4 workspace will succeed
37403749
os.environ["GIT_DIR"] = cmd.gitdir
37413750

37423751
if not cmd.run(args):

t/t9800-git-p4-basic.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,26 @@ test_expect_success 'submit from detached head' '
257257
)
258258
'
259259

260+
test_expect_success 'submit from worktree' '
261+
test_when_finished cleanup_git &&
262+
git p4 clone --dest="$git" //depot &&
263+
(
264+
cd "$git" &&
265+
git worktree add ../worktree-test
266+
) &&
267+
(
268+
cd "$git/../worktree-test" &&
269+
test_commit "worktree-commit" &&
270+
git config git-p4.skipSubmitEdit true &&
271+
git p4 submit
272+
) &&
273+
(
274+
cd "$cli" &&
275+
p4 sync &&
276+
test_path_is_file worktree-commit.t
277+
)
278+
'
279+
260280
test_expect_success 'kill p4d' '
261281
kill_p4d
262282
'

t/t9806-git-p4-options.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,38 @@ test_expect_success 'submit works with two branches' '
269269
)
270270
'
271271

272+
test_expect_success 'use --git-dir option and GIT_DIR' '
273+
test_when_finished cleanup_git &&
274+
git p4 clone //depot --destination="$git" &&
275+
(
276+
cd "$git" &&
277+
git config git-p4.skipSubmitEdit true &&
278+
test_commit first-change &&
279+
git p4 submit --git-dir "$git"
280+
) &&
281+
(
282+
cd "$cli" &&
283+
p4 sync &&
284+
test_path_is_file first-change.t &&
285+
echo "cli_file" >cli_file.t &&
286+
p4 add cli_file.t &&
287+
p4 submit -d "cli change"
288+
) &&
289+
(git --git-dir "$git" p4 sync) &&
290+
(cd "$git" && git checkout -q p4/master) &&
291+
test_path_is_file "$git"/cli_file.t &&
292+
(
293+
cd "$cli" &&
294+
echo "cli_file2" >cli_file2.t &&
295+
p4 add cli_file2.t &&
296+
p4 submit -d "cli change2"
297+
) &&
298+
(GIT_DIR="$git" git p4 sync) &&
299+
(cd "$git" && git checkout -q p4/master) &&
300+
test_path_is_file "$git"/cli_file2.t
301+
'
302+
303+
272304
test_expect_success 'kill p4d' '
273305
kill_p4d
274306
'

0 commit comments

Comments
 (0)