]> git.eshelyaron.com Git - emacs.git/commitdiff
Factor out vc-git--with-apply-temp-to-staging
authorSean Whitton <spwhitton@spwhitton.name>
Sun, 18 May 2025 11:35:13 +0000 (12:35 +0100)
committerEshel Yaron <me@eshelyaron.com>
Wed, 21 May 2025 06:09:52 +0000 (08:09 +0200)
* lisp/vc/vc-git.el (vc-git--with-apply-temp-to-staging): New macro.
(vc-git-checkin): Use it.

(cherry picked from commit a4424fb8cd2d638ee348ee7fd08d0e5f397fad75)

lisp/vc/vc-git.el

index 3e9d0b2841ce3811e4515a2851c811fcc7aad30c..ed71a278887c57a87b349c861e35b49d8772d9fd 100644 (file)
@@ -1111,6 +1111,19 @@ It is based on `log-edit-mode', and has Git-specific extensions."
        ("Sign-Off" . ,(boolean-arg-fn "--signoff")))
      comment)))
 
+(defmacro vc-git--with-apply-temp-to-staging (temp &rest body)
+  (declare (indent 1) (debug (symbolp body)))
+  `(let ((,temp (make-nearby-temp-file ,(format "git-%s" temp))))
+     (unwind-protect (progn ,@body
+                            ;; This uses `file-local-name' to strip the
+                            ;; TRAMP prefix, not `file-relative-name',
+                            ;; because we've had at least one problem
+                            ;; report where relativizing the file name
+                            ;; meant that Git failed to find it.
+                            (vc-git-command nil 0 nil "apply" "--cached"
+                                            (file-local-name ,temp)))
+       (delete-file ,temp))))
+
 (defun vc-git-checkin (files comment &optional _rev)
   (let* ((file1 (or (car files) default-directory))
          (root (vc-git-root file1))
@@ -1194,8 +1207,7 @@ It is based on `log-edit-mode', and has Git-specific extensions."
                     (t (push file-name to-stash)))
               (setq pos (point))))))
       (unless (string-empty-p vc-git-patch-string)
-        (let ((patch-file (make-nearby-temp-file "git-patch"))
-              ;; Temporarily countermand the let-binding at the
+        (let (;; Temporarily countermand the let-binding at the
               ;; beginning of this function.
               (coding-system-for-write
                (coding-system-change-eol-conversion
@@ -1203,12 +1215,9 @@ It is based on `log-edit-mode', and has Git-specific extensions."
                 ;; to have the Unix EOL format, because Git expects
                 ;; that, even on Windows.
                 (or pcsw vc-git-commits-coding-system) 'unix)))
-          (with-temp-file patch-file
-            (insert vc-git-patch-string))
-          (unwind-protect
-              (vc-git-command nil 0 nil "apply" "--cached"
-                              (file-local-name patch-file))
-            (delete-file patch-file))))
+          (vc-git--with-apply-temp-to-staging patch
+            (with-temp-file patch
+              (insert vc-git-patch-string)))))
       (when to-stash (vc-git--stash-staged-changes to-stash)))
     (let ((files (and only (not vc-git-patch-string) files))
           (args (vc-git--log-edit-extract-headers comment))
@@ -1218,15 +1227,9 @@ It is based on `log-edit-mode', and has Git-specific extensions."
              (when (and msg-file (file-exists-p msg-file))
                (delete-file msg-file))
              (when to-stash
-               (let ((cached (make-nearby-temp-file "git-cached")))
-                 (unwind-protect
-                     (progn
-                       (with-temp-file cached
-                         (vc-git-command t 0 nil "stash" "show" "-p"))
-                       (vc-git-command nil 0 "apply" "--cached"
-                                       (file-local-name cached)))
-                   (delete-file cached))
-                 (vc-git-command nil 0 nil "stash" "drop"))))))
+               (vc-git--with-apply-temp-to-staging cached
+                 (with-temp-file cached
+                   (vc-git-command t 0 nil "stash" "show" "-p")))))))
       (when msg-file
         (let ((coding-system-for-write
                (or pcsw vc-git-commits-coding-system)))
@@ -1282,6 +1285,8 @@ It is based on `log-edit-mode', and has Git-specific extensions."
                 (unwind-protect
                     (progn
                       (vc-git-command nil 0 nil "read-tree" "HEAD")
+                      ;; See `vc-git--with-apply-temp-to-staging'
+                      ;; regarding use of `file-local-name'.
                       (vc-git-command nil 0 nil "apply" "--cached"
                                       (file-local-name cached))
                       (setq tree (git-string "write-tree")))