Skip to content

Commit 378f7be

Browse files
luked99gitster
authored andcommitted
git-p4: support git worktrees
git-p4 would attempt to find the git directory using its own specific code, which did not know about git worktrees. Rework it to use "git rev-parse --git-dir" instead. Add test cases for worktree usage and specifying git directory via --git-dir and $GIT_DIR. Signed-off-by: Luke Diamand <luke@diamand.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 8d7a455 commit 378f7be

3 files changed

Lines changed: 65 additions & 4 deletions

File tree

git-p4.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ def p4_build_cmd(cmd):
8585
real_cmd += cmd
8686
return real_cmd
8787

88+
def git_dir(path):
89+
""" Return TRUE if the given path is a git directory (/path/to/dir/.git).
90+
This won't automatically add ".git" to a directory.
91+
"""
92+
d = read_pipe(["git", "--git-dir", path, "rev-parse", "--git-dir"], True).strip()
93+
if not d or len(d) == 0:
94+
return None
95+
else:
96+
return d
97+
8898
def chdir(path, is_client_path=False):
8999
"""Do chdir to the given path, and set the PWD environment
90100
variable for use by P4. It does not look at getcwd() output.
@@ -563,10 +573,7 @@ def currentGitBranch():
563573
return read_pipe(["git", "name-rev", "HEAD"]).split(" ")[1].strip()
564574

565575
def isValidGitDir(path):
566-
if (os.path.exists(path + "/HEAD")
567-
and os.path.exists(path + "/refs") and os.path.exists(path + "/objects")):
568-
return True;
569-
return False
576+
return git_dir(path) != None
570577

571578
def parseRevision(ref):
572579
return read_pipe("git rev-parse %s" % ref).strip()
@@ -3682,6 +3689,7 @@ def main():
36823689
if cmd.gitdir == None:
36833690
cmd.gitdir = os.path.abspath(".git")
36843691
if not isValidGitDir(cmd.gitdir):
3692+
# "rev-parse --git-dir" without arguments will try $PWD/.git
36853693
cmd.gitdir = read_pipe("git rev-parse --git-dir").strip()
36863694
if os.path.exists(cmd.gitdir):
36873695
cdup = read_pipe("git rev-parse --show-cdup").strip()
@@ -3694,6 +3702,7 @@ def main():
36943702
else:
36953703
die("fatal: cannot locate git repository at %s" % cmd.gitdir)
36963704

3705+
# so git commands invoked from the P4 workspace will succeed
36973706
os.environ["GIT_DIR"] = cmd.gitdir
36983707

36993708
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)