From 4a49c50a4c351503a94c223da05888e5fd3d4fa1 Mon Sep 17 00:00:00 2001 From: Sean Whitton Date: Tue, 29 Oct 2024 19:25:31 +0800 Subject: [PATCH] VC Git: Use vc-deduce-fileset to determine what to stash * 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. --- lisp/vc/vc-git.el | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el index e4969aede74..b751ead3f1d 100644 --- a/lisp/vc/vc-git.el +++ b/lisp/vc/vc-git.el @@ -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))