]>
Commit | Line | Data |
---|---|---|
711fc8f6 AJ |
1 | ;;; git.el --- A user interface for git |
2 | ||
d55552f6 | 3 | ;; Copyright (C) 2005, 2006, 2007 Alexandre Julliard <julliard@winehq.org> |
711fc8f6 AJ |
4 | |
5 | ;; Version: 1.0 | |
6 | ||
7 | ;; This program is free software; you can redistribute it and/or | |
8 | ;; modify it under the terms of the GNU General Public License as | |
9 | ;; published by the Free Software Foundation; either version 2 of | |
10 | ;; the License, or (at your option) any later version. | |
11 | ;; | |
12 | ;; This program is distributed in the hope that it will be | |
13 | ;; useful, but WITHOUT ANY WARRANTY; without even the implied | |
14 | ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | |
15 | ;; PURPOSE. See the GNU General Public License for more details. | |
16 | ;; | |
17 | ;; You should have received a copy of the GNU General Public | |
18 | ;; License along with this program; if not, write to the Free | |
19 | ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, | |
20 | ;; MA 02111-1307 USA | |
21 | ||
22 | ;;; Commentary: | |
23 | ||
24 | ;; This file contains an interface for the git version control | |
25 | ;; system. It provides easy access to the most frequently used git | |
26 | ;; commands. The user interface is as far as possible identical to | |
27 | ;; that of the PCL-CVS mode. | |
28 | ;; | |
29 | ;; To install: put this file on the load-path and place the following | |
30 | ;; in your .emacs file: | |
31 | ;; | |
32 | ;; (require 'git) | |
33 | ;; | |
34 | ;; To start: `M-x git-status' | |
35 | ;; | |
36 | ;; TODO | |
37 | ;; - portability to XEmacs | |
711fc8f6 AJ |
38 | ;; - diff against other branch |
39 | ;; - renaming files from the status buffer | |
711fc8f6 AJ |
40 | ;; - creating tags |
41 | ;; - fetch/pull | |
42 | ;; - switching branches | |
43 | ;; - revlist browser | |
44 | ;; - git-show-branch browser | |
711fc8f6 AJ |
45 | ;; - menus |
46 | ;; | |
47 | ||
48 | (eval-when-compile (require 'cl)) | |
49 | (require 'ewoc) | |
9fa77a51 | 50 | (require 'log-edit) |
18ff365f | 51 | (require 'easymenu) |
711fc8f6 AJ |
52 | |
53 | ||
a79656e6 | 54 | ;;;; Customizations |
711fc8f6 AJ |
55 | ;;;; ------------------------------------------------------------ |
56 | ||
a79656e6 | 57 | (defgroup git nil |
5df52584 AJ |
58 | "A user interface for the git versioning system." |
59 | :group 'tools) | |
a79656e6 AJ |
60 | |
61 | (defcustom git-committer-name nil | |
62 | "User name to use for commits. | |
1b3a6674 JN |
63 | The default is to fall back to the repository config, |
64 | then to `add-log-full-name' and then to `user-full-name'." | |
a79656e6 AJ |
65 | :group 'git |
66 | :type '(choice (const :tag "Default" nil) | |
67 | (string :tag "Name"))) | |
68 | ||
69 | (defcustom git-committer-email nil | |
70 | "Email address to use for commits. | |
1b3a6674 JN |
71 | The default is to fall back to the git repository config, |
72 | then to `add-log-mailing-address' and then to `user-mail-address'." | |
a79656e6 AJ |
73 | :group 'git |
74 | :type '(choice (const :tag "Default" nil) | |
75 | (string :tag "Email"))) | |
76 | ||
14b4f2db | 77 | (defcustom git-commits-coding-system nil |
a79656e6 AJ |
78 | "Default coding system for the log message of git commits." |
79 | :group 'git | |
14b4f2db AJ |
80 | :type '(choice (const :tag "From repository config" nil) |
81 | (coding-system))) | |
a79656e6 AJ |
82 | |
83 | (defcustom git-append-signed-off-by nil | |
84 | "Whether to append a Signed-off-by line to the commit message before editing." | |
85 | :group 'git | |
86 | :type 'boolean) | |
87 | ||
73389f12 AJ |
88 | (defcustom git-reuse-status-buffer t |
89 | "Whether `git-status' should try to reuse an existing buffer | |
90 | if there is already one that displays the same directory." | |
91 | :group 'git | |
92 | :type 'boolean) | |
93 | ||
a79656e6 AJ |
94 | (defcustom git-per-dir-ignore-file ".gitignore" |
95 | "Name of the per-directory ignore file." | |
96 | :group 'git | |
97 | :type 'string) | |
98 | ||
98acc3fa AJ |
99 | (defcustom git-show-uptodate nil |
100 | "Whether to display up-to-date files." | |
101 | :group 'git | |
102 | :type 'boolean) | |
103 | ||
104 | (defcustom git-show-ignored nil | |
105 | "Whether to display ignored files." | |
106 | :group 'git | |
107 | :type 'boolean) | |
108 | ||
109 | (defcustom git-show-unknown t | |
110 | "Whether to display unknown files." | |
111 | :group 'git | |
112 | :type 'boolean) | |
113 | ||
1b3a6674 | 114 | |
711fc8f6 | 115 | (defface git-status-face |
1ff55ff2 DK |
116 | '((((class color) (background light)) (:foreground "purple")) |
117 | (((class color) (background dark)) (:foreground "salmon"))) | |
a79656e6 AJ |
118 | "Git mode face used to highlight added and modified files." |
119 | :group 'git) | |
711fc8f6 AJ |
120 | |
121 | (defface git-unmerged-face | |
1ff55ff2 DK |
122 | '((((class color) (background light)) (:foreground "red" :bold t)) |
123 | (((class color) (background dark)) (:foreground "red" :bold t))) | |
a79656e6 AJ |
124 | "Git mode face used to highlight unmerged files." |
125 | :group 'git) | |
711fc8f6 AJ |
126 | |
127 | (defface git-unknown-face | |
1ff55ff2 DK |
128 | '((((class color) (background light)) (:foreground "goldenrod" :bold t)) |
129 | (((class color) (background dark)) (:foreground "goldenrod" :bold t))) | |
a79656e6 AJ |
130 | "Git mode face used to highlight unknown files." |
131 | :group 'git) | |
711fc8f6 AJ |
132 | |
133 | (defface git-uptodate-face | |
1ff55ff2 DK |
134 | '((((class color) (background light)) (:foreground "grey60")) |
135 | (((class color) (background dark)) (:foreground "grey40"))) | |
a79656e6 AJ |
136 | "Git mode face used to highlight up-to-date files." |
137 | :group 'git) | |
711fc8f6 AJ |
138 | |
139 | (defface git-ignored-face | |
1ff55ff2 DK |
140 | '((((class color) (background light)) (:foreground "grey60")) |
141 | (((class color) (background dark)) (:foreground "grey40"))) | |
a79656e6 AJ |
142 | "Git mode face used to highlight ignored files." |
143 | :group 'git) | |
711fc8f6 AJ |
144 | |
145 | (defface git-mark-face | |
1ff55ff2 DK |
146 | '((((class color) (background light)) (:foreground "red" :bold t)) |
147 | (((class color) (background dark)) (:foreground "tomato" :bold t))) | |
a79656e6 AJ |
148 | "Git mode face used for the file marks." |
149 | :group 'git) | |
711fc8f6 AJ |
150 | |
151 | (defface git-header-face | |
1ff55ff2 DK |
152 | '((((class color) (background light)) (:foreground "blue")) |
153 | (((class color) (background dark)) (:foreground "blue"))) | |
a79656e6 AJ |
154 | "Git mode face used for commit headers." |
155 | :group 'git) | |
711fc8f6 AJ |
156 | |
157 | (defface git-separator-face | |
1ff55ff2 DK |
158 | '((((class color) (background light)) (:foreground "brown")) |
159 | (((class color) (background dark)) (:foreground "brown"))) | |
a79656e6 AJ |
160 | "Git mode face used for commit separator." |
161 | :group 'git) | |
711fc8f6 AJ |
162 | |
163 | (defface git-permission-face | |
1ff55ff2 DK |
164 | '((((class color) (background light)) (:foreground "green" :bold t)) |
165 | (((class color) (background dark)) (:foreground "green" :bold t))) | |
a79656e6 AJ |
166 | "Git mode face used for permission changes." |
167 | :group 'git) | |
711fc8f6 AJ |
168 | |
169 | ||
170 | ;;;; Utilities | |
171 | ;;;; ------------------------------------------------------------ | |
172 | ||
a79656e6 AJ |
173 | (defconst git-log-msg-separator "--- log message follows this line ---") |
174 | ||
9fa77a51 | 175 | (defvar git-log-edit-font-lock-keywords |
6fb20426 | 176 | `(("^\\(Author:\\|Date:\\|Merge:\\|Signed-off-by:\\)\\(.*\\)$" |
9fa77a51 AJ |
177 | (1 font-lock-keyword-face) |
178 | (2 font-lock-function-name-face)) | |
179 | (,(concat "^\\(" (regexp-quote git-log-msg-separator) "\\)$") | |
180 | (1 font-lock-comment-face)))) | |
181 | ||
711fc8f6 AJ |
182 | (defun git-get-env-strings (env) |
183 | "Build a list of NAME=VALUE strings from a list of environment strings." | |
184 | (mapcar (lambda (entry) (concat (car entry) "=" (cdr entry))) env)) | |
185 | ||
9ddf6d7c | 186 | (defun git-call-process (buffer &rest args) |
711fc8f6 | 187 | "Wrapper for call-process that sets environment strings." |
9ddf6d7c | 188 | (apply #'call-process "git" nil buffer nil args)) |
711fc8f6 | 189 | |
0520e215 AJ |
190 | (defun git-call-process-display-error (&rest args) |
191 | "Wrapper for call-process that displays error messages." | |
192 | (let* ((dir default-directory) | |
193 | (buffer (get-buffer-create "*Git Command Output*")) | |
194 | (ok (with-current-buffer buffer | |
195 | (let ((default-directory dir) | |
196 | (buffer-read-only nil)) | |
197 | (erase-buffer) | |
9ddf6d7c | 198 | (eq 0 (apply #'git-call-process (list buffer t) args)))))) |
0520e215 AJ |
199 | (unless ok (display-message-or-buffer buffer)) |
200 | ok)) | |
201 | ||
9ddf6d7c AJ |
202 | (defun git-call-process-string (&rest args) |
203 | "Wrapper for call-process that returns the process output as a string, | |
204 | or nil if the git command failed." | |
9de83169 | 205 | (with-temp-buffer |
9ddf6d7c | 206 | (and (eq 0 (apply #'git-call-process t args)) |
9de83169 AJ |
207 | (buffer-string)))) |
208 | ||
36d2078f AJ |
209 | (defun git-call-process-string-display-error (&rest args) |
210 | "Wrapper for call-process that displays error message and returns | |
211 | the process output as a string, or nil if the git command failed." | |
212 | (with-temp-buffer | |
9ddf6d7c | 213 | (if (eq 0 (apply #'git-call-process (list t t) args)) |
36d2078f AJ |
214 | (buffer-string) |
215 | (display-message-or-buffer (current-buffer)) | |
216 | nil))) | |
217 | ||
711fc8f6 AJ |
218 | (defun git-run-process-region (buffer start end program args) |
219 | "Run a git process with a buffer region as input." | |
220 | (let ((output-buffer (current-buffer)) | |
221 | (dir default-directory)) | |
222 | (with-current-buffer buffer | |
223 | (cd dir) | |
224 | (apply #'call-process-region start end program | |
225 | nil (list output-buffer nil) nil args)))) | |
226 | ||
227 | (defun git-run-command-buffer (buffer-name &rest args) | |
228 | "Run a git command, sending the output to a buffer named BUFFER-NAME." | |
229 | (let ((dir default-directory) | |
230 | (buffer (get-buffer-create buffer-name))) | |
231 | (message "Running git %s..." (car args)) | |
232 | (with-current-buffer buffer | |
233 | (let ((default-directory dir) | |
234 | (buffer-read-only nil)) | |
235 | (erase-buffer) | |
9ddf6d7c | 236 | (apply #'git-call-process buffer args))) |
711fc8f6 AJ |
237 | (message "Running git %s...done" (car args)) |
238 | buffer)) | |
239 | ||
711fc8f6 AJ |
240 | (defun git-run-command-region (buffer start end env &rest args) |
241 | "Run a git command with specified buffer region as input." | |
3db4723e KW |
242 | (unless (eq 0 (if env |
243 | (git-run-process-region | |
244 | buffer start end "env" | |
245 | (append (git-get-env-strings env) (list "git") args)) | |
711fc8f6 AJ |
246 | (git-run-process-region |
247 | buffer start end "git" args))) | |
9f5599b9 | 248 | (error "Failed to run \"git %s\":\n%s" (mapconcat (lambda (x) x) args " ") (buffer-string)))) |
711fc8f6 | 249 | |
d55552f6 AJ |
250 | (defun git-run-hook (hook env &rest args) |
251 | "Run a git hook and display its output if any." | |
252 | (let ((dir default-directory) | |
253 | (hook-name (expand-file-name (concat ".git/hooks/" hook)))) | |
254 | (or (not (file-executable-p hook-name)) | |
255 | (let (status (buffer (get-buffer-create "*Git Hook Output*"))) | |
256 | (with-current-buffer buffer | |
257 | (erase-buffer) | |
258 | (cd dir) | |
259 | (setq status | |
3db4723e KW |
260 | (if env |
261 | (apply #'call-process "env" nil (list buffer t) nil | |
262 | (append (git-get-env-strings env) (list hook-name) args)) | |
d55552f6 AJ |
263 | (apply #'call-process hook-name nil (list buffer t) nil args)))) |
264 | (display-message-or-buffer buffer) | |
265 | (eq 0 status))))) | |
266 | ||
711fc8f6 AJ |
267 | (defun git-get-string-sha1 (string) |
268 | "Read a SHA1 from the specified string." | |
9de83169 AJ |
269 | (and string |
270 | (string-match "[0-9a-f]\\{40\\}" string) | |
271 | (match-string 0 string))) | |
711fc8f6 AJ |
272 | |
273 | (defun git-get-committer-name () | |
274 | "Return the name to use as GIT_COMMITTER_NAME." | |
275 | ; copied from log-edit | |
276 | (or git-committer-name | |
e0d10e1c | 277 | (git-config "user.name") |
711fc8f6 AJ |
278 | (and (boundp 'add-log-full-name) add-log-full-name) |
279 | (and (fboundp 'user-full-name) (user-full-name)) | |
280 | (and (boundp 'user-full-name) user-full-name))) | |
281 | ||
282 | (defun git-get-committer-email () | |
283 | "Return the email address to use as GIT_COMMITTER_EMAIL." | |
284 | ; copied from log-edit | |
285 | (or git-committer-email | |
e0d10e1c | 286 | (git-config "user.email") |
711fc8f6 AJ |
287 | (and (boundp 'add-log-mailing-address) add-log-mailing-address) |
288 | (and (fboundp 'user-mail-address) (user-mail-address)) | |
289 | (and (boundp 'user-mail-address) user-mail-address))) | |
290 | ||
14b4f2db AJ |
291 | (defun git-get-commits-coding-system () |
292 | "Return the coding system to use for commits." | |
293 | (let ((repo-config (git-config "i18n.commitencoding"))) | |
294 | (or git-commits-coding-system | |
295 | (and repo-config | |
296 | (fboundp 'locale-charset-to-coding-system) | |
297 | (locale-charset-to-coding-system repo-config)) | |
298 | 'utf-8))) | |
299 | ||
b704e589 AJ |
300 | (defun git-get-logoutput-coding-system () |
301 | "Return the coding system used for git-log output." | |
302 | (let ((repo-config (or (git-config "i18n.logoutputencoding") | |
303 | (git-config "i18n.commitencoding")))) | |
304 | (or git-commits-coding-system | |
305 | (and repo-config | |
306 | (fboundp 'locale-charset-to-coding-system) | |
307 | (locale-charset-to-coding-system repo-config)) | |
308 | 'utf-8))) | |
309 | ||
711fc8f6 AJ |
310 | (defun git-escape-file-name (name) |
311 | "Escape a file name if necessary." | |
312 | (if (string-match "[\n\t\"\\]" name) | |
313 | (concat "\"" | |
314 | (mapconcat (lambda (c) | |
315 | (case c | |
316 | (?\n "\\n") | |
317 | (?\t "\\t") | |
318 | (?\\ "\\\\") | |
319 | (?\" "\\\"") | |
320 | (t (char-to-string c)))) | |
321 | name "") | |
322 | "\"") | |
323 | name)) | |
324 | ||
9f5599b9 AJ |
325 | (defun git-success-message (text files) |
326 | "Print a success message after having handled FILES." | |
327 | (let ((n (length files))) | |
328 | (if (equal n 1) | |
329 | (message "%s %s" text (car files)) | |
330 | (message "%s %d files" text n)))) | |
331 | ||
711fc8f6 AJ |
332 | (defun git-get-top-dir (dir) |
333 | "Retrieve the top-level directory of a git tree." | |
334 | (let ((cdup (with-output-to-string | |
335 | (with-current-buffer standard-output | |
336 | (cd dir) | |
9ddf6d7c | 337 | (unless (eq 0 (git-call-process t "rev-parse" "--show-cdup")) |
711fc8f6 AJ |
338 | (error "cannot find top-level git tree for %s." dir)))))) |
339 | (expand-file-name (concat (file-name-as-directory dir) | |
340 | (car (split-string cdup "\n")))))) | |
341 | ||
342 | ;stolen from pcl-cvs | |
343 | (defun git-append-to-ignore (file) | |
344 | "Add a file name to the ignore file in its directory." | |
345 | (let* ((fullname (expand-file-name file)) | |
346 | (dir (file-name-directory fullname)) | |
b23761d9 AJ |
347 | (name (file-name-nondirectory fullname)) |
348 | (ignore-name (expand-file-name git-per-dir-ignore-file dir)) | |
349 | (created (not (file-exists-p ignore-name)))) | |
711fc8f6 | 350 | (save-window-excursion |
b23761d9 | 351 | (set-buffer (find-file-noselect ignore-name)) |
711fc8f6 AJ |
352 | (goto-char (point-max)) |
353 | (unless (zerop (current-column)) (insert "\n")) | |
9f56a7fd | 354 | (insert "/" name "\n") |
711fc8f6 | 355 | (sort-lines nil (point-min) (point-max)) |
b23761d9 AJ |
356 | (save-buffer)) |
357 | (when created | |
9ddf6d7c | 358 | (git-call-process nil "update-index" "--add" "--" (file-relative-name ignore-name))) |
433ee03f | 359 | (git-update-status-files (list (file-relative-name ignore-name))))) |
711fc8f6 | 360 | |
03d311ed AJ |
361 | ; propertize definition for XEmacs, stolen from erc-compat |
362 | (eval-when-compile | |
363 | (unless (fboundp 'propertize) | |
364 | (defun propertize (string &rest props) | |
365 | (let ((string (copy-sequence string))) | |
366 | (while props | |
367 | (put-text-property 0 (length string) (nth 0 props) (nth 1 props) string) | |
368 | (setq props (cddr props))) | |
369 | string)))) | |
711fc8f6 AJ |
370 | |
371 | ;;;; Wrappers for basic git commands | |
372 | ;;;; ------------------------------------------------------------ | |
373 | ||
374 | (defun git-rev-parse (rev) | |
375 | "Parse a revision name and return its SHA1." | |
376 | (git-get-string-sha1 | |
9ddf6d7c | 377 | (git-call-process-string "rev-parse" rev))) |
711fc8f6 | 378 | |
e0d10e1c | 379 | (defun git-config (key) |
75a8180d | 380 | "Retrieve the value associated to KEY in the git repository config file." |
9ddf6d7c | 381 | (let ((str (git-call-process-string "config" key))) |
75a8180d AJ |
382 | (and str (car (split-string str "\n"))))) |
383 | ||
711fc8f6 AJ |
384 | (defun git-symbolic-ref (ref) |
385 | "Wrapper for the git-symbolic-ref command." | |
9ddf6d7c | 386 | (let ((str (git-call-process-string "symbolic-ref" ref))) |
9de83169 | 387 | (and str (car (split-string str "\n"))))) |
711fc8f6 | 388 | |
413689d3 | 389 | (defun git-update-ref (ref newval &optional oldval reason) |
711fc8f6 | 390 | "Update a reference by calling git-update-ref." |
413689d3 | 391 | (let ((args (and oldval (list oldval)))) |
db18a182 | 392 | (when newval (push newval args)) |
413689d3 AJ |
393 | (push ref args) |
394 | (when reason | |
395 | (push reason args) | |
396 | (push "-m" args)) | |
db18a182 | 397 | (unless newval (push "-d" args)) |
0520e215 | 398 | (apply 'git-call-process-display-error "update-ref" args))) |
711fc8f6 AJ |
399 | |
400 | (defun git-read-tree (tree &optional index-file) | |
401 | "Read a tree into the index file." | |
36d2078f AJ |
402 | (let ((process-environment |
403 | (append (and index-file (list (concat "GIT_INDEX_FILE=" index-file))) process-environment))) | |
404 | (apply 'git-call-process-display-error "read-tree" (if tree (list tree))))) | |
711fc8f6 AJ |
405 | |
406 | (defun git-write-tree (&optional index-file) | |
407 | "Call git-write-tree and return the resulting tree SHA1 as a string." | |
36d2078f AJ |
408 | (let ((process-environment |
409 | (append (and index-file (list (concat "GIT_INDEX_FILE=" index-file))) process-environment))) | |
410 | (git-get-string-sha1 | |
411 | (git-call-process-string-display-error "write-tree")))) | |
711fc8f6 AJ |
412 | |
413 | (defun git-commit-tree (buffer tree head) | |
414 | "Call git-commit-tree with buffer as input and return the resulting commit SHA1." | |
415 | (let ((author-name (git-get-committer-name)) | |
416 | (author-email (git-get-committer-email)) | |
413689d3 | 417 | (subject "commit (initial): ") |
14b4f2db | 418 | author-date log-start log-end args coding-system-for-write) |
711fc8f6 | 419 | (when head |
413689d3 | 420 | (setq subject "commit: ") |
711fc8f6 AJ |
421 | (push "-p" args) |
422 | (push head args)) | |
423 | (with-current-buffer buffer | |
424 | (goto-char (point-min)) | |
425 | (if | |
a79656e6 | 426 | (setq log-start (re-search-forward (concat "^" (regexp-quote git-log-msg-separator) "\n") nil t)) |
711fc8f6 AJ |
427 | (save-restriction |
428 | (narrow-to-region (point-min) log-start) | |
429 | (goto-char (point-min)) | |
430 | (when (re-search-forward "^Author: +\\(.*?\\) *<\\(.*\\)> *$" nil t) | |
431 | (setq author-name (match-string 1) | |
432 | author-email (match-string 2))) | |
433 | (goto-char (point-min)) | |
434 | (when (re-search-forward "^Date: +\\(.*\\)$" nil t) | |
435 | (setq author-date (match-string 1))) | |
436 | (goto-char (point-min)) | |
6fb20426 AJ |
437 | (when (re-search-forward "^Merge: +\\(.*\\)" nil t) |
438 | (setq subject "commit (merge): ") | |
439 | (dolist (parent (split-string (match-string 1) " +" t)) | |
711fc8f6 | 440 | (push "-p" args) |
6fb20426 | 441 | (push parent args)))) |
711fc8f6 | 442 | (setq log-start (point-min))) |
14b4f2db | 443 | (setq log-end (point-max)) |
413689d3 AJ |
444 | (goto-char log-start) |
445 | (when (re-search-forward ".*$" nil t) | |
446 | (setq subject (concat subject (match-string 0)))) | |
14b4f2db | 447 | (setq coding-system-for-write buffer-file-coding-system)) |
413689d3 AJ |
448 | (let ((commit |
449 | (git-get-string-sha1 | |
450 | (with-output-to-string | |
451 | (with-current-buffer standard-output | |
452 | (let ((env `(("GIT_AUTHOR_NAME" . ,author-name) | |
453 | ("GIT_AUTHOR_EMAIL" . ,author-email) | |
454 | ("GIT_COMMITTER_NAME" . ,(git-get-committer-name)) | |
455 | ("GIT_COMMITTER_EMAIL" . ,(git-get-committer-email))))) | |
456 | (when author-date (push `("GIT_AUTHOR_DATE" . ,author-date) env)) | |
457 | (apply #'git-run-command-region | |
458 | buffer log-start log-end env | |
459 | "commit-tree" tree (nreverse args)))))))) | |
460 | (and (git-update-ref "HEAD" commit head subject) | |
461 | commit)))) | |
711fc8f6 AJ |
462 | |
463 | (defun git-empty-db-p () | |
464 | "Check if the git db is empty (no commit done yet)." | |
9ddf6d7c | 465 | (not (eq 0 (git-call-process nil "rev-parse" "--verify" "HEAD")))) |
711fc8f6 AJ |
466 | |
467 | (defun git-get-merge-heads () | |
468 | "Retrieve the merge heads from the MERGE_HEAD file if present." | |
469 | (let (heads) | |
470 | (when (file-readable-p ".git/MERGE_HEAD") | |
471 | (with-temp-buffer | |
472 | (insert-file-contents ".git/MERGE_HEAD" nil nil nil t) | |
473 | (goto-char (point-min)) | |
474 | (while (re-search-forward "[0-9a-f]\\{40\\}" nil t) | |
475 | (push (match-string 0) heads)))) | |
476 | (nreverse heads))) | |
477 | ||
b704e589 AJ |
478 | (defun git-get-commit-description (commit) |
479 | "Get a one-line description of COMMIT." | |
480 | (let ((coding-system-for-read (git-get-logoutput-coding-system))) | |
9ddf6d7c | 481 | (let ((descr (git-call-process-string "log" "--max-count=1" "--pretty=oneline" commit))) |
b704e589 AJ |
482 | (if (and descr (string-match "\\`\\([0-9a-f]\\{40\\}\\) *\\(.*\\)$" descr)) |
483 | (concat (substring (match-string 1 descr) 0 10) " - " (match-string 2 descr)) | |
484 | descr)))) | |
485 | ||
711fc8f6 AJ |
486 | ;;;; File info structure |
487 | ;;;; ------------------------------------------------------------ | |
488 | ||
489 | ; fileinfo structure stolen from pcl-cvs | |
490 | (defstruct (git-fileinfo | |
491 | (:copier nil) | |
492 | (:constructor git-create-fileinfo (state name &optional old-perm new-perm rename-state orig-name marked)) | |
493 | (:conc-name git-fileinfo->)) | |
494 | marked ;; t/nil | |
495 | state ;; current state | |
496 | name ;; file name | |
497 | old-perm new-perm ;; permission flags | |
498 | rename-state ;; rename or copy state | |
499 | orig-name ;; original name for renames or copies | |
433ee03f | 500 | needs-update ;; whether file needs to be updated |
711fc8f6 AJ |
501 | needs-refresh) ;; whether file needs to be refreshed |
502 | ||
503 | (defvar git-status nil) | |
504 | ||
72dc52bf AJ |
505 | (defun git-set-fileinfo-state (info state) |
506 | "Set the state of a file info." | |
507 | (unless (eq (git-fileinfo->state info) state) | |
508 | (setf (git-fileinfo->state info) state | |
40f162b0 | 509 | (git-fileinfo->new-perm info) (git-fileinfo->old-perm info) |
72dc52bf AJ |
510 | (git-fileinfo->rename-state info) nil |
511 | (git-fileinfo->orig-name info) nil | |
433ee03f | 512 | (git-fileinfo->needs-update info) nil |
72dc52bf | 513 | (git-fileinfo->needs-refresh info) t))) |
711fc8f6 | 514 | |
b9b7bab4 AJ |
515 | (defun git-status-filenames-map (status func files &rest args) |
516 | "Apply FUNC to the status files names in the FILES list." | |
1b655040 AJ |
517 | (when files |
518 | (setq files (sort files #'string-lessp)) | |
519 | (let ((file (pop files)) | |
520 | (node (ewoc-nth status 0))) | |
521 | (while (and file node) | |
433ee03f AJ |
522 | (let* ((info (ewoc-data node)) |
523 | (name (git-fileinfo->name info))) | |
524 | (if (string-lessp name file) | |
b9b7bab4 | 525 | (setq node (ewoc-next status node)) |
433ee03f | 526 | (if (string-equal name file) |
b9b7bab4 AJ |
527 | (apply func info args)) |
528 | (setq file (pop files)))))))) | |
529 | ||
530 | (defun git-set-filenames-state (status files state) | |
531 | "Set the state of a list of named files." | |
532 | (when files | |
72dc52bf | 533 | (git-status-filenames-map status #'git-set-fileinfo-state files state) |
1b655040 AJ |
534 | (unless state ;; delete files whose state has been set to nil |
535 | (ewoc-filter status (lambda (info) (git-fileinfo->state info)))))) | |
536 | ||
711fc8f6 AJ |
537 | (defun git-state-code (code) |
538 | "Convert from a string to a added/deleted/modified state." | |
539 | (case (string-to-char code) | |
540 | (?M 'modified) | |
541 | (?? 'unknown) | |
542 | (?A 'added) | |
543 | (?D 'deleted) | |
544 | (?U 'unmerged) | |
40f162b0 | 545 | (?T 'modified) |
711fc8f6 AJ |
546 | (t nil))) |
547 | ||
548 | (defun git-status-code-as-string (code) | |
549 | "Format a git status code as string." | |
550 | (case code | |
551 | ('modified (propertize "Modified" 'face 'git-status-face)) | |
552 | ('unknown (propertize "Unknown " 'face 'git-unknown-face)) | |
553 | ('added (propertize "Added " 'face 'git-status-face)) | |
554 | ('deleted (propertize "Deleted " 'face 'git-status-face)) | |
555 | ('unmerged (propertize "Unmerged" 'face 'git-unmerged-face)) | |
556 | ('uptodate (propertize "Uptodate" 'face 'git-uptodate-face)) | |
557 | ('ignored (propertize "Ignored " 'face 'git-ignored-face)) | |
558 | (t "? "))) | |
559 | ||
ef40b3ef AJ |
560 | (defun git-file-type-as-string (old-perm new-perm) |
561 | "Return a string describing the file type based on its permissions." | |
562 | (let* ((old-type (lsh (or old-perm 0) -9)) | |
563 | (new-type (lsh (or new-perm 0) -9)) | |
40f162b0 AJ |
564 | (str (case new-type |
565 | (?\100 ;; file | |
566 | (case old-type | |
567 | (?\100 nil) | |
568 | (?\120 " (type change symlink -> file)") | |
569 | (?\160 " (type change subproject -> file)"))) | |
570 | (?\120 ;; symlink | |
571 | (case old-type | |
572 | (?\100 " (type change file -> symlink)") | |
573 | (?\160 " (type change subproject -> symlink)") | |
574 | (t " (symlink)"))) | |
575 | (?\160 ;; subproject | |
576 | (case old-type | |
577 | (?\100 " (type change file -> subproject)") | |
578 | (?\120 " (type change symlink -> subproject)") | |
579 | (t " (subproject)"))) | |
3f3d564a | 580 | (?\110 nil) ;; directory (internal, not a real git state) |
40f162b0 AJ |
581 | (?\000 ;; deleted or unknown |
582 | (case old-type | |
583 | (?\120 " (symlink)") | |
584 | (?\160 " (subproject)"))) | |
585 | (t (format " (unknown type %o)" new-type))))) | |
3f3d564a AJ |
586 | (cond (str (propertize str 'face 'git-status-face)) |
587 | ((eq new-type ?\110) "/") | |
588 | (t "")))) | |
40f162b0 | 589 | |
711fc8f6 AJ |
590 | (defun git-rename-as-string (info) |
591 | "Return a string describing the copy or rename associated with INFO, or an empty string if none." | |
592 | (let ((state (git-fileinfo->rename-state info))) | |
593 | (if state | |
594 | (propertize | |
595 | (concat " (" | |
596 | (if (eq state 'copy) "copied from " | |
c530c5aa AJ |
597 | (if (eq (git-fileinfo->state info) 'added) "renamed from " |
598 | "renamed to ")) | |
711fc8f6 AJ |
599 | (git-escape-file-name (git-fileinfo->orig-name info)) |
600 | ")") 'face 'git-status-face) | |
601 | ""))) | |
602 | ||
603 | (defun git-permissions-as-string (old-perm new-perm) | |
604 | "Format a permission change as string." | |
605 | (propertize | |
606 | (if (or (not old-perm) | |
607 | (not new-perm) | |
18e3e99e | 608 | (eq 0 (logand ?\111 (logxor old-perm new-perm)))) |
711fc8f6 | 609 | " " |
18e3e99e | 610 | (if (eq 0 (logand ?\111 old-perm)) "+x" "-x")) |
711fc8f6 AJ |
611 | 'face 'git-permission-face)) |
612 | ||
613 | (defun git-fileinfo-prettyprint (info) | |
614 | "Pretty-printer for the git-fileinfo structure." | |
ef40b3ef AJ |
615 | (let ((old-perm (git-fileinfo->old-perm info)) |
616 | (new-perm (git-fileinfo->new-perm info))) | |
617 | (insert (concat " " (if (git-fileinfo->marked info) (propertize "*" 'face 'git-mark-face) " ") | |
618 | " " (git-status-code-as-string (git-fileinfo->state info)) | |
619 | " " (git-permissions-as-string old-perm new-perm) | |
620 | " " (git-escape-file-name (git-fileinfo->name info)) | |
621 | (git-file-type-as-string old-perm new-perm) | |
622 | (git-rename-as-string info))))) | |
711fc8f6 | 623 | |
433ee03f AJ |
624 | (defun git-update-node-fileinfo (node info) |
625 | "Update the fileinfo of the specified node. The names are assumed to match already." | |
626 | (let ((data (ewoc-data node))) | |
627 | (setf | |
628 | ;; preserve the marked flag | |
629 | (git-fileinfo->marked info) (git-fileinfo->marked data) | |
630 | (git-fileinfo->needs-update data) nil) | |
631 | (when (not (equal info data)) | |
632 | (setf (git-fileinfo->needs-refresh info) t | |
633 | (ewoc-data node) info)))) | |
634 | ||
635 | (defun git-insert-info-list (status infolist files) | |
636 | "Insert a sorted list of file infos in the status buffer, replacing existing ones if any." | |
637 | (let* ((info (pop infolist)) | |
638 | (node (ewoc-nth status 0)) | |
639 | (name (and info (git-fileinfo->name info))) | |
640 | remaining) | |
1b655040 | 641 | (while info |
433ee03f AJ |
642 | (let ((nodename (and node (git-fileinfo->name (ewoc-data node))))) |
643 | (while (and files (string-lessp (car files) name)) | |
644 | (push (pop files) remaining)) | |
645 | (when (and files (string-equal (car files) name)) | |
646 | (setq files (cdr files))) | |
647 | (cond ((not nodename) | |
648 | (setq node (ewoc-enter-last status info)) | |
649 | (setq info (pop infolist)) | |
650 | (setq name (and info (git-fileinfo->name info)))) | |
651 | ((string-lessp nodename name) | |
652 | (setq node (ewoc-next status node))) | |
653 | ((string-equal nodename name) | |
654 | ;; preserve the marked flag | |
655 | (git-update-node-fileinfo node info) | |
656 | (setq info (pop infolist)) | |
657 | (setq name (and info (git-fileinfo->name info)))) | |
658 | (t | |
659 | (setq node (ewoc-enter-before status node info)) | |
660 | (setq info (pop infolist)) | |
661 | (setq name (and info (git-fileinfo->name info))))))) | |
662 | (nconc (nreverse remaining) files))) | |
93c22eeb AJ |
663 | |
664 | (defun git-run-diff-index (status files) | |
665 | "Run git-diff-index on FILES and parse the results into STATUS. | |
666 | Return the list of files that haven't been handled." | |
433ee03f | 667 | (let (infolist) |
93c22eeb | 668 | (with-temp-buffer |
9ddf6d7c | 669 | (apply #'git-call-process t "diff-index" "-z" "-M" "HEAD" "--" files) |
93c22eeb AJ |
670 | (goto-char (point-min)) |
671 | (while (re-search-forward | |
40f162b0 | 672 | ":\\([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" |
93c22eeb AJ |
673 | nil t 1) |
674 | (let ((old-perm (string-to-number (match-string 1) 8)) | |
675 | (new-perm (string-to-number (match-string 2) 8)) | |
676 | (state (or (match-string 4) (match-string 6))) | |
677 | (name (or (match-string 5) (match-string 7))) | |
678 | (new-name (match-string 8))) | |
679 | (if new-name ; copy or rename | |
680 | (if (eq ?C (string-to-char state)) | |
1b655040 AJ |
681 | (push (git-create-fileinfo 'added new-name old-perm new-perm 'copy name) infolist) |
682 | (push (git-create-fileinfo 'deleted name 0 0 'rename new-name) infolist) | |
683 | (push (git-create-fileinfo 'added new-name old-perm new-perm 'rename name) infolist)) | |
433ee03f AJ |
684 | (push (git-create-fileinfo (git-state-code state) name old-perm new-perm) infolist))))) |
685 | (setq infolist (sort (nreverse infolist) | |
686 | (lambda (info1 info2) | |
687 | (string-lessp (git-fileinfo->name info1) | |
688 | (git-fileinfo->name info2))))) | |
689 | (git-insert-info-list status infolist files))) | |
711fc8f6 AJ |
690 | |
691 | (defun git-find-status-file (status file) | |
692 | "Find a given file in the status ewoc and return its node." | |
693 | (let ((node (ewoc-nth status 0))) | |
694 | (while (and node (not (string= file (git-fileinfo->name (ewoc-data node))))) | |
695 | (setq node (ewoc-next status node))) | |
696 | node)) | |
697 | ||
93c22eeb AJ |
698 | (defun git-run-ls-files (status files default-state &rest options) |
699 | "Run git-ls-files on FILES and parse the results into STATUS. | |
700 | Return the list of files that haven't been handled." | |
1b655040 | 701 | (let (infolist) |
93c22eeb | 702 | (with-temp-buffer |
9ddf6d7c | 703 | (apply #'git-call-process t "ls-files" "-z" (append options (list "--") files)) |
93c22eeb | 704 | (goto-char (point-min)) |
3f3d564a | 705 | (while (re-search-forward "\\([^\0]*?\\)\\(/?\\)\0" nil t 1) |
1b655040 | 706 | (let ((name (match-string 1))) |
3f3d564a AJ |
707 | (push (git-create-fileinfo default-state name 0 |
708 | (if (string-equal "/" (match-string 2)) (lsh ?\110 9) 0)) | |
433ee03f AJ |
709 | infolist)))) |
710 | (setq infolist (nreverse infolist)) ;; assume it is sorted already | |
711 | (git-insert-info-list status infolist files))) | |
93c22eeb | 712 | |
5e3cb7e5 AJ |
713 | (defun git-run-ls-files-cached (status files default-state) |
714 | "Run git-ls-files -c on FILES and parse the results into STATUS. | |
715 | Return the list of files that haven't been handled." | |
433ee03f | 716 | (let (infolist) |
5e3cb7e5 | 717 | (with-temp-buffer |
9ddf6d7c | 718 | (apply #'git-call-process t "ls-files" "-z" "-s" "-c" "--" files) |
5e3cb7e5 | 719 | (goto-char (point-min)) |
87e3d812 | 720 | (while (re-search-forward "\\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} 0\t\\([^\0]+\\)\0" nil t) |
5e3cb7e5 AJ |
721 | (let* ((new-perm (string-to-number (match-string 1) 8)) |
722 | (old-perm (if (eq default-state 'added) 0 new-perm)) | |
723 | (name (match-string 2))) | |
433ee03f AJ |
724 | (push (git-create-fileinfo default-state name old-perm new-perm) infolist)))) |
725 | (setq infolist (nreverse infolist)) ;; assume it is sorted already | |
726 | (git-insert-info-list status infolist files))) | |
5e3cb7e5 | 727 | |
93c22eeb AJ |
728 | (defun git-run-ls-unmerged (status files) |
729 | "Run git-ls-files -u on FILES and parse the results into STATUS." | |
730 | (with-temp-buffer | |
9ddf6d7c | 731 | (apply #'git-call-process t "ls-files" "-z" "-u" "--" files) |
93c22eeb AJ |
732 | (goto-char (point-min)) |
733 | (let (unmerged-files) | |
734 | (while (re-search-forward "[0-7]\\{6\\} [0-9a-f]\\{40\\} [123]\t\\([^\0]+\\)\0" nil t) | |
1b655040 AJ |
735 | (push (match-string 1) unmerged-files)) |
736 | (git-set-filenames-state status unmerged-files 'unmerged)))) | |
93c22eeb | 737 | |
274e13e0 AJ |
738 | (defun git-get-exclude-files () |
739 | "Get the list of exclude files to pass to git-ls-files." | |
740 | (let (files | |
741 | (config (git-config "core.excludesfile"))) | |
742 | (when (file-readable-p ".git/info/exclude") | |
743 | (push ".git/info/exclude" files)) | |
744 | (when (and config (file-readable-p config)) | |
745 | (push config files)) | |
746 | files)) | |
747 | ||
98acc3fa AJ |
748 | (defun git-run-ls-files-with-excludes (status files default-state &rest options) |
749 | "Run git-ls-files on FILES with appropriate --exclude-from options." | |
750 | (let ((exclude-files (git-get-exclude-files))) | |
21a2d69b | 751 | (apply #'git-run-ls-files status files default-state "--directory" "--no-empty-directory" |
98acc3fa AJ |
752 | (concat "--exclude-per-directory=" git-per-dir-ignore-file) |
753 | (append options (mapcar (lambda (f) (concat "--exclude-from=" f)) exclude-files))))) | |
754 | ||
c4e8b72f | 755 | (defun git-update-status-files (&optional files mark-files) |
93c22eeb | 756 | "Update the status of FILES from the index." |
711fc8f6 | 757 | (unless git-status (error "Not in git-status buffer.")) |
433ee03f AJ |
758 | ;; set the needs-update flag on existing files |
759 | (if (setq files (sort files #'string-lessp)) | |
760 | (git-status-filenames-map | |
761 | git-status (lambda (info) (setf (git-fileinfo->needs-update info) t)) files) | |
762 | (ewoc-map (lambda (info) (setf (git-fileinfo->needs-update info) t) nil) git-status) | |
763 | (git-call-process nil "update-index" "--refresh") | |
764 | (when git-show-uptodate | |
765 | (git-run-ls-files-cached git-status nil 'uptodate))) | |
98acc3fa | 766 | (let* ((remaining-files |
93c22eeb | 767 | (if (git-empty-db-p) ; we need some special handling for an empty db |
5e3cb7e5 | 768 | (git-run-ls-files-cached git-status files 'added) |
98acc3fa AJ |
769 | (git-run-diff-index git-status files)))) |
770 | (git-run-ls-unmerged git-status files) | |
771 | (when (or remaining-files (and git-show-unknown (not files))) | |
772 | (setq remaining-files (git-run-ls-files-with-excludes git-status remaining-files 'unknown "-o"))) | |
773 | (when (or remaining-files (and git-show-ignored (not files))) | |
774 | (setq remaining-files (git-run-ls-files-with-excludes git-status remaining-files 'ignored "-o" "-i"))) | |
433ee03f AJ |
775 | (unless files |
776 | (setq remaining-files (git-get-filenames (ewoc-collect git-status #'git-fileinfo->needs-update)))) | |
777 | (when remaining-files | |
778 | (setq remaining-files (git-run-ls-files-cached git-status remaining-files 'uptodate))) | |
779 | (git-set-filenames-state git-status remaining-files nil) | |
c4e8b72f | 780 | (when mark-files (git-mark-files git-status files)) |
93c22eeb | 781 | (git-refresh-files) |
98acc3fa | 782 | (git-refresh-ewoc-hf git-status))) |
711fc8f6 | 783 | |
76127b3a AJ |
784 | (defun git-mark-files (status files) |
785 | "Mark all the specified FILES, and unmark the others." | |
76127b3a AJ |
786 | (let ((file (and files (pop files))) |
787 | (node (ewoc-nth status 0))) | |
788 | (while node | |
789 | (let ((info (ewoc-data node))) | |
790 | (if (and file (string-equal (git-fileinfo->name info) file)) | |
791 | (progn | |
792 | (unless (git-fileinfo->marked info) | |
793 | (setf (git-fileinfo->marked info) t) | |
794 | (setf (git-fileinfo->needs-refresh info) t)) | |
795 | (setq file (pop files)) | |
796 | (setq node (ewoc-next status node))) | |
797 | (when (git-fileinfo->marked info) | |
798 | (setf (git-fileinfo->marked info) nil) | |
799 | (setf (git-fileinfo->needs-refresh info) t)) | |
800 | (if (and file (string-lessp file (git-fileinfo->name info))) | |
801 | (setq file (pop files)) | |
802 | (setq node (ewoc-next status node)))))))) | |
803 | ||
711fc8f6 AJ |
804 | (defun git-marked-files () |
805 | "Return a list of all marked files, or if none a list containing just the file at cursor position." | |
806 | (unless git-status (error "Not in git-status buffer.")) | |
807 | (or (ewoc-collect git-status (lambda (info) (git-fileinfo->marked info))) | |
808 | (list (ewoc-data (ewoc-locate git-status))))) | |
809 | ||
810 | (defun git-marked-files-state (&rest states) | |
811 | "Return marked files that are in the specified states." | |
812 | (let ((files (git-marked-files)) | |
813 | result) | |
814 | (dolist (info files) | |
815 | (when (memq (git-fileinfo->state info) states) | |
816 | (push info result))) | |
817 | result)) | |
818 | ||
819 | (defun git-refresh-files () | |
820 | "Refresh all files that need it and clear the needs-refresh flag." | |
821 | (unless git-status (error "Not in git-status buffer.")) | |
822 | (ewoc-map | |
823 | (lambda (info) | |
824 | (let ((refresh (git-fileinfo->needs-refresh info))) | |
825 | (setf (git-fileinfo->needs-refresh info) nil) | |
826 | refresh)) | |
827 | git-status) | |
828 | ; move back to goal column | |
829 | (when goal-column (move-to-column goal-column))) | |
830 | ||
831 | (defun git-refresh-ewoc-hf (status) | |
832 | "Refresh the ewoc header and footer." | |
833 | (let ((branch (git-symbolic-ref "HEAD")) | |
834 | (head (if (git-empty-db-p) "Nothing committed yet" | |
b704e589 | 835 | (git-get-commit-description "HEAD"))) |
711fc8f6 AJ |
836 | (merge-heads (git-get-merge-heads))) |
837 | (ewoc-set-hf status | |
838 | (format "Directory: %s\nBranch: %s\nHead: %s%s\n" | |
839 | default-directory | |
ef08c149 AJ |
840 | (if branch |
841 | (if (string-match "^refs/heads/" branch) | |
842 | (substring branch (match-end 0)) | |
843 | branch) | |
844 | "none (detached HEAD)") | |
711fc8f6 AJ |
845 | head |
846 | (if merge-heads | |
847 | (concat "\nMerging: " | |
b704e589 | 848 | (mapconcat (lambda (str) (git-get-commit-description str)) merge-heads "\n ")) |
711fc8f6 AJ |
849 | "")) |
850 | (if (ewoc-nth status 0) "" " No changes.")))) | |
851 | ||
852 | (defun git-get-filenames (files) | |
853 | (mapcar (lambda (info) (git-fileinfo->name info)) files)) | |
854 | ||
855 | (defun git-update-index (index-file files) | |
856 | "Run git-update-index on a list of files." | |
36d2078f AJ |
857 | (let ((process-environment (append (and index-file (list (concat "GIT_INDEX_FILE=" index-file))) |
858 | process-environment)) | |
711fc8f6 AJ |
859 | added deleted modified) |
860 | (dolist (info files) | |
861 | (case (git-fileinfo->state info) | |
862 | ('added (push info added)) | |
863 | ('deleted (push info deleted)) | |
864 | ('modified (push info modified)))) | |
36d2078f AJ |
865 | (and |
866 | (or (not added) (apply #'git-call-process-display-error "update-index" "--add" "--" (git-get-filenames added))) | |
867 | (or (not deleted) (apply #'git-call-process-display-error "update-index" "--remove" "--" (git-get-filenames deleted))) | |
868 | (or (not modified) (apply #'git-call-process-display-error "update-index" "--" (git-get-filenames modified)))))) | |
711fc8f6 | 869 | |
d55552f6 AJ |
870 | (defun git-run-pre-commit-hook () |
871 | "Run the pre-commit hook if any." | |
872 | (unless git-status (error "Not in git-status buffer.")) | |
873 | (let ((files (git-marked-files-state 'added 'deleted 'modified))) | |
874 | (or (not files) | |
875 | (not (file-executable-p ".git/hooks/pre-commit")) | |
876 | (let ((index-file (make-temp-file "gitidx"))) | |
877 | (unwind-protect | |
878 | (let ((head-tree (unless (git-empty-db-p) (git-rev-parse "HEAD^{tree}")))) | |
879 | (git-read-tree head-tree index-file) | |
880 | (git-update-index index-file files) | |
881 | (git-run-hook "pre-commit" `(("GIT_INDEX_FILE" . ,index-file)))) | |
882 | (delete-file index-file)))))) | |
883 | ||
711fc8f6 AJ |
884 | (defun git-do-commit () |
885 | "Perform the actual commit using the current buffer as log message." | |
886 | (interactive) | |
887 | (let ((buffer (current-buffer)) | |
888 | (index-file (make-temp-file "gitidx"))) | |
889 | (with-current-buffer log-edit-parent-buffer | |
890 | (if (git-marked-files-state 'unmerged) | |
891 | (message "You cannot commit unmerged files, resolve them first.") | |
892 | (unwind-protect | |
893 | (let ((files (git-marked-files-state 'added 'deleted 'modified)) | |
36d2078f | 894 | head tree head-tree) |
711fc8f6 AJ |
895 | (unless (git-empty-db-p) |
896 | (setq head (git-rev-parse "HEAD") | |
897 | head-tree (git-rev-parse "HEAD^{tree}"))) | |
898 | (if files | |
899 | (progn | |
9f5599b9 | 900 | (message "Running git commit...") |
36d2078f AJ |
901 | (when |
902 | (and | |
903 | (git-read-tree head-tree index-file) | |
904 | (git-update-index nil files) ;update both the default index | |
905 | (git-update-index index-file files) ;and the temporary one | |
906 | (setq tree (git-write-tree index-file))) | |
711fc8f6 AJ |
907 | (if (or (not (string-equal tree head-tree)) |
908 | (yes-or-no-p "The tree was not modified, do you really want to perform an empty commit? ")) | |
909 | (let ((commit (git-commit-tree buffer tree head))) | |
0520e215 AJ |
910 | (when commit |
911 | (condition-case nil (delete-file ".git/MERGE_HEAD") (error nil)) | |
912 | (condition-case nil (delete-file ".git/MERGE_MSG") (error nil)) | |
913 | (with-current-buffer buffer (erase-buffer)) | |
433ee03f | 914 | (git-update-status-files (git-get-filenames files)) |
9ddf6d7c AJ |
915 | (git-call-process nil "rerere") |
916 | (git-call-process nil "gc" "--auto") | |
0520e215 AJ |
917 | (message "Committed %s." commit) |
918 | (git-run-hook "post-commit" nil))) | |
711fc8f6 AJ |
919 | (message "Commit aborted.")))) |
920 | (message "No files to commit."))) | |
921 | (delete-file index-file)))))) | |
922 | ||
923 | ||
924 | ;;;; Interactive functions | |
925 | ;;;; ------------------------------------------------------------ | |
926 | ||
927 | (defun git-mark-file () | |
928 | "Mark the file that the cursor is on and move to the next one." | |
929 | (interactive) | |
930 | (unless git-status (error "Not in git-status buffer.")) | |
931 | (let* ((pos (ewoc-locate git-status)) | |
932 | (info (ewoc-data pos))) | |
933 | (setf (git-fileinfo->marked info) t) | |
934 | (ewoc-invalidate git-status pos) | |
935 | (ewoc-goto-next git-status 1))) | |
936 | ||
937 | (defun git-unmark-file () | |
938 | "Unmark the file that the cursor is on and move to the next one." | |
939 | (interactive) | |
940 | (unless git-status (error "Not in git-status buffer.")) | |
941 | (let* ((pos (ewoc-locate git-status)) | |
942 | (info (ewoc-data pos))) | |
943 | (setf (git-fileinfo->marked info) nil) | |
944 | (ewoc-invalidate git-status pos) | |
945 | (ewoc-goto-next git-status 1))) | |
946 | ||
947 | (defun git-unmark-file-up () | |
948 | "Unmark the file that the cursor is on and move to the previous one." | |
949 | (interactive) | |
950 | (unless git-status (error "Not in git-status buffer.")) | |
951 | (let* ((pos (ewoc-locate git-status)) | |
952 | (info (ewoc-data pos))) | |
953 | (setf (git-fileinfo->marked info) nil) | |
954 | (ewoc-invalidate git-status pos) | |
955 | (ewoc-goto-prev git-status 1))) | |
956 | ||
957 | (defun git-mark-all () | |
958 | "Mark all files." | |
959 | (interactive) | |
960 | (unless git-status (error "Not in git-status buffer.")) | |
2f6e86a8 AJ |
961 | (ewoc-map (lambda (info) (unless (git-fileinfo->marked info) |
962 | (setf (git-fileinfo->marked info) t))) git-status) | |
711fc8f6 AJ |
963 | ; move back to goal column after invalidate |
964 | (when goal-column (move-to-column goal-column))) | |
965 | ||
966 | (defun git-unmark-all () | |
967 | "Unmark all files." | |
968 | (interactive) | |
969 | (unless git-status (error "Not in git-status buffer.")) | |
2f6e86a8 AJ |
970 | (ewoc-map (lambda (info) (when (git-fileinfo->marked info) |
971 | (setf (git-fileinfo->marked info) nil) | |
972 | t)) git-status) | |
711fc8f6 AJ |
973 | ; move back to goal column after invalidate |
974 | (when goal-column (move-to-column goal-column))) | |
975 | ||
976 | (defun git-toggle-all-marks () | |
977 | "Toggle all file marks." | |
978 | (interactive) | |
979 | (unless git-status (error "Not in git-status buffer.")) | |
980 | (ewoc-map (lambda (info) (setf (git-fileinfo->marked info) (not (git-fileinfo->marked info))) t) git-status) | |
981 | ; move back to goal column after invalidate | |
982 | (when goal-column (move-to-column goal-column))) | |
983 | ||
984 | (defun git-next-file (&optional n) | |
985 | "Move the selection down N files." | |
986 | (interactive "p") | |
987 | (unless git-status (error "Not in git-status buffer.")) | |
988 | (ewoc-goto-next git-status n)) | |
989 | ||
990 | (defun git-prev-file (&optional n) | |
991 | "Move the selection up N files." | |
992 | (interactive "p") | |
993 | (unless git-status (error "Not in git-status buffer.")) | |
994 | (ewoc-goto-prev git-status n)) | |
995 | ||
8a078c3f AJ |
996 | (defun git-next-unmerged-file (&optional n) |
997 | "Move the selection down N unmerged files." | |
998 | (interactive "p") | |
999 | (unless git-status (error "Not in git-status buffer.")) | |
1000 | (let* ((last (ewoc-locate git-status)) | |
1001 | (node (ewoc-next git-status last))) | |
1002 | (while (and node (> n 0)) | |
1003 | (when (eq 'unmerged (git-fileinfo->state (ewoc-data node))) | |
1004 | (setq n (1- n)) | |
1005 | (setq last node)) | |
1006 | (setq node (ewoc-next git-status node))) | |
1007 | (ewoc-goto-node git-status last))) | |
1008 | ||
1009 | (defun git-prev-unmerged-file (&optional n) | |
1010 | "Move the selection up N unmerged files." | |
1011 | (interactive "p") | |
1012 | (unless git-status (error "Not in git-status buffer.")) | |
1013 | (let* ((last (ewoc-locate git-status)) | |
1014 | (node (ewoc-prev git-status last))) | |
1015 | (while (and node (> n 0)) | |
1016 | (when (eq 'unmerged (git-fileinfo->state (ewoc-data node))) | |
1017 | (setq n (1- n)) | |
1018 | (setq last node)) | |
1019 | (setq node (ewoc-prev git-status node))) | |
1020 | (ewoc-goto-node git-status last))) | |
1021 | ||
b0a53e9e AJ |
1022 | (defun git-insert-file (file) |
1023 | "Insert file(s) into the git-status buffer." | |
1024 | (interactive "fInsert file: ") | |
1025 | (git-update-status-files (list (file-relative-name file)))) | |
1026 | ||
711fc8f6 AJ |
1027 | (defun git-add-file () |
1028 | "Add marked file(s) to the index cache." | |
1029 | (interactive) | |
568d2cde | 1030 | (let ((files (git-get-filenames (git-marked-files-state 'unknown 'ignored)))) |
3f3d564a | 1031 | ;; FIXME: add support for directories |
711fc8f6 | 1032 | (unless files |
93c22eeb | 1033 | (push (file-relative-name (read-file-name "File to add: " nil nil t)) files)) |
0520e215 | 1034 | (when (apply 'git-call-process-display-error "update-index" "--add" "--" files) |
433ee03f | 1035 | (git-update-status-files files) |
0520e215 | 1036 | (git-success-message "Added" files)))) |
711fc8f6 AJ |
1037 | |
1038 | (defun git-ignore-file () | |
1039 | "Add marked file(s) to the ignore list." | |
1040 | (interactive) | |
93c22eeb | 1041 | (let ((files (git-get-filenames (git-marked-files-state 'unknown)))) |
711fc8f6 | 1042 | (unless files |
93c22eeb AJ |
1043 | (push (file-relative-name (read-file-name "File to ignore: " nil nil t)) files)) |
1044 | (dolist (f files) (git-append-to-ignore f)) | |
433ee03f | 1045 | (git-update-status-files files) |
9f5599b9 | 1046 | (git-success-message "Ignored" files))) |
711fc8f6 AJ |
1047 | |
1048 | (defun git-remove-file () | |
1049 | "Remove the marked file(s)." | |
1050 | (interactive) | |
568d2cde | 1051 | (let ((files (git-get-filenames (git-marked-files-state 'added 'modified 'unknown 'uptodate 'ignored)))) |
711fc8f6 | 1052 | (unless files |
93c22eeb | 1053 | (push (file-relative-name (read-file-name "File to remove: " nil nil t)) files)) |
711fc8f6 AJ |
1054 | (if (yes-or-no-p |
1055 | (format "Remove %d file%s? " (length files) (if (> (length files) 1) "s" ""))) | |
1056 | (progn | |
93c22eeb | 1057 | (dolist (name files) |
3f3d564a AJ |
1058 | (ignore-errors |
1059 | (if (file-directory-p name) | |
1060 | (delete-directory name) | |
1061 | (delete-file name)))) | |
0520e215 | 1062 | (when (apply 'git-call-process-display-error "update-index" "--remove" "--" files) |
433ee03f | 1063 | (git-update-status-files files) |
0520e215 | 1064 | (git-success-message "Removed" files))) |
711fc8f6 AJ |
1065 | (message "Aborting")))) |
1066 | ||
1067 | (defun git-revert-file () | |
1068 | "Revert changes to the marked file(s)." | |
1069 | (interactive) | |
3f3d564a | 1070 | (let ((files (git-marked-files-state 'added 'deleted 'modified 'unmerged)) |
711fc8f6 AJ |
1071 | added modified) |
1072 | (when (and files | |
1073 | (yes-or-no-p | |
1074 | (format "Revert %d file%s? " (length files) (if (> (length files) 1) "s" "")))) | |
1075 | (dolist (info files) | |
1076 | (case (git-fileinfo->state info) | |
93c22eeb AJ |
1077 | ('added (push (git-fileinfo->name info) added)) |
1078 | ('deleted (push (git-fileinfo->name info) modified)) | |
1079 | ('unmerged (push (git-fileinfo->name info) modified)) | |
1080 | ('modified (push (git-fileinfo->name info) modified)))) | |
928323af | 1081 | ;; check if a buffer contains one of the files and isn't saved |
0520e215 | 1082 | (dolist (file modified) |
928323af AJ |
1083 | (let ((buffer (get-file-buffer file))) |
1084 | (when (and buffer (buffer-modified-p buffer)) | |
1085 | (error "Buffer %s is modified. Please kill or save modified buffers before reverting." (buffer-name buffer))))) | |
0520e215 AJ |
1086 | (let ((ok (and |
1087 | (or (not added) | |
1088 | (apply 'git-call-process-display-error "update-index" "--force-remove" "--" added)) | |
1089 | (or (not modified) | |
1090 | (apply 'git-call-process-display-error "checkout" "HEAD" modified))))) | |
433ee03f | 1091 | (git-update-status-files (append added modified)) |
0520e215 AJ |
1092 | (when ok |
1093 | (dolist (file modified) | |
1094 | (let ((buffer (get-file-buffer file))) | |
1095 | (when buffer (with-current-buffer buffer (revert-buffer t t t))))) | |
1096 | (git-success-message "Reverted" (git-get-filenames files))))))) | |
711fc8f6 AJ |
1097 | |
1098 | (defun git-resolve-file () | |
1099 | "Resolve conflicts in marked file(s)." | |
1100 | (interactive) | |
93c22eeb | 1101 | (let ((files (git-get-filenames (git-marked-files-state 'unmerged)))) |
711fc8f6 | 1102 | (when files |
0520e215 | 1103 | (when (apply 'git-call-process-display-error "update-index" "--" files) |
433ee03f | 1104 | (git-update-status-files files) |
0520e215 | 1105 | (git-success-message "Resolved" files))))) |
711fc8f6 AJ |
1106 | |
1107 | (defun git-remove-handled () | |
1108 | "Remove handled files from the status list." | |
1109 | (interactive) | |
1110 | (ewoc-filter git-status | |
1111 | (lambda (info) | |
98acc3fa AJ |
1112 | (case (git-fileinfo->state info) |
1113 | ('ignored git-show-ignored) | |
1114 | ('uptodate git-show-uptodate) | |
1115 | ('unknown git-show-unknown) | |
1116 | (t t)))) | |
711fc8f6 AJ |
1117 | (unless (ewoc-nth git-status 0) ; refresh header if list is empty |
1118 | (git-refresh-ewoc-hf git-status))) | |
1119 | ||
98acc3fa AJ |
1120 | (defun git-toggle-show-uptodate () |
1121 | "Toogle the option for showing up-to-date files." | |
1122 | (interactive) | |
1123 | (if (setq git-show-uptodate (not git-show-uptodate)) | |
1124 | (git-refresh-status) | |
1125 | (git-remove-handled))) | |
1126 | ||
1127 | (defun git-toggle-show-ignored () | |
1128 | "Toogle the option for showing ignored files." | |
1129 | (interactive) | |
1130 | (if (setq git-show-ignored (not git-show-ignored)) | |
1131 | (progn | |
9f5599b9 | 1132 | (message "Inserting ignored files...") |
98acc3fa AJ |
1133 | (git-run-ls-files-with-excludes git-status nil 'ignored "-o" "-i") |
1134 | (git-refresh-files) | |
9f5599b9 AJ |
1135 | (git-refresh-ewoc-hf git-status) |
1136 | (message "Inserting ignored files...done")) | |
98acc3fa AJ |
1137 | (git-remove-handled))) |
1138 | ||
1139 | (defun git-toggle-show-unknown () | |
1140 | "Toogle the option for showing unknown files." | |
1141 | (interactive) | |
1142 | (if (setq git-show-unknown (not git-show-unknown)) | |
1143 | (progn | |
9f5599b9 | 1144 | (message "Inserting unknown files...") |
98acc3fa AJ |
1145 | (git-run-ls-files-with-excludes git-status nil 'unknown "-o") |
1146 | (git-refresh-files) | |
9f5599b9 AJ |
1147 | (git-refresh-ewoc-hf git-status) |
1148 | (message "Inserting unknown files...done")) | |
98acc3fa AJ |
1149 | (git-remove-handled))) |
1150 | ||
3f3d564a AJ |
1151 | (defun git-expand-directory (info) |
1152 | "Expand the directory represented by INFO to list its files." | |
1153 | (when (eq (lsh (git-fileinfo->new-perm info) -9) ?\110) | |
1154 | (let ((dir (git-fileinfo->name info))) | |
1155 | (git-set-filenames-state git-status (list dir) nil) | |
1156 | (git-run-ls-files-with-excludes git-status (list (concat dir "/")) 'unknown "-o") | |
1157 | (git-refresh-files) | |
1158 | (git-refresh-ewoc-hf git-status) | |
1159 | t))) | |
1160 | ||
711fc8f6 AJ |
1161 | (defun git-setup-diff-buffer (buffer) |
1162 | "Setup a buffer for displaying a diff." | |
8fdc3972 AJ |
1163 | (let ((dir default-directory)) |
1164 | (with-current-buffer buffer | |
1165 | (diff-mode) | |
1166 | (goto-char (point-min)) | |
1167 | (setq default-directory dir) | |
1168 | (setq buffer-read-only t))) | |
711fc8f6 | 1169 | (display-buffer buffer) |
8b30aa50 AJ |
1170 | ; shrink window only if it displays the status buffer |
1171 | (when (eq (window-buffer) (current-buffer)) | |
1172 | (shrink-window-if-larger-than-buffer))) | |
711fc8f6 AJ |
1173 | |
1174 | (defun git-diff-file () | |
1175 | "Diff the marked file(s) against HEAD." | |
1176 | (interactive) | |
1177 | (let ((files (git-marked-files))) | |
1178 | (git-setup-diff-buffer | |
1179 | (apply #'git-run-command-buffer "*git-diff*" "diff-index" "-p" "-M" "HEAD" "--" (git-get-filenames files))))) | |
1180 | ||
2b1c0ef2 AJ |
1181 | (defun git-diff-file-merge-head (arg) |
1182 | "Diff the marked file(s) against the first merge head (or the nth one with a numeric prefix)." | |
1183 | (interactive "p") | |
1184 | (let ((files (git-marked-files)) | |
1185 | (merge-heads (git-get-merge-heads))) | |
1186 | (unless merge-heads (error "No merge in progress")) | |
1187 | (git-setup-diff-buffer | |
1188 | (apply #'git-run-command-buffer "*git-diff*" "diff-index" "-p" "-M" | |
1189 | (or (nth (1- arg) merge-heads) "HEAD") "--" (git-get-filenames files))))) | |
1190 | ||
711fc8f6 AJ |
1191 | (defun git-diff-unmerged-file (stage) |
1192 | "Diff the marked unmerged file(s) against the specified stage." | |
1193 | (let ((files (git-marked-files))) | |
1194 | (git-setup-diff-buffer | |
1195 | (apply #'git-run-command-buffer "*git-diff*" "diff-files" "-p" stage "--" (git-get-filenames files))))) | |
1196 | ||
1197 | (defun git-diff-file-base () | |
1198 | "Diff the marked unmerged file(s) against the common base file." | |
1199 | (interactive) | |
1200 | (git-diff-unmerged-file "-1")) | |
1201 | ||
1202 | (defun git-diff-file-mine () | |
1203 | "Diff the marked unmerged file(s) against my pre-merge version." | |
1204 | (interactive) | |
1205 | (git-diff-unmerged-file "-2")) | |
1206 | ||
1207 | (defun git-diff-file-other () | |
1208 | "Diff the marked unmerged file(s) against the other's pre-merge version." | |
1209 | (interactive) | |
1210 | (git-diff-unmerged-file "-3")) | |
1211 | ||
1212 | (defun git-diff-file-combined () | |
1213 | "Do a combined diff of the marked unmerged file(s)." | |
1214 | (interactive) | |
1215 | (git-diff-unmerged-file "-c")) | |
1216 | ||
1217 | (defun git-diff-file-idiff () | |
1218 | "Perform an interactive diff on the current file." | |
1219 | (interactive) | |
09afcd69 AJ |
1220 | (let ((files (git-marked-files-state 'added 'deleted 'modified))) |
1221 | (unless (eq 1 (length files)) | |
1222 | (error "Cannot perform an interactive diff on multiple files.")) | |
1223 | (let* ((filename (car (git-get-filenames files))) | |
1224 | (buff1 (find-file-noselect filename)) | |
1225 | (buff2 (git-run-command-buffer (concat filename ".~HEAD~") "cat-file" "blob" (concat "HEAD:" filename)))) | |
1226 | (ediff-buffers buff1 buff2)))) | |
711fc8f6 AJ |
1227 | |
1228 | (defun git-log-file () | |
1229 | "Display a log of changes to the marked file(s)." | |
1230 | (interactive) | |
1231 | (let* ((files (git-marked-files)) | |
1232 | (coding-system-for-read git-commits-coding-system) | |
1233 | (buffer (apply #'git-run-command-buffer "*git-log*" "rev-list" "--pretty" "HEAD" "--" (git-get-filenames files)))) | |
1234 | (with-current-buffer buffer | |
1235 | ; (git-log-mode) FIXME: implement log mode | |
1236 | (goto-char (point-min)) | |
1237 | (setq buffer-read-only t)) | |
1238 | (display-buffer buffer))) | |
1239 | ||
1240 | (defun git-log-edit-files () | |
1241 | "Return a list of marked files for use in the log-edit buffer." | |
1242 | (with-current-buffer log-edit-parent-buffer | |
1243 | (git-get-filenames (git-marked-files-state 'added 'deleted 'modified)))) | |
1244 | ||
8b30aa50 AJ |
1245 | (defun git-log-edit-diff () |
1246 | "Run a diff of the current files being committed from a log-edit buffer." | |
1247 | (with-current-buffer log-edit-parent-buffer | |
1248 | (git-diff-file))) | |
1249 | ||
38448147 AJ |
1250 | (defun git-append-sign-off (name email) |
1251 | "Append a Signed-off-by entry to the current buffer, avoiding duplicates." | |
1252 | (let ((sign-off (format "Signed-off-by: %s <%s>" name email)) | |
1253 | (case-fold-search t)) | |
1254 | (goto-char (point-min)) | |
1255 | (unless (re-search-forward (concat "^" (regexp-quote sign-off)) nil t) | |
1256 | (goto-char (point-min)) | |
1257 | (unless (re-search-forward "^Signed-off-by: " nil t) | |
1258 | (setq sign-off (concat "\n" sign-off))) | |
1259 | (goto-char (point-max)) | |
1260 | (insert sign-off "\n")))) | |
1261 | ||
ef5133df | 1262 | (defun git-setup-log-buffer (buffer &optional merge-heads author-name author-email subject date msg) |
60fa08ed | 1263 | "Setup the log buffer for a commit." |
711fc8f6 | 1264 | (unless git-status (error "Not in git-status buffer.")) |
ef5133df | 1265 | (let ((dir default-directory) |
38448147 AJ |
1266 | (committer-name (git-get-committer-name)) |
1267 | (committer-email (git-get-committer-email)) | |
45033ad9 | 1268 | (sign-off git-append-signed-off-by)) |
711fc8f6 | 1269 | (with-current-buffer buffer |
60fa08ed AJ |
1270 | (cd dir) |
1271 | (erase-buffer) | |
1272 | (insert | |
1273 | (propertize | |
1274 | (format "Author: %s <%s>\n%s%s" | |
1275 | (or author-name committer-name) | |
1276 | (or author-email committer-email) | |
1277 | (if date (format "Date: %s\n" date) "") | |
1278 | (if merge-heads | |
6fb20426 AJ |
1279 | (format "Merge: %s\n" |
1280 | (mapconcat 'identity merge-heads " ")) | |
60fa08ed AJ |
1281 | "")) |
1282 | 'face 'git-header-face) | |
1283 | (propertize git-log-msg-separator 'face 'git-separator-face) | |
1284 | "\n") | |
1285 | (when subject (insert subject "\n\n")) | |
1286 | (cond (msg (insert msg "\n")) | |
51ef1daa JS |
1287 | ((file-readable-p ".git/rebase-apply/msg") |
1288 | (insert-file-contents ".git/rebase-apply/msg")) | |
60fa08ed AJ |
1289 | ((file-readable-p ".git/MERGE_MSG") |
1290 | (insert-file-contents ".git/MERGE_MSG"))) | |
1291 | ; delete empty lines at end | |
1292 | (goto-char (point-min)) | |
1293 | (when (re-search-forward "\n+\\'" nil t) | |
1294 | (replace-match "\n" t t)) | |
76127b3a AJ |
1295 | (when sign-off (git-append-sign-off committer-name committer-email))) |
1296 | buffer)) | |
60fa08ed AJ |
1297 | |
1298 | (defun git-commit-file () | |
1299 | "Commit the marked file(s), asking for a commit message." | |
1300 | (interactive) | |
1301 | (unless git-status (error "Not in git-status buffer.")) | |
d55552f6 AJ |
1302 | (when (git-run-pre-commit-hook) |
1303 | (let ((buffer (get-buffer-create "*git-commit*")) | |
1304 | (coding-system (git-get-commits-coding-system)) | |
1305 | author-name author-email subject date) | |
1306 | (when (eq 0 (buffer-size buffer)) | |
51ef1daa | 1307 | (when (file-readable-p ".git/rebase-apply/info") |
d55552f6 | 1308 | (with-temp-buffer |
51ef1daa | 1309 | (insert-file-contents ".git/rebase-apply/info") |
d55552f6 AJ |
1310 | (goto-char (point-min)) |
1311 | (when (re-search-forward "^Author: \\(.*\\)\nEmail: \\(.*\\)$" nil t) | |
1312 | (setq author-name (match-string 1)) | |
1313 | (setq author-email (match-string 2))) | |
1314 | (goto-char (point-min)) | |
1315 | (when (re-search-forward "^Subject: \\(.*\\)$" nil t) | |
1316 | (setq subject (match-string 1))) | |
1317 | (goto-char (point-min)) | |
1318 | (when (re-search-forward "^Date: \\(.*\\)$" nil t) | |
1319 | (setq date (match-string 1))))) | |
ef5133df | 1320 | (git-setup-log-buffer buffer (git-get-merge-heads) author-name author-email subject date)) |
8b30aa50 AJ |
1321 | (if (boundp 'log-edit-diff-function) |
1322 | (log-edit 'git-do-commit nil '((log-edit-listfun . git-log-edit-files) | |
1323 | (log-edit-diff-function . git-log-edit-diff)) buffer) | |
1324 | (log-edit 'git-do-commit nil 'git-log-edit-files buffer)) | |
d55552f6 AJ |
1325 | (setq font-lock-keywords (font-lock-compile-keywords git-log-edit-font-lock-keywords)) |
1326 | (setq buffer-file-coding-system coding-system) | |
1327 | (re-search-forward (regexp-quote (concat git-log-msg-separator "\n")) nil t)))) | |
711fc8f6 | 1328 | |
76127b3a AJ |
1329 | (defun git-setup-commit-buffer (commit) |
1330 | "Setup the commit buffer with the contents of COMMIT." | |
ef5133df | 1331 | (let (parents author-name author-email subject date msg) |
76127b3a AJ |
1332 | (with-temp-buffer |
1333 | (let ((coding-system (git-get-logoutput-coding-system))) | |
ef5133df | 1334 | (git-call-process t "log" "-1" "--pretty=medium" "--abbrev=40" commit) |
76127b3a | 1335 | (goto-char (point-min)) |
ef5133df AJ |
1336 | (when (re-search-forward "^Merge: *\\(.*\\)$" nil t) |
1337 | (setq parents (cdr (split-string (match-string 1) " +")))) | |
76127b3a AJ |
1338 | (when (re-search-forward "^Author: *\\(.*\\) <\\(.*\\)>$" nil t) |
1339 | (setq author-name (match-string 1)) | |
1340 | (setq author-email (match-string 2))) | |
1341 | (when (re-search-forward "^Date: *\\(.*\\)$" nil t) | |
1342 | (setq date (match-string 1))) | |
1343 | (while (re-search-forward "^ \\(.*\\)$" nil t) | |
1344 | (push (match-string 1) msg)) | |
1345 | (setq msg (nreverse msg)) | |
1346 | (setq subject (pop msg)) | |
1347 | (while (and msg (zerop (length (car msg))) (pop msg))))) | |
1348 | (git-setup-log-buffer (get-buffer-create "*git-commit*") | |
ef5133df | 1349 | parents author-name author-email subject date |
76127b3a AJ |
1350 | (mapconcat #'identity msg "\n")))) |
1351 | ||
1352 | (defun git-get-commit-files (commit) | |
1353 | "Retrieve the list of files modified by COMMIT." | |
1354 | (let (files) | |
1355 | (with-temp-buffer | |
db18a182 | 1356 | (git-call-process t "diff-tree" "-m" "-r" "-z" "--name-only" "--no-commit-id" "--root" commit) |
76127b3a AJ |
1357 | (goto-char (point-min)) |
1358 | (while (re-search-forward "\\([^\0]*\\)\0" nil t 1) | |
1359 | (push (match-string 1) files))) | |
1360 | files)) | |
1361 | ||
1362 | (defun git-amend-commit () | |
1363 | "Undo the last commit on HEAD, and set things up to commit an | |
1364 | amended version of it." | |
1365 | (interactive) | |
1366 | (unless git-status (error "Not in git-status buffer.")) | |
1367 | (when (git-empty-db-p) (error "No commit to amend.")) | |
1368 | (let* ((commit (git-rev-parse "HEAD")) | |
1369 | (files (git-get-commit-files commit))) | |
db18a182 AJ |
1370 | (when (if (git-rev-parse "HEAD^") |
1371 | (git-call-process-display-error "reset" "--soft" "HEAD^") | |
1372 | (and (git-update-ref "ORIG_HEAD" commit) | |
1373 | (git-update-ref "HEAD" nil commit))) | |
c4e8b72f | 1374 | (git-update-status-files files t) |
0520e215 AJ |
1375 | (git-setup-commit-buffer commit) |
1376 | (git-commit-file)))) | |
76127b3a | 1377 | |
711fc8f6 AJ |
1378 | (defun git-find-file () |
1379 | "Visit the current file in its own buffer." | |
1380 | (interactive) | |
1381 | (unless git-status (error "Not in git-status buffer.")) | |
1382 | (let ((info (ewoc-data (ewoc-locate git-status)))) | |
3f3d564a AJ |
1383 | (unless (git-expand-directory info) |
1384 | (find-file (git-fileinfo->name info)) | |
1385 | (when (eq 'unmerged (git-fileinfo->state info)) | |
1386 | (smerge-mode 1))))) | |
711fc8f6 | 1387 | |
b8ee5181 AJ |
1388 | (defun git-find-file-other-window () |
1389 | "Visit the current file in its own buffer in another window." | |
1390 | (interactive) | |
1391 | (unless git-status (error "Not in git-status buffer.")) | |
1392 | (let ((info (ewoc-data (ewoc-locate git-status)))) | |
1393 | (find-file-other-window (git-fileinfo->name info)) | |
1394 | (when (eq 'unmerged (git-fileinfo->state info)) | |
1395 | (smerge-mode)))) | |
1396 | ||
711fc8f6 AJ |
1397 | (defun git-find-file-imerge () |
1398 | "Visit the current file in interactive merge mode." | |
1399 | (interactive) | |
1400 | (unless git-status (error "Not in git-status buffer.")) | |
1401 | (let ((info (ewoc-data (ewoc-locate git-status)))) | |
1402 | (find-file (git-fileinfo->name info)) | |
1403 | (smerge-ediff))) | |
1404 | ||
1405 | (defun git-view-file () | |
1406 | "View the current file in its own buffer." | |
1407 | (interactive) | |
1408 | (unless git-status (error "Not in git-status buffer.")) | |
1409 | (let ((info (ewoc-data (ewoc-locate git-status)))) | |
1410 | (view-file (git-fileinfo->name info)))) | |
1411 | ||
1412 | (defun git-refresh-status () | |
1413 | "Refresh the git status buffer." | |
1414 | (interactive) | |
433ee03f AJ |
1415 | (unless git-status (error "Not in git-status buffer.")) |
1416 | (message "Refreshing git status...") | |
1417 | (git-update-status-files) | |
1418 | (message "Refreshing git status...done")) | |
711fc8f6 AJ |
1419 | |
1420 | (defun git-status-quit () | |
1421 | "Quit git-status mode." | |
1422 | (interactive) | |
1423 | (bury-buffer)) | |
1424 | ||
1425 | ;;;; Major Mode | |
1426 | ;;;; ------------------------------------------------------------ | |
1427 | ||
1428 | (defvar git-status-mode-hook nil | |
1429 | "Run after `git-status-mode' is setup.") | |
1430 | ||
1431 | (defvar git-status-mode-map nil | |
1432 | "Keymap for git major mode.") | |
1433 | ||
1434 | (defvar git-status nil | |
1435 | "List of all files managed by the git-status mode.") | |
1436 | ||
1437 | (unless git-status-mode-map | |
1438 | (let ((map (make-keymap)) | |
76127b3a | 1439 | (commit-map (make-sparse-keymap)) |
98acc3fa AJ |
1440 | (diff-map (make-sparse-keymap)) |
1441 | (toggle-map (make-sparse-keymap))) | |
711fc8f6 | 1442 | (suppress-keymap map) |
5716e794 JN |
1443 | (define-key map "?" 'git-help) |
1444 | (define-key map "h" 'git-help) | |
711fc8f6 AJ |
1445 | (define-key map " " 'git-next-file) |
1446 | (define-key map "a" 'git-add-file) | |
1447 | (define-key map "c" 'git-commit-file) | |
76127b3a | 1448 | (define-key map "\C-c" commit-map) |
711fc8f6 AJ |
1449 | (define-key map "d" diff-map) |
1450 | (define-key map "=" 'git-diff-file) | |
1451 | (define-key map "f" 'git-find-file) | |
18e3e99e | 1452 | (define-key map "\r" 'git-find-file) |
711fc8f6 AJ |
1453 | (define-key map "g" 'git-refresh-status) |
1454 | (define-key map "i" 'git-ignore-file) | |
b0a53e9e | 1455 | (define-key map "I" 'git-insert-file) |
711fc8f6 AJ |
1456 | (define-key map "l" 'git-log-file) |
1457 | (define-key map "m" 'git-mark-file) | |
1458 | (define-key map "M" 'git-mark-all) | |
1459 | (define-key map "n" 'git-next-file) | |
8a078c3f | 1460 | (define-key map "N" 'git-next-unmerged-file) |
b8ee5181 | 1461 | (define-key map "o" 'git-find-file-other-window) |
711fc8f6 | 1462 | (define-key map "p" 'git-prev-file) |
8a078c3f | 1463 | (define-key map "P" 'git-prev-unmerged-file) |
711fc8f6 AJ |
1464 | (define-key map "q" 'git-status-quit) |
1465 | (define-key map "r" 'git-remove-file) | |
1466 | (define-key map "R" 'git-resolve-file) | |
98acc3fa | 1467 | (define-key map "t" toggle-map) |
711fc8f6 AJ |
1468 | (define-key map "T" 'git-toggle-all-marks) |
1469 | (define-key map "u" 'git-unmark-file) | |
1470 | (define-key map "U" 'git-revert-file) | |
1471 | (define-key map "v" 'git-view-file) | |
1472 | (define-key map "x" 'git-remove-handled) | |
1473 | (define-key map "\C-?" 'git-unmark-file-up) | |
1474 | (define-key map "\M-\C-?" 'git-unmark-all) | |
76127b3a AJ |
1475 | ; the commit submap |
1476 | (define-key commit-map "\C-a" 'git-amend-commit) | |
711fc8f6 AJ |
1477 | ; the diff submap |
1478 | (define-key diff-map "b" 'git-diff-file-base) | |
1479 | (define-key diff-map "c" 'git-diff-file-combined) | |
1480 | (define-key diff-map "=" 'git-diff-file) | |
1481 | (define-key diff-map "e" 'git-diff-file-idiff) | |
1482 | (define-key diff-map "E" 'git-find-file-imerge) | |
2b1c0ef2 | 1483 | (define-key diff-map "h" 'git-diff-file-merge-head) |
711fc8f6 AJ |
1484 | (define-key diff-map "m" 'git-diff-file-mine) |
1485 | (define-key diff-map "o" 'git-diff-file-other) | |
98acc3fa AJ |
1486 | ; the toggle submap |
1487 | (define-key toggle-map "u" 'git-toggle-show-uptodate) | |
1488 | (define-key toggle-map "i" 'git-toggle-show-ignored) | |
1489 | (define-key toggle-map "k" 'git-toggle-show-unknown) | |
1490 | (define-key toggle-map "m" 'git-toggle-all-marks) | |
18ff365f AJ |
1491 | (setq git-status-mode-map map)) |
1492 | (easy-menu-define git-menu git-status-mode-map | |
1493 | "Git Menu" | |
1494 | `("Git" | |
1495 | ["Refresh" git-refresh-status t] | |
1496 | ["Commit" git-commit-file t] | |
1497 | ("Merge" | |
1498 | ["Next Unmerged File" git-next-unmerged-file t] | |
1499 | ["Prev Unmerged File" git-prev-unmerged-file t] | |
1500 | ["Mark as Resolved" git-resolve-file t] | |
1501 | ["Interactive Merge File" git-find-file-imerge t] | |
1502 | ["Diff Against Common Base File" git-diff-file-base t] | |
1503 | ["Diff Combined" git-diff-file-combined t] | |
1504 | ["Diff Against Merge Head" git-diff-file-merge-head t] | |
1505 | ["Diff Against Mine" git-diff-file-mine t] | |
1506 | ["Diff Against Other" git-diff-file-other t]) | |
1507 | "--------" | |
1508 | ["Add File" git-add-file t] | |
1509 | ["Revert File" git-revert-file t] | |
1510 | ["Ignore File" git-ignore-file t] | |
1511 | ["Remove File" git-remove-file t] | |
b0a53e9e | 1512 | ["Insert File" git-insert-file t] |
18ff365f AJ |
1513 | "--------" |
1514 | ["Find File" git-find-file t] | |
1515 | ["View File" git-view-file t] | |
1516 | ["Diff File" git-diff-file t] | |
1517 | ["Interactive Diff File" git-diff-file-idiff t] | |
1518 | ["Log" git-log-file t] | |
1519 | "--------" | |
1520 | ["Mark" git-mark-file t] | |
1521 | ["Mark All" git-mark-all t] | |
1522 | ["Unmark" git-unmark-file t] | |
1523 | ["Unmark All" git-unmark-all t] | |
1524 | ["Toggle All Marks" git-toggle-all-marks t] | |
1525 | ["Hide Handled Files" git-remove-handled t] | |
1526 | "--------" | |
1527 | ["Show Uptodate Files" git-toggle-show-uptodate :style toggle :selected git-show-uptodate] | |
1528 | ["Show Ignored Files" git-toggle-show-ignored :style toggle :selected git-show-ignored] | |
1529 | ["Show Unknown Files" git-toggle-show-unknown :style toggle :selected git-show-unknown] | |
1530 | "--------" | |
1531 | ["Quit" git-status-quit t]))) | |
1532 | ||
711fc8f6 AJ |
1533 | |
1534 | ;; git mode should only run in the *git status* buffer | |
1535 | (put 'git-status-mode 'mode-class 'special) | |
1536 | ||
1537 | (defun git-status-mode () | |
1538 | "Major mode for interacting with Git. | |
1539 | Commands: | |
1540 | \\{git-status-mode-map}" | |
1541 | (kill-all-local-variables) | |
1542 | (buffer-disable-undo) | |
1543 | (setq mode-name "git status" | |
1544 | major-mode 'git-status-mode | |
1545 | goal-column 17 | |
1546 | buffer-read-only t) | |
1547 | (use-local-map git-status-mode-map) | |
1548 | (let ((buffer-read-only nil)) | |
1549 | (erase-buffer) | |
1550 | (let ((status (ewoc-create 'git-fileinfo-prettyprint "" ""))) | |
1551 | (set (make-local-variable 'git-status) status)) | |
a944652c | 1552 | (set (make-local-variable 'list-buffers-directory) default-directory) |
98acc3fa AJ |
1553 | (make-local-variable 'git-show-uptodate) |
1554 | (make-local-variable 'git-show-ignored) | |
1555 | (make-local-variable 'git-show-unknown) | |
711fc8f6 AJ |
1556 | (run-hooks 'git-status-mode-hook))) |
1557 | ||
73389f12 AJ |
1558 | (defun git-find-status-buffer (dir) |
1559 | "Find the git status buffer handling a specified directory." | |
1560 | (let ((list (buffer-list)) | |
1561 | (fulldir (expand-file-name dir)) | |
1562 | found) | |
1563 | (while (and list (not found)) | |
1564 | (let ((buffer (car list))) | |
1565 | (with-current-buffer buffer | |
1566 | (when (and list-buffers-directory | |
1567 | (string-equal fulldir (expand-file-name list-buffers-directory)) | |
a1eebfb3 | 1568 | (eq major-mode 'git-status-mode)) |
73389f12 AJ |
1569 | (setq found buffer)))) |
1570 | (setq list (cdr list))) | |
1571 | found)) | |
1572 | ||
711fc8f6 AJ |
1573 | (defun git-status (dir) |
1574 | "Entry point into git-status mode." | |
1575 | (interactive "DSelect directory: ") | |
1576 | (setq dir (git-get-top-dir dir)) | |
1577 | (if (file-directory-p (concat (file-name-as-directory dir) ".git")) | |
73389f12 AJ |
1578 | (let ((buffer (or (and git-reuse-status-buffer (git-find-status-buffer dir)) |
1579 | (create-file-buffer (expand-file-name "*git-status*" dir))))) | |
711fc8f6 | 1580 | (switch-to-buffer buffer) |
711fc8f6 | 1581 | (cd dir) |
a944652c | 1582 | (git-status-mode) |
711fc8f6 | 1583 | (git-refresh-status) |
0365d885 AJ |
1584 | (goto-char (point-min)) |
1585 | (add-hook 'after-save-hook 'git-update-saved-file)) | |
711fc8f6 AJ |
1586 | (message "%s is not a git working tree." dir))) |
1587 | ||
0365d885 AJ |
1588 | (defun git-update-saved-file () |
1589 | "Update the corresponding git-status buffer when a file is saved. | |
1590 | Meant to be used in `after-save-hook'." | |
1591 | (let* ((file (expand-file-name buffer-file-name)) | |
6df02388 | 1592 | (dir (condition-case nil (git-get-top-dir (file-name-directory file)) (error nil))) |
0365d885 AJ |
1593 | (buffer (and dir (git-find-status-buffer dir)))) |
1594 | (when buffer | |
1595 | (with-current-buffer buffer | |
1596 | (let ((filename (file-relative-name file dir))) | |
1597 | ; skip files located inside the .git directory | |
1598 | (unless (string-match "^\\.git/" filename) | |
9ddf6d7c | 1599 | (git-call-process nil "add" "--refresh" "--" filename) |
433ee03f | 1600 | (git-update-status-files (list filename)))))))) |
0365d885 | 1601 | |
5716e794 JN |
1602 | (defun git-help () |
1603 | "Display help for Git mode." | |
1604 | (interactive) | |
1605 | (describe-function 'git-status-mode)) | |
1606 | ||
711fc8f6 AJ |
1607 | (provide 'git) |
1608 | ;;; git.el ends here |