Merge from trunk.
[bpt/emacs.git] / lisp / vc / vc-git.el
1 ;;; vc-git.el --- VC backend for the git version control system -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2006-2013 Free Software Foundation, Inc.
4
5 ;; Author: Alexandre Julliard <julliard@winehq.org>
6 ;; Keywords: vc tools
7 ;; Package: vc
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This file contains a VC backend for the git version control
27 ;; system.
28 ;;
29
30 ;;; Installation:
31
32 ;; To install: put this file on the load-path and add Git to the list
33 ;; of supported backends in `vc-handled-backends'; the following line,
34 ;; placed in your init file, will accomplish this:
35 ;;
36 ;; (add-to-list 'vc-handled-backends 'Git)
37
38 ;;; Todo:
39 ;; - check if more functions could use vc-git-command instead
40 ;; of start-process.
41 ;; - changelog generation
42
43 ;; Implement the rest of the vc interface. See the comment at the
44 ;; beginning of vc.el. The current status is:
45 ;; ("??" means: "figure out what to do about it")
46 ;;
47 ;; FUNCTION NAME STATUS
48 ;; BACKEND PROPERTIES
49 ;; * revision-granularity OK
50 ;; STATE-QUERYING FUNCTIONS
51 ;; * registered (file) OK
52 ;; * state (file) OK
53 ;; - state-heuristic (file) NOT NEEDED
54 ;; * working-revision (file) OK
55 ;; - latest-on-branch-p (file) NOT NEEDED
56 ;; * checkout-model (files) OK
57 ;; - workfile-unchanged-p (file) OK
58 ;; - mode-line-string (file) OK
59 ;; STATE-CHANGING FUNCTIONS
60 ;; * create-repo () OK
61 ;; * register (files &optional rev comment) OK
62 ;; - init-revision (file) NOT NEEDED
63 ;; - responsible-p (file) OK
64 ;; - could-register (file) NOT NEEDED, DEFAULT IS GOOD
65 ;; - receive-file (file rev) NOT NEEDED
66 ;; - unregister (file) OK
67 ;; * checkin (files rev comment) OK
68 ;; * find-revision (file rev buffer) OK
69 ;; * checkout (file &optional editable rev) OK
70 ;; * revert (file &optional contents-done) OK
71 ;; - rollback (files) COULD BE SUPPORTED
72 ;; - merge (file rev1 rev2) It would be possible to merge
73 ;; changes into a single file, but
74 ;; when committing they wouldn't
75 ;; be identified as a merge
76 ;; by git, so it's probably
77 ;; not a good idea.
78 ;; - merge-news (file) see `merge'
79 ;; - steal-lock (file &optional revision) NOT NEEDED
80 ;; HISTORY FUNCTIONS
81 ;; * print-log (files buffer &optional shortlog start-revision limit) OK
82 ;; - log-view-mode () OK
83 ;; - show-log-entry (revision) OK
84 ;; - comment-history (file) ??
85 ;; - update-changelog (files) COULD BE SUPPORTED
86 ;; * diff (file &optional rev1 rev2 buffer) OK
87 ;; - revision-completion-table (files) OK
88 ;; - annotate-command (file buf &optional rev) OK
89 ;; - annotate-time () OK
90 ;; - annotate-current-time () NOT NEEDED
91 ;; - annotate-extract-revision-at-line () OK
92 ;; TAG SYSTEM
93 ;; - create-tag (dir name branchp) OK
94 ;; - retrieve-tag (dir name update) OK
95 ;; MISCELLANEOUS
96 ;; - make-version-backups-p (file) NOT NEEDED
97 ;; - repository-hostname (dirname) NOT NEEDED
98 ;; - previous-revision (file rev) OK
99 ;; - next-revision (file rev) OK
100 ;; - check-headers () COULD BE SUPPORTED
101 ;; - clear-headers () NOT NEEDED
102 ;; - delete-file (file) OK
103 ;; - rename-file (old new) OK
104 ;; - find-file-hook () NOT NEEDED
105
106 (eval-when-compile
107 (require 'cl-lib)
108 (require 'vc)
109 (require 'vc-dir)
110 (require 'grep))
111
112 (defgroup vc-git nil
113 "VC Git backend."
114 :version "24.1"
115 :group 'vc)
116
117 (defcustom vc-git-diff-switches t
118 "String or list of strings specifying switches for Git diff under VC.
119 If nil, use the value of `vc-diff-switches'. If t, use no switches."
120 :type '(choice (const :tag "Unspecified" nil)
121 (const :tag "None" t)
122 (string :tag "Argument String")
123 (repeat :tag "Argument List" :value ("") string))
124 :version "23.1"
125 :group 'vc-git)
126
127 (defcustom vc-git-program "git"
128 "Name of the Git executable (excluding any arguments)."
129 :version "24.1"
130 :type 'string
131 :group 'vc-git)
132
133 (defcustom vc-git-root-log-format
134 '("%d%h..: %an %ad %s"
135 ;; The first shy group matches the characters drawn by --graph.
136 ;; We use numbered groups because `log-view-message-re' wants the
137 ;; revision number to be group 1.
138 "^\\(?:[*/\\| ]+ \\)?\\(?2: ([^)]+)\\)?\\(?1:[0-9a-z]+\\)..: \
139 \\(?3:.*?\\)[ \t]+\\(?4:[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\)"
140 ((1 'log-view-message-face)
141 (2 'change-log-list nil lax)
142 (3 'change-log-name)
143 (4 'change-log-date)))
144 "Git log format for `vc-print-root-log'.
145 This should be a list (FORMAT REGEXP KEYWORDS), where FORMAT is a
146 format string (which is passed to \"git log\" via the argument
147 \"--pretty=tformat:FORMAT\"), REGEXP is a regular expression
148 matching the resulting Git log output, and KEYWORDS is a list of
149 `font-lock-keywords' for highlighting the Log View buffer."
150 :type '(list string string (repeat sexp))
151 :group 'vc-git
152 :version "24.1")
153
154 (defvar vc-git-commits-coding-system 'utf-8
155 "Default coding system for git commits.")
156
157 ;; History of Git commands.
158 (defvar vc-git-history nil)
159
160 ;;; BACKEND PROPERTIES
161
162 (defun vc-git-revision-granularity () 'repository)
163 (defun vc-git-checkout-model (_files) 'implicit)
164
165 ;;; STATE-QUERYING FUNCTIONS
166
167 ;;;###autoload (defun vc-git-registered (file)
168 ;;;###autoload "Return non-nil if FILE is registered with git."
169 ;;;###autoload (if (vc-find-root file ".git") ; Short cut.
170 ;;;###autoload (progn
171 ;;;###autoload (load "vc-git" nil t)
172 ;;;###autoload (vc-git-registered file))))
173
174 (defun vc-git-registered (file)
175 "Check whether FILE is registered with git."
176 (let ((dir (vc-git-root file)))
177 (when dir
178 (with-temp-buffer
179 (let* (process-file-side-effects
180 ;; Do not use the `file-name-directory' here: git-ls-files
181 ;; sometimes fails to return the correct status for relative
182 ;; path specs.
183 ;; See also: http://marc.info/?l=git&m=125787684318129&w=2
184 (name (file-relative-name file dir))
185 (str (ignore-errors
186 (cd dir)
187 (vc-git--out-ok "ls-files" "-c" "-z" "--" name)
188 ;; If result is empty, use ls-tree to check for deleted
189 ;; file.
190 (when (eq (point-min) (point-max))
191 (vc-git--out-ok "ls-tree" "--name-only" "-z" "HEAD"
192 "--" name))
193 (buffer-string))))
194 (and str
195 (> (length str) (length name))
196 (string= (substring str 0 (1+ (length name)))
197 (concat name "\0"))))))))
198
199 (defun vc-git--state-code (code)
200 "Convert from a string to a added/deleted/modified state."
201 (pcase (string-to-char code)
202 (?M 'edited)
203 (?A 'added)
204 (?D 'removed)
205 (?U 'edited) ;; FIXME
206 (?T 'edited))) ;; FIXME
207
208 (defun vc-git-state (file)
209 "Git-specific version of `vc-state'."
210 ;; FIXME: This can't set 'ignored or 'conflict yet
211 ;; The 'ignored state could be detected with `git ls-files -i -o
212 ;; --exclude-standard` It also can't set 'needs-update or
213 ;; 'needs-merge. The rough equivalent would be that upstream branch
214 ;; for current branch is in fast-forward state i.e. current branch
215 ;; is direct ancestor of corresponding upstream branch, and the file
216 ;; was modified upstream. But we can't check that without a network
217 ;; operation.
218 ;; This assumes that status is known to be not `unregistered' because
219 ;; we've been successfully dispatched here from `vc-state', that
220 ;; means `vc-git-registered' returned t earlier once. Bug#11757
221 (let ((diff (vc-git--run-command-string
222 file "diff-index" "-p" "--raw" "-z" "HEAD" "--")))
223 (if (and diff
224 (string-match ":[0-7]\\{6\\} [0-7]\\{6\\} [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} \\([ADMUT]\\)\0[^\0]+\0\\(.*\n.\\)?"
225 diff))
226 (let ((diff-letter (match-string 1 diff)))
227 (if (not (match-beginning 2))
228 ;; Empty diff: file contents is the same as the HEAD
229 ;; revision, but timestamps are different (eg, file
230 ;; was "touch"ed). Update timestamp in index:
231 (prog1 'up-to-date
232 (vc-git--call nil "add" "--refresh" "--"
233 (file-relative-name file)))
234 (vc-git--state-code diff-letter)))
235 (if (vc-git--empty-db-p) 'added 'up-to-date))))
236
237 (defun vc-git-working-revision (file)
238 "Git-specific version of `vc-working-revision'."
239 (let* (process-file-side-effects
240 (str (vc-git--run-command-string nil "symbolic-ref" "HEAD")))
241 (vc-file-setprop file 'vc-git-detached (null str))
242 (if str
243 (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
244 (match-string 2 str)
245 str)
246 (vc-git--rev-parse "HEAD"))))
247
248 (defun vc-git-workfile-unchanged-p (file)
249 (eq 'up-to-date (vc-git-state file)))
250
251 (defun vc-git-mode-line-string (file)
252 "Return a string for `vc-mode-line' to put in the mode line for FILE."
253 (let* ((rev (vc-working-revision file))
254 (detached (vc-file-getprop file 'vc-git-detached))
255 (def-ml (vc-default-mode-line-string 'Git file))
256 (help-echo (get-text-property 0 'help-echo def-ml)))
257 (propertize (if detached
258 (substring def-ml 0 (- 7 (length rev)))
259 def-ml)
260 'help-echo (concat help-echo "\nCurrent revision: " rev))))
261
262 (cl-defstruct (vc-git-extra-fileinfo
263 (:copier nil)
264 (:constructor vc-git-create-extra-fileinfo
265 (old-perm new-perm &optional rename-state orig-name))
266 (:conc-name vc-git-extra-fileinfo->))
267 old-perm new-perm ;; Permission flags.
268 rename-state ;; Rename or copy state.
269 orig-name) ;; Original name for renames or copies.
270
271 (defun vc-git-escape-file-name (name)
272 "Escape a file name if necessary."
273 (if (string-match "[\n\t\"\\]" name)
274 (concat "\""
275 (mapconcat (lambda (c)
276 (pcase c
277 (?\n "\\n")
278 (?\t "\\t")
279 (?\\ "\\\\")
280 (?\" "\\\"")
281 (_ (char-to-string c))))
282 name "")
283 "\"")
284 name))
285
286 (defun vc-git-file-type-as-string (old-perm new-perm)
287 "Return a string describing the file type based on its permissions."
288 (let* ((old-type (lsh (or old-perm 0) -9))
289 (new-type (lsh (or new-perm 0) -9))
290 (str (pcase new-type
291 (?\100 ;; File.
292 (pcase old-type
293 (?\100 nil)
294 (?\120 " (type change symlink -> file)")
295 (?\160 " (type change subproject -> file)")))
296 (?\120 ;; Symlink.
297 (pcase old-type
298 (?\100 " (type change file -> symlink)")
299 (?\160 " (type change subproject -> symlink)")
300 (t " (symlink)")))
301 (?\160 ;; Subproject.
302 (pcase old-type
303 (?\100 " (type change file -> subproject)")
304 (?\120 " (type change symlink -> subproject)")
305 (t " (subproject)")))
306 (?\110 nil) ;; Directory (internal, not a real git state).
307 (?\000 ;; Deleted or unknown.
308 (pcase old-type
309 (?\120 " (symlink)")
310 (?\160 " (subproject)")))
311 (_ (format " (unknown type %o)" new-type)))))
312 (cond (str (propertize str 'face 'font-lock-comment-face))
313 ((eq new-type ?\110) "/")
314 (t ""))))
315
316 (defun vc-git-rename-as-string (state extra)
317 "Return a string describing the copy or rename associated with INFO,
318 or an empty string if none."
319 (let ((rename-state (when extra
320 (vc-git-extra-fileinfo->rename-state extra))))
321 (if rename-state
322 (propertize
323 (concat " ("
324 (if (eq rename-state 'copy) "copied from "
325 (if (eq state 'added) "renamed from "
326 "renamed to "))
327 (vc-git-escape-file-name
328 (vc-git-extra-fileinfo->orig-name extra))
329 ")")
330 'face 'font-lock-comment-face)
331 "")))
332
333 (defun vc-git-permissions-as-string (old-perm new-perm)
334 "Format a permission change as string."
335 (propertize
336 (if (or (not old-perm)
337 (not new-perm)
338 (eq 0 (logand ?\111 (logxor old-perm new-perm))))
339 " "
340 (if (eq 0 (logand ?\111 old-perm)) "+x" "-x"))
341 'face 'font-lock-type-face))
342
343 (defun vc-git-dir-printer (info)
344 "Pretty-printer for the vc-dir-fileinfo structure."
345 (let* ((isdir (vc-dir-fileinfo->directory info))
346 (state (if isdir "" (vc-dir-fileinfo->state info)))
347 (extra (vc-dir-fileinfo->extra info))
348 (old-perm (when extra (vc-git-extra-fileinfo->old-perm extra)))
349 (new-perm (when extra (vc-git-extra-fileinfo->new-perm extra))))
350 (insert
351 " "
352 (propertize (format "%c" (if (vc-dir-fileinfo->marked info) ?* ? ))
353 'face 'font-lock-type-face)
354 " "
355 (propertize
356 (format "%-12s" state)
357 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
358 ((eq state 'missing) 'font-lock-warning-face)
359 (t 'font-lock-variable-name-face))
360 'mouse-face 'highlight)
361 " " (vc-git-permissions-as-string old-perm new-perm)
362 " "
363 (propertize (vc-git-escape-file-name (vc-dir-fileinfo->name info))
364 'face (if isdir 'font-lock-comment-delimiter-face
365 'font-lock-function-name-face)
366 'help-echo
367 (if isdir
368 "Directory\nVC operations can be applied to it\nmouse-3: Pop-up menu"
369 "File\nmouse-3: Pop-up menu")
370 'keymap vc-dir-filename-mouse-map
371 'mouse-face 'highlight)
372 (vc-git-file-type-as-string old-perm new-perm)
373 (vc-git-rename-as-string state extra))))
374
375 (defun vc-git-after-dir-status-stage (stage files update-function)
376 "Process sentinel for the various dir-status stages."
377 (let (next-stage result)
378 (goto-char (point-min))
379 (pcase stage
380 (`update-index
381 (setq next-stage (if (vc-git--empty-db-p) 'ls-files-added
382 (if files 'ls-files-up-to-date 'diff-index))))
383 (`ls-files-added
384 (setq next-stage 'ls-files-unknown)
385 (while (re-search-forward "\\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} 0\t\\([^\0]+\\)\0" nil t)
386 (let ((new-perm (string-to-number (match-string 1) 8))
387 (name (match-string 2)))
388 (push (list name 'added (vc-git-create-extra-fileinfo 0 new-perm))
389 result))))
390 (`ls-files-up-to-date
391 (setq next-stage 'diff-index)
392 (while (re-search-forward "\\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} 0\t\\([^\0]+\\)\0" nil t)
393 (let ((perm (string-to-number (match-string 1) 8))
394 (name (match-string 2)))
395 (push (list name 'up-to-date
396 (vc-git-create-extra-fileinfo perm perm))
397 result))))
398 (`ls-files-unknown
399 (when files (setq next-stage 'ls-files-ignored))
400 (while (re-search-forward "\\([^\0]*?\\)\0" nil t 1)
401 (push (list (match-string 1) 'unregistered
402 (vc-git-create-extra-fileinfo 0 0))
403 result)))
404 (`ls-files-ignored
405 (while (re-search-forward "\\([^\0]*?\\)\0" nil t 1)
406 (push (list (match-string 1) 'ignored
407 (vc-git-create-extra-fileinfo 0 0))
408 result)))
409 (`diff-index
410 (setq next-stage 'ls-files-unknown)
411 (while (re-search-forward
412 ":\\([0-7]\\{6\\}\\) \\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} \\(\\([ADMUT]\\)\0\\([^\0]+\\)\\|\\([CR]\\)[0-9]*\0\\([^\0]+\\)\0\\([^\0]+\\)\\)\0"
413 nil t 1)
414 (let ((old-perm (string-to-number (match-string 1) 8))
415 (new-perm (string-to-number (match-string 2) 8))
416 (state (or (match-string 4) (match-string 6)))
417 (name (or (match-string 5) (match-string 7)))
418 (new-name (match-string 8)))
419 (if new-name ; Copy or rename.
420 (if (eq ?C (string-to-char state))
421 (push (list new-name 'added
422 (vc-git-create-extra-fileinfo old-perm new-perm
423 'copy name))
424 result)
425 (push (list name 'removed
426 (vc-git-create-extra-fileinfo 0 0
427 'rename new-name))
428 result)
429 (push (list new-name 'added
430 (vc-git-create-extra-fileinfo old-perm new-perm
431 'rename name))
432 result))
433 (push (list name (vc-git--state-code state)
434 (vc-git-create-extra-fileinfo old-perm new-perm))
435 result))))))
436 (when result
437 (setq result (nreverse result))
438 (when files
439 (dolist (entry result) (setq files (delete (car entry) files)))
440 (unless files (setq next-stage nil))))
441 (when (or result (not next-stage))
442 (funcall update-function result next-stage))
443 (when next-stage
444 (vc-git-dir-status-goto-stage next-stage files update-function))))
445
446 ;; Follows vc-git-command (or vc-do-async-command), which uses vc-do-command
447 ;; from vc-dispatcher.
448 (declare-function vc-exec-after "vc-dispatcher" (code))
449 ;; Follows vc-exec-after.
450 (declare-function vc-set-async-update "vc-dispatcher" (process-buffer))
451
452 (defun vc-git-dir-status-goto-stage (stage files update-function)
453 (erase-buffer)
454 (pcase stage
455 (`update-index
456 (if files
457 (vc-git-command (current-buffer) 'async files "add" "--refresh" "--")
458 (vc-git-command (current-buffer) 'async nil
459 "update-index" "--refresh")))
460 (`ls-files-added
461 (vc-git-command (current-buffer) 'async files
462 "ls-files" "-z" "-c" "-s" "--"))
463 (`ls-files-up-to-date
464 (vc-git-command (current-buffer) 'async files
465 "ls-files" "-z" "-c" "-s" "--"))
466 (`ls-files-unknown
467 (vc-git-command (current-buffer) 'async files
468 "ls-files" "-z" "-o" "--directory"
469 "--no-empty-directory" "--exclude-standard" "--"))
470 (`ls-files-ignored
471 (vc-git-command (current-buffer) 'async files
472 "ls-files" "-z" "-o" "-i" "--directory"
473 "--no-empty-directory" "--exclude-standard" "--"))
474 ;; --relative added in Git 1.5.5.
475 (`diff-index
476 (vc-git-command (current-buffer) 'async files
477 "diff-index" "--relative" "-z" "-M" "HEAD" "--")))
478 (vc-exec-after
479 `(vc-git-after-dir-status-stage ',stage ',files ',update-function)))
480
481 (defun vc-git-dir-status (_dir update-function)
482 "Return a list of (FILE STATE EXTRA) entries for DIR."
483 ;; Further things that would have to be fixed later:
484 ;; - how to handle unregistered directories
485 ;; - how to support vc-dir on a subdir of the project tree
486 (vc-git-dir-status-goto-stage 'update-index nil update-function))
487
488 (defun vc-git-dir-status-files (_dir files _default-state update-function)
489 "Return a list of (FILE STATE EXTRA) entries for FILES in DIR."
490 (vc-git-dir-status-goto-stage 'update-index files update-function))
491
492 (defvar vc-git-stash-map
493 (let ((map (make-sparse-keymap)))
494 ;; Turn off vc-dir marking
495 (define-key map [mouse-2] 'ignore)
496
497 (define-key map [down-mouse-3] 'vc-git-stash-menu)
498 (define-key map "\C-k" 'vc-git-stash-delete-at-point)
499 (define-key map "=" 'vc-git-stash-show-at-point)
500 (define-key map "\C-m" 'vc-git-stash-show-at-point)
501 (define-key map "A" 'vc-git-stash-apply-at-point)
502 (define-key map "P" 'vc-git-stash-pop-at-point)
503 (define-key map "S" 'vc-git-stash-snapshot)
504 map))
505
506 (defvar vc-git-stash-menu-map
507 (let ((map (make-sparse-keymap "Git Stash")))
508 (define-key map [de]
509 '(menu-item "Delete Stash" vc-git-stash-delete-at-point
510 :help "Delete the current stash"))
511 (define-key map [ap]
512 '(menu-item "Apply Stash" vc-git-stash-apply-at-point
513 :help "Apply the current stash and keep it in the stash list"))
514 (define-key map [po]
515 '(menu-item "Apply and Remove Stash (Pop)" vc-git-stash-pop-at-point
516 :help "Apply the current stash and remove it"))
517 (define-key map [sh]
518 '(menu-item "Show Stash" vc-git-stash-show-at-point
519 :help "Show the contents of the current stash"))
520 map))
521
522 (defun vc-git-dir-extra-headers (_dir)
523 (let ((str (with-output-to-string
524 (with-current-buffer standard-output
525 (vc-git--out-ok "symbolic-ref" "HEAD"))))
526 (stash (vc-git-stash-list))
527 (stash-help-echo "Use M-x vc-git-stash to create stashes.")
528 branch remote remote-url)
529 (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
530 (progn
531 (setq branch (match-string 2 str))
532 (setq remote
533 (with-output-to-string
534 (with-current-buffer standard-output
535 (vc-git--out-ok "config"
536 (concat "branch." branch ".remote")))))
537 (when (string-match "\\([^\n]+\\)" remote)
538 (setq remote (match-string 1 remote)))
539 (when remote
540 (setq remote-url
541 (with-output-to-string
542 (with-current-buffer standard-output
543 (vc-git--out-ok "config"
544 (concat "remote." remote ".url"))))))
545 (when (string-match "\\([^\n]+\\)" remote-url)
546 (setq remote-url (match-string 1 remote-url))))
547 (setq branch "not (detached HEAD)"))
548 ;; FIXME: maybe use a different face when nothing is stashed.
549 (concat
550 (propertize "Branch : " 'face 'font-lock-type-face)
551 (propertize branch
552 'face 'font-lock-variable-name-face)
553 (when remote
554 (concat
555 "\n"
556 (propertize "Remote : " 'face 'font-lock-type-face)
557 (propertize remote-url
558 'face 'font-lock-variable-name-face)))
559 "\n"
560 (if stash
561 (concat
562 (propertize "Stash :\n" 'face 'font-lock-type-face
563 'help-echo stash-help-echo)
564 (mapconcat
565 (lambda (x)
566 (propertize x
567 'face 'font-lock-variable-name-face
568 'mouse-face 'highlight
569 'help-echo "mouse-3: Show stash menu\nRET: Show stash\nA: Apply stash\nP: Apply and remove stash (pop)\nC-k: Delete stash"
570 'keymap vc-git-stash-map))
571 stash "\n"))
572 (concat
573 (propertize "Stash : " 'face 'font-lock-type-face
574 'help-echo stash-help-echo)
575 (propertize "Nothing stashed"
576 'help-echo stash-help-echo
577 'face 'font-lock-variable-name-face))))))
578
579 (defun vc-git-branches ()
580 "Return the existing branches, as a list of strings.
581 The car of the list is the current branch."
582 (with-temp-buffer
583 (vc-git--call t "branch")
584 (goto-char (point-min))
585 (let (current-branch branches)
586 (while (not (eobp))
587 (when (looking-at "^\\([ *]\\) \\(.+\\)$")
588 (if (string-equal (match-string 1) "*")
589 (setq current-branch (match-string 2))
590 (push (match-string 2) branches)))
591 (forward-line 1))
592 (cons current-branch (nreverse branches)))))
593
594 ;;; STATE-CHANGING FUNCTIONS
595
596 (defun vc-git-create-repo ()
597 "Create a new Git repository."
598 (vc-git-command nil 0 nil "init"))
599
600 (defun vc-git-register (files &optional _rev _comment)
601 "Register FILES into the git version-control system."
602 (let (flist dlist)
603 (dolist (crt files)
604 (if (file-directory-p crt)
605 (push crt dlist)
606 (push crt flist)))
607 (when flist
608 (vc-git-command nil 0 flist "update-index" "--add" "--"))
609 (when dlist
610 (vc-git-command nil 0 dlist "add"))))
611
612 (defalias 'vc-git-responsible-p 'vc-git-root)
613
614 (defun vc-git-unregister (file)
615 (vc-git-command nil 0 file "rm" "-f" "--cached" "--"))
616
617 (declare-function log-edit-mode "log-edit" ())
618 (declare-function log-edit-toggle-header "log-edit" (header value))
619 (declare-function log-edit-extract-headers "log-edit" (headers string))
620
621 (defun vc-git-log-edit-toggle-signoff ()
622 "Toggle whether to add the \"Signed-off-by\" line at the end of
623 the commit message."
624 (interactive)
625 (log-edit-toggle-header "Sign-Off" "yes"))
626
627 (defun vc-git-log-edit-toggle-amend ()
628 "Toggle whether this will amend the previous commit.
629 If toggling on, also insert its message into the buffer."
630 (interactive)
631 (when (log-edit-toggle-header "Amend" "yes")
632 (goto-char (point-max))
633 (unless (bolp) (insert "\n"))
634 (insert (with-output-to-string
635 (vc-git-command
636 standard-output 1 nil
637 "log" "--max-count=1" "--pretty=format:%B" "HEAD")))))
638
639 (defvar vc-git-log-edit-mode-map
640 (let ((map (make-sparse-keymap "Git-Log-Edit")))
641 (define-key map "\C-c\C-s" 'vc-git-log-edit-toggle-signoff)
642 (define-key map "\C-c\C-e" 'vc-git-log-edit-toggle-amend)
643 map))
644
645 (define-derived-mode vc-git-log-edit-mode log-edit-mode "Log-Edit/git"
646 "Major mode for editing Git log messages.
647 It is based on `log-edit-mode', and has Git-specific extensions.")
648
649 (defun vc-git-checkin (files _rev comment)
650 (let ((coding-system-for-write vc-git-commits-coding-system))
651 (cl-flet ((boolean-arg-fn
652 (argument)
653 (lambda (value) (when (equal value "yes") (list argument)))))
654 (apply 'vc-git-command nil 0 files
655 (nconc (list "commit" "-m")
656 (log-edit-extract-headers
657 `(("Author" . "--author")
658 ("Date" . "--date")
659 ("Amend" . ,(boolean-arg-fn "--amend"))
660 ("Sign-Off" . ,(boolean-arg-fn "--signoff")))
661 comment)
662 (list "--only" "--"))))))
663
664 (defun vc-git-find-revision (file rev buffer)
665 (let* (process-file-side-effects
666 (coding-system-for-read 'binary)
667 (coding-system-for-write 'binary)
668 (fullname
669 (let ((fn (vc-git--run-command-string
670 file "ls-files" "-z" "--full-name" "--")))
671 ;; ls-files does not return anything when looking for a
672 ;; revision of a file that has been renamed or removed.
673 (if (string= fn "")
674 (file-relative-name file (vc-git-root default-directory))
675 (substring fn 0 -1)))))
676 (vc-git-command
677 buffer 0
678 nil
679 "cat-file" "blob" (concat (if rev rev "HEAD") ":" fullname))))
680
681 (defun vc-git-checkout (file &optional _editable rev)
682 (vc-git-command nil 0 file "checkout" (or rev "HEAD")))
683
684 (defun vc-git-revert (file &optional contents-done)
685 "Revert FILE to the version stored in the git repository."
686 (if contents-done
687 (vc-git-command nil 0 file "update-index" "--")
688 (vc-git-command nil 0 file "reset" "-q" "--")
689 (vc-git-command nil nil file "checkout" "-q" "--")))
690
691 (defvar vc-git-error-regexp-alist
692 '(("^ \\(.+\\) |" 1 nil nil 0))
693 "Value of `compilation-error-regexp-alist' in *vc-git* buffers.")
694
695 (defun vc-git-pull (prompt)
696 "Pull changes into the current Git branch.
697 Normally, this runs \"git pull\". If PROMPT is non-nil, prompt
698 for the Git command to run."
699 (let* ((root (vc-git-root default-directory))
700 (buffer (format "*vc-git : %s*" (expand-file-name root)))
701 (command "pull")
702 (git-program vc-git-program)
703 args)
704 ;; If necessary, prompt for the exact command.
705 (when prompt
706 (setq args (split-string
707 (read-shell-command "Git pull command: "
708 (format "%s pull" git-program)
709 'vc-git-history)
710 " " t))
711 (setq git-program (car args)
712 command (cadr args)
713 args (cddr args)))
714 (apply 'vc-do-async-command buffer root git-program command args)
715 (with-current-buffer buffer (vc-exec-after '(vc-compilation-mode 'git)))
716 (vc-set-async-update buffer)))
717
718 (defun vc-git-merge-branch ()
719 "Merge changes into the current Git branch.
720 This prompts for a branch to merge from."
721 (let* ((root (vc-git-root default-directory))
722 (buffer (format "*vc-git : %s*" (expand-file-name root)))
723 (branches (cdr (vc-git-branches)))
724 (merge-source
725 (completing-read "Merge from branch: "
726 (if (or (member "FETCH_HEAD" branches)
727 (not (file-readable-p
728 (expand-file-name ".git/FETCH_HEAD"
729 root))))
730 branches
731 (cons "FETCH_HEAD" branches))
732 nil t)))
733 (apply 'vc-do-async-command buffer root vc-git-program "merge"
734 (list merge-source))
735 (with-current-buffer buffer (vc-exec-after '(vc-compilation-mode 'git)))
736 (vc-set-async-update buffer)))
737
738 ;;; HISTORY FUNCTIONS
739
740 (autoload 'vc-setup-buffer "vc-dispatcher")
741
742 (defun vc-git-print-log (files buffer &optional shortlog start-revision limit)
743 "Print commit log associated with FILES into specified BUFFER.
744 If SHORTLOG is non-nil, use a short format based on `vc-git-root-log-format'.
745 \(This requires at least Git version 1.5.6, for the --graph option.)
746 If START-REVISION is non-nil, it is the newest revision to show.
747 If LIMIT is non-nil, show no more than this many entries."
748 (let ((coding-system-for-read vc-git-commits-coding-system))
749 ;; `vc-do-command' creates the buffer, but we need it before running
750 ;; the command.
751 (vc-setup-buffer buffer)
752 ;; If the buffer exists from a previous invocation it might be
753 ;; read-only.
754 (let ((inhibit-read-only t))
755 (with-current-buffer
756 buffer
757 (apply 'vc-git-command buffer
758 'async files
759 (append
760 '("log" "--no-color")
761 (when shortlog
762 `("--graph" "--decorate" "--date=short"
763 ,(format "--pretty=tformat:%s"
764 (car vc-git-root-log-format))
765 "--abbrev-commit"))
766 (when limit (list "-n" (format "%s" limit)))
767 (when start-revision (list start-revision))
768 '("--")))))))
769
770 (defun vc-git-log-outgoing (buffer remote-location)
771 (interactive)
772 (vc-git-command
773 buffer 0 nil
774 "log"
775 "--no-color" "--graph" "--decorate" "--date=short"
776 (format "--pretty=tformat:%s" (car vc-git-root-log-format))
777 "--abbrev-commit"
778 (concat (if (string= remote-location "")
779 "@{upstream}"
780 remote-location)
781 "..HEAD")))
782
783 (defun vc-git-log-incoming (buffer remote-location)
784 (interactive)
785 (vc-git-command nil 0 nil "fetch")
786 (vc-git-command
787 buffer 0 nil
788 "log"
789 "--no-color" "--graph" "--decorate" "--date=short"
790 (format "--pretty=tformat:%s" (car vc-git-root-log-format))
791 "--abbrev-commit"
792 (concat "HEAD.." (if (string= remote-location "")
793 "@{upstream}"
794 remote-location))))
795
796 (defvar log-view-message-re)
797 (defvar log-view-file-re)
798 (defvar log-view-font-lock-keywords)
799 (defvar log-view-per-file-logs)
800 (defvar log-view-expanded-log-entry-function)
801
802 (define-derived-mode vc-git-log-view-mode log-view-mode "Git-Log-View"
803 (require 'add-log) ;; We need the faces add-log.
804 ;; Don't have file markers, so use impossible regexp.
805 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
806 (set (make-local-variable 'log-view-per-file-logs) nil)
807 (set (make-local-variable 'log-view-message-re)
808 (if (not (eq vc-log-view-type 'long))
809 (cadr vc-git-root-log-format)
810 "^commit *\\([0-9a-z]+\\)"))
811 ;; Allow expanding short log entries
812 (when (eq vc-log-view-type 'short)
813 (setq truncate-lines t)
814 (set (make-local-variable 'log-view-expanded-log-entry-function)
815 'vc-git-expanded-log-entry))
816 (set (make-local-variable 'log-view-font-lock-keywords)
817 (if (not (eq vc-log-view-type 'long))
818 (list (cons (nth 1 vc-git-root-log-format)
819 (nth 2 vc-git-root-log-format)))
820 (append
821 `((,log-view-message-re (1 'change-log-acknowledgment)))
822 ;; Handle the case:
823 ;; user: foo@bar
824 '(("^Author:[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
825 (1 'change-log-email))
826 ;; Handle the case:
827 ;; user: FirstName LastName <foo@bar>
828 ("^Author:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
829 (1 'change-log-name)
830 (2 'change-log-email))
831 ("^ +\\(?:\\(?:[Aa]cked\\|[Ss]igned-[Oo]ff\\)-[Bb]y:\\)[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
832 (1 'change-log-name))
833 ("^ +\\(?:\\(?:[Aa]cked\\|[Ss]igned-[Oo]ff\\)-[Bb]y:\\)[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
834 (1 'change-log-name)
835 (2 'change-log-email))
836 ("^Merge: \\([0-9a-z]+\\) \\([0-9a-z]+\\)"
837 (1 'change-log-acknowledgment)
838 (2 'change-log-acknowledgment))
839 ("^Date: \\(.+\\)" (1 'change-log-date))
840 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message)))))))
841
842
843 (defun vc-git-show-log-entry (revision)
844 "Move to the log entry for REVISION.
845 REVISION may have the form BRANCH, BRANCH~N,
846 or BRANCH^ (where \"^\" can be repeated)."
847 (goto-char (point-min))
848 (prog1
849 (when revision
850 (search-forward
851 (format "\ncommit %s" revision) nil t
852 (cond ((string-match "~\\([0-9]\\)\\'" revision)
853 (1+ (string-to-number (match-string 1 revision))))
854 ((string-match "\\^+\\'" revision)
855 (1+ (length (match-string 0 revision))))
856 (t nil))))
857 (beginning-of-line)))
858
859 (defun vc-git-expanded-log-entry (revision)
860 (with-temp-buffer
861 (apply 'vc-git-command t nil nil (list "log" revision "-1"))
862 (goto-char (point-min))
863 (unless (eobp)
864 ;; Indent the expanded log entry.
865 (indent-region (point-min) (point-max) 2)
866 (buffer-string))))
867
868 (autoload 'vc-switches "vc")
869
870 (defun vc-git-diff (files &optional rev1 rev2 buffer)
871 "Get a difference report using Git between two revisions of FILES."
872 (let (process-file-side-effects)
873 (apply #'vc-git-command (or buffer "*vc-diff*") 1 files
874 (if (and rev1 rev2) "diff-tree" "diff-index")
875 "--exit-code"
876 (append (vc-switches 'git 'diff)
877 (list "-p" (or rev1 "HEAD") rev2 "--")))))
878
879 (defun vc-git-revision-table (_files)
880 ;; What about `files'?!? --Stef
881 (let (process-file-side-effects
882 (table (list "HEAD")))
883 (with-temp-buffer
884 (vc-git-command t nil nil "for-each-ref" "--format=%(refname)")
885 (goto-char (point-min))
886 (while (re-search-forward "^refs/\\(heads\\|tags\\|remotes\\)/\\(.*\\)$"
887 nil t)
888 (push (match-string 2) table)))
889 table))
890
891 (defun vc-git-revision-completion-table (files)
892 (letrec ((table (lazy-completion-table
893 table (lambda () (vc-git-revision-table files)))))
894 table))
895
896 (defun vc-git-annotate-command (file buf &optional rev)
897 (let ((name (file-relative-name file)))
898 (vc-git-command buf 'async nil "blame" "--date=iso" "-C" "-C" rev "--" name)))
899
900 (declare-function vc-annotate-convert-time "vc-annotate" (time))
901
902 (defun vc-git-annotate-time ()
903 (and (re-search-forward "[0-9a-f]+[^()]+(.* \\([0-9]+\\)-\\([0-9]+\\)-\\([0-9]+\\) \\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\) \\([-+0-9]+\\) +[0-9]+) " nil t)
904 (vc-annotate-convert-time
905 (apply #'encode-time (mapcar (lambda (match)
906 (string-to-number (match-string match)))
907 '(6 5 4 3 2 1 7))))))
908
909 (defun vc-git-annotate-extract-revision-at-line ()
910 (save-excursion
911 (move-beginning-of-line 1)
912 (when (looking-at "\\([0-9a-f^][0-9a-f]+\\) \\(\\([^(]+\\) \\)?")
913 (let ((revision (match-string-no-properties 1)))
914 (if (match-beginning 2)
915 (let ((fname (match-string-no-properties 3)))
916 ;; Remove trailing whitespace from the file name.
917 (when (string-match " +\\'" fname)
918 (setq fname (substring fname 0 (match-beginning 0))))
919 (cons revision
920 (expand-file-name fname (vc-git-root default-directory))))
921 revision)))))
922
923 ;;; TAG SYSTEM
924
925 (defun vc-git-create-tag (dir name branchp)
926 (let ((default-directory dir))
927 (and (vc-git-command nil 0 nil "update-index" "--refresh")
928 (if branchp
929 (vc-git-command nil 0 nil "checkout" "-b" name)
930 (vc-git-command nil 0 nil "tag" name)))))
931
932 (defun vc-git-retrieve-tag (dir name _update)
933 (let ((default-directory dir))
934 (vc-git-command nil 0 nil "checkout" name)
935 ;; FIXME: update buffers if `update' is true
936 ))
937
938
939 ;;; MISCELLANEOUS
940
941 (defun vc-git-previous-revision (file rev)
942 "Git-specific version of `vc-previous-revision'."
943 (if file
944 (let* ((fname (file-relative-name file))
945 (prev-rev (with-temp-buffer
946 (and
947 (vc-git--out-ok "rev-list" "-2" rev "--" fname)
948 (goto-char (point-max))
949 (bolp)
950 (zerop (forward-line -1))
951 (not (bobp))
952 (buffer-substring-no-properties
953 (point)
954 (1- (point-max)))))))
955 (or (vc-git-symbolic-commit prev-rev) prev-rev))
956 (vc-git--rev-parse (concat rev "^"))))
957
958 (defun vc-git--rev-parse (rev)
959 (with-temp-buffer
960 (and
961 (vc-git--out-ok "rev-parse" rev)
962 (buffer-substring-no-properties (point-min) (+ (point-min) 40)))))
963
964 (defun vc-git-next-revision (file rev)
965 "Git-specific version of `vc-next-revision'."
966 (let* ((default-directory (file-name-directory
967 (expand-file-name file)))
968 (file (file-name-nondirectory file))
969 (current-rev
970 (with-temp-buffer
971 (and
972 (vc-git--out-ok "rev-list" "-1" rev "--" file)
973 (goto-char (point-max))
974 (bolp)
975 (zerop (forward-line -1))
976 (bobp)
977 (buffer-substring-no-properties
978 (point)
979 (1- (point-max))))))
980 (next-rev
981 (and current-rev
982 (with-temp-buffer
983 (and
984 (vc-git--out-ok "rev-list" "HEAD" "--" file)
985 (goto-char (point-min))
986 (search-forward current-rev nil t)
987 (zerop (forward-line -1))
988 (buffer-substring-no-properties
989 (point)
990 (progn (forward-line 1) (1- (point)))))))))
991 (or (vc-git-symbolic-commit next-rev) next-rev)))
992
993 (defun vc-git-delete-file (file)
994 (vc-git-command nil 0 file "rm" "-f" "--"))
995
996 (defun vc-git-rename-file (old new)
997 (vc-git-command nil 0 (list old new) "mv" "-f" "--"))
998
999 (defvar vc-git-extra-menu-map
1000 (let ((map (make-sparse-keymap)))
1001 (define-key map [git-grep]
1002 '(menu-item "Git grep..." vc-git-grep
1003 :help "Run the `git grep' command"))
1004 (define-key map [git-sn]
1005 '(menu-item "Stash a Snapshot" vc-git-stash-snapshot
1006 :help "Stash the current state of the tree and keep the current state"))
1007 (define-key map [git-st]
1008 '(menu-item "Create Stash..." vc-git-stash
1009 :help "Stash away changes"))
1010 (define-key map [git-ss]
1011 '(menu-item "Show Stash..." vc-git-stash-show
1012 :help "Show stash contents"))
1013 map))
1014
1015 (defun vc-git-extra-menu () vc-git-extra-menu-map)
1016
1017 (defun vc-git-extra-status-menu () vc-git-extra-menu-map)
1018
1019 (defun vc-git-root (file)
1020 (or (vc-file-getprop file 'git-root)
1021 (vc-file-setprop file 'git-root (vc-find-root file ".git"))))
1022
1023 ;; grep-compute-defaults autoloads grep.
1024 (declare-function grep-read-regexp "grep" ())
1025 (declare-function grep-read-files "grep" (regexp))
1026 (declare-function grep-expand-template "grep"
1027 (template &optional regexp files dir excl))
1028
1029 ;; Derived from `lgrep'.
1030 (defun vc-git-grep (regexp &optional files dir)
1031 "Run git grep, searching for REGEXP in FILES in directory DIR.
1032 The search is limited to file names matching shell pattern FILES.
1033 FILES may use abbreviations defined in `grep-files-aliases', e.g.
1034 entering `ch' is equivalent to `*.[ch]'.
1035
1036 With \\[universal-argument] prefix, you can edit the constructed shell command line
1037 before it is executed.
1038 With two \\[universal-argument] prefixes, directly edit and run `grep-command'.
1039
1040 Collect output in a buffer. While git grep runs asynchronously, you
1041 can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error] \
1042 in the grep output buffer,
1043 to go to the lines where grep found matches.
1044
1045 This command shares argument histories with \\[rgrep] and \\[grep]."
1046 (interactive
1047 (progn
1048 (grep-compute-defaults)
1049 (cond
1050 ((equal current-prefix-arg '(16))
1051 (list (read-from-minibuffer "Run: " "git grep"
1052 nil nil 'grep-history)
1053 nil))
1054 (t (let* ((regexp (grep-read-regexp))
1055 (files (grep-read-files regexp))
1056 (dir (read-directory-name "In directory: "
1057 nil default-directory t)))
1058 (list regexp files dir))))))
1059 (require 'grep)
1060 (when (and (stringp regexp) (> (length regexp) 0))
1061 (let ((command regexp))
1062 (if (null files)
1063 (if (string= command "git grep")
1064 (setq command nil))
1065 (setq dir (file-name-as-directory (expand-file-name dir)))
1066 (setq command
1067 (grep-expand-template "git grep -n -e <R> -- <F>"
1068 regexp files))
1069 (when command
1070 (if (equal current-prefix-arg '(4))
1071 (setq command
1072 (read-from-minibuffer "Confirm: "
1073 command nil nil 'grep-history))
1074 (add-to-history 'grep-history command))))
1075 (when command
1076 (let ((default-directory dir)
1077 (compilation-environment (cons "PAGER=" compilation-environment)))
1078 ;; Setting process-setup-function makes exit-message-function work
1079 ;; even when async processes aren't supported.
1080 (compilation-start command 'grep-mode))
1081 (if (eq next-error-last-buffer (current-buffer))
1082 (setq default-directory dir))))))
1083
1084 ;; Everywhere but here, follows vc-git-command, which uses vc-do-command
1085 ;; from vc-dispatcher.
1086 (autoload 'vc-resynch-buffer "vc-dispatcher")
1087
1088 (defun vc-git-stash (name)
1089 "Create a stash."
1090 (interactive "sStash name: ")
1091 (let ((root (vc-git-root default-directory)))
1092 (when root
1093 (vc-git--call nil "stash" "save" name)
1094 (vc-resynch-buffer root t t))))
1095
1096 (defun vc-git-stash-show (name)
1097 "Show the contents of stash NAME."
1098 (interactive "sStash name: ")
1099 (vc-setup-buffer "*vc-git-stash*")
1100 (vc-git-command "*vc-git-stash*" 'async nil "stash" "show" "-p" name)
1101 (set-buffer "*vc-git-stash*")
1102 (diff-mode)
1103 (setq buffer-read-only t)
1104 (pop-to-buffer (current-buffer)))
1105
1106 (defun vc-git-stash-apply (name)
1107 "Apply stash NAME."
1108 (interactive "sApply stash: ")
1109 (vc-git-command "*vc-git-stash*" 0 nil "stash" "apply" "-q" name)
1110 (vc-resynch-buffer (vc-git-root default-directory) t t))
1111
1112 (defun vc-git-stash-pop (name)
1113 "Pop stash NAME."
1114 (interactive "sPop stash: ")
1115 (vc-git-command "*vc-git-stash*" 0 nil "stash" "pop" "-q" name)
1116 (vc-resynch-buffer (vc-git-root default-directory) t t))
1117
1118 (defun vc-git-stash-snapshot ()
1119 "Create a stash with the current tree state."
1120 (interactive)
1121 (vc-git--call nil "stash" "save"
1122 (let ((ct (current-time)))
1123 (concat
1124 (format-time-string "Snapshot on %Y-%m-%d" ct)
1125 (format-time-string " at %H:%M" ct))))
1126 (vc-git-command "*vc-git-stash*" 0 nil "stash" "apply" "-q" "stash@{0}")
1127 (vc-resynch-buffer (vc-git-root default-directory) t t))
1128
1129 (defun vc-git-stash-list ()
1130 (delete
1131 ""
1132 (split-string
1133 (replace-regexp-in-string
1134 "^stash@" " " (vc-git--run-command-string nil "stash" "list"))
1135 "\n")))
1136
1137 (defun vc-git-stash-get-at-point (point)
1138 (save-excursion
1139 (goto-char point)
1140 (beginning-of-line)
1141 (if (looking-at "^ +\\({[0-9]+}\\):")
1142 (match-string 1)
1143 (error "Cannot find stash at point"))))
1144
1145 ;; vc-git-stash-delete-at-point must be called from a vc-dir buffer.
1146 (declare-function vc-dir-refresh "vc-dir" ())
1147
1148 (defun vc-git-stash-delete-at-point ()
1149 (interactive)
1150 (let ((stash (vc-git-stash-get-at-point (point))))
1151 (when (y-or-n-p (format "Remove stash %s ? " stash))
1152 (vc-git--run-command-string nil "stash" "drop" (format "stash@%s" stash))
1153 (vc-dir-refresh))))
1154
1155 (defun vc-git-stash-show-at-point ()
1156 (interactive)
1157 (vc-git-stash-show (format "stash@%s" (vc-git-stash-get-at-point (point)))))
1158
1159 (defun vc-git-stash-apply-at-point ()
1160 (interactive)
1161 (vc-git-stash-apply (format "stash@%s" (vc-git-stash-get-at-point (point)))))
1162
1163 (defun vc-git-stash-pop-at-point ()
1164 (interactive)
1165 (vc-git-stash-pop (format "stash@%s" (vc-git-stash-get-at-point (point)))))
1166
1167 (defun vc-git-stash-menu (e)
1168 (interactive "e")
1169 (vc-dir-at-event e (popup-menu vc-git-stash-menu-map e)))
1170
1171 \f
1172 ;;; Internal commands
1173
1174 (defun vc-git-command (buffer okstatus file-or-list &rest flags)
1175 "A wrapper around `vc-do-command' for use in vc-git.el.
1176 The difference to vc-do-command is that this function always invokes
1177 `vc-git-program'."
1178 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-git-program
1179 file-or-list (cons "--no-pager" flags)))
1180
1181 (defun vc-git--empty-db-p ()
1182 "Check if the git db is empty (no commit done yet)."
1183 (let (process-file-side-effects)
1184 (not (eq 0 (vc-git--call nil "rev-parse" "--verify" "HEAD")))))
1185
1186 (defun vc-git--call (buffer command &rest args)
1187 ;; We don't need to care the arguments. If there is a file name, it
1188 ;; is always a relative one. This works also for remote
1189 ;; directories. We enable `inhibit-null-byte-detection', otherwise
1190 ;; Tramp's eol conversion might be confused.
1191 (let ((inhibit-null-byte-detection t)
1192 (process-environment (cons "PAGER=" process-environment)))
1193 (apply 'process-file vc-git-program nil buffer nil command args)))
1194
1195 (defun vc-git--out-ok (command &rest args)
1196 (zerop (apply 'vc-git--call '(t nil) command args)))
1197
1198 (defun vc-git--run-command-string (file &rest args)
1199 "Run a git command on FILE and return its output as string.
1200 FILE can be nil."
1201 (let* ((ok t)
1202 (str (with-output-to-string
1203 (with-current-buffer standard-output
1204 (unless (apply 'vc-git--out-ok
1205 (if file
1206 (append args (list (file-relative-name
1207 file)))
1208 args))
1209 (setq ok nil))))))
1210 (and ok str)))
1211
1212 (defun vc-git-symbolic-commit (commit)
1213 "Translate COMMIT string into symbolic form.
1214 Returns nil if not possible."
1215 (and commit
1216 (let ((name (with-temp-buffer
1217 (and
1218 (vc-git--out-ok "name-rev" "--name-only" commit)
1219 (goto-char (point-min))
1220 (= (forward-line 2) 1)
1221 (bolp)
1222 (buffer-substring-no-properties (point-min)
1223 (1- (point-max)))))))
1224 (and name (not (string= name "undefined")) name))))
1225
1226 (provide 'vc-git)
1227
1228 ;;; vc-git.el ends here