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 <luke@diamand.org> Signed-off-by: Jeff King <peff@peff.net>
Luke Diamand committed
Nov 21, 2015 at 09:54 UTC
00ad6e3182bbe68a94eaedf322918e4f266d184f
2 files changed
+17
-8
git-p4.py
+16
-7
@@ -544,7 +544,12 @@ def p4Where(depotPath):
544
return clientPath
545
546
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()
553
554
def isValidGitDir(path):
555
if (os.path.exists(path + "/HEAD")
@@ -1653,8 +1658,6 @@ class P4Submit(Command, P4UserMap):
1658
def run(self, args):
1659
if len(args) == 0:
1660
self.master = currentGitBranch()
1656
- if len(self.master) == 0 or not gitBranchExists("refs/heads/%s" % self.master):
1657
- die("Detecting current git branch failed!")
1661
elif len(args) == 1:
1662
self.master = args[0]
1663
if not branchExists(self.master):
@@ -1662,9 +1665,10 @@ class P4Submit(Command, P4UserMap):
1665
else:
1666
return False
1667
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)
1672
1673
[upstream, settings] = findUpstreamBranchPoint()
1674
self.depotPath = settings['depot-paths'][0]
@@ -1732,7 +1736,12 @@ class P4Submit(Command, P4UserMap):
1736
self.check()
1737
1738
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)]):
1745
commits.append(line.strip())
1746
commits.reverse()
1747
t/t9800-git-p4-basic.sh
+1
-1
@@ -241,7 +241,7 @@ test_expect_success 'unresolvable host in P4PORT should display error' '
241
)
242
'
243
244
-test_expect_failure 'submit from detached head' '
244
+test_expect_success 'submit from detached head' '
245
test_when_finished cleanup_git &&
246
git p4 clone --dest="$git" //depot &&
247
(