Skip to content

Commit 5b228f9

Browse files
committed
Merge branch 'ld/p4-detached-head' into maint
Make git-p4 work on a detached head. * ld/p4-detached-head: git-p4: work with a detached head git-p4: add option to system() to return subshell status git-p4: add failing test for submit from detached head
2 parents 978b576 + 00ad6e3 commit 5b228f9

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

git-p4.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,16 @@ def p4_has_move_command():
190190
# assume it failed because @... was invalid changelist
191191
return True
192192

193-
def system(cmd):
193+
def system(cmd, ignore_error=False):
194194
expand = isinstance(cmd,basestring)
195195
if verbose:
196196
sys.stderr.write("executing %s\n" % str(cmd))
197197
retcode = subprocess.call(cmd, shell=expand)
198-
if retcode:
198+
if retcode and not ignore_error:
199199
raise CalledProcessError(retcode, cmd)
200200

201+
return retcode
202+
201203
def p4_system(cmd):
202204
"""Specifically invoke p4 as the system command. """
203205
real_cmd = p4_build_cmd(cmd)
@@ -540,7 +542,12 @@ def p4Where(depotPath):
540542
return clientPath
541543

542544
def currentGitBranch():
543-
return read_pipe("git name-rev HEAD").split(" ")[1].strip()
545+
retcode = system(["git", "symbolic-ref", "-q", "HEAD"], ignore_error=True)
546+
if retcode != 0:
547+
# on a detached head
548+
return None
549+
else:
550+
return read_pipe(["git", "name-rev", "HEAD"]).split(" ")[1].strip()
544551

545552
def isValidGitDir(path):
546553
if (os.path.exists(path + "/HEAD")
@@ -1649,18 +1656,17 @@ def exportGitTags(self, gitTags):
16491656
def run(self, args):
16501657
if len(args) == 0:
16511658
self.master = currentGitBranch()
1652-
if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master):
1653-
die("Detecting current git branch failed!")
16541659
elif len(args) == 1:
16551660
self.master = args[0]
16561661
if not branchExists(self.master):
16571662
die("Branch %s does not exist" % self.master)
16581663
else:
16591664
return False
16601665

1661-
allowSubmit = gitConfig("git-p4.allowSubmit")
1662-
if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
1663-
die("%s is not in git-p4.allowSubmit" % self.master)
1666+
if self.master:
1667+
allowSubmit = gitConfig("git-p4.allowSubmit")
1668+
if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
1669+
die("%s is not in git-p4.allowSubmit" % self.master)
16641670

16651671
[upstream, settings] = findUpstreamBranchPoint()
16661672
self.depotPath = settings['depot-paths'][0]
@@ -1728,7 +1734,12 @@ def run(self, args):
17281734
self.check()
17291735

17301736
commits = []
1731-
for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, self.master)]):
1737+
if self.master:
1738+
commitish = self.master
1739+
else:
1740+
commitish = 'HEAD'
1741+
1742+
for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, commitish)]):
17321743
commits.append(line.strip())
17331744
commits.reverse()
17341745

t/t9800-git-p4-basic.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,22 @@ test_expect_success 'unresolvable host in P4PORT should display error' '
241241
)
242242
'
243243

244+
test_expect_success 'submit from detached head' '
245+
test_when_finished cleanup_git &&
246+
git p4 clone --dest="$git" //depot &&
247+
(
248+
cd "$git" &&
249+
git checkout p4/master &&
250+
>detached_head_test &&
251+
git add detached_head_test &&
252+
git commit -m "add detached_head" &&
253+
git config git-p4.skipSubmitEdit true &&
254+
git p4 submit &&
255+
git p4 rebase &&
256+
git log p4/master | grep detached_head
257+
)
258+
'
259+
244260
test_expect_success 'kill p4d' '
245261
kill_p4d
246262
'

0 commit comments

Comments
 (0)