Subject: Re: bug#73320: [PATCH] project--vc-list-files: use Git's sparse-index

From: Dmitry Gutov <[email-masked]>
Date: Fri, 4 Oct 2024 02:57:22 +0300

On 19/09/2024 07:25, Sean Allred wrote:
> awkward state between Git
> 2.25 (where sparse-checkout was introduced) and 2.35 (where git-ls-files
> learned --sparse)

Speaking of versions, I've triggered an error on a remote host (with 
Tramp) because it had older Git installed.

It was running a Trisquel 9.0, which is pretty old, but looking at 
https://repology.org/project/git/versions it appears even the latest 
Trisquel (11.0) only uses Git 2.34. Same for Ubuntu 22.04 which it's 
based on, both officially supported until 2027.

The attached makes detected version connection-local, that's step 1 (*).

It could use some review, though. There aren't many examples of doing 
that in Emacs code.

Michael, could you take a look? Does this look reasonable? Should 
:application be 'tramp' or 'vc-git'? Is the 
'connection-local-set-profiles' call needed here at all or can be skipped?

(*) Step 2 might be to limit the --sparse argument to Emacs 31 and 
newer. :-/

diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el
index 05400523048..2a40539adeb 100644
--- a/lisp/vc/vc-git.el
+++ b/lisp/vc/vc-git.el
@@ -315,21 +315,31 @@ vc-git--state-code
 
 (defvar vc-git--program-version nil)
 
+(connection-local-set-profile-variables
+ 'vc-git-connection-default-profile
+ '((vc-git--program-version . nil)))
+
+(connection-local-set-profiles
+ '(:application vc-git)
+ 'vc-git-connection-default-profile)
+
 (defun vc-git--program-version ()
-  (or vc-git--program-version
-      (let ((version-string
-             (vc-git--run-command-string nil "version")))
-        (setq vc-git--program-version
-              (if (and version-string
-                       ;; Some Git versions append additional strings
-                       ;; to the numerical version string. E.g., Git
-                       ;; for Windows appends ".windows.N", while Git
-                       ;; for Mac appends " (Apple Git-N)". Capture
-                       ;; numerical version and ignore the rest.
-                       (string-match "git version \\([0-9][0-9.]+\\)"
-                                     version-string))
-                  (string-trim-right (match-string 1 version-string) "\\.")
-                "0")))))
+  (with-connection-local-variables
+   (or vc-git--program-version
+       (let ((version-string
+              (vc-git--run-command-string nil "version")))
+         (setq-connection-local
+          vc-git--program-version
+          (if (and version-string
+                   ;; Some Git versions append additional strings
+                   ;; to the numerical version string. E.g., Git
+                   ;; for Windows appends ".windows.N", while Git
+                   ;; for Mac appends " (Apple Git-N)". Capture
+                   ;; numerical version and ignore the rest.
+                   (string-match "git version \\([0-9][0-9.]+\\)"
+                                 version-string))
+              (string-trim-right (match-string 1 version-string) "\\.")
+            "0"))))))
 
 (defun vc-git--git-path (&optional path)
   "Resolve .git/PATH for the current working tree.