(defvar vc-git-stash-read-history nil
"History for `vc-git-stash-read'.")
-(defun vc-git-stash-read (prompt)
- "Read a Git stash. PROMPT is a string to prompt with."
- (let ((stash (completing-read
- prompt
- (split-string
- (or (vc-git--run-command-string nil "stash" "list") "") "\n" t)
- nil :require-match nil 'vc-git-stash-read-history)))
- (if (string-equal stash "")
- (user-error "Not a stash")
- (string-match "^stash@{[[:digit:]]+}" stash)
- (match-string 0 stash))))
+(defun vc-git-stash-read (prompt &optional default-most-recent)
+ "Prompt the user, with PROMPT, to select a git stash.
+PROMPT is passed to `format-prompt'. If DEFAULT-MOST-RECENT is non-nil,
+then the most recently pushed stash is the default selection."
+ (if-let* ((stashes
+ (split-string (vc-git--run-command-string nil
+ "stash" "list")
+ "\n" t)))
+ (let* ((default (and default-most-recent (car stashes)))
+ (prompt (format-prompt prompt default))
+ (stash (completing-read prompt stashes
+ nil :require-match nil
+ 'vc-git-stash-read-history
+ default)))
+ (if (string-equal stash "")
+ (user-error "Not a stash")
+ (string-match "^stash@{[[:digit:]]+}" stash)
+ (match-string 0 stash)))
+ (user-error "No stashes")))
(defun vc-git-stash-show (name)
"Show the contents of stash NAME."
- (interactive (list (vc-git-stash-read "Show stash: ")))
+ (interactive (list (vc-git-stash-read "Show stash")))
(vc-setup-buffer "*vc-git-stash*")
(vc-git-command "*vc-git-stash*" 'async nil
"stash" "show" "--color=never" "-p" name)
(defun vc-git-stash-apply (name)
"Apply stash NAME."
- (interactive (list (vc-git-stash-read "Apply stash: ")))
+ (interactive (list (vc-git-stash-read "Apply stash")))
(vc-git-command "*vc-git-stash*" 0 nil "stash" "apply" "-q" name)
(vc-resynch-buffer (vc-git-root default-directory) t t))
(defun vc-git-stash-pop (name)
"Pop stash NAME."
- (interactive (list (vc-git-stash-read "Pop stash: ")))
+ ;; Stashes are commonly popped off in reverse order, so pass non-nil
+ ;; DEFAULT-MOST-RECENT to `vc-git-stash-read'.
+ (interactive (list (vc-git-stash-read "Pop stash" t)))
(vc-git-command "*vc-git-stash*" 0 nil "stash" "pop" "-q" name)
(vc-resynch-buffer (vc-git-root default-directory) t t))
(defun vc-git-stash-delete (name)
"Delete stash NAME."
- (interactive (list (vc-git-stash-read "Delete stash: ")))
+ (interactive (list (vc-git-stash-read "Delete stash")))
(vc-git-command "*vc-git-stash*" 0 nil "stash" "drop" "-q" name)
(vc-resynch-buffer (vc-git-root default-directory) t t))