Subject: bug#21457: 25.0.50; vc-test-git03-working-revision test fails

From: Eli Zaretskii <[email-masked]>
Date: Sat, 12 Sep 2015 12:02:24 +0300

> From: Glenn Morris <[email-masked]>
> Cc: [email-masked]
> Date: Fri, 11 Sep 2015 16:34:45 -0400
> 
> Glenn Morris wrote:
> 
> > Perhaps it should use (min (point-max) (+ (point-min) 40)).
> 
> Scratch that, better to test for an actual hash, "[0-9a-f]\\{40\\}".

But it doesn't return a hash in a repo that was just initted, it
returns "HEAD".  Try this:

  $ mkdir ttt
  $ cd ttt
  $ git init
  Initialized empty Git repository in D:/gnu/git/ttt/.git/
  $ git rev-parse HEAD
  HEAD
  fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
  Use '--' to separate paths from revisions, like this:
  'git <command> [<revision>...] -- [<file>...]'
  $ echo $?
  128

IOW, it returns "HEAD" and exits with an error status.  Maybe the
problem started when I upgraded Git to 2.5.1 lately.

The patches below make this work for me.  The first one should
probably be applied regardless, as without it vc-git--rev-parse is not
safe to call in some borderline cases.

diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el
index 8a0f554..af2b39f 100644
--- a/lisp/vc/vc-git.el
+++ b/lisp/vc/vc-git.el
@@ -1137,7 +1137,9 @@ (defun vc-git--rev-parse (rev)
   (with-temp-buffer
     (and
      (vc-git--out-ok "rev-parse" rev)
-     (buffer-substring-no-properties (point-min) (+ (point-min) 40)))))
+     (buffer-substring-no-properties (point-min)
+                                     (min (1- (point-max))
+                                          (+ (point-min) 40))))))
 
 (defun vc-git-next-revision (file rev)
   "Git-specific version of `vc-next-revision'."
diff --git a/test/automated/vc-tests.el b/test/automated/vc-tests.el
index a7242e9..3476846 100644
--- a/test/automated/vc-tests.el
+++ b/test/automated/vc-tests.el
@@ -367,12 +367,15 @@ (defun vc-test--working-revision (backend)
 
 	  ;; nil: CVS Mtn RCS SCCS
 	  ;; "0": Bzr Hg SRC SVN
-	  ;; "master": Git
+	  ;; "master" or "HEAD": Git
+          (message "%s: %s"
+                   backend
+                   (vc-working-revision default-directory backend))
 	  (should (eq (vc-working-revision default-directory)
 		      (vc-working-revision default-directory backend)))
 	  (should
 	   (member
-	    (vc-working-revision default-directory) '(nil "0" "master")))
+	    (vc-working-revision default-directory) '(nil "0" "master" "HEAD")))
 
 	  (let ((tmp-name (expand-file-name "foo" default-directory)))
 	    ;; Check initial working revision, should be nil until
@@ -380,22 +383,22 @@ (defun vc-test--working-revision (backend)
 
 	    ;; nil: CVS Mtn RCS SCCS SVN
 	    ;; "0": Bzr Hg SRC
-	    ;; "master": Git
+	    ;; "master" or "HEAD: Git
 	    (should (eq (vc-working-revision tmp-name)
 			(vc-working-revision tmp-name backend)))
 	    (should
-	     (member (vc-working-revision tmp-name) '(nil "0" "master")))
+	     (member (vc-working-revision tmp-name) '(nil "0" "master" "HEAD")))
 
 	    ;; Write a new file.  Check working revision.
 	    (write-region "foo" nil tmp-name nil 'nomessage)
 
 	    ;; nil: CVS Mtn RCS SCCS SVN
 	    ;; "0": Bzr Hg SRC
-	    ;; "master": Git
+	    ;; "master" or "HEAD": Git
 	    (should (eq (vc-working-revision tmp-name)
 			(vc-working-revision tmp-name backend)))
 	    (should
-	     (member (vc-working-revision tmp-name) '(nil "0" "master")))
+	     (member (vc-working-revision tmp-name) '(nil "0" "master" "HEAD")))
 
 	    ;; Register a file.  Check working revision.
 	    (vc-register
@@ -403,11 +406,11 @@ (defun vc-test--working-revision (backend)
 
 	    ;; nil: Mtn RCS SCCS
 	    ;; "0": Bzr CVS Hg SRC SVN
-	    ;; "master": Git
+	    ;; "master" or "HEAD": Git
 	    (should (eq (vc-working-revision tmp-name)
 			(vc-working-revision tmp-name backend)))
 	    (should
-	     (member (vc-working-revision tmp-name) '(nil "0" "master")))
+	     (member (vc-working-revision tmp-name) '(nil "0" "master" "HEAD")))
 
 	    ;; Unregister the file.  Check working revision.
 	    (condition-case nil
@@ -416,13 +419,13 @@ (defun vc-test--working-revision (backend)
 
 		  ;; nil: RCS
 		  ;; "0": Bzr Hg
-		  ;; "master": Git
+		  ;; "master" or "HEAD": Git
 		  ;; unsupported: CVS Mtn SCCS SRC SVN
 		  (should (eq (vc-working-revision tmp-name)
 			      (vc-working-revision tmp-name backend)))
 		  (should
 		   (member
-		    (vc-working-revision tmp-name) '(nil "0" "master"))))
+		    (vc-working-revision tmp-name) '(nil "0" "master" "HEAD"))))
 	      (vc-not-supported t))))
 
       ;; Save exit.