{"thread":{"id":"40848","subject":"[PATCH v2 0/2] git-p4: allow submission from detached head","startedAt":"2015-11-21T09:54:38Z","lastAt":"2015-11-21T09:54:41Z","messageCount":4,"participants":["Luke Diamand"],"isPatch":true,"patchVersion":2,"patchTotal":2},"messages":[{"id":"273572","messageId":"1448099681-20183-1-git-send-email-luke@diamand.org","threadId":"40848","inReplyTo":null,"subject":"[PATCH v2 0/2] git-p4: allow submission from detached head","fromName":"Luke Diamand","fromEmail":"luke@diamand.org","sentAt":"2015-11-21T09:54:38Z","receivedAt":"2015-11-21T09:54:38Z","isPatch":true,"sender":{"key":"luke@diamand.org","avatar":"https://avatars.githubusercontent.com/u/5330967?v=4"},"body":"I'm resending my reroll of my earlier patch to teach git-p4\nabout detached heads.\n\nIt uses Junio's suggestion of calling \"git symbolic-ref\"\nto determine if we're on a detached head, rather than\nparsing text strings.\n\nLuke Diamand (3):\n  git-p4: add failing test for submit from detached head\n  git-p4: add option to system() to return subshell status\n  git-p4: work with a detached head\n\n git-p4.py               | 29 ++++++++++++++++++++---------\n t/t9800-git-p4-basic.sh | 16 ++++++++++++++++\n 2 files changed, 36 insertions(+), 9 deletions(-)\n\n-- \n2.6.0.rc3.238.gc07a1e8\n"},{"id":"273573","messageId":"1448099681-20183-2-git-send-email-luke@diamand.org","threadId":"40848","inReplyTo":"1448099681-20183-1-git-send-email-luke@diamand.org","subject":"[PATCH v2 1/3] git-p4: add failing test for submit from detached head","fromName":"Luke Diamand","fromEmail":"luke@diamand.org","sentAt":"2015-11-21T09:54:39Z","receivedAt":"2015-11-21T09:54:39Z","isPatch":true,"sender":{"key":"luke@diamand.org","avatar":"https://avatars.githubusercontent.com/u/5330967?v=4"},"body":"git-p4 can't submit from a detached head. This test case\ndemonstrates the problem.\n\nSigned-off-by: Luke Diamand <luke@diamand.org>\n---\n t/t9800-git-p4-basic.sh | 16 ++++++++++++++++\n 1 file changed, 16 insertions(+)\n\ndiff --git a/t/t9800-git-p4-basic.sh b/t/t9800-git-p4-basic.sh\nindex 90d41ed..114b19f 100755\n--- a/t/t9800-git-p4-basic.sh\n+++ b/t/t9800-git-p4-basic.sh\n@@ -241,6 +241,22 @@ test_expect_success 'unresolvable host in P4PORT should display error' '\n \t)\n '\n \n+test_expect_failure 'submit from detached head' '\n+\ttest_when_finished cleanup_git &&\n+\tgit p4 clone --dest=\"$git\" //depot &&\n+\t(\n+\t\tcd \"$git\" &&\n+\t\tgit checkout p4/master &&\n+\t\t>detached_head_test &&\n+\t\tgit add detached_head_test &&\n+\t\tgit commit -m \"add detached_head\" &&\n+\t\tgit config git-p4.skipSubmitEdit true &&\n+\t\tgit p4 submit &&\n+\t\tgit p4 rebase &&\n+\t\tgit log p4/master | grep detached_head\n+\t)\n+'\n+\n test_expect_success 'kill p4d' '\n \tkill_p4d\n '\n-- \n2.6.0.rc3.238.gc07a1e8\n"},{"id":"273574","messageId":"1448099681-20183-3-git-send-email-luke@diamand.org","threadId":"40848","inReplyTo":"1448099681-20183-1-git-send-email-luke@diamand.org","subject":"[PATCH v2 2/3] git-p4: add option to system() to return subshell status","fromName":"Luke Diamand","fromEmail":"luke@diamand.org","sentAt":"2015-11-21T09:54:40Z","receivedAt":"2015-11-21T09:54:40Z","isPatch":true,"sender":{"key":"luke@diamand.org","avatar":"https://avatars.githubusercontent.com/u/5330967?v=4"},"body":"Add an optional parameter ignore_error to the git-p4 system()\nfunction. If used, it will return the subshell exit status\nrather than throwing an exception.\n\nSigned-off-by: Luke Diamand <luke@diamand.org>\n---\n git-p4.py | 6 ++++--\n 1 file changed, 4 insertions(+), 2 deletions(-)\n\ndiff --git a/git-p4.py b/git-p4.py\nindex 0093fa3..9d55f9c 100755\n--- a/git-p4.py\n+++ b/git-p4.py\n@@ -192,14 +192,16 @@ def p4_has_move_command():\n     # assume it failed because @... was invalid changelist\n     return True\n \n-def system(cmd):\n+def system(cmd, ignore_error=False):\n     expand = isinstance(cmd,basestring)\n     if verbose:\n         sys.stderr.write(\"executing %s\\n\" % str(cmd))\n     retcode = subprocess.call(cmd, shell=expand)\n-    if retcode:\n+    if retcode and not ignore_error:\n         raise CalledProcessError(retcode, cmd)\n \n+    return retcode\n+\n def p4_system(cmd):\n     \"\"\"Specifically invoke p4 as the system command. \"\"\"\n     real_cmd = p4_build_cmd(cmd)\n-- \n2.6.0.rc3.238.gc07a1e8\n"},{"id":"273575","messageId":"1448099681-20183-4-git-send-email-luke@diamand.org","threadId":"40848","inReplyTo":"1448099681-20183-1-git-send-email-luke@diamand.org","subject":"[PATCH v2 3/3] git-p4: work with a detached head","fromName":"Luke Diamand","fromEmail":"luke@diamand.org","sentAt":"2015-11-21T09:54:41Z","receivedAt":"2015-11-21T09:54:41Z","isPatch":true,"sender":{"key":"luke@diamand.org","avatar":"https://avatars.githubusercontent.com/u/5330967?v=4"},"body":"When submitting, git-p4 finds the current branch in\norder to know if it is allowed to submit (configuration\n\"git-p4.allowSubmit\").\n\nOn a detached head, detecting the branch would fail, and\ngit-p4 would report a cryptic error.\n\nThis change teaches git-p4 to recognise a detached head and\nsubmit successfully.\n\nSigned-off-by: Luke Diamand <luke@diamand.org>\n---\n git-p4.py               | 23 ++++++++++++++++-------\n t/t9800-git-p4-basic.sh |  2 +-\n 2 files changed, 17 insertions(+), 8 deletions(-)\n\ndiff --git a/git-p4.py b/git-p4.py\nindex 9d55f9c..0cfc866 100755\n--- a/git-p4.py\n+++ b/git-p4.py\n@@ -544,7 +544,12 @@ def p4Where(depotPath):\n     return clientPath\n \n def currentGitBranch():\n-    return read_pipe(\"git name-rev HEAD\").split(\" \")[1].strip()\n+    retcode = system([\"git\", \"symbolic-ref\", \"-q\", \"HEAD\"], ignore_error=True)\n+    if retcode != 0:\n+        # on a detached head\n+        return None\n+    else:\n+        return read_pipe([\"git\", \"name-rev\", \"HEAD\"]).split(\" \")[1].strip()\n \n def isValidGitDir(path):\n     if (os.path.exists(path + \"/HEAD\")\n@@ -1653,8 +1658,6 @@ class P4Submit(Command, P4UserMap):\n     def run(self, args):\n         if len(args) == 0:\n             self.master = currentGitBranch()\n-            if len(self.master) == 0 or not gitBranchExists(\"refs/heads/%s\" % self.master):\n-                die(\"Detecting current git branch failed!\")\n         elif len(args) == 1:\n             self.master = args[0]\n             if not branchExists(self.master):\n@@ -1662,9 +1665,10 @@ class P4Submit(Command, P4UserMap):\n         else:\n             return False\n \n-        allowSubmit = gitConfig(\"git-p4.allowSubmit\")\n-        if len(allowSubmit) > 0 and not self.master in allowSubmit.split(\",\"):\n-            die(\"%s is not in git-p4.allowSubmit\" % self.master)\n+        if self.master:\n+            allowSubmit = gitConfig(\"git-p4.allowSubmit\")\n+            if len(allowSubmit) > 0 and not self.master in allowSubmit.split(\",\"):\n+                die(\"%s is not in git-p4.allowSubmit\" % self.master)\n \n         [upstream, settings] = findUpstreamBranchPoint()\n         self.depotPath = settings['depot-paths'][0]\n@@ -1732,7 +1736,12 @@ class P4Submit(Command, P4UserMap):\n         self.check()\n \n         commits = []\n-        for line in read_pipe_lines([\"git\", \"rev-list\", \"--no-merges\", \"%s..%s\" % (self.origin, self.master)]):\n+        if self.master:\n+            commitish = self.master\n+        else:\n+            commitish = 'HEAD'\n+\n+        for line in read_pipe_lines([\"git\", \"rev-list\", \"--no-merges\", \"%s..%s\" % (self.origin, commitish)]):\n             commits.append(line.strip())\n         commits.reverse()\n \ndiff --git a/t/t9800-git-p4-basic.sh b/t/t9800-git-p4-basic.sh\nindex 114b19f..0730f18 100755\n--- a/t/t9800-git-p4-basic.sh\n+++ b/t/t9800-git-p4-basic.sh\n@@ -241,7 +241,7 @@ test_expect_success 'unresolvable host in P4PORT should display error' '\n \t)\n '\n \n-test_expect_failure 'submit from detached head' '\n+test_expect_success 'submit from detached head' '\n \ttest_when_finished cleanup_git &&\n \tgit p4 clone --dest=\"$git\" //depot &&\n \t(\n-- \n2.6.0.rc3.238.gc07a1e8\n"}]}