Skip to content

Commit 00ad6e3

Browse files
luked99peff
authored andcommitted
git-p4: work with a detached head
When submitting, git-p4 finds the current branch in order to know if it is allowed to submit (configuration "git-p4.allowSubmit"). On a detached head, detecting the branch would fail, and git-p4 would report a cryptic error. This change teaches git-p4 to recognise a detached head and submit successfully. Signed-off-by: Luke Diamand <[email protected]> Signed-off-by: Jeff King <[email protected]>
1 parent cbff4b2 commit 00ad6e3

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

git-p4.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,12 @@ def p4Where(depotPath):
544544
return clientPath
545545

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

549554
def isValidGitDir(path):
550555
if (os.path.exists(path + "/HEAD")
@@ -1653,18 +1658,17 @@ def exportGitTags(self, gitTags):
16531658
def run(self, args):
16541659
if len(args) == 0:
16551660
self.master = currentGitBranch()
1656-
if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master):
1657-
die("Detecting current git branch failed!")
16581661
elif len(args) == 1:
16591662
self.master = args[0]
16601663
if not branchExists(self.master):
16611664
die("Branch %s does not exist" % self.master)
16621665
else:
16631666
return False
16641667

1665-
allowSubmit = gitConfig("git-p4.allowSubmit")
1666-
if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
1667-
die("%s is not in git-p4.allowSubmit" % self.master)
1668+
if self.master:
1669+
allowSubmit = gitConfig("git-p4.allowSubmit")
1670+
if len(allowSubmit) > 0 and not self.master in allowSubmit.split(","):
1671+
die("%s is not in git-p4.allowSubmit" % self.master)
16681672

16691673
[upstream, settings] = findUpstreamBranchPoint()
16701674
self.depotPath = settings['depot-paths'][0]
@@ -1732,7 +1736,12 @@ def run(self, args):
17321736
self.check()
17331737

17341738
commits = []
1735-
for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, self.master)]):
1739+
if self.master:
1740+
commitish = self.master
1741+
else:
1742+
commitish = 'HEAD'
1743+
1744+
for line in read_pipe_lines(["git", "rev-list", "--no-merges", "%s..%s" % (self.origin, commitish)]):
17361745
commits.append(line.strip())
17371746
commits.reverse()
17381747

t/t9800-git-p4-basic.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ test_expect_success 'unresolvable host in P4PORT should display error' '
241241
)
242242
'
243243

244-
test_expect_failure 'submit from detached head' '
244+
test_expect_success 'submit from detached head' '
245245
test_when_finished cleanup_git &&
246246
git p4 clone --dest="$git" //depot &&
247247
(

0 commit comments

Comments
 (0)