]> git.eshelyaron.com Git - emacs.git/commitdiff
VC Git: Use vc-deduce-fileset to determine what to stash
authorSean Whitton <spwhitton@spwhitton.name>
Tue, 29 Oct 2024 11:25:31 +0000 (19:25 +0800)
committerEshel Yaron <me@eshelyaron.com>
Fri, 1 Nov 2024 13:17:22 +0000 (14:17 +0100)
* lisp/vc/vc-git.el (vc-git--deduce-files-for-stash): New function.
(vc-git-stash, vc-git-stash-snapshot): Use it to determine what
to stash.  Update and expand docstrings.
(vc-git-stash-snapshot): No longer unconditionally snapshot all
uncommitted changes across the whole working tree.

(cherry picked from commit 4a49c50a4c351503a94c223da05888e5fd3d4fa1)

lisp/vc/vc-git.el

index 399a32a91234bb91a5e0bd59348291310b6bc49a..7a7646fa3126138bf217ee72beed828f2a85a63e 100644 (file)
@@ -2179,14 +2179,24 @@ This command shares argument histories with \\[rgrep] and \\[grep]."
 
 (autoload 'vc-dir-marked-files "vc-dir")
 
+(defun vc-git--deduce-files-for-stash ()
+  ;; In *vc-dir*, if nothing is marked, act on the whole working tree
+  ;; regardless of the position of point.  This preserves historical
+  ;; behavior and is also probably more useful.
+  (if (derived-mode-p 'vc-dir-mode)
+      (vc-dir-marked-files)
+    (cadr (vc-deduce-fileset))))
+
 (defun vc-git-stash (name)
-  "Create a stash given the name NAME."
+  "Create a stash named NAME.
+In `vc-dir-mode', if there are files marked, stash the changes to those.
+If no files are marked, stash all uncommitted changes to tracked files.
+In other modes, call `vc-deduce-fileset' to determine files to stash."
   (interactive "sStash name: ")
   (let ((root (vc-git-root default-directory)))
     (when root
       (apply #'vc-git--call nil "stash" "push" "-m" name
-             (when (derived-mode-p 'vc-dir-mode)
-               (vc-dir-marked-files)))
+             (vc-git--deduce-files-for-stash))
       (vc-resynch-buffer root t t))))
 
 (defvar vc-git-stash-read-history nil
@@ -2234,10 +2244,14 @@ This command shares argument histories with \\[rgrep] and \\[grep]."
   (vc-resynch-buffer (vc-git-root default-directory) t t))
 
 (defun vc-git-stash-snapshot ()
-  "Create a stash with the current tree state."
+  "Create a stash with the current uncommitted changes.
+In `vc-dir-mode', if there are files marked, stash the changes to those.
+If no files are marked, stash all uncommitted changes to tracked files.
+In other modes, call `vc-deduce-fileset' to determine files to stash."
   (interactive)
-  (vc-git--call nil "stash" "save"
-               (format-time-string "Snapshot on %Y-%m-%d at %H:%M"))
+  (apply #'vc-git--call nil "stash" "push" "-m"
+        (format-time-string "Snapshot on %Y-%m-%d at %H:%M")
+         (vc-git--deduce-files-for-stash))
   (vc-git-command "*vc-git-stash*" 0 nil "stash" "apply" "-q" "stash@{0}")
   (vc-resynch-buffer (vc-git-root default-directory) t t))