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

[PATCH] Add missing functions to contrib/emacs/vc-git.el

From
David Kastrup <dak@gnu.org>
Date
Jul 12, 2007, 14:48 UTC
Message-ID
<86lkdlu0tr.fsf_-_@lola.quinscape.zz>
In-Reply-To
<A084830C-F1CD-4EDC-A13D-573DFC68DF77@silverinsanity.com>

This is necessary to make several editing functions work, like C-u C-x v =

Signed-off-by: David Kastrup <dak@gnu.org>
---
 contrib/emacs/vc-git.el |   65 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 65 insertions(+), 0 deletions(-)
diff --git a/contrib/emacs/vc-git.el b/contrib/emacs/vc-git.el
index e456ab9..b8f6be5 100644
--- a/contrib/emacs/vc-git.el
+++ b/contrib/emacs/vc-git.el
@@ -81,6 +81,71 @@
         (match-string 2 str)
       str)))
 
+(defun vc-git-symbolic-commit (commit)
+  "Translate COMMIT string into symbolic form.
+Returns nil if not possible."
+  (and commit
+       (with-temp-buffer
+	 (and
+	  (zerop
+	   (call-process "git" nil '(t nil) nil "name-rev"
+			 "--name-only" "--tags"
+			 commit))
+	  (goto-char (point-min))
+	  (= (forward-line 2) 1)
+	  (bolp)
+	  (buffer-substring-no-properties (point-min) (1- (point-max)))))))
+
+(defun vc-git-previous-version (file rev)
+  "git-specific version of `vc-previous-version'."
+  (let ((default-directory (file-name-directory (expand-file-name file)))
+	(file (file-name-nondirectory file)))
+    (vc-git-symbolic-commit
+     (with-temp-buffer
+       (and
+	(zerop
+	 (call-process "git" nil '(t nil) nil "rev-list"
+		       "-2" rev "--" file))
+	(goto-char (point-max))
+	(bolp)
+	(zerop (forward-line -1))
+	(not (bobp))
+	(buffer-substring-no-properties
+	   (point)
+	   (1- (point-max))))))))
+
+(defun vc-git-next-version (file rev)
+  "git-specific version of `vc-next-version'."
+  (let* ((default-directory (file-name-directory
+			     (expand-file-name file)))
+	(file (file-name-nondirectory file))
+	(current-rev
+	 (with-temp-buffer
+	   (and
+	    (zerop
+	     (call-process "git" nil '(t nil) nil "rev-list"
+			   "-1" rev "--" file))
+	    (goto-char (point-max))
+	    (bolp)
+	    (zerop (forward-line -1))
+	    (bobp)
+	    (buffer-substring-no-properties
+	     (point)
+	     (1- (point-max)))))))
+    (and current-rev
+	 (vc-git-symbolic-commit
+	  (with-temp-buffer
+	    (and
+	     (zerop
+	      (call-process "git" nil '(t nil) nil "rev-list"
+			    "HEAD" "--" file))
+	     (goto-char (point-min))
+	     (search-forward current-rev nil t)
+	     (zerop (forward-line -1))
+	     (buffer-substring-no-properties
+	      (point)
+	      (progn (forward-line 1) (1- (point))))))))))
+
 (defun vc-git-revert (file &optional contents-done)
   "Revert FILE to the version stored in the git repository."
   (if contents-done
-- 
1.5.3.rc0.51.gb0b1f


-- 
David Kastrup
Previous: Brian Gernhardt
Message 3 of 3 in “Getting version output for a file.”
  1. David KastrupJul 11, 2007
  2. Brian GernhardtJul 11, 2007
  3. Add missing functions to contrib/emacs/vc-git.elDavid Kastrup, Jul 12, 2007

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.