git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/3] git-p4: don't use name-rev to get current branch

From
Luke Diamand <luke@diamand.org>
Date
Apr 15, 2017, 10:36 UTC
Message-ID
<20170415103609.6002-4-luke@diamand.org>
In-Reply-To
<20170415103609.6002-1-luke@diamand.org>
git-p4 was using "git name-rev" to find out the current branch.

That is not safe, since if multiple branches or tags point at the same revision, the result obtained might not be what is expected.

Instead use "git symbolic-ref".
Signed-off-by: Luke Diamand <luke@diamand.org>
---
 git-p4.py                | 7 +------
 t/t9807-git-p4-submit.sh | 2 +-
 2 files changed, 2 insertions(+), 7 deletions(-)
diff --git a/git-p4.py b/git-p4.py
index 584b81775..8d151da91 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -602,12 +602,7 @@ def p4Where(depotPath):
     return clientPath
 
 def currentGitBranch():
-    retcode = system(["git", "symbolic-ref", "-q", "HEAD"], ignore_error=True)
-    if retcode != 0:
-        # on a detached head
-        return None
-    else:
-        return read_pipe(["git", "name-rev", "HEAD"]).split(" ")[1].strip()
+    return read_pipe_text(["git", "symbolic-ref", "--short", "-q", "HEAD"])
 
 def isValidGitDir(path):
     return git_dir(path) != None
diff --git a/t/t9807-git-p4-submit.sh b/t/t9807-git-p4-submit.sh
index ae05816e0..3457d5db6 100755
--- a/t/t9807-git-p4-submit.sh
+++ b/t/t9807-git-p4-submit.sh
@@ -139,7 +139,7 @@ test_expect_success 'submit with master branch name from argv' '
 	)
 '
 
-test_expect_failure 'allow submit from branch with same revision but different name' '
+test_expect_success 'allow submit from branch with same revision but different name' '
 	test_when_finished cleanup_git &&
 	git p4 clone --dest="$git" //depot &&
 	(
-- 
2.12.2.719.gcbd162c
Previous: Luke Diamand
Message 4 of 4 in “git-p4: use symbolic-ref instead of name-rev”
  1. 0/3 git-p4: use symbolic-ref instead of name-revLuke Diamand, Apr 15, 2017
  2. 1/3 git-p4: add failing test for name-rev rather than symbolic-refLuke Diamand, Apr 15, 2017
  3. 2/3 git-p4: add read_pipe_text() internal functionLuke Diamand, Apr 15, 2017
  4. 3/3 git-p4: don't use name-rev to get current branchLuke Diamand, Apr 15, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.