]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-git.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / vc / vc-git.el
1 ;;; vc-git.el --- VC backend for the git version control system
2
3 ;; Copyright (C) 2006-2011 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 ~/.emacs, 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)
108 (require 'vc)
109 (require 'vc-dir)
110 (require 'grep))
111
112 (defcustom vc-git-diff-switches t
113 "String or list of strings specifying switches for Git diff under VC.
114 If nil, use the value of `vc-diff-switches'. If t, use no switches."
115 :type '(choice (const :tag "Unspecified" nil)
116 (const :tag "None" t)
117 (string :tag "Argument String")
118 (repeat :tag "Argument List" :value ("") string))
119 :version "23.1"
120 :group 'vc)
121
122 (defvar vc-git-commits-coding-system 'utf-8
123 "Default coding system for git commits.")
124
125 ;;; BACKEND PROPERTIES
126
127 (defun vc-git-revision-granularity () 'repository)
128 (defun vc-git-checkout-model (files) 'implicit)
129
130 ;;; STATE-QUERYING FUNCTIONS
131
132 ;;;###autoload (defun vc-git-registered (file)
133 ;;;###autoload "Return non-nil if FILE is registered with git."
134 ;;;###autoload (if (vc-find-root file ".git") ; Short cut.
135 ;;;###autoload (progn
136 ;;;###autoload (load "vc-git")
137 ;;;###autoload (vc-git-registered file))))
138
139 (defun vc-git-registered (file)
140 "Check whether FILE is registered with git."
141 (let ((dir (vc-git-root file)))
142 (when dir
143 (with-temp-buffer
144 (let* (process-file-side-effects
145 ;; Do not use the `file-name-directory' here: git-ls-files
146 ;; sometimes fails to return the correct status for relative
147 ;; path specs.
148 ;; See also: http://marc.info/?l=git&m=125787684318129&w=2
149 (name (file-relative-name file dir))
150 (str (ignore-errors
151 (cd dir)
152 (vc-git--out-ok "ls-files" "-c" "-z" "--" name)
153 ;; If result is empty, use ls-tree to check for deleted
154 ;; file.
155 (when (eq (point-min) (point-max))
156 (vc-git--out-ok "ls-tree" "--name-only" "-z" "HEAD"
157 "--" name))
158 (buffer-string))))
159 (and str
160 (> (length str) (length name))
161 (string= (substring str 0 (1+ (length name)))
162 (concat name "\0"))))))))
163
164 (defun vc-git--state-code (code)
165 "Convert from a string to a added/deleted/modified state."
166 (case (string-to-char code)
167 (?M 'edited)
168 (?A 'added)
169 (?D 'removed)
170 (?U 'edited) ;; FIXME
171 (?T 'edited))) ;; FIXME
172
173 (defun vc-git-state (file)
174 "Git-specific version of `vc-state'."
175 ;; FIXME: This can't set 'ignored or 'conflict yet
176 ;; The 'ignored state could be detected with `git ls-files -i -o
177 ;; --exclude-standard` It also can't set 'needs-update or
178 ;; 'needs-merge. The rough equivalent would be that upstream branch
179 ;; for current branch is in fast-forward state i.e. current branch
180 ;; is direct ancestor of corresponding upstream branch, and the file
181 ;; was modified upstream. But we can't check that without a network
182 ;; operation.
183 (if (not (vc-git-registered file))
184 'unregistered
185 (vc-git--call nil "add" "--refresh" "--" (file-relative-name file))
186 (let ((diff (vc-git--run-command-string
187 file "diff-index" "-z" "HEAD" "--")))
188 (if (and diff (string-match ":[0-7]\\{6\\} [0-7]\\{6\\} [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} \\([ADMUT]\\)\0[^\0]+\0"
189 diff))
190 (vc-git--state-code (match-string 1 diff))
191 (if (vc-git--empty-db-p) 'added 'up-to-date)))))
192
193 (defun vc-git-working-revision (file)
194 "Git-specific version of `vc-working-revision'."
195 (let* (process-file-side-effects
196 (str (with-output-to-string
197 (with-current-buffer standard-output
198 (vc-git--out-ok "symbolic-ref" "HEAD")))))
199 (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
200 (match-string 2 str)
201 str)))
202
203 (defun vc-git-workfile-unchanged-p (file)
204 (eq 'up-to-date (vc-git-state file)))
205
206 (defun vc-git-mode-line-string (file)
207 "Return string for placement into the modeline for FILE."
208 (let* ((branch (vc-git-working-revision file))
209 (def-ml (vc-default-mode-line-string 'Git file))
210 (help-echo (get-text-property 0 'help-echo def-ml)))
211 (if (zerop (length branch))
212 (propertize
213 (concat def-ml "!")
214 'help-echo (concat help-echo "\nNo current branch (detached HEAD)"))
215 (propertize def-ml
216 'help-echo (concat help-echo "\nCurrent branch: " branch)))))
217
218 (defstruct (vc-git-extra-fileinfo
219 (:copier nil)
220 (:constructor vc-git-create-extra-fileinfo
221 (old-perm new-perm &optional rename-state orig-name))
222 (:conc-name vc-git-extra-fileinfo->))
223 old-perm new-perm ;; Permission flags.
224 rename-state ;; Rename or copy state.
225 orig-name) ;; Original name for renames or copies.
226
227 (defun vc-git-escape-file-name (name)
228 "Escape a file name if necessary."
229 (if (string-match "[\n\t\"\\]" name)
230 (concat "\""
231 (mapconcat (lambda (c)
232 (case c
233 (?\n "\\n")
234 (?\t "\\t")
235 (?\\ "\\\\")
236 (?\" "\\\"")
237 (t (char-to-string c))))
238 name "")
239 "\"")
240 name))
241
242 (defun vc-git-file-type-as-string (old-perm new-perm)
243 "Return a string describing the file type based on its permissions."
244 (let* ((old-type (lsh (or old-perm 0) -9))
245 (new-type (lsh (or new-perm 0) -9))
246 (str (case new-type
247 (?\100 ;; File.
248 (case old-type
249 (?\100 nil)
250 (?\120 " (type change symlink -> file)")
251 (?\160 " (type change subproject -> file)")))
252 (?\120 ;; Symlink.
253 (case old-type
254 (?\100 " (type change file -> symlink)")
255 (?\160 " (type change subproject -> symlink)")
256 (t " (symlink)")))
257 (?\160 ;; Subproject.
258 (case old-type
259 (?\100 " (type change file -> subproject)")
260 (?\120 " (type change symlink -> subproject)")
261 (t " (subproject)")))
262 (?\110 nil) ;; Directory (internal, not a real git state).
263 (?\000 ;; Deleted or unknown.
264 (case old-type
265 (?\120 " (symlink)")
266 (?\160 " (subproject)")))
267 (t (format " (unknown type %o)" new-type)))))
268 (cond (str (propertize str 'face 'font-lock-comment-face))
269 ((eq new-type ?\110) "/")
270 (t ""))))
271
272 (defun vc-git-rename-as-string (state extra)
273 "Return a string describing the copy or rename associated with INFO,
274 or an empty string if none."
275 (let ((rename-state (when extra
276 (vc-git-extra-fileinfo->rename-state extra))))
277 (if rename-state
278 (propertize
279 (concat " ("
280 (if (eq rename-state 'copy) "copied from "
281 (if (eq state 'added) "renamed from "
282 "renamed to "))
283 (vc-git-escape-file-name
284 (vc-git-extra-fileinfo->orig-name extra))
285 ")")
286 'face 'font-lock-comment-face)
287 "")))
288
289 (defun vc-git-permissions-as-string (old-perm new-perm)
290 "Format a permission change as string."
291 (propertize
292 (if (or (not old-perm)
293 (not new-perm)
294 (eq 0 (logand ?\111 (logxor old-perm new-perm))))
295 " "
296 (if (eq 0 (logand ?\111 old-perm)) "+x" "-x"))
297 'face 'font-lock-type-face))
298
299 (defun vc-git-dir-printer (info)
300 "Pretty-printer for the vc-dir-fileinfo structure."
301 (let* ((isdir (vc-dir-fileinfo->directory info))
302 (state (if isdir "" (vc-dir-fileinfo->state info)))
303 (extra (vc-dir-fileinfo->extra info))
304 (old-perm (when extra (vc-git-extra-fileinfo->old-perm extra)))
305 (new-perm (when extra (vc-git-extra-fileinfo->new-perm extra))))
306 (insert
307 " "
308 (propertize (format "%c" (if (vc-dir-fileinfo->marked info) ?* ? ))
309 'face 'font-lock-type-face)
310 " "
311 (propertize
312 (format "%-12s" state)
313 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
314 ((eq state 'missing) 'font-lock-warning-face)
315 (t 'font-lock-variable-name-face))
316 'mouse-face 'highlight)
317 " " (vc-git-permissions-as-string old-perm new-perm)
318 " "
319 (propertize (vc-git-escape-file-name (vc-dir-fileinfo->name info))
320 'face (if isdir 'font-lock-comment-delimiter-face
321 'font-lock-function-name-face)
322 'help-echo
323 (if isdir
324 "Directory\nVC operations can be applied to it\nmouse-3: Pop-up menu"
325 "File\nmouse-3: Pop-up menu")
326 'keymap vc-dir-filename-mouse-map
327 'mouse-face 'highlight)
328 (vc-git-file-type-as-string old-perm new-perm)
329 (vc-git-rename-as-string state extra))))
330
331 (defun vc-git-after-dir-status-stage (stage files update-function)
332 "Process sentinel for the various dir-status stages."
333 (let (next-stage result)
334 (goto-char (point-min))
335 (case stage
336 (update-index
337 (setq next-stage (if (vc-git--empty-db-p) 'ls-files-added
338 (if files 'ls-files-up-to-date 'diff-index))))
339 (ls-files-added
340 (setq next-stage 'ls-files-unknown)
341 (while (re-search-forward "\\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} 0\t\\([^\0]+\\)\0" nil t)
342 (let ((new-perm (string-to-number (match-string 1) 8))
343 (name (match-string 2)))
344 (push (list name 'added (vc-git-create-extra-fileinfo 0 new-perm))
345 result))))
346 (ls-files-up-to-date
347 (setq next-stage 'diff-index)
348 (while (re-search-forward "\\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} 0\t\\([^\0]+\\)\0" nil t)
349 (let ((perm (string-to-number (match-string 1) 8))
350 (name (match-string 2)))
351 (push (list name 'up-to-date
352 (vc-git-create-extra-fileinfo perm perm))
353 result))))
354 (ls-files-unknown
355 (when files (setq next-stage 'ls-files-ignored))
356 (while (re-search-forward "\\([^\0]*?\\)\0" nil t 1)
357 (push (list (match-string 1) 'unregistered
358 (vc-git-create-extra-fileinfo 0 0))
359 result)))
360 (ls-files-ignored
361 (while (re-search-forward "\\([^\0]*?\\)\0" nil t 1)
362 (push (list (match-string 1) 'ignored
363 (vc-git-create-extra-fileinfo 0 0))
364 result)))
365 (diff-index
366 (setq next-stage 'ls-files-unknown)
367 (while (re-search-forward
368 ":\\([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"
369 nil t 1)
370 (let ((old-perm (string-to-number (match-string 1) 8))
371 (new-perm (string-to-number (match-string 2) 8))
372 (state (or (match-string 4) (match-string 6)))
373 (name (or (match-string 5) (match-string 7)))
374 (new-name (match-string 8)))
375 (if new-name ; Copy or rename.
376 (if (eq ?C (string-to-char state))
377 (push (list new-name 'added
378 (vc-git-create-extra-fileinfo old-perm new-perm
379 'copy name))
380 result)
381 (push (list name 'removed
382 (vc-git-create-extra-fileinfo 0 0
383 'rename new-name))
384 result)
385 (push (list new-name 'added
386 (vc-git-create-extra-fileinfo old-perm new-perm
387 'rename name))
388 result))
389 (push (list name (vc-git--state-code state)
390 (vc-git-create-extra-fileinfo old-perm new-perm))
391 result))))))
392 (when result
393 (setq result (nreverse result))
394 (when files
395 (dolist (entry result) (setq files (delete (car entry) files)))
396 (unless files (setq next-stage nil))))
397 (when (or result (not next-stage))
398 (funcall update-function result next-stage))
399 (when next-stage
400 (vc-git-dir-status-goto-stage next-stage files update-function))))
401
402 (defun vc-git-dir-status-goto-stage (stage files update-function)
403 (erase-buffer)
404 (case stage
405 (update-index
406 (if files
407 (vc-git-command (current-buffer) 'async files "add" "--refresh" "--")
408 (vc-git-command (current-buffer) 'async nil
409 "update-index" "--refresh")))
410 (ls-files-added
411 (vc-git-command (current-buffer) 'async files
412 "ls-files" "-z" "-c" "-s" "--"))
413 (ls-files-up-to-date
414 (vc-git-command (current-buffer) 'async files
415 "ls-files" "-z" "-c" "-s" "--"))
416 (ls-files-unknown
417 (vc-git-command (current-buffer) 'async files
418 "ls-files" "-z" "-o" "--directory"
419 "--no-empty-directory" "--exclude-standard" "--"))
420 (ls-files-ignored
421 (vc-git-command (current-buffer) 'async files
422 "ls-files" "-z" "-o" "-i" "--directory"
423 "--no-empty-directory" "--exclude-standard" "--"))
424 ;; --relative added in Git 1.5.5.
425 (diff-index
426 (vc-git-command (current-buffer) 'async files
427 "diff-index" "--relative" "-z" "-M" "HEAD" "--")))
428 (vc-exec-after
429 `(vc-git-after-dir-status-stage ',stage ',files ',update-function)))
430
431 (defun vc-git-dir-status (dir update-function)
432 "Return a list of (FILE STATE EXTRA) entries for DIR."
433 ;; Further things that would have to be fixed later:
434 ;; - how to handle unregistered directories
435 ;; - how to support vc-dir on a subdir of the project tree
436 (vc-git-dir-status-goto-stage 'update-index nil update-function))
437
438 (defun vc-git-dir-status-files (dir files default-state update-function)
439 "Return a list of (FILE STATE EXTRA) entries for FILES in DIR."
440 (vc-git-dir-status-goto-stage 'update-index files update-function))
441
442 (defvar vc-git-stash-map
443 (let ((map (make-sparse-keymap)))
444 ;; Turn off vc-dir marking
445 (define-key map [mouse-2] 'ignore)
446
447 (define-key map [down-mouse-3] 'vc-git-stash-menu)
448 (define-key map "\C-k" 'vc-git-stash-delete-at-point)
449 (define-key map "=" 'vc-git-stash-show-at-point)
450 (define-key map "\C-m" 'vc-git-stash-show-at-point)
451 (define-key map "A" 'vc-git-stash-apply-at-point)
452 (define-key map "P" 'vc-git-stash-pop-at-point)
453 (define-key map "S" 'vc-git-stash-snapshot)
454 map))
455
456 (defvar vc-git-stash-menu-map
457 (let ((map (make-sparse-keymap "Git Stash")))
458 (define-key map [de]
459 '(menu-item "Delete stash" vc-git-stash-delete-at-point
460 :help "Delete the current stash"))
461 (define-key map [ap]
462 '(menu-item "Apply stash" vc-git-stash-apply-at-point
463 :help "Apply the current stash and keep it in the stash list"))
464 (define-key map [po]
465 '(menu-item "Apply and remove stash (pop)" vc-git-stash-pop-at-point
466 :help "Apply the current stash and remove it"))
467 (define-key map [sh]
468 '(menu-item "Show stash" vc-git-stash-show-at-point
469 :help "Show the contents of the current stash"))
470 map))
471
472 (defun vc-git-dir-extra-headers (dir)
473 (let ((str (with-output-to-string
474 (with-current-buffer standard-output
475 (vc-git--out-ok "symbolic-ref" "HEAD"))))
476 (stash (vc-git-stash-list))
477 (stash-help-echo "Use M-x vc-git-stash to create stashes.")
478 branch remote remote-url)
479 (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str)
480 (progn
481 (setq branch (match-string 2 str))
482 (setq remote
483 (with-output-to-string
484 (with-current-buffer standard-output
485 (vc-git--out-ok "config"
486 (concat "branch." branch ".remote")))))
487 (when (string-match "\\([^\n]+\\)" remote)
488 (setq remote (match-string 1 remote)))
489 (when remote
490 (setq remote-url
491 (with-output-to-string
492 (with-current-buffer standard-output
493 (vc-git--out-ok "config"
494 (concat "remote." remote ".url"))))))
495 (when (string-match "\\([^\n]+\\)" remote-url)
496 (setq remote-url (match-string 1 remote-url))))
497 (setq branch "not (detached HEAD)"))
498 ;; FIXME: maybe use a different face when nothing is stashed.
499 (concat
500 (propertize "Branch : " 'face 'font-lock-type-face)
501 (propertize branch
502 'face 'font-lock-variable-name-face)
503 (when remote
504 (concat
505 "\n"
506 (propertize "Remote : " 'face 'font-lock-type-face)
507 (propertize remote-url
508 'face 'font-lock-variable-name-face)))
509 "\n"
510 (if stash
511 (concat
512 (propertize "Stash :\n" 'face 'font-lock-type-face
513 'help-echo stash-help-echo)
514 (mapconcat
515 (lambda (x)
516 (propertize x
517 'face 'font-lock-variable-name-face
518 'mouse-face 'highlight
519 'help-echo "mouse-3: Show stash menu\nRET: Show stash\nA: Apply stash\nP: Apply and remove stash (pop)\nC-k: Delete stash"
520 'keymap vc-git-stash-map))
521 stash "\n"))
522 (concat
523 (propertize "Stash : " 'face 'font-lock-type-face
524 'help-echo stash-help-echo)
525 (propertize "Nothing stashed"
526 'help-echo stash-help-echo
527 'face 'font-lock-variable-name-face))))))
528
529 ;;; STATE-CHANGING FUNCTIONS
530
531 (defun vc-git-create-repo ()
532 "Create a new Git repository."
533 (vc-git-command nil 0 nil "init"))
534
535 (defun vc-git-register (files &optional rev comment)
536 "Register FILES into the git version-control system."
537 (let (flist dlist)
538 (dolist (crt files)
539 (if (file-directory-p crt)
540 (push crt dlist)
541 (push crt flist)))
542 (when flist
543 (vc-git-command nil 0 flist "update-index" "--add" "--"))
544 (when dlist
545 (vc-git-command nil 0 dlist "add"))))
546
547 (defalias 'vc-git-responsible-p 'vc-git-root)
548
549 (defun vc-git-unregister (file)
550 (vc-git-command nil 0 file "rm" "-f" "--cached" "--"))
551
552 (declare-function log-edit-extract-headers "log-edit" (headers string))
553
554 (defun vc-git-checkin (files rev comment)
555 (let ((coding-system-for-write vc-git-commits-coding-system))
556 (apply 'vc-git-command nil 0 files
557 (nconc (list "commit" "-m")
558 (log-edit-extract-headers '(("Author" . "--author")
559 ("Date" . "--date"))
560 comment)
561 (list "--only" "--")))))
562
563 (defun vc-git-find-revision (file rev buffer)
564 (let* (process-file-side-effects
565 (coding-system-for-read 'binary)
566 (coding-system-for-write 'binary)
567 (fullname
568 (let ((fn (vc-git--run-command-string
569 file "ls-files" "-z" "--full-name" "--")))
570 ;; ls-files does not return anything when looking for a
571 ;; revision of a file that has been renamed or removed.
572 (if (string= fn "")
573 (file-relative-name file (vc-git-root default-directory))
574 (substring fn 0 -1)))))
575 (vc-git-command
576 buffer 0
577 nil
578 "cat-file" "blob" (concat (if rev rev "HEAD") ":" fullname))))
579
580 (defun vc-git-checkout (file &optional editable rev)
581 (vc-git-command nil 0 file "checkout" (or rev "HEAD")))
582
583 (defun vc-git-revert (file &optional contents-done)
584 "Revert FILE to the version stored in the git repository."
585 (if contents-done
586 (vc-git-command nil 0 file "update-index" "--")
587 (vc-git-command nil 0 file "reset" "-q" "--")
588 (vc-git-command nil nil file "checkout" "-q" "--")))
589
590 ;;; HISTORY FUNCTIONS
591
592 (defun vc-git-print-log (files buffer &optional shortlog start-revision limit)
593 "Get change log associated with FILES.
594 Note that using SHORTLOG requires at least Git version 1.5.6,
595 for the --graph option."
596 (let ((coding-system-for-read vc-git-commits-coding-system))
597 ;; `vc-do-command' creates the buffer, but we need it before running
598 ;; the command.
599 (vc-setup-buffer buffer)
600 ;; If the buffer exists from a previous invocation it might be
601 ;; read-only.
602 (let ((inhibit-read-only t))
603 (with-current-buffer
604 buffer
605 (apply 'vc-git-command buffer
606 'async files
607 (append
608 '("log" "--no-color")
609 (when shortlog
610 '("--graph" "--decorate" "--date=short"
611 "--pretty=tformat:%d%h %ad %s" "--abbrev-commit"))
612 (when limit (list "-n" (format "%s" limit)))
613 (when start-revision (list start-revision))
614 '("--")))))))
615
616 (defun vc-git-log-outgoing (buffer remote-location)
617 (interactive)
618 (vc-git-command
619 buffer 0 nil
620 "log"
621 "--no-color" "--graph" "--decorate" "--date=short"
622 "--pretty=tformat:%d%h %ad %s" "--abbrev-commit"
623 (concat (if (string= remote-location "")
624 "@{upstream}"
625 remote-location)
626 "..HEAD")))
627
628 (defun vc-git-log-incoming (buffer remote-location)
629 (interactive)
630 (vc-git-command nil 0 nil "fetch")
631 (vc-git-command
632 buffer 0 nil
633 "log"
634 "--no-color" "--graph" "--decorate" "--date=short"
635 "--pretty=tformat:%d%h %ad %s" "--abbrev-commit"
636 (concat "HEAD.." (if (string= remote-location "")
637 "@{upstream}"
638 remote-location))))
639
640 (defvar log-view-message-re)
641 (defvar log-view-file-re)
642 (defvar log-view-font-lock-keywords)
643 (defvar log-view-per-file-logs)
644
645 (define-derived-mode vc-git-log-view-mode log-view-mode "Git-Log-View"
646 (require 'add-log) ;; We need the faces add-log.
647 ;; Don't have file markers, so use impossible regexp.
648 (set (make-local-variable 'log-view-file-re) "\\`a\\`")
649 (set (make-local-variable 'log-view-per-file-logs) nil)
650 (set (make-local-variable 'log-view-message-re)
651 (if (not (eq vc-log-view-type 'long))
652 "^\\(?:[*/\\| ]+ \\)?\\(?: ([^)]+)\\)?\\([0-9a-z]+\\) \\([-a-z0-9]+\\) \\(.*\\)"
653 "^commit *\\([0-9a-z]+\\)"))
654 (set (make-local-variable 'log-view-font-lock-keywords)
655 (if (not (eq vc-log-view-type 'long))
656 '(
657 ;; Same as log-view-message-re, except that we don't
658 ;; want the shy group for the tag name.
659 ("^\\(?:[*/\\| ]+ \\)?\\( ([^)]+)\\)?\\([0-9a-z]+\\) \\([-a-z0-9]+\\) \\(.*\\)"
660 (1 'highlight nil lax)
661 (2 'change-log-acknowledgement)
662 (3 'change-log-date)))
663 (append
664 `((,log-view-message-re (1 'change-log-acknowledgement)))
665 ;; Handle the case:
666 ;; user: foo@bar
667 '(("^Author:[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
668 (1 'change-log-email))
669 ;; Handle the case:
670 ;; user: FirstName LastName <foo@bar>
671 ("^Author:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
672 (1 'change-log-name)
673 (2 'change-log-email))
674 ("^ +\\(?:\\(?:[Aa]cked\\|[Ss]igned-[Oo]ff\\)-[Bb]y:\\)[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
675 (1 'change-log-name))
676 ("^ +\\(?:\\(?:[Aa]cked\\|[Ss]igned-[Oo]ff\\)-[Bb]y:\\)[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
677 (1 'change-log-name)
678 (2 'change-log-email))
679 ("^Merge: \\([0-9a-z]+\\) \\([0-9a-z]+\\)"
680 (1 'change-log-acknowledgement)
681 (2 'change-log-acknowledgement))
682 ("^Date: \\(.+\\)" (1 'change-log-date))
683 ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message)))))))
684
685
686 (defun vc-git-show-log-entry (revision)
687 "Move to the log entry for REVISION.
688 REVISION may have the form BRANCH, BRANCH~N,
689 or BRANCH^ (where \"^\" can be repeated)."
690 (goto-char (point-min))
691 (prog1
692 (when revision
693 (search-forward
694 (format "\ncommit %s" revision) nil t
695 (cond ((string-match "~\\([0-9]\\)\\'" revision)
696 (1+ (string-to-number (match-string 1 revision))))
697 ((string-match "\\^+\\'" revision)
698 (1+ (length (match-string 0 revision))))
699 (t nil))))
700 (beginning-of-line)))
701
702 (defun vc-git-diff (files &optional rev1 rev2 buffer)
703 "Get a difference report using Git between two revisions of FILES."
704 (let (process-file-side-effects)
705 (apply #'vc-git-command (or buffer "*vc-diff*") 1 files
706 (if (and rev1 rev2) "diff-tree" "diff-index")
707 "--exit-code"
708 (append (vc-switches 'git 'diff)
709 (list "-p" (or rev1 "HEAD") rev2 "--")))))
710
711 (defun vc-git-revision-table (files)
712 ;; What about `files'?!? --Stef
713 (let (process-file-side-effects
714 (table (list "HEAD")))
715 (with-temp-buffer
716 (vc-git-command t nil nil "for-each-ref" "--format=%(refname)")
717 (goto-char (point-min))
718 (while (re-search-forward "^refs/\\(heads\\|tags\\|remotes\\)/\\(.*\\)$"
719 nil t)
720 (push (match-string 2) table)))
721 table))
722
723 (defun vc-git-revision-completion-table (files)
724 (lexical-let ((files files)
725 table)
726 (setq table (lazy-completion-table
727 table (lambda () (vc-git-revision-table files))))
728 table))
729
730 (defun vc-git-annotate-command (file buf &optional rev)
731 (let ((name (file-relative-name file)))
732 (vc-git-command buf 'async nil "blame" "--date=iso" "-C" "-C" rev "--" name)))
733
734 (declare-function vc-annotate-convert-time "vc-annotate" (time))
735
736 (defun vc-git-annotate-time ()
737 (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)
738 (vc-annotate-convert-time
739 (apply #'encode-time (mapcar (lambda (match)
740 (string-to-number (match-string match)))
741 '(6 5 4 3 2 1 7))))))
742
743 (defun vc-git-annotate-extract-revision-at-line ()
744 (save-excursion
745 (move-beginning-of-line 1)
746 (when (looking-at "\\([0-9a-f^][0-9a-f]+\\) \\(\\([^(]+\\) \\)?")
747 (let ((revision (match-string-no-properties 1)))
748 (if (match-beginning 2)
749 (let ((fname (match-string-no-properties 3)))
750 ;; Remove trailing whitespace from the file name.
751 (when (string-match " +\\'" fname)
752 (setq fname (substring fname 0 (match-beginning 0))))
753 (cons revision
754 (expand-file-name fname (vc-git-root default-directory))))
755 revision)))))
756
757 ;;; TAG SYSTEM
758
759 (defun vc-git-create-tag (dir name branchp)
760 (let ((default-directory dir))
761 (and (vc-git-command nil 0 nil "update-index" "--refresh")
762 (if branchp
763 (vc-git-command nil 0 nil "checkout" "-b" name)
764 (vc-git-command nil 0 nil "tag" name)))))
765
766 (defun vc-git-retrieve-tag (dir name update)
767 (let ((default-directory dir))
768 (vc-git-command nil 0 nil "checkout" name)
769 ;; FIXME: update buffers if `update' is true
770 ))
771
772
773 ;;; MISCELLANEOUS
774
775 (defun vc-git-previous-revision (file rev)
776 "Git-specific version of `vc-previous-revision'."
777 (if file
778 (let* ((fname (file-relative-name file))
779 (prev-rev (with-temp-buffer
780 (and
781 (vc-git--out-ok "rev-list" "-2" rev "--" fname)
782 (goto-char (point-max))
783 (bolp)
784 (zerop (forward-line -1))
785 (not (bobp))
786 (buffer-substring-no-properties
787 (point)
788 (1- (point-max)))))))
789 (or (vc-git-symbolic-commit prev-rev) prev-rev))
790 (with-temp-buffer
791 (and
792 (vc-git--out-ok "rev-parse" (concat rev "^"))
793 (buffer-substring-no-properties (point-min) (+ (point-min) 40))))))
794
795 (defun vc-git-next-revision (file rev)
796 "Git-specific version of `vc-next-revision'."
797 (let* ((default-directory (file-name-directory
798 (expand-file-name file)))
799 (file (file-name-nondirectory file))
800 (current-rev
801 (with-temp-buffer
802 (and
803 (vc-git--out-ok "rev-list" "-1" rev "--" file)
804 (goto-char (point-max))
805 (bolp)
806 (zerop (forward-line -1))
807 (bobp)
808 (buffer-substring-no-properties
809 (point)
810 (1- (point-max))))))
811 (next-rev
812 (and current-rev
813 (with-temp-buffer
814 (and
815 (vc-git--out-ok "rev-list" "HEAD" "--" file)
816 (goto-char (point-min))
817 (search-forward current-rev nil t)
818 (zerop (forward-line -1))
819 (buffer-substring-no-properties
820 (point)
821 (progn (forward-line 1) (1- (point)))))))))
822 (or (vc-git-symbolic-commit next-rev) next-rev)))
823
824 (defun vc-git-delete-file (file)
825 (vc-git-command nil 0 file "rm" "-f" "--"))
826
827 (defun vc-git-rename-file (old new)
828 (vc-git-command nil 0 (list old new) "mv" "-f" "--"))
829
830 (defvar vc-git-extra-menu-map
831 (let ((map (make-sparse-keymap)))
832 (define-key map [git-grep]
833 '(menu-item "Git grep..." vc-git-grep
834 :help "Run the `git grep' command"))
835 (define-key map [git-sn]
836 '(menu-item "Stash a snapshot" vc-git-stash-snapshot
837 :help "Stash the current state of the tree and keep the current state"))
838 (define-key map [git-st]
839 '(menu-item "Create Stash..." vc-git-stash
840 :help "Stash away changes"))
841 (define-key map [git-ss]
842 '(menu-item "Show Stash..." vc-git-stash-show
843 :help "Show stash contents"))
844 map))
845
846 (defun vc-git-extra-menu () vc-git-extra-menu-map)
847
848 (defun vc-git-extra-status-menu () vc-git-extra-menu-map)
849
850 (defun vc-git-root (file)
851 (vc-find-root file ".git"))
852
853 ;; Derived from `lgrep'.
854 (defun vc-git-grep (regexp &optional files dir)
855 "Run git grep, searching for REGEXP in FILES in directory DIR.
856 The search is limited to file names matching shell pattern FILES.
857 FILES may use abbreviations defined in `grep-files-aliases', e.g.
858 entering `ch' is equivalent to `*.[ch]'.
859
860 With \\[universal-argument] prefix, you can edit the constructed shell command line
861 before it is executed.
862 With two \\[universal-argument] prefixes, directly edit and run `grep-command'.
863
864 Collect output in a buffer. While git grep runs asynchronously, you
865 can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error] \
866 in the grep output buffer,
867 to go to the lines where grep found matches.
868
869 This command shares argument histories with \\[rgrep] and \\[grep]."
870 (interactive
871 (progn
872 (grep-compute-defaults)
873 (cond
874 ((equal current-prefix-arg '(16))
875 (list (read-from-minibuffer "Run: " "git grep"
876 nil nil 'grep-history)
877 nil))
878 (t (let* ((regexp (grep-read-regexp))
879 (files (grep-read-files regexp))
880 (dir (read-directory-name "In directory: "
881 nil default-directory t)))
882 (list regexp files dir))))))
883 (require 'grep)
884 (when (and (stringp regexp) (> (length regexp) 0))
885 (let ((command regexp))
886 (if (null files)
887 (if (string= command "git grep")
888 (setq command nil))
889 (setq dir (file-name-as-directory (expand-file-name dir)))
890 (setq command
891 (grep-expand-template "git grep -n -e <R> -- <F>" regexp files))
892 (when command
893 (if (equal current-prefix-arg '(4))
894 (setq command
895 (read-from-minibuffer "Confirm: "
896 command nil nil 'grep-history))
897 (add-to-history 'grep-history command))))
898 (when command
899 (let ((default-directory dir)
900 (compilation-environment '("PAGER=")))
901 ;; Setting process-setup-function makes exit-message-function work
902 ;; even when async processes aren't supported.
903 (compilation-start command 'grep-mode))
904 (if (eq next-error-last-buffer (current-buffer))
905 (setq default-directory dir))))))
906
907 (defun vc-git-stash (name)
908 "Create a stash."
909 (interactive "sStash name: ")
910 (let ((root (vc-git-root default-directory)))
911 (when root
912 (vc-git--call nil "stash" "save" name)
913 (vc-resynch-buffer root t t))))
914
915 (defun vc-git-stash-show (name)
916 "Show the contents of stash NAME."
917 (interactive "sStash name: ")
918 (vc-setup-buffer "*vc-git-stash*")
919 (vc-git-command "*vc-git-stash*" 'async nil "stash" "show" "-p" name)
920 (set-buffer "*vc-git-stash*")
921 (diff-mode)
922 (setq buffer-read-only t)
923 (pop-to-buffer (current-buffer)))
924
925 (defun vc-git-stash-apply (name)
926 "Apply stash NAME."
927 (interactive "sApply stash: ")
928 (vc-git-command "*vc-git-stash*" 0 nil "stash" "apply" "-q" name)
929 (vc-resynch-buffer (vc-git-root default-directory) t t))
930
931 (defun vc-git-stash-pop (name)
932 "Pop stash NAME."
933 (interactive "sPop stash: ")
934 (vc-git-command "*vc-git-stash*" 0 nil "stash" "pop" "-q" name)
935 (vc-resynch-buffer (vc-git-root default-directory) t t))
936
937 (defun vc-git-stash-snapshot ()
938 "Create a stash with the current tree state."
939 (interactive)
940 (vc-git--call nil "stash" "save"
941 (let ((ct (current-time)))
942 (concat
943 (format-time-string "Snapshot on %Y-%m-%d" ct)
944 (format-time-string " at %H:%M" ct))))
945 (vc-git-command "*vc-git-stash*" 0 nil "stash" "apply" "-q" "stash@{0}")
946 (vc-resynch-buffer (vc-git-root default-directory) t t))
947
948 (defun vc-git-stash-list ()
949 (delete
950 ""
951 (split-string
952 (replace-regexp-in-string
953 "^stash@" " " (vc-git--run-command-string nil "stash" "list"))
954 "\n")))
955
956 (defun vc-git-stash-get-at-point (point)
957 (save-excursion
958 (goto-char point)
959 (beginning-of-line)
960 (if (looking-at "^ +\\({[0-9]+}\\):")
961 (match-string 1)
962 (error "Cannot find stash at point"))))
963
964 (defun vc-git-stash-delete-at-point ()
965 (interactive)
966 (let ((stash (vc-git-stash-get-at-point (point))))
967 (when (y-or-n-p (format "Remove stash %s ? " stash))
968 (vc-git--run-command-string nil "stash" "drop" (format "stash@%s" stash))
969 (vc-dir-refresh))))
970
971 (defun vc-git-stash-show-at-point ()
972 (interactive)
973 (vc-git-stash-show (format "stash@%s" (vc-git-stash-get-at-point (point)))))
974
975 (defun vc-git-stash-apply-at-point ()
976 (interactive)
977 (vc-git-stash-apply (format "stash@%s" (vc-git-stash-get-at-point (point)))))
978
979 (defun vc-git-stash-pop-at-point ()
980 (interactive)
981 (vc-git-stash-pop (format "stash@%s" (vc-git-stash-get-at-point (point)))))
982
983 (defun vc-git-stash-menu (e)
984 (interactive "e")
985 (vc-dir-at-event e (popup-menu vc-git-stash-menu-map e)))
986
987 \f
988 ;;; Internal commands
989
990 (defun vc-git-command (buffer okstatus file-or-list &rest flags)
991 "A wrapper around `vc-do-command' for use in vc-git.el.
992 The difference to vc-do-command is that this function always invokes `git'."
993 (apply 'vc-do-command (or buffer "*vc*") okstatus "git" file-or-list flags))
994
995 (defun vc-git--empty-db-p ()
996 "Check if the git db is empty (no commit done yet)."
997 (let (process-file-side-effects)
998 (not (eq 0 (vc-git--call nil "rev-parse" "--verify" "HEAD")))))
999
1000 (defun vc-git--call (buffer command &rest args)
1001 ;; We don't need to care the arguments. If there is a file name, it
1002 ;; is always a relative one. This works also for remote
1003 ;; directories.
1004 (apply 'process-file "git" nil buffer nil command args))
1005
1006 (defun vc-git--out-ok (command &rest args)
1007 (zerop (apply 'vc-git--call '(t nil) command args)))
1008
1009 (defun vc-git--run-command-string (file &rest args)
1010 "Run a git command on FILE and return its output as string.
1011 FILE can be nil."
1012 (let* ((ok t)
1013 (str (with-output-to-string
1014 (with-current-buffer standard-output
1015 (unless (apply 'vc-git--out-ok
1016 (if file
1017 (append args (list (file-relative-name
1018 file)))
1019 args))
1020 (setq ok nil))))))
1021 (and ok str)))
1022
1023 (defun vc-git-symbolic-commit (commit)
1024 "Translate COMMIT string into symbolic form.
1025 Returns nil if not possible."
1026 (and commit
1027 (let ((name (with-temp-buffer
1028 (and
1029 (vc-git--out-ok "name-rev" "--name-only" commit)
1030 (goto-char (point-min))
1031 (= (forward-line 2) 1)
1032 (bolp)
1033 (buffer-substring-no-properties (point-min)
1034 (1- (point-max)))))))
1035 (and name (not (string= name "undefined")) name))))
1036
1037 (provide 'vc-git)
1038
1039 ;;; vc-git.el ends here