Commit | Line | Data |
---|---|---|
0d42eb3e | 1 | ;;; vc-git.el --- VC backend for the git version control system -*- lexical-binding: t -*- |
fff4a046 | 2 | |
ab422c4d | 3 | ;; Copyright (C) 2006-2013 Free Software Foundation, Inc. |
fff4a046 | 4 | |
8b38ce20 | 5 | ;; Author: Alexandre Julliard <julliard@winehq.org> |
9766adfb | 6 | ;; Keywords: vc tools |
bd78fa1d | 7 | ;; Package: vc |
fff4a046 DN |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
eb3fa2cf | 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
fff4a046 | 12 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
13 | ;; the Free Software Foundation, either version 3 of the License, or |
14 | ;; (at your option) any later version. | |
fff4a046 DN |
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 | |
eb3fa2cf | 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
fff4a046 DN |
23 | |
24 | ;;; Commentary: | |
25 | ||
26 | ;; This file contains a VC backend for the git version control | |
27 | ;; system. | |
28 | ;; | |
29 | ||
30 | ;;; Installation: | |
31 | ||
4211679b | 32 | ;; To install: put this file on the load-path and add Git to the list |
fff4a046 | 33 | ;; of supported backends in `vc-handled-backends'; the following line, |
865fe16f | 34 | ;; placed in your init file, will accomplish this: |
fff4a046 | 35 | ;; |
4211679b | 36 | ;; (add-to-list 'vc-handled-backends 'Git) |
fff4a046 DN |
37 | |
38 | ;;; Todo: | |
53cc90ab DN |
39 | ;; - check if more functions could use vc-git-command instead |
40 | ;; of start-process. | |
fff4a046 | 41 | ;; - changelog generation |
53cc90ab DN |
42 | |
43 | ;; Implement the rest of the vc interface. See the comment at the | |
44 | ;; beginning of vc.el. The current status is: | |
7546c767 | 45 | ;; ("??" means: "figure out what to do about it") |
fff4a046 | 46 | ;; |
b91f0762 | 47 | ;; FUNCTION NAME STATUS |
53cc90ab | 48 | ;; BACKEND PROPERTIES |
b91f0762 | 49 | ;; * revision-granularity OK |
53cc90ab | 50 | ;; STATE-QUERYING FUNCTIONS |
b91f0762 DN |
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 | |
53cc90ab | 59 | ;; STATE-CHANGING FUNCTIONS |
b91f0762 DN |
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 | |
370fded4 | 72 | ;; - merge (file rev1 rev2) It would be possible to merge |
4c83ed3d SM |
73 | ;; changes into a single file, but |
74 | ;; when committing they wouldn't | |
370fded4 ER |
75 | ;; be identified as a merge |
76 | ;; by git, so it's probably | |
77 | ;; not a good idea. | |
b91f0762 DN |
78 | ;; - merge-news (file) see `merge' |
79 | ;; - steal-lock (file &optional revision) NOT NEEDED | |
53cc90ab | 80 | ;; HISTORY FUNCTIONS |
662c5698 | 81 | ;; * print-log (files buffer &optional shortlog start-revision limit) OK |
b91f0762 DN |
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 | |
370fded4 | 92 | ;; TAG SYSTEM |
b91f0762 DN |
93 | ;; - create-tag (dir name branchp) OK |
94 | ;; - retrieve-tag (dir name update) OK | |
53cc90ab | 95 | ;; MISCELLANEOUS |
b91f0762 DN |
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 | |
fff4a046 | 105 | |
c8149699 | 106 | (eval-when-compile |
a464a6c7 | 107 | (require 'cl-lib) |
c8149699 | 108 | (require 'vc) |
10c7e431 | 109 | (require 'vc-dir) |
c8149699 | 110 | (require 'grep)) |
fff4a046 | 111 | |
67b0de11 CY |
112 | (defgroup vc-git nil |
113 | "VC Git backend." | |
114 | :version "24.1" | |
115 | :group 'vc) | |
116 | ||
a0bea999 | 117 | (defcustom vc-git-diff-switches t |
b6889b65 GM |
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." | |
a0bea999 GM |
120 | :type '(choice (const :tag "Unspecified" nil) |
121 | (const :tag "None" t) | |
122 | (string :tag "Argument String") | |
b6889b65 | 123 | (repeat :tag "Argument List" :value ("") string)) |
a0bea999 | 124 | :version "23.1" |
67b0de11 | 125 | :group 'vc-git) |
a0bea999 | 126 | |
02da65ff GM |
127 | (defcustom vc-git-program "git" |
128 | "Name of the Git executable (excluding any arguments)." | |
129 | :version "24.1" | |
130 | :type 'string | |
67b0de11 | 131 | :group 'vc-git) |
02da65ff | 132 | |
33f6cf7b CY |
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)) | |
67b0de11 | 151 | :group 'vc-git |
33f6cf7b CY |
152 | :version "24.1") |
153 | ||
e97a42c1 | 154 | (defvar vc-git-commits-coding-system 'utf-8 |
fff4a046 DN |
155 | "Default coding system for git commits.") |
156 | ||
659114fd CY |
157 | ;; History of Git commands. |
158 | (defvar vc-git-history nil) | |
159 | ||
53cc90ab DN |
160 | ;;; BACKEND PROPERTIES |
161 | ||
70e2f6c7 | 162 | (defun vc-git-revision-granularity () 'repository) |
0d42eb3e | 163 | (defun vc-git-checkout-model (_files) 'implicit) |
53cc90ab DN |
164 | |
165 | ;;; STATE-QUERYING FUNCTIONS | |
166 | ||
167 | ;;;###autoload (defun vc-git-registered (file) | |
168 | ;;;###autoload "Return non-nil if FILE is registered with git." | |
4c83ed3d | 169 | ;;;###autoload (if (vc-find-root file ".git") ; Short cut. |
53cc90ab | 170 | ;;;###autoload (progn |
af314ba0 | 171 | ;;;###autoload (load "vc-git" nil t) |
53cc90ab DN |
172 | ;;;###autoload (vc-git-registered file)))) |
173 | ||
fff4a046 DN |
174 | (defun vc-git-registered (file) |
175 | "Check whether FILE is registered with git." | |
eceb6feb DG |
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")))))))) | |
108607bc | 198 | |
75cb52be DN |
199 | (defun vc-git--state-code (code) |
200 | "Convert from a string to a added/deleted/modified state." | |
a464a6c7 | 201 | (pcase (string-to-char code) |
75cb52be DN |
202 | (?M 'edited) |
203 | (?A 'added) | |
204 | (?D 'removed) | |
205 | (?U 'edited) ;; FIXME | |
206 | (?T 'edited))) ;; FIXME | |
207 | ||
fff4a046 | 208 | (defun vc-git-state (file) |
53cc90ab | 209 | "Git-specific version of `vc-state'." |
f6d90772 ER |
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. | |
eceb6feb DG |
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)))) | |
fff4a046 | 236 | |
0d42eb3e | 237 | (defun vc-git-working-revision (_file) |
ac3f4c6f | 238 | "Git-specific version of `vc-working-revision'." |
20c76c55 MA |
239 | (let* (process-file-side-effects |
240 | (str (with-output-to-string | |
241 | (with-current-buffer standard-output | |
242 | (vc-git--out-ok "symbolic-ref" "HEAD"))))) | |
fff4a046 DN |
243 | (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str) |
244 | (match-string 2 str) | |
245 | str))) | |
246 | ||
fff4a046 | 247 | (defun vc-git-workfile-unchanged-p (file) |
00d67080 | 248 | (eq 'up-to-date (vc-git-state file))) |
fff4a046 | 249 | |
6f222162 | 250 | (defun vc-git-mode-line-string (file) |
37269466 | 251 | "Return a string for `vc-mode-line' to put in the mode line for FILE." |
a40c87a0 | 252 | (let* ((branch (vc-working-revision file)) |
6f222162 DN |
253 | (def-ml (vc-default-mode-line-string 'Git file)) |
254 | (help-echo (get-text-property 0 'help-echo def-ml))) | |
255 | (if (zerop (length branch)) | |
256 | (propertize | |
257 | (concat def-ml "!") | |
258 | 'help-echo (concat help-echo "\nNo current branch (detached HEAD)")) | |
259 | (propertize def-ml | |
260 | 'help-echo (concat help-echo "\nCurrent branch: " branch))))) | |
261 | ||
a464a6c7 | 262 | (cl-defstruct (vc-git-extra-fileinfo |
236b5827 | 263 | (:copier nil) |
4c83ed3d SM |
264 | (:constructor vc-git-create-extra-fileinfo |
265 | (old-perm new-perm &optional rename-state orig-name)) | |
236b5827 | 266 | (:conc-name vc-git-extra-fileinfo->)) |
4c83ed3d SM |
267 | old-perm new-perm ;; Permission flags. |
268 | rename-state ;; Rename or copy state. | |
269 | orig-name) ;; Original name for renames or copies. | |
236b5827 DN |
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) | |
a464a6c7 | 276 | (pcase c |
236b5827 DN |
277 | (?\n "\\n") |
278 | (?\t "\\t") | |
279 | (?\\ "\\\\") | |
280 | (?\" "\\\"") | |
a464a6c7 | 281 | (_ (char-to-string c)))) |
236b5827 DN |
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)) | |
a464a6c7 | 290 | (str (pcase new-type |
4c83ed3d | 291 | (?\100 ;; File. |
a464a6c7 | 292 | (pcase old-type |
236b5827 DN |
293 | (?\100 nil) |
294 | (?\120 " (type change symlink -> file)") | |
295 | (?\160 " (type change subproject -> file)"))) | |
4c83ed3d | 296 | (?\120 ;; Symlink. |
a464a6c7 | 297 | (pcase old-type |
236b5827 DN |
298 | (?\100 " (type change file -> symlink)") |
299 | (?\160 " (type change subproject -> symlink)") | |
300 | (t " (symlink)"))) | |
4c83ed3d | 301 | (?\160 ;; Subproject. |
a464a6c7 | 302 | (pcase old-type |
236b5827 DN |
303 | (?\100 " (type change file -> subproject)") |
304 | (?\120 " (type change symlink -> subproject)") | |
305 | (t " (subproject)"))) | |
4c83ed3d SM |
306 | (?\110 nil) ;; Directory (internal, not a real git state). |
307 | (?\000 ;; Deleted or unknown. | |
a464a6c7 | 308 | (pcase old-type |
236b5827 DN |
309 | (?\120 " (symlink)") |
310 | (?\160 " (subproject)"))) | |
a464a6c7 | 311 | (_ (format " (unknown type %o)" new-type))))) |
236b5827 DN |
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) | |
4c83ed3d SM |
317 | "Return a string describing the copy or rename associated with INFO, |
318 | or an empty string if none." | |
20c76c55 | 319 | (let ((rename-state (when extra |
236b5827 DN |
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 ")) | |
4c83ed3d SM |
327 | (vc-git-escape-file-name |
328 | (vc-git-extra-fileinfo->orig-name extra)) | |
329 | ")") | |
330 | 'face 'font-lock-comment-face) | |
236b5827 DN |
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 | ||
13ad7457 | 343 | (defun vc-git-dir-printer (info) |
99e1b0c0 | 344 | "Pretty-printer for the vc-dir-fileinfo structure." |
d1bfcce1 DN |
345 | (let* ((isdir (vc-dir-fileinfo->directory info)) |
346 | (state (if isdir "" (vc-dir-fileinfo->state info))) | |
99e1b0c0 | 347 | (extra (vc-dir-fileinfo->extra info)) |
236b5827 DN |
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 | " " | |
99e1b0c0 | 352 | (propertize (format "%c" (if (vc-dir-fileinfo->marked info) ?* ? )) |
236b5827 DN |
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) | |
7f4a3168 | 362 | " " |
99e1b0c0 | 363 | (propertize (vc-git-escape-file-name (vc-dir-fileinfo->name info)) |
4c83ed3d SM |
364 | 'face (if isdir 'font-lock-comment-delimiter-face |
365 | 'font-lock-function-name-face) | |
631601b5 DN |
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") | |
c7f9e440 | 370 | 'keymap vc-dir-filename-mouse-map |
631601b5 | 371 | 'mouse-face 'highlight) |
236b5827 DN |
372 | (vc-git-file-type-as-string old-perm new-perm) |
373 | (vc-git-rename-as-string state extra)))) | |
374 | ||
d41080ca AJ |
375 | (defun vc-git-after-dir-status-stage (stage files update-function) |
376 | "Process sentinel for the various dir-status stages." | |
4c83ed3d | 377 | (let (next-stage result) |
d41080ca | 378 | (goto-char (point-min)) |
a464a6c7 SM |
379 | (pcase stage |
380 | (`update-index | |
d41080ca AJ |
381 | (setq next-stage (if (vc-git--empty-db-p) 'ls-files-added |
382 | (if files 'ls-files-up-to-date 'diff-index)))) | |
a464a6c7 | 383 | (`ls-files-added |
d41080ca AJ |
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))) | |
4c83ed3d SM |
388 | (push (list name 'added (vc-git-create-extra-fileinfo 0 new-perm)) |
389 | result)))) | |
a464a6c7 | 390 | (`ls-files-up-to-date |
d41080ca AJ |
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))) | |
4c83ed3d SM |
395 | (push (list name 'up-to-date |
396 | (vc-git-create-extra-fileinfo perm perm)) | |
397 | result)))) | |
a464a6c7 | 398 | (`ls-files-unknown |
d41080ca AJ |
399 | (when files (setq next-stage 'ls-files-ignored)) |
400 | (while (re-search-forward "\\([^\0]*?\\)\0" nil t 1) | |
4c83ed3d SM |
401 | (push (list (match-string 1) 'unregistered |
402 | (vc-git-create-extra-fileinfo 0 0)) | |
403 | result))) | |
a464a6c7 | 404 | (`ls-files-ignored |
d41080ca | 405 | (while (re-search-forward "\\([^\0]*?\\)\0" nil t 1) |
4c83ed3d SM |
406 | (push (list (match-string 1) 'ignored |
407 | (vc-git-create-extra-fileinfo 0 0)) | |
408 | result))) | |
a464a6c7 | 409 | (`diff-index |
d41080ca AJ |
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))) | |
4c83ed3d | 419 | (if new-name ; Copy or rename. |
d41080ca | 420 | (if (eq ?C (string-to-char state)) |
4c83ed3d SM |
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)))))) | |
d41080ca AJ |
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)))) | |
4c83ed3d SM |
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)))) | |
d41080ca AJ |
445 | |
446 | (defun vc-git-dir-status-goto-stage (stage files update-function) | |
8e4e4aef | 447 | (erase-buffer) |
a464a6c7 SM |
448 | (pcase stage |
449 | (`update-index | |
d41080ca AJ |
450 | (if files |
451 | (vc-git-command (current-buffer) 'async files "add" "--refresh" "--") | |
4c83ed3d SM |
452 | (vc-git-command (current-buffer) 'async nil |
453 | "update-index" "--refresh"))) | |
a464a6c7 | 454 | (`ls-files-added |
4c83ed3d SM |
455 | (vc-git-command (current-buffer) 'async files |
456 | "ls-files" "-z" "-c" "-s" "--")) | |
a464a6c7 | 457 | (`ls-files-up-to-date |
4c83ed3d SM |
458 | (vc-git-command (current-buffer) 'async files |
459 | "ls-files" "-z" "-c" "-s" "--")) | |
a464a6c7 | 460 | (`ls-files-unknown |
4c83ed3d SM |
461 | (vc-git-command (current-buffer) 'async files |
462 | "ls-files" "-z" "-o" "--directory" | |
463 | "--no-empty-directory" "--exclude-standard" "--")) | |
a464a6c7 | 464 | (`ls-files-ignored |
4c83ed3d SM |
465 | (vc-git-command (current-buffer) 'async files |
466 | "ls-files" "-z" "-o" "-i" "--directory" | |
467 | "--no-empty-directory" "--exclude-standard" "--")) | |
10a31174 | 468 | ;; --relative added in Git 1.5.5. |
a464a6c7 | 469 | (`diff-index |
4c83ed3d SM |
470 | (vc-git-command (current-buffer) 'async files |
471 | "diff-index" "--relative" "-z" "-M" "HEAD" "--"))) | |
8e4e4aef | 472 | (vc-exec-after |
4c83ed3d | 473 | `(vc-git-after-dir-status-stage ',stage ',files ',update-function))) |
8e4e4aef | 474 | |
0d42eb3e | 475 | (defun vc-git-dir-status (_dir update-function) |
d41080ca | 476 | "Return a list of (FILE STATE EXTRA) entries for DIR." |
12cb746e | 477 | ;; Further things that would have to be fixed later: |
12cb746e | 478 | ;; - how to handle unregistered directories |
99e1b0c0 | 479 | ;; - how to support vc-dir on a subdir of the project tree |
d41080ca AJ |
480 | (vc-git-dir-status-goto-stage 'update-index nil update-function)) |
481 | ||
0d42eb3e | 482 | (defun vc-git-dir-status-files (_dir files _default-state update-function) |
d41080ca AJ |
483 | "Return a list of (FILE STATE EXTRA) entries for FILES in DIR." |
484 | (vc-git-dir-status-goto-stage 'update-index files update-function)) | |
758dc0cc | 485 | |
881e4184 GM |
486 | (defvar vc-git-stash-map |
487 | (let ((map (make-sparse-keymap))) | |
7fa4876f DN |
488 | ;; Turn off vc-dir marking |
489 | (define-key map [mouse-2] 'ignore) | |
490 | ||
491 | (define-key map [down-mouse-3] 'vc-git-stash-menu) | |
881e4184 GM |
492 | (define-key map "\C-k" 'vc-git-stash-delete-at-point) |
493 | (define-key map "=" 'vc-git-stash-show-at-point) | |
494 | (define-key map "\C-m" 'vc-git-stash-show-at-point) | |
7fa4876f DN |
495 | (define-key map "A" 'vc-git-stash-apply-at-point) |
496 | (define-key map "P" 'vc-git-stash-pop-at-point) | |
e2f3c692 | 497 | (define-key map "S" 'vc-git-stash-snapshot) |
7fa4876f DN |
498 | map)) |
499 | ||
500 | (defvar vc-git-stash-menu-map | |
501 | (let ((map (make-sparse-keymap "Git Stash"))) | |
502 | (define-key map [de] | |
7cc6e154 | 503 | '(menu-item "Delete Stash" vc-git-stash-delete-at-point |
7fa4876f DN |
504 | :help "Delete the current stash")) |
505 | (define-key map [ap] | |
7cc6e154 | 506 | '(menu-item "Apply Stash" vc-git-stash-apply-at-point |
7fa4876f DN |
507 | :help "Apply the current stash and keep it in the stash list")) |
508 | (define-key map [po] | |
7cc6e154 | 509 | '(menu-item "Apply and Remove Stash (Pop)" vc-git-stash-pop-at-point |
7fa4876f DN |
510 | :help "Apply the current stash and remove it")) |
511 | (define-key map [sh] | |
7cc6e154 | 512 | '(menu-item "Show Stash" vc-git-stash-show-at-point |
7fa4876f | 513 | :help "Show the contents of the current stash")) |
881e4184 GM |
514 | map)) |
515 | ||
0d42eb3e | 516 | (defun vc-git-dir-extra-headers (_dir) |
15c5c970 DN |
517 | (let ((str (with-output-to-string |
518 | (with-current-buffer standard-output | |
2a0e3379 | 519 | (vc-git--out-ok "symbolic-ref" "HEAD")))) |
60308853 | 520 | (stash (vc-git-stash-list)) |
7fa4876f | 521 | (stash-help-echo "Use M-x vc-git-stash to create stashes.") |
60308853 DN |
522 | branch remote remote-url) |
523 | (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str) | |
524 | (progn | |
525 | (setq branch (match-string 2 str)) | |
60308853 DN |
526 | (setq remote |
527 | (with-output-to-string | |
528 | (with-current-buffer standard-output | |
4c83ed3d SM |
529 | (vc-git--out-ok "config" |
530 | (concat "branch." branch ".remote"))))) | |
60308853 DN |
531 | (when (string-match "\\([^\n]+\\)" remote) |
532 | (setq remote (match-string 1 remote))) | |
533 | (when remote | |
534 | (setq remote-url | |
535 | (with-output-to-string | |
536 | (with-current-buffer standard-output | |
4c83ed3d SM |
537 | (vc-git--out-ok "config" |
538 | (concat "remote." remote ".url")))))) | |
60308853 DN |
539 | (when (string-match "\\([^\n]+\\)" remote-url) |
540 | (setq remote-url (match-string 1 remote-url)))) | |
52964e54 | 541 | (setq branch "not (detached HEAD)")) |
2a0e3379 | 542 | ;; FIXME: maybe use a different face when nothing is stashed. |
15c5c970 DN |
543 | (concat |
544 | (propertize "Branch : " 'face 'font-lock-type-face) | |
60308853 DN |
545 | (propertize branch |
546 | 'face 'font-lock-variable-name-face) | |
547 | (when remote | |
548 | (concat | |
549 | "\n" | |
550 | (propertize "Remote : " 'face 'font-lock-type-face) | |
551 | (propertize remote-url | |
552 | 'face 'font-lock-variable-name-face))) | |
2a0e3379 | 553 | "\n" |
0e172cc2 DN |
554 | (if stash |
555 | (concat | |
7fa4876f DN |
556 | (propertize "Stash :\n" 'face 'font-lock-type-face |
557 | 'help-echo stash-help-echo) | |
0e172cc2 DN |
558 | (mapconcat |
559 | (lambda (x) | |
560 | (propertize x | |
561 | 'face 'font-lock-variable-name-face | |
562 | 'mouse-face 'highlight | |
7fa4876f | 563 | 'help-echo "mouse-3: Show stash menu\nRET: Show stash\nA: Apply stash\nP: Apply and remove stash (pop)\nC-k: Delete stash" |
0e172cc2 DN |
564 | 'keymap vc-git-stash-map)) |
565 | stash "\n")) | |
566 | (concat | |
7fa4876f DN |
567 | (propertize "Stash : " 'face 'font-lock-type-face |
568 | 'help-echo stash-help-echo) | |
0e172cc2 | 569 | (propertize "Nothing stashed" |
7fa4876f | 570 | 'help-echo stash-help-echo |
0e172cc2 | 571 | 'face 'font-lock-variable-name-face)))))) |
15c5c970 | 572 | |
659114fd CY |
573 | (defun vc-git-branches () |
574 | "Return the existing branches, as a list of strings. | |
575 | The car of the list is the current branch." | |
576 | (with-temp-buffer | |
eceb6feb | 577 | (vc-git--call t "branch") |
659114fd CY |
578 | (goto-char (point-min)) |
579 | (let (current-branch branches) | |
580 | (while (not (eobp)) | |
581 | (when (looking-at "^\\([ *]\\) \\(.+\\)$") | |
582 | (if (string-equal (match-string 1) "*") | |
583 | (setq current-branch (match-string 2)) | |
584 | (push (match-string 2) branches))) | |
585 | (forward-line 1)) | |
586 | (cons current-branch (nreverse branches))))) | |
587 | ||
53cc90ab DN |
588 | ;;; STATE-CHANGING FUNCTIONS |
589 | ||
590 | (defun vc-git-create-repo () | |
4211679b | 591 | "Create a new Git repository." |
00d67080 | 592 | (vc-git-command nil 0 nil "init")) |
53cc90ab | 593 | |
0d42eb3e | 594 | (defun vc-git-register (files &optional _rev _comment) |
b91f0762 DN |
595 | "Register FILES into the git version-control system." |
596 | (let (flist dlist) | |
597 | (dolist (crt files) | |
598 | (if (file-directory-p crt) | |
599 | (push crt dlist) | |
600 | (push crt flist))) | |
601 | (when flist | |
602 | (vc-git-command nil 0 flist "update-index" "--add" "--")) | |
603 | (when dlist | |
604 | (vc-git-command nil 0 dlist "add")))) | |
fff4a046 | 605 | |
53cc90ab DN |
606 | (defalias 'vc-git-responsible-p 'vc-git-root) |
607 | ||
d7009f45 DN |
608 | (defun vc-git-unregister (file) |
609 | (vc-git-command nil 0 file "rm" "-f" "--cached" "--")) | |
108607bc | 610 | |
9f7b98f8 DG |
611 | (declare-function log-edit-mode "log-edit" ()) |
612 | (declare-function log-edit-toggle-header "log-edit" (header value)) | |
e97a42c1 | 613 | (declare-function log-edit-extract-headers "log-edit" (headers string)) |
d7009f45 | 614 | |
9f7b98f8 DG |
615 | (defun vc-git-log-edit-toggle-signoff () |
616 | "Toggle whether to add the \"Signed-off-by\" line at the end of | |
617 | the commit message." | |
618 | (interactive) | |
619 | (log-edit-toggle-header "Sign-Off" "yes")) | |
620 | ||
621 | (defun vc-git-log-edit-toggle-amend () | |
622 | "Toggle whether this will amend the previous commit. | |
623 | If toggling on, also insert its message into the buffer." | |
624 | (interactive) | |
625 | (when (log-edit-toggle-header "Amend" "yes") | |
626 | (goto-char (point-max)) | |
627 | (unless (bolp) (insert "\n")) | |
628 | (insert (with-output-to-string | |
629 | (vc-git-command | |
630 | standard-output 1 nil | |
631 | "log" "--max-count=1" "--pretty=format:%B" "HEAD"))))) | |
632 | ||
633 | (defvar vc-git-log-edit-mode-map | |
634 | (let ((map (make-sparse-keymap "Git-Log-Edit"))) | |
635 | (define-key map "\C-c\C-s" 'vc-git-log-edit-toggle-signoff) | |
636 | (define-key map "\C-c\C-e" 'vc-git-log-edit-toggle-amend) | |
637 | map)) | |
638 | ||
639 | (define-derived-mode vc-git-log-edit-mode log-edit-mode "Log-Edit/git" | |
640 | "Major mode for editing Git log messages. | |
641 | It is based on `log-edit-mode', and has Git-specific extensions.") | |
642 | ||
0d42eb3e | 643 | (defun vc-git-checkin (files _rev comment) |
e97a42c1 | 644 | (let ((coding-system-for-write vc-git-commits-coding-system)) |
9f7b98f8 DG |
645 | (cl-flet ((boolean-arg-fn |
646 | (argument) | |
647 | (lambda (value) (when (equal value "yes") (list argument))))) | |
648 | (apply 'vc-git-command nil 0 files | |
649 | (nconc (list "commit" "-m") | |
650 | (log-edit-extract-headers | |
651 | `(("Author" . "--author") | |
652 | ("Date" . "--date") | |
653 | ("Amend" . ,(boolean-arg-fn "--amend")) | |
654 | ("Sign-Off" . ,(boolean-arg-fn "--signoff"))) | |
655 | comment) | |
656 | (list "--only" "--")))))) | |
fff4a046 | 657 | |
ac3f4c6f | 658 | (defun vc-git-find-revision (file rev buffer) |
20c76c55 MA |
659 | (let* (process-file-side-effects |
660 | (coding-system-for-read 'binary) | |
661 | (coding-system-for-write 'binary) | |
50d76a9f DN |
662 | (fullname |
663 | (let ((fn (vc-git--run-command-string | |
664 | file "ls-files" "-z" "--full-name" "--"))) | |
665 | ;; ls-files does not return anything when looking for a | |
666 | ;; revision of a file that has been renamed or removed. | |
667 | (if (string= fn "") | |
668 | (file-relative-name file (vc-git-root default-directory)) | |
669 | (substring fn 0 -1))))) | |
108607bc DN |
670 | (vc-git-command |
671 | buffer 0 | |
50d76a9f DN |
672 | nil |
673 | "cat-file" "blob" (concat (if rev rev "HEAD") ":" fullname)))) | |
b0f90937 | 674 | |
0d42eb3e | 675 | (defun vc-git-checkout (file &optional _editable rev) |
7546c767 | 676 | (vc-git-command nil 0 file "checkout" (or rev "HEAD"))) |
fff4a046 DN |
677 | |
678 | (defun vc-git-revert (file &optional contents-done) | |
679 | "Revert FILE to the version stored in the git repository." | |
680 | (if contents-done | |
b0f90937 | 681 | (vc-git-command nil 0 file "update-index" "--") |
cebf8ec6 DN |
682 | (vc-git-command nil 0 file "reset" "-q" "--") |
683 | (vc-git-command nil nil file "checkout" "-q" "--"))) | |
fff4a046 | 684 | |
8a4e6db8 SS |
685 | (defvar vc-git-error-regexp-alist |
686 | '(("^ \\(.+\\) |" 1 nil nil 0)) | |
687 | "Value of `compilation-error-regexp-alist' in *vc-git* buffers.") | |
688 | ||
659114fd CY |
689 | (defun vc-git-pull (prompt) |
690 | "Pull changes into the current Git branch. | |
a2b6e5d6 CY |
691 | Normally, this runs \"git pull\". If PROMPT is non-nil, prompt |
692 | for the Git command to run." | |
659114fd CY |
693 | (let* ((root (vc-git-root default-directory)) |
694 | (buffer (format "*vc-git : %s*" (expand-file-name root))) | |
695 | (command "pull") | |
02da65ff | 696 | (git-program vc-git-program) |
659114fd CY |
697 | args) |
698 | ;; If necessary, prompt for the exact command. | |
699 | (when prompt | |
700 | (setq args (split-string | |
a2b6e5d6 | 701 | (read-shell-command "Git pull command: " |
02da65ff | 702 | (format "%s pull" git-program) |
659114fd CY |
703 | 'vc-git-history) |
704 | " " t)) | |
705 | (setq git-program (car args) | |
706 | command (cadr args) | |
707 | args (cddr args))) | |
a2b6e5d6 | 708 | (apply 'vc-do-async-command buffer root git-program command args) |
8a4e6db8 | 709 | (with-current-buffer buffer (vc-exec-after '(vc-compilation-mode 'git))) |
a2b6e5d6 | 710 | (vc-set-async-update buffer))) |
659114fd CY |
711 | |
712 | (defun vc-git-merge-branch () | |
713 | "Merge changes into the current Git branch. | |
714 | This prompts for a branch to merge from." | |
715 | (let* ((root (vc-git-root default-directory)) | |
716 | (buffer (format "*vc-git : %s*" (expand-file-name root))) | |
717 | (branches (cdr (vc-git-branches))) | |
718 | (merge-source | |
a2b6e5d6 CY |
719 | (completing-read "Merge from branch: " |
720 | (if (or (member "FETCH_HEAD" branches) | |
721 | (not (file-readable-p | |
722 | (expand-file-name ".git/FETCH_HEAD" | |
723 | root)))) | |
724 | branches | |
725 | (cons "FETCH_HEAD" branches)) | |
726 | nil t))) | |
02da65ff | 727 | (apply 'vc-do-async-command buffer root vc-git-program "merge" |
a2b6e5d6 | 728 | (list merge-source)) |
8a4e6db8 | 729 | (with-current-buffer buffer (vc-exec-after '(vc-compilation-mode 'git))) |
a2b6e5d6 | 730 | (vc-set-async-update buffer))) |
659114fd | 731 | |
53cc90ab DN |
732 | ;;; HISTORY FUNCTIONS |
733 | ||
662c5698 | 734 | (defun vc-git-print-log (files buffer &optional shortlog start-revision limit) |
bb7cdf58 GM |
735 | "Print commit log associated with FILES into specified BUFFER. |
736 | If SHORTLOG is non-nil, use a short format based on `vc-git-root-log-format'. | |
737 | \(This requires at least Git version 1.5.6, for the --graph option.) | |
738 | If START-REVISION is non-nil, it is the newest revision to show. | |
739 | If LIMIT is non-nil, show no more than this many entries." | |
e97a42c1 | 740 | (let ((coding-system-for-read vc-git-commits-coding-system)) |
53cc90ab DN |
741 | ;; `vc-do-command' creates the buffer, but we need it before running |
742 | ;; the command. | |
743 | (vc-setup-buffer buffer) | |
744 | ;; If the buffer exists from a previous invocation it might be | |
745 | ;; read-only. | |
746 | (let ((inhibit-read-only t)) | |
934a944e AJ |
747 | (with-current-buffer |
748 | buffer | |
0d3f8a78 DN |
749 | (apply 'vc-git-command buffer |
750 | 'async files | |
751 | (append | |
58941d03 | 752 | '("log" "--no-color") |
0d3f8a78 | 753 | (when shortlog |
33f6cf7b CY |
754 | `("--graph" "--decorate" "--date=short" |
755 | ,(format "--pretty=tformat:%s" | |
756 | (car vc-git-root-log-format)) | |
757 | "--abbrev-commit")) | |
0d3f8a78 | 758 | (when limit (list "-n" (format "%s" limit))) |
662c5698 | 759 | (when start-revision (list start-revision)) |
0d3f8a78 | 760 | '("--"))))))) |
53cc90ab | 761 | |
31527c56 DN |
762 | (defun vc-git-log-outgoing (buffer remote-location) |
763 | (interactive) | |
764 | (vc-git-command | |
765 | buffer 0 nil | |
6941ffec DN |
766 | "log" |
767 | "--no-color" "--graph" "--decorate" "--date=short" | |
33f6cf7b CY |
768 | (format "--pretty=tformat:%s" (car vc-git-root-log-format)) |
769 | "--abbrev-commit" | |
6941ffec | 770 | (concat (if (string= remote-location "") |
eccdfe5f | 771 | "@{upstream}" |
6941ffec DN |
772 | remote-location) |
773 | "..HEAD"))) | |
61158bfa DN |
774 | |
775 | (defun vc-git-log-incoming (buffer remote-location) | |
776 | (interactive) | |
6941ffec | 777 | (vc-git-command nil 0 nil "fetch") |
61158bfa DN |
778 | (vc-git-command |
779 | buffer 0 nil | |
33f6cf7b | 780 | "log" |
6941ffec | 781 | "--no-color" "--graph" "--decorate" "--date=short" |
33f6cf7b CY |
782 | (format "--pretty=tformat:%s" (car vc-git-root-log-format)) |
783 | "--abbrev-commit" | |
6941ffec | 784 | (concat "HEAD.." (if (string= remote-location "") |
eccdfe5f | 785 | "@{upstream}" |
6941ffec | 786 | remote-location)))) |
31527c56 | 787 | |
53cc90ab DN |
788 | (defvar log-view-message-re) |
789 | (defvar log-view-file-re) | |
790 | (defvar log-view-font-lock-keywords) | |
934a944e | 791 | (defvar log-view-per-file-logs) |
33f6cf7b | 792 | (defvar log-view-expanded-log-entry-function) |
53cc90ab | 793 | |
4211679b | 794 | (define-derived-mode vc-git-log-view-mode log-view-mode "Git-Log-View" |
4c83ed3d | 795 | (require 'add-log) ;; We need the faces add-log. |
53cc90ab | 796 | ;; Don't have file markers, so use impossible regexp. |
934a944e AJ |
797 | (set (make-local-variable 'log-view-file-re) "\\`a\\`") |
798 | (set (make-local-variable 'log-view-per-file-logs) nil) | |
53cc90ab | 799 | (set (make-local-variable 'log-view-message-re) |
eccdfe5f | 800 | (if (not (eq vc-log-view-type 'long)) |
33f6cf7b | 801 | (cadr vc-git-root-log-format) |
79d316d3 | 802 | "^commit *\\([0-9a-z]+\\)")) |
33f6cf7b CY |
803 | ;; Allow expanding short log entries |
804 | (when (eq vc-log-view-type 'short) | |
805 | (setq truncate-lines t) | |
806 | (set (make-local-variable 'log-view-expanded-log-entry-function) | |
807 | 'vc-git-expanded-log-entry)) | |
53cc90ab | 808 | (set (make-local-variable 'log-view-font-lock-keywords) |
eccdfe5f | 809 | (if (not (eq vc-log-view-type 'long)) |
33f6cf7b CY |
810 | (list (cons (nth 1 vc-git-root-log-format) |
811 | (nth 2 vc-git-root-log-format))) | |
812 | (append | |
09ae5da1 | 813 | `((,log-view-message-re (1 'change-log-acknowledgment))) |
33f6cf7b CY |
814 | ;; Handle the case: |
815 | ;; user: foo@bar | |
816 | '(("^Author:[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)" | |
817 | (1 'change-log-email)) | |
818 | ;; Handle the case: | |
819 | ;; user: FirstName LastName <foo@bar> | |
820 | ("^Author:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" | |
821 | (1 'change-log-name) | |
822 | (2 'change-log-email)) | |
823 | ("^ +\\(?:\\(?:[Aa]cked\\|[Ss]igned-[Oo]ff\\)-[Bb]y:\\)[ \t]+\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)" | |
824 | (1 'change-log-name)) | |
825 | ("^ +\\(?:\\(?:[Aa]cked\\|[Ss]igned-[Oo]ff\\)-[Bb]y:\\)[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" | |
826 | (1 'change-log-name) | |
827 | (2 'change-log-email)) | |
828 | ("^Merge: \\([0-9a-z]+\\) \\([0-9a-z]+\\)" | |
09ae5da1 PE |
829 | (1 'change-log-acknowledgment) |
830 | (2 'change-log-acknowledgment)) | |
33f6cf7b | 831 | ("^Date: \\(.+\\)" (1 'change-log-date)) |
32ba3abc DN |
832 | ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message))))))) |
833 | ||
fff4a046 | 834 | |
b16bd82d TTN |
835 | (defun vc-git-show-log-entry (revision) |
836 | "Move to the log entry for REVISION. | |
837 | REVISION may have the form BRANCH, BRANCH~N, | |
838 | or BRANCH^ (where \"^\" can be repeated)." | |
839 | (goto-char (point-min)) | |
4c83ed3d SM |
840 | (prog1 |
841 | (when revision | |
842 | (search-forward | |
843 | (format "\ncommit %s" revision) nil t | |
844 | (cond ((string-match "~\\([0-9]\\)\\'" revision) | |
845 | (1+ (string-to-number (match-string 1 revision)))) | |
846 | ((string-match "\\^+\\'" revision) | |
847 | (1+ (length (match-string 0 revision)))) | |
848 | (t nil)))) | |
849 | (beginning-of-line))) | |
b16bd82d | 850 | |
33f6cf7b CY |
851 | (defun vc-git-expanded-log-entry (revision) |
852 | (with-temp-buffer | |
853 | (apply 'vc-git-command t nil nil (list "log" revision "-1")) | |
854 | (goto-char (point-min)) | |
855 | (unless (eobp) | |
856 | ;; Indent the expanded log entry. | |
857 | (indent-region (point-min) (point-max) 2) | |
858 | (buffer-string)))) | |
859 | ||
b747d346 | 860 | (defun vc-git-diff (files &optional rev1 rev2 buffer) |
a0bea999 | 861 | "Get a difference report using Git between two revisions of FILES." |
20c76c55 MA |
862 | (let (process-file-side-effects) |
863 | (apply #'vc-git-command (or buffer "*vc-diff*") 1 files | |
864 | (if (and rev1 rev2) "diff-tree" "diff-index") | |
865 | "--exit-code" | |
866 | (append (vc-switches 'git 'diff) | |
867 | (list "-p" (or rev1 "HEAD") rev2 "--"))))) | |
fff4a046 | 868 | |
0d42eb3e | 869 | (defun vc-git-revision-table (_files) |
9f11ce4e | 870 | ;; What about `files'?!? --Stef |
20c76c55 MA |
871 | (let (process-file-side-effects |
872 | (table (list "HEAD"))) | |
108607bc DN |
873 | (with-temp-buffer |
874 | (vc-git-command t nil nil "for-each-ref" "--format=%(refname)") | |
875 | (goto-char (point-min)) | |
53ef91b1 SM |
876 | (while (re-search-forward "^refs/\\(heads\\|tags\\|remotes\\)/\\(.*\\)$" |
877 | nil t) | |
108607bc DN |
878 | (push (match-string 2) table))) |
879 | table)) | |
880 | ||
9f11ce4e | 881 | (defun vc-git-revision-completion-table (files) |
0d42eb3e SM |
882 | (letrec ((table (lazy-completion-table |
883 | table (lambda () (vc-git-revision-table files))))) | |
108607bc DN |
884 | table)) |
885 | ||
fff4a046 | 886 | (defun vc-git-annotate-command (file buf &optional rev) |
fff4a046 | 887 | (let ((name (file-relative-name file))) |
50d76a9f | 888 | (vc-git-command buf 'async nil "blame" "--date=iso" "-C" "-C" rev "--" name))) |
fff4a046 | 889 | |
f8bd9ac6 DN |
890 | (declare-function vc-annotate-convert-time "vc-annotate" (time)) |
891 | ||
fff4a046 | 892 | (defun vc-git-annotate-time () |
0bcc6163 | 893 | (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) |
fff4a046 | 894 | (vc-annotate-convert-time |
2aa0736a TTN |
895 | (apply #'encode-time (mapcar (lambda (match) |
896 | (string-to-number (match-string match))) | |
897 | '(6 5 4 3 2 1 7)))))) | |
fff4a046 | 898 | |
53cc90ab | 899 | (defun vc-git-annotate-extract-revision-at-line () |
2aa0736a TTN |
900 | (save-excursion |
901 | (move-beginning-of-line 1) | |
d1e4c403 DN |
902 | (when (looking-at "\\([0-9a-f^][0-9a-f]+\\) \\(\\([^(]+\\) \\)?") |
903 | (let ((revision (match-string-no-properties 1))) | |
904 | (if (match-beginning 2) | |
c036381c DN |
905 | (let ((fname (match-string-no-properties 3))) |
906 | ;; Remove trailing whitespace from the file name. | |
907 | (when (string-match " +\\'" fname) | |
908 | (setq fname (substring fname 0 (match-beginning 0)))) | |
909 | (cons revision | |
910 | (expand-file-name fname (vc-git-root default-directory)))) | |
d1e4c403 | 911 | revision))))) |
53cc90ab | 912 | |
370fded4 | 913 | ;;; TAG SYSTEM |
b747d346 | 914 | |
370fded4 | 915 | (defun vc-git-create-tag (dir name branchp) |
b747d346 DN |
916 | (let ((default-directory dir)) |
917 | (and (vc-git-command nil 0 nil "update-index" "--refresh") | |
918 | (if branchp | |
919 | (vc-git-command nil 0 nil "checkout" "-b" name) | |
920 | (vc-git-command nil 0 nil "tag" name))))) | |
921 | ||
0d42eb3e | 922 | (defun vc-git-retrieve-tag (dir name _update) |
b747d346 DN |
923 | (let ((default-directory dir)) |
924 | (vc-git-command nil 0 nil "checkout" name) | |
925 | ;; FIXME: update buffers if `update' is true | |
926 | )) | |
927 | ||
928 | ||
53cc90ab | 929 | ;;; MISCELLANEOUS |
fff4a046 | 930 | |
5b5afd50 ER |
931 | (defun vc-git-previous-revision (file rev) |
932 | "Git-specific version of `vc-previous-revision'." | |
934a944e | 933 | (if file |
50d76a9f | 934 | (let* ((fname (file-relative-name file)) |
4537363c AJ |
935 | (prev-rev (with-temp-buffer |
936 | (and | |
50d76a9f | 937 | (vc-git--out-ok "rev-list" "-2" rev "--" fname) |
4537363c AJ |
938 | (goto-char (point-max)) |
939 | (bolp) | |
940 | (zerop (forward-line -1)) | |
941 | (not (bobp)) | |
942 | (buffer-substring-no-properties | |
943 | (point) | |
944 | (1- (point-max))))))) | |
945 | (or (vc-git-symbolic-commit prev-rev) prev-rev)) | |
934a944e AJ |
946 | (with-temp-buffer |
947 | (and | |
948 | (vc-git--out-ok "rev-parse" (concat rev "^")) | |
949 | (buffer-substring-no-properties (point-min) (+ (point-min) 40)))))) | |
fff4a046 | 950 | |
5b5afd50 ER |
951 | (defun vc-git-next-revision (file rev) |
952 | "Git-specific version of `vc-next-revision'." | |
fff4a046 DN |
953 | (let* ((default-directory (file-name-directory |
954 | (expand-file-name file))) | |
2aa0736a TTN |
955 | (file (file-name-nondirectory file)) |
956 | (current-rev | |
957 | (with-temp-buffer | |
958 | (and | |
5fdbecd8 | 959 | (vc-git--out-ok "rev-list" "-1" rev "--" file) |
2aa0736a TTN |
960 | (goto-char (point-max)) |
961 | (bolp) | |
962 | (zerop (forward-line -1)) | |
963 | (bobp) | |
964 | (buffer-substring-no-properties | |
965 | (point) | |
4537363c AJ |
966 | (1- (point-max)))))) |
967 | (next-rev | |
968 | (and current-rev | |
969 | (with-temp-buffer | |
970 | (and | |
971 | (vc-git--out-ok "rev-list" "HEAD" "--" file) | |
972 | (goto-char (point-min)) | |
973 | (search-forward current-rev nil t) | |
974 | (zerop (forward-line -1)) | |
975 | (buffer-substring-no-properties | |
976 | (point) | |
977 | (progn (forward-line 1) (1- (point))))))))) | |
978 | (or (vc-git-symbolic-commit next-rev) next-rev))) | |
fff4a046 | 979 | |
8b38ce20 DN |
980 | (defun vc-git-delete-file (file) |
981 | (vc-git-command nil 0 file "rm" "-f" "--")) | |
b0f90937 | 982 | |
8b38ce20 DN |
983 | (defun vc-git-rename-file (old new) |
984 | (vc-git-command nil 0 (list old new) "mv" "-f" "--")) | |
b0f90937 | 985 | |
f0e1713e DN |
986 | (defvar vc-git-extra-menu-map |
987 | (let ((map (make-sparse-keymap))) | |
988 | (define-key map [git-grep] | |
989 | '(menu-item "Git grep..." vc-git-grep | |
990 | :help "Run the `git grep' command")) | |
e2f3c692 | 991 | (define-key map [git-sn] |
7cc6e154 | 992 | '(menu-item "Stash a Snapshot" vc-git-stash-snapshot |
e2f3c692 | 993 | :help "Stash the current state of the tree and keep the current state")) |
2ddf440d | 994 | (define-key map [git-st] |
e2f3c692 | 995 | '(menu-item "Create Stash..." vc-git-stash |
2ddf440d DN |
996 | :help "Stash away changes")) |
997 | (define-key map [git-ss] | |
998 | '(menu-item "Show Stash..." vc-git-stash-show | |
999 | :help "Show stash contents")) | |
f0e1713e DN |
1000 | map)) |
1001 | ||
1002 | (defun vc-git-extra-menu () vc-git-extra-menu-map) | |
1003 | ||
1004 | (defun vc-git-extra-status-menu () vc-git-extra-menu-map) | |
1005 | ||
32ba3abc | 1006 | (defun vc-git-root (file) |
a40c87a0 MA |
1007 | (or (vc-file-getprop file 'git-root) |
1008 | (vc-file-setprop file 'git-root (vc-find-root file ".git")))) | |
32ba3abc | 1009 | |
f0e1713e DN |
1010 | ;; Derived from `lgrep'. |
1011 | (defun vc-git-grep (regexp &optional files dir) | |
1012 | "Run git grep, searching for REGEXP in FILES in directory DIR. | |
1013 | The search is limited to file names matching shell pattern FILES. | |
1014 | FILES may use abbreviations defined in `grep-files-aliases', e.g. | |
1015 | entering `ch' is equivalent to `*.[ch]'. | |
1016 | ||
1017 | With \\[universal-argument] prefix, you can edit the constructed shell command line | |
1018 | before it is executed. | |
1019 | With two \\[universal-argument] prefixes, directly edit and run `grep-command'. | |
1020 | ||
1021 | Collect output in a buffer. While git grep runs asynchronously, you | |
1022 | can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error] \ | |
1023 | in the grep output buffer, | |
1024 | to go to the lines where grep found matches. | |
1025 | ||
1026 | This command shares argument histories with \\[rgrep] and \\[grep]." | |
1027 | (interactive | |
1028 | (progn | |
1029 | (grep-compute-defaults) | |
1030 | (cond | |
1031 | ((equal current-prefix-arg '(16)) | |
1032 | (list (read-from-minibuffer "Run: " "git grep" | |
1033 | nil nil 'grep-history) | |
1034 | nil)) | |
1035 | (t (let* ((regexp (grep-read-regexp)) | |
1036 | (files (grep-read-files regexp)) | |
1037 | (dir (read-directory-name "In directory: " | |
1038 | nil default-directory t))) | |
1039 | (list regexp files dir)))))) | |
1040 | (require 'grep) | |
1041 | (when (and (stringp regexp) (> (length regexp) 0)) | |
1042 | (let ((command regexp)) | |
1043 | (if (null files) | |
1044 | (if (string= command "git grep") | |
1045 | (setq command nil)) | |
1046 | (setq dir (file-name-as-directory (expand-file-name dir))) | |
1047 | (setq command | |
91ab9c13 | 1048 | (grep-expand-template "git grep -n -e <R> -- <F>" |
793d32bb | 1049 | regexp files)) |
f0e1713e DN |
1050 | (when command |
1051 | (if (equal current-prefix-arg '(4)) | |
1052 | (setq command | |
1053 | (read-from-minibuffer "Confirm: " | |
1054 | command nil nil 'grep-history)) | |
1055 | (add-to-history 'grep-history command)))) | |
1056 | (when command | |
1057 | (let ((default-directory dir) | |
91ab9c13 | 1058 | (compilation-environment (cons "PAGER=" compilation-environment))) |
f0e1713e DN |
1059 | ;; Setting process-setup-function makes exit-message-function work |
1060 | ;; even when async processes aren't supported. | |
1061 | (compilation-start command 'grep-mode)) | |
1062 | (if (eq next-error-last-buffer (current-buffer)) | |
1063 | (setq default-directory dir)))))) | |
2a0e3379 | 1064 | |
2ddf440d DN |
1065 | (defun vc-git-stash (name) |
1066 | "Create a stash." | |
1067 | (interactive "sStash name: ") | |
1068 | (let ((root (vc-git-root default-directory))) | |
1069 | (when root | |
1070 | (vc-git--call nil "stash" "save" name) | |
1071 | (vc-resynch-buffer root t t)))) | |
1072 | ||
1073 | (defun vc-git-stash-show (name) | |
1074 | "Show the contents of stash NAME." | |
1075 | (interactive "sStash name: ") | |
1076 | (vc-setup-buffer "*vc-git-stash*") | |
1077 | (vc-git-command "*vc-git-stash*" 'async nil "stash" "show" "-p" name) | |
1078 | (set-buffer "*vc-git-stash*") | |
1079 | (diff-mode) | |
1080 | (setq buffer-read-only t) | |
1081 | (pop-to-buffer (current-buffer))) | |
1082 | ||
7fa4876f DN |
1083 | (defun vc-git-stash-apply (name) |
1084 | "Apply stash NAME." | |
1085 | (interactive "sApply stash: ") | |
1086 | (vc-git-command "*vc-git-stash*" 0 nil "stash" "apply" "-q" name) | |
1087 | (vc-resynch-buffer (vc-git-root default-directory) t t)) | |
1088 | ||
1089 | (defun vc-git-stash-pop (name) | |
1090 | "Pop stash NAME." | |
1091 | (interactive "sPop stash: ") | |
1092 | (vc-git-command "*vc-git-stash*" 0 nil "stash" "pop" "-q" name) | |
1093 | (vc-resynch-buffer (vc-git-root default-directory) t t)) | |
1094 | ||
e2f3c692 DN |
1095 | (defun vc-git-stash-snapshot () |
1096 | "Create a stash with the current tree state." | |
1097 | (interactive) | |
1098 | (vc-git--call nil "stash" "save" | |
1099 | (let ((ct (current-time))) | |
1100 | (concat | |
1101 | (format-time-string "Snapshot on %Y-%m-%d" ct) | |
1102 | (format-time-string " at %H:%M" ct)))) | |
1103 | (vc-git-command "*vc-git-stash*" 0 nil "stash" "apply" "-q" "stash@{0}") | |
1104 | (vc-resynch-buffer (vc-git-root default-directory) t t)) | |
1105 | ||
2a0e3379 | 1106 | (defun vc-git-stash-list () |
0e172cc2 DN |
1107 | (delete |
1108 | "" | |
1109 | (split-string | |
1110 | (replace-regexp-in-string | |
1111 | "^stash@" " " (vc-git--run-command-string nil "stash" "list")) | |
1112 | "\n"))) | |
1113 | ||
1114 | (defun vc-git-stash-get-at-point (point) | |
1115 | (save-excursion | |
1116 | (goto-char point) | |
1117 | (beginning-of-line) | |
1118 | (if (looking-at "^ +\\({[0-9]+}\\):") | |
1119 | (match-string 1) | |
1120 | (error "Cannot find stash at point")))) | |
1121 | ||
1122 | (defun vc-git-stash-delete-at-point () | |
1123 | (interactive) | |
1124 | (let ((stash (vc-git-stash-get-at-point (point)))) | |
e2f3c692 | 1125 | (when (y-or-n-p (format "Remove stash %s ? " stash)) |
0e172cc2 DN |
1126 | (vc-git--run-command-string nil "stash" "drop" (format "stash@%s" stash)) |
1127 | (vc-dir-refresh)))) | |
1128 | ||
1129 | (defun vc-git-stash-show-at-point () | |
1130 | (interactive) | |
1131 | (vc-git-stash-show (format "stash@%s" (vc-git-stash-get-at-point (point))))) | |
1132 | ||
7fa4876f DN |
1133 | (defun vc-git-stash-apply-at-point () |
1134 | (interactive) | |
1135 | (vc-git-stash-apply (format "stash@%s" (vc-git-stash-get-at-point (point))))) | |
1136 | ||
1137 | (defun vc-git-stash-pop-at-point () | |
1138 | (interactive) | |
1139 | (vc-git-stash-pop (format "stash@%s" (vc-git-stash-get-at-point (point))))) | |
1140 | ||
1141 | (defun vc-git-stash-menu (e) | |
1142 | (interactive "e") | |
1143 | (vc-dir-at-event e (popup-menu vc-git-stash-menu-map e))) | |
1144 | ||
fff4a046 | 1145 | \f |
b747d346 | 1146 | ;;; Internal commands |
fff4a046 | 1147 | |
8b9783e0 | 1148 | (defun vc-git-command (buffer okstatus file-or-list &rest flags) |
53cc90ab | 1149 | "A wrapper around `vc-do-command' for use in vc-git.el. |
02da65ff GM |
1150 | The difference to vc-do-command is that this function always invokes |
1151 | `vc-git-program'." | |
1152 | (apply 'vc-do-command (or buffer "*vc*") okstatus vc-git-program | |
a8b733a5 | 1153 | file-or-list (cons "--no-pager" flags))) |
53cc90ab | 1154 | |
8e4e4aef DN |
1155 | (defun vc-git--empty-db-p () |
1156 | "Check if the git db is empty (no commit done yet)." | |
20c76c55 MA |
1157 | (let (process-file-side-effects) |
1158 | (not (eq 0 (vc-git--call nil "rev-parse" "--verify" "HEAD"))))) | |
8e4e4aef | 1159 | |
5fdbecd8 | 1160 | (defun vc-git--call (buffer command &rest args) |
0664ff72 MA |
1161 | ;; We don't need to care the arguments. If there is a file name, it |
1162 | ;; is always a relative one. This works also for remote | |
99a54f21 MA |
1163 | ;; directories. We enable `inhibit-null-byte-detection', otherwise |
1164 | ;; Tramp's eol conversion might be confused. | |
1165 | (let ((inhibit-null-byte-detection t) | |
1166 | (process-environment (cons "PAGER=" process-environment))) | |
1167 | (apply 'process-file vc-git-program nil buffer nil command args))) | |
5fdbecd8 TTN |
1168 | |
1169 | (defun vc-git--out-ok (command &rest args) | |
1170 | (zerop (apply 'vc-git--call '(t nil) command args))) | |
1171 | ||
fff4a046 | 1172 | (defun vc-git--run-command-string (file &rest args) |
2a0e3379 DN |
1173 | "Run a git command on FILE and return its output as string. |
1174 | FILE can be nil." | |
fff4a046 DN |
1175 | (let* ((ok t) |
1176 | (str (with-output-to-string | |
1177 | (with-current-buffer standard-output | |
5fdbecd8 | 1178 | (unless (apply 'vc-git--out-ok |
2a0e3379 DN |
1179 | (if file |
1180 | (append args (list (file-relative-name | |
1181 | file))) | |
1182 | args)) | |
fff4a046 DN |
1183 | (setq ok nil)))))) |
1184 | (and ok str))) | |
1185 | ||
fff4a046 DN |
1186 | (defun vc-git-symbolic-commit (commit) |
1187 | "Translate COMMIT string into symbolic form. | |
1188 | Returns nil if not possible." | |
1189 | (and commit | |
4537363c AJ |
1190 | (let ((name (with-temp-buffer |
1191 | (and | |
1192 | (vc-git--out-ok "name-rev" "--name-only" commit) | |
1193 | (goto-char (point-min)) | |
1194 | (= (forward-line 2) 1) | |
1195 | (bolp) | |
4c83ed3d SM |
1196 | (buffer-substring-no-properties (point-min) |
1197 | (1- (point-max))))))) | |
4537363c | 1198 | (and name (not (string= name "undefined")) name)))) |
fff4a046 DN |
1199 | |
1200 | (provide 'vc-git) | |
53cc90ab DN |
1201 | |
1202 | ;;; vc-git.el ends here |