]> git.ipfire.org Git - thirdparty/git.git/blame - contrib/emacs/git.el
Add an Emacs interface in contrib.
[thirdparty/git.git] / contrib / emacs / git.el
CommitLineData
711fc8f6
AJ
1;;; git.el --- A user interface for git
2
3;; Copyright (C) 2005, 2006 Alexandre Julliard <julliard@winehq.org>
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
38;; - better handling of subprocess errors
39;; - hook into file save (after-save-hook)
40;; - diff against other branch
41;; - renaming files from the status buffer
42;; - support for appending signed-off-by
43;; - creating tags
44;; - fetch/pull
45;; - switching branches
46;; - revlist browser
47;; - git-show-branch browser
48;; - customize support
49;; - menus
50;;
51
52(eval-when-compile (require 'cl))
53(require 'ewoc)
54
55
56;;;; Faces
57;;;; ------------------------------------------------------------
58
59(defface git-status-face
60 '((((class color) (background light)) (:foreground "purple")))
61 "Git mode face used to highlight added and modified files.")
62
63(defface git-unmerged-face
64 '((((class color) (background light)) (:foreground "red" :bold t)))
65 "Git mode face used to highlight unmerged files.")
66
67(defface git-unknown-face
68 '((((class color) (background light)) (:foreground "goldenrod" :bold t)))
69 "Git mode face used to highlight unknown files.")
70
71(defface git-uptodate-face
72 '((((class color) (background light)) (:foreground "grey60")))
73 "Git mode face used to highlight up-to-date files.")
74
75(defface git-ignored-face
76 '((((class color) (background light)) (:foreground "grey60")))
77 "Git mode face used to highlight ignored files.")
78
79(defface git-mark-face
80 '((((class color) (background light)) (:foreground "red" :bold t)))
81 "Git mode face used for the file marks.")
82
83(defface git-header-face
84 '((((class color) (background light)) (:foreground "blue")))
85 "Git mode face used for commit headers.")
86
87(defface git-separator-face
88 '((((class color) (background light)) (:foreground "brown")))
89 "Git mode face used for commit separator.")
90
91(defface git-permission-face
92 '((((class color) (background light)) (:foreground "green" :bold t)))
93 "Git mode face used for permission changes.")
94
95(defvar git-committer-name nil
96 "*User name to use for commits.
97If not set, fall back to `add-log-full-name' and then `user-full-name'.")
98
99(defvar git-committer-email nil
100 "*Email address to use for commits.
101If not set, fall back to `add-log-mailing-address' and then `user-mail-address'.")
102
103(defvar git-commits-coding-system 'utf-8
104 "Default coding system for git commits.")
105
106(defconst git-log-msg-separator "--- log message follows this line ---")
107
108(defconst git-per-dir-ignore-file ".gitignore"
109 "Name of the per-directory ignore file.")
110
111
112;;;; Utilities
113;;;; ------------------------------------------------------------
114
115(defun git-get-env-strings (env)
116 "Build a list of NAME=VALUE strings from a list of environment strings."
117 (mapcar (lambda (entry) (concat (car entry) "=" (cdr entry))) env))
118
119(defun git-call-process-env (buffer env &rest args)
120 "Wrapper for call-process that sets environment strings."
121 (if env
122 (apply #'call-process "env" nil buffer nil
123 (append (git-get-env-strings env) (list "git") args))
124 (apply #'call-process "git" nil buffer nil args)))
125
126(defun git-run-process-region (buffer start end program args)
127 "Run a git process with a buffer region as input."
128 (let ((output-buffer (current-buffer))
129 (dir default-directory))
130 (with-current-buffer buffer
131 (cd dir)
132 (apply #'call-process-region start end program
133 nil (list output-buffer nil) nil args))))
134
135(defun git-run-command-buffer (buffer-name &rest args)
136 "Run a git command, sending the output to a buffer named BUFFER-NAME."
137 (let ((dir default-directory)
138 (buffer (get-buffer-create buffer-name)))
139 (message "Running git %s..." (car args))
140 (with-current-buffer buffer
141 (let ((default-directory dir)
142 (buffer-read-only nil))
143 (erase-buffer)
144 (apply #'git-call-process-env buffer nil args)))
145 (message "Running git %s...done" (car args))
146 buffer))
147
148(defun git-run-command (buffer env &rest args)
149 (message "Running git %s..." (car args))
150 (apply #'git-call-process-env buffer env args)
151 (message "Running git %s...done" (car args)))
152
153(defun git-run-command-region (buffer start end env &rest args)
154 "Run a git command with specified buffer region as input."
155 (message "Running git %s..." (car args))
156 (unless (eq 0 (if env
157 (git-run-process-region
158 buffer start end "env"
159 (append (git-get-env-strings env) (list "git") args))
160 (git-run-process-region
161 buffer start end "git" args)))
162 (error "Failed to run \"git %s\":\n%s" (mapconcat (lambda (x) x) args " ") (buffer-string)))
163 (message "Running git %s...done" (car args)))
164
165(defun git-get-string-sha1 (string)
166 "Read a SHA1 from the specified string."
167 (let ((pos (string-match "[0-9a-f]\\{40\\}" string)))
168 (and pos (substring string pos (match-end 0)))))
169
170(defun git-get-committer-name ()
171 "Return the name to use as GIT_COMMITTER_NAME."
172 ; copied from log-edit
173 (or git-committer-name
174 (and (boundp 'add-log-full-name) add-log-full-name)
175 (and (fboundp 'user-full-name) (user-full-name))
176 (and (boundp 'user-full-name) user-full-name)))
177
178(defun git-get-committer-email ()
179 "Return the email address to use as GIT_COMMITTER_EMAIL."
180 ; copied from log-edit
181 (or git-committer-email
182 (and (boundp 'add-log-mailing-address) add-log-mailing-address)
183 (and (fboundp 'user-mail-address) (user-mail-address))
184 (and (boundp 'user-mail-address) user-mail-address)))
185
186(defun git-escape-file-name (name)
187 "Escape a file name if necessary."
188 (if (string-match "[\n\t\"\\]" name)
189 (concat "\""
190 (mapconcat (lambda (c)
191 (case c
192 (?\n "\\n")
193 (?\t "\\t")
194 (?\\ "\\\\")
195 (?\" "\\\"")
196 (t (char-to-string c))))
197 name "")
198 "\"")
199 name))
200
201(defun git-get-top-dir (dir)
202 "Retrieve the top-level directory of a git tree."
203 (let ((cdup (with-output-to-string
204 (with-current-buffer standard-output
205 (cd dir)
206 (unless (eq 0 (call-process "git" nil t nil "rev-parse" "--show-cdup"))
207 (error "cannot find top-level git tree for %s." dir))))))
208 (expand-file-name (concat (file-name-as-directory dir)
209 (car (split-string cdup "\n"))))))
210
211;stolen from pcl-cvs
212(defun git-append-to-ignore (file)
213 "Add a file name to the ignore file in its directory."
214 (let* ((fullname (expand-file-name file))
215 (dir (file-name-directory fullname))
216 (name (file-name-nondirectory fullname)))
217 (save-window-excursion
218 (set-buffer (find-file-noselect (expand-file-name git-per-dir-ignore-file dir)))
219 (goto-char (point-max))
220 (unless (zerop (current-column)) (insert "\n"))
221 (insert name "\n")
222 (sort-lines nil (point-min) (point-max))
223 (save-buffer))))
224
225
226;;;; Wrappers for basic git commands
227;;;; ------------------------------------------------------------
228
229(defun git-rev-parse (rev)
230 "Parse a revision name and return its SHA1."
231 (git-get-string-sha1
232 (with-output-to-string
233 (with-current-buffer standard-output
234 (git-call-process-env t nil "rev-parse" rev)))))
235
236(defun git-symbolic-ref (ref)
237 "Wrapper for the git-symbolic-ref command."
238 (car
239 (split-string
240 (with-output-to-string
241 (with-current-buffer standard-output
242 (git-call-process-env t nil "symbolic-ref" ref)))
243 "\n")))
244
245(defun git-update-ref (ref val &optional oldval)
246 "Update a reference by calling git-update-ref."
247 (apply #'git-call-process-env nil nil "update-ref" ref val (if oldval (list oldval))))
248
249(defun git-read-tree (tree &optional index-file)
250 "Read a tree into the index file."
251 (apply #'git-call-process-env nil
252 (if index-file `(("GIT_INDEX_FILE" . ,index-file)) nil)
253 "read-tree" (if tree (list tree))))
254
255(defun git-write-tree (&optional index-file)
256 "Call git-write-tree and return the resulting tree SHA1 as a string."
257 (git-get-string-sha1
258 (with-output-to-string
259 (with-current-buffer standard-output
260 (git-call-process-env t
261 (if index-file `(("GIT_INDEX_FILE" . ,index-file)) nil)
262 "write-tree")))))
263
264(defun git-commit-tree (buffer tree head)
265 "Call git-commit-tree with buffer as input and return the resulting commit SHA1."
266 (let ((author-name (git-get-committer-name))
267 (author-email (git-get-committer-email))
268 author-date log-start log-end args)
269 (when head
270 (push "-p" args)
271 (push head args))
272 (with-current-buffer buffer
273 (goto-char (point-min))
274 (if
275 (setq log-start (re-search-forward (concat "^" git-log-msg-separator "\n") nil t))
276 (save-restriction
277 (narrow-to-region (point-min) log-start)
278 (goto-char (point-min))
279 (when (re-search-forward "^Author: +\\(.*?\\) *<\\(.*\\)> *$" nil t)
280 (setq author-name (match-string 1)
281 author-email (match-string 2)))
282 (goto-char (point-min))
283 (when (re-search-forward "^Date: +\\(.*\\)$" nil t)
284 (setq author-date (match-string 1)))
285 (goto-char (point-min))
286 (while (re-search-forward "^Parent: +\\([0-9a-f]+\\)" nil t)
287 (unless (string-equal head (match-string 1))
288 (push "-p" args)
289 (push (match-string 1) args))))
290 (setq log-start (point-min)))
291 (setq log-end (point-max)))
292 (git-get-string-sha1
293 (with-output-to-string
294 (with-current-buffer standard-output
295 (let ((coding-system-for-write git-commits-coding-system)
296 (env `(("GIT_AUTHOR_NAME" . ,author-name)
297 ("GIT_AUTHOR_EMAIL" . ,author-email)
298 ("GIT_COMMITTER_NAME" . ,(git-get-committer-name))
299 ("GIT_COMMITTER_EMAIL" . ,(git-get-committer-email)))))
300 (when author-date (push `("GIT_AUTHOR_DATE" . ,author-date) env))
301 (apply #'git-run-command-region
302 buffer log-start log-end env
303 "commit-tree" tree (nreverse args))))))))
304
305(defun git-empty-db-p ()
306 "Check if the git db is empty (no commit done yet)."
307 (not (eq 0 (call-process "git" nil nil nil "rev-parse" "--verify" "HEAD"))))
308
309(defun git-get-merge-heads ()
310 "Retrieve the merge heads from the MERGE_HEAD file if present."
311 (let (heads)
312 (when (file-readable-p ".git/MERGE_HEAD")
313 (with-temp-buffer
314 (insert-file-contents ".git/MERGE_HEAD" nil nil nil t)
315 (goto-char (point-min))
316 (while (re-search-forward "[0-9a-f]\\{40\\}" nil t)
317 (push (match-string 0) heads))))
318 (nreverse heads)))
319
320;;;; File info structure
321;;;; ------------------------------------------------------------
322
323; fileinfo structure stolen from pcl-cvs
324(defstruct (git-fileinfo
325 (:copier nil)
326 (:constructor git-create-fileinfo (state name &optional old-perm new-perm rename-state orig-name marked))
327 (:conc-name git-fileinfo->))
328 marked ;; t/nil
329 state ;; current state
330 name ;; file name
331 old-perm new-perm ;; permission flags
332 rename-state ;; rename or copy state
333 orig-name ;; original name for renames or copies
334 needs-refresh) ;; whether file needs to be refreshed
335
336(defvar git-status nil)
337
338(defun git-clear-status (status)
339 "Remove everything from the status list."
340 (ewoc-filter status (lambda (info) nil)))
341
342(defun git-set-files-state (files state)
343 "Set the state of a list of files."
344 (dolist (info files)
345 (unless (eq (git-fileinfo->state info) state)
346 (setf (git-fileinfo->state info) state)
347 (setf (git-fileinfo->rename-state info) nil)
348 (setf (git-fileinfo->orig-name info) nil)
349 (setf (git-fileinfo->needs-refresh info) t))))
350
351(defun git-state-code (code)
352 "Convert from a string to a added/deleted/modified state."
353 (case (string-to-char code)
354 (?M 'modified)
355 (?? 'unknown)
356 (?A 'added)
357 (?D 'deleted)
358 (?U 'unmerged)
359 (t nil)))
360
361(defun git-status-code-as-string (code)
362 "Format a git status code as string."
363 (case code
364 ('modified (propertize "Modified" 'face 'git-status-face))
365 ('unknown (propertize "Unknown " 'face 'git-unknown-face))
366 ('added (propertize "Added " 'face 'git-status-face))
367 ('deleted (propertize "Deleted " 'face 'git-status-face))
368 ('unmerged (propertize "Unmerged" 'face 'git-unmerged-face))
369 ('uptodate (propertize "Uptodate" 'face 'git-uptodate-face))
370 ('ignored (propertize "Ignored " 'face 'git-ignored-face))
371 (t "? ")))
372
373(defun git-rename-as-string (info)
374 "Return a string describing the copy or rename associated with INFO, or an empty string if none."
375 (let ((state (git-fileinfo->rename-state info)))
376 (if state
377 (propertize
378 (concat " ("
379 (if (eq state 'copy) "copied from "
380 (if (eq (git-fileinfo->state info) 'added) "renamed to "
381 "renamed from "))
382 (git-escape-file-name (git-fileinfo->orig-name info))
383 ")") 'face 'git-status-face)
384 "")))
385
386(defun git-permissions-as-string (old-perm new-perm)
387 "Format a permission change as string."
388 (propertize
389 (if (or (not old-perm)
390 (not new-perm)
391 (eq 0 (logand #O111 (logxor old-perm new-perm))))
392 " "
393 (if (eq 0 (logand #O111 old-perm)) "+x" "-x"))
394 'face 'git-permission-face))
395
396(defun git-fileinfo-prettyprint (info)
397 "Pretty-printer for the git-fileinfo structure."
398 (insert (format " %s %s %s %s%s"
399 (if (git-fileinfo->marked info) (propertize "*" 'face 'git-mark-face) " ")
400 (git-status-code-as-string (git-fileinfo->state info))
401 (git-permissions-as-string (git-fileinfo->old-perm info) (git-fileinfo->new-perm info))
402 (git-escape-file-name (git-fileinfo->name info))
403 (git-rename-as-string info))))
404
405(defun git-parse-status (status)
406 "Parse the output of git-diff-index in the current buffer."
407 (goto-char (point-min))
408 (while (re-search-forward
409 ":\\([0-7]\\{6\\}\\) \\([0-7]\\{6\\}\\) [0-9a-f]\\{40\\} [0-9a-f]\\{40\\} \\(\\([ADMU]\\)\0\\([^\0]+\\)\\|\\([CR]\\)[0-9]*\0\\([^\0]+\\)\0\\([^\0]+\\)\\)\0"
410 nil t 1)
411 (let ((old-perm (string-to-number (match-string 1) 8))
412 (new-perm (string-to-number (match-string 2) 8))
413 (state (or (match-string 4) (match-string 6)))
414 (name (or (match-string 5) (match-string 7)))
415 (new-name (match-string 8)))
416 (if new-name ; copy or rename
417 (if (eq ?C (string-to-char state))
418 (ewoc-enter-last status (git-create-fileinfo 'added new-name old-perm new-perm 'copy name))
419 (ewoc-enter-last status (git-create-fileinfo 'deleted name 0 0 'rename new-name))
420 (ewoc-enter-last status (git-create-fileinfo 'added new-name old-perm new-perm 'rename name)))
421 (ewoc-enter-last status (git-create-fileinfo (git-state-code state) name old-perm new-perm))))))
422
423(defun git-find-status-file (status file)
424 "Find a given file in the status ewoc and return its node."
425 (let ((node (ewoc-nth status 0)))
426 (while (and node (not (string= file (git-fileinfo->name (ewoc-data node)))))
427 (setq node (ewoc-next status node)))
428 node))
429
430(defun git-parse-ls-files (status default-state &optional skip-existing)
431 "Parse the output of git-ls-files in the current buffer."
432 (goto-char (point-min))
433 (let (infolist)
434 (while (re-search-forward "\\([HMRCK?]\\) \\([^\0]*\\)\0" nil t 1)
435 (let ((state (match-string 1))
436 (name (match-string 2)))
437 (unless (and skip-existing (git-find-status-file status name))
438 (push (git-create-fileinfo (or (git-state-code state) default-state) name) infolist))))
439 (dolist (info (nreverse infolist))
440 (ewoc-enter-last status info))))
441
442(defun git-parse-ls-unmerged (status)
443 "Parse the output of git-ls-files -u in the current buffer."
444 (goto-char (point-min))
445 (let (files)
446 (while (re-search-forward "[0-7]\\{6\\} [0-9a-f]\\{40\\} [123]\t\\([^\0]+\\)\0" nil t)
447 (let ((node (git-find-status-file status (match-string 1))))
448 (when node (push (ewoc-data node) files))))
449 (git-set-files-state files 'unmerged)))
450
451(defun git-add-status-file (state name)
452 "Add a new file to the status list (if not existing already) and return its node."
453 (unless git-status (error "Not in git-status buffer."))
454 (or (git-find-status-file git-status name)
455 (ewoc-enter-last git-status (git-create-fileinfo state name))))
456
457(defun git-marked-files ()
458 "Return a list of all marked files, or if none a list containing just the file at cursor position."
459 (unless git-status (error "Not in git-status buffer."))
460 (or (ewoc-collect git-status (lambda (info) (git-fileinfo->marked info)))
461 (list (ewoc-data (ewoc-locate git-status)))))
462
463(defun git-marked-files-state (&rest states)
464 "Return marked files that are in the specified states."
465 (let ((files (git-marked-files))
466 result)
467 (dolist (info files)
468 (when (memq (git-fileinfo->state info) states)
469 (push info result)))
470 result))
471
472(defun git-refresh-files ()
473 "Refresh all files that need it and clear the needs-refresh flag."
474 (unless git-status (error "Not in git-status buffer."))
475 (ewoc-map
476 (lambda (info)
477 (let ((refresh (git-fileinfo->needs-refresh info)))
478 (setf (git-fileinfo->needs-refresh info) nil)
479 refresh))
480 git-status)
481 ; move back to goal column
482 (when goal-column (move-to-column goal-column)))
483
484(defun git-refresh-ewoc-hf (status)
485 "Refresh the ewoc header and footer."
486 (let ((branch (git-symbolic-ref "HEAD"))
487 (head (if (git-empty-db-p) "Nothing committed yet"
488 (substring (git-rev-parse "HEAD") 0 10)))
489 (merge-heads (git-get-merge-heads)))
490 (ewoc-set-hf status
491 (format "Directory: %s\nBranch: %s\nHead: %s%s\n"
492 default-directory
493 (if (string-match "^refs/heads/" branch)
494 (substring branch (match-end 0))
495 branch)
496 head
497 (if merge-heads
498 (concat "\nMerging: "
499 (mapconcat (lambda (str) (substring str 0 10)) merge-heads " "))
500 ""))
501 (if (ewoc-nth status 0) "" " No changes."))))
502
503(defun git-get-filenames (files)
504 (mapcar (lambda (info) (git-fileinfo->name info)) files))
505
506(defun git-update-index (index-file files)
507 "Run git-update-index on a list of files."
508 (let ((env (and index-file `(("GIT_INDEX_FILE" . ,index-file))))
509 added deleted modified)
510 (dolist (info files)
511 (case (git-fileinfo->state info)
512 ('added (push info added))
513 ('deleted (push info deleted))
514 ('modified (push info modified))))
515 (when added
516 (apply #'git-run-command nil env "update-index" "--add" "--" (git-get-filenames added)))
517 (when deleted
518 (apply #'git-run-command nil env "update-index" "--remove" "--" (git-get-filenames deleted)))
519 (when modified
520 (apply #'git-run-command nil env "update-index" "--" (git-get-filenames modified)))))
521
522(defun git-do-commit ()
523 "Perform the actual commit using the current buffer as log message."
524 (interactive)
525 (let ((buffer (current-buffer))
526 (index-file (make-temp-file "gitidx")))
527 (with-current-buffer log-edit-parent-buffer
528 (if (git-marked-files-state 'unmerged)
529 (message "You cannot commit unmerged files, resolve them first.")
530 (unwind-protect
531 (let ((files (git-marked-files-state 'added 'deleted 'modified))
532 head head-tree)
533 (unless (git-empty-db-p)
534 (setq head (git-rev-parse "HEAD")
535 head-tree (git-rev-parse "HEAD^{tree}")))
536 (if files
537 (progn
538 (git-read-tree head-tree index-file)
539 (git-update-index nil files) ;update both the default index
540 (git-update-index index-file files) ;and the temporary one
541 (let ((tree (git-write-tree index-file)))
542 (if (or (not (string-equal tree head-tree))
543 (yes-or-no-p "The tree was not modified, do you really want to perform an empty commit? "))
544 (let ((commit (git-commit-tree buffer tree head)))
545 (git-update-ref "HEAD" commit head)
546 (condition-case nil (delete-file ".git/MERGE_HEAD") (error nil))
547 (with-current-buffer buffer (erase-buffer))
548 (git-set-files-state files 'uptodate)
549 (git-refresh-files)
550 (git-refresh-ewoc-hf git-status)
551 (message "Committed %s." commit))
552 (message "Commit aborted."))))
553 (message "No files to commit.")))
554 (delete-file index-file))))))
555
556
557;;;; Interactive functions
558;;;; ------------------------------------------------------------
559
560(defun git-mark-file ()
561 "Mark the file that the cursor is on and move to the next one."
562 (interactive)
563 (unless git-status (error "Not in git-status buffer."))
564 (let* ((pos (ewoc-locate git-status))
565 (info (ewoc-data pos)))
566 (setf (git-fileinfo->marked info) t)
567 (ewoc-invalidate git-status pos)
568 (ewoc-goto-next git-status 1)))
569
570(defun git-unmark-file ()
571 "Unmark the file that the cursor is on and move to the next one."
572 (interactive)
573 (unless git-status (error "Not in git-status buffer."))
574 (let* ((pos (ewoc-locate git-status))
575 (info (ewoc-data pos)))
576 (setf (git-fileinfo->marked info) nil)
577 (ewoc-invalidate git-status pos)
578 (ewoc-goto-next git-status 1)))
579
580(defun git-unmark-file-up ()
581 "Unmark the file that the cursor is on and move to the previous one."
582 (interactive)
583 (unless git-status (error "Not in git-status buffer."))
584 (let* ((pos (ewoc-locate git-status))
585 (info (ewoc-data pos)))
586 (setf (git-fileinfo->marked info) nil)
587 (ewoc-invalidate git-status pos)
588 (ewoc-goto-prev git-status 1)))
589
590(defun git-mark-all ()
591 "Mark all files."
592 (interactive)
593 (unless git-status (error "Not in git-status buffer."))
594 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info) t) t) git-status)
595 ; move back to goal column after invalidate
596 (when goal-column (move-to-column goal-column)))
597
598(defun git-unmark-all ()
599 "Unmark all files."
600 (interactive)
601 (unless git-status (error "Not in git-status buffer."))
602 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info) nil) t) git-status)
603 ; move back to goal column after invalidate
604 (when goal-column (move-to-column goal-column)))
605
606(defun git-toggle-all-marks ()
607 "Toggle all file marks."
608 (interactive)
609 (unless git-status (error "Not in git-status buffer."))
610 (ewoc-map (lambda (info) (setf (git-fileinfo->marked info) (not (git-fileinfo->marked info))) t) git-status)
611 ; move back to goal column after invalidate
612 (when goal-column (move-to-column goal-column)))
613
614(defun git-next-file (&optional n)
615 "Move the selection down N files."
616 (interactive "p")
617 (unless git-status (error "Not in git-status buffer."))
618 (ewoc-goto-next git-status n))
619
620(defun git-prev-file (&optional n)
621 "Move the selection up N files."
622 (interactive "p")
623 (unless git-status (error "Not in git-status buffer."))
624 (ewoc-goto-prev git-status n))
625
626(defun git-add-file ()
627 "Add marked file(s) to the index cache."
628 (interactive)
629 (let ((files (git-marked-files-state 'unknown)))
630 (unless files
631 (push (ewoc-data
632 (git-add-status-file 'added (file-relative-name
633 (read-file-name "File to add: " nil nil t))))
634 files))
635 (apply #'git-run-command nil nil "update-index" "--info-only" "--add" "--" (git-get-filenames files))
636 (git-set-files-state files 'added)
637 (git-refresh-files)))
638
639(defun git-ignore-file ()
640 "Add marked file(s) to the ignore list."
641 (interactive)
642 (let ((files (git-marked-files-state 'unknown)))
643 (unless files
644 (push (ewoc-data
645 (git-add-status-file 'unknown (file-relative-name
646 (read-file-name "File to ignore: " nil nil t))))
647 files))
648 (dolist (info files) (git-append-to-ignore (git-fileinfo->name info)))
649 (git-set-files-state files 'ignored)
650 (git-refresh-files)))
651
652(defun git-remove-file ()
653 "Remove the marked file(s)."
654 (interactive)
655 (let ((files (git-marked-files-state 'added 'modified 'unknown 'uptodate)))
656 (unless files
657 (push (ewoc-data
658 (git-add-status-file 'unknown (file-relative-name
659 (read-file-name "File to remove: " nil nil t))))
660 files))
661 (if (yes-or-no-p
662 (format "Remove %d file%s? " (length files) (if (> (length files) 1) "s" "")))
663 (progn
664 (dolist (info files)
665 (let ((name (git-fileinfo->name info)))
666 (when (file-exists-p name) (delete-file name))))
667 (apply #'git-run-command nil nil "update-index" "--info-only" "--remove" "--" (git-get-filenames files))
668 ; remove unknown files from the list, set the others to deleted
669 (ewoc-filter git-status
670 (lambda (info files)
671 (not (and (memq info files) (eq (git-fileinfo->state info) 'unknown))))
672 files)
673 (git-set-files-state files 'deleted)
674 (git-refresh-files)
675 (unless (ewoc-nth git-status 0) ; refresh header if list is empty
676 (git-refresh-ewoc-hf git-status)))
677 (message "Aborting"))))
678
679(defun git-revert-file ()
680 "Revert changes to the marked file(s)."
681 (interactive)
682 (let ((files (git-marked-files))
683 added modified)
684 (when (and files
685 (yes-or-no-p
686 (format "Revert %d file%s? " (length files) (if (> (length files) 1) "s" ""))))
687 (dolist (info files)
688 (case (git-fileinfo->state info)
689 ('added (push info added))
690 ('deleted (push info modified))
691 ('unmerged (push info modified))
692 ('modified (push info modified))))
693 (when added
694 (apply #'git-run-command nil nil "update-index" "--force-remove" "--" (git-get-filenames added))
695 (git-set-files-state added 'unknown))
696 (when modified
697 (apply #'git-run-command nil nil "checkout" "HEAD" (git-get-filenames modified))
698 (git-set-files-state modified 'uptodate))
699 (git-refresh-files))))
700
701(defun git-resolve-file ()
702 "Resolve conflicts in marked file(s)."
703 (interactive)
704 (let ((files (git-marked-files-state 'unmerged)))
705 (when files
706 (apply #'git-run-command nil nil "update-index" "--info-only" "--" (git-get-filenames files))
707 (git-set-files-state files 'modified)
708 (git-refresh-files))))
709
710(defun git-remove-handled ()
711 "Remove handled files from the status list."
712 (interactive)
713 (ewoc-filter git-status
714 (lambda (info)
715 (not (or (eq (git-fileinfo->state info) 'ignored)
716 (eq (git-fileinfo->state info) 'uptodate)))))
717 (unless (ewoc-nth git-status 0) ; refresh header if list is empty
718 (git-refresh-ewoc-hf git-status)))
719
720(defun git-setup-diff-buffer (buffer)
721 "Setup a buffer for displaying a diff."
722 (with-current-buffer buffer
723 (diff-mode)
724 (goto-char (point-min))
725 (setq buffer-read-only t))
726 (display-buffer buffer)
727 (shrink-window-if-larger-than-buffer))
728
729(defun git-diff-file ()
730 "Diff the marked file(s) against HEAD."
731 (interactive)
732 (let ((files (git-marked-files)))
733 (git-setup-diff-buffer
734 (apply #'git-run-command-buffer "*git-diff*" "diff-index" "-p" "-M" "HEAD" "--" (git-get-filenames files)))))
735
736(defun git-diff-unmerged-file (stage)
737 "Diff the marked unmerged file(s) against the specified stage."
738 (let ((files (git-marked-files)))
739 (git-setup-diff-buffer
740 (apply #'git-run-command-buffer "*git-diff*" "diff-files" "-p" stage "--" (git-get-filenames files)))))
741
742(defun git-diff-file-base ()
743 "Diff the marked unmerged file(s) against the common base file."
744 (interactive)
745 (git-diff-unmerged-file "-1"))
746
747(defun git-diff-file-mine ()
748 "Diff the marked unmerged file(s) against my pre-merge version."
749 (interactive)
750 (git-diff-unmerged-file "-2"))
751
752(defun git-diff-file-other ()
753 "Diff the marked unmerged file(s) against the other's pre-merge version."
754 (interactive)
755 (git-diff-unmerged-file "-3"))
756
757(defun git-diff-file-combined ()
758 "Do a combined diff of the marked unmerged file(s)."
759 (interactive)
760 (git-diff-unmerged-file "-c"))
761
762(defun git-diff-file-idiff ()
763 "Perform an interactive diff on the current file."
764 (interactive)
765 (error "Interactive diffs not implemented yet."))
766
767(defun git-log-file ()
768 "Display a log of changes to the marked file(s)."
769 (interactive)
770 (let* ((files (git-marked-files))
771 (coding-system-for-read git-commits-coding-system)
772 (buffer (apply #'git-run-command-buffer "*git-log*" "rev-list" "--pretty" "HEAD" "--" (git-get-filenames files))))
773 (with-current-buffer buffer
774 ; (git-log-mode) FIXME: implement log mode
775 (goto-char (point-min))
776 (setq buffer-read-only t))
777 (display-buffer buffer)))
778
779(defun git-log-edit-files ()
780 "Return a list of marked files for use in the log-edit buffer."
781 (with-current-buffer log-edit-parent-buffer
782 (git-get-filenames (git-marked-files-state 'added 'deleted 'modified))))
783
784(defun git-commit-file ()
785 "Commit the marked file(s), asking for a commit message."
786 (interactive)
787 (unless git-status (error "Not in git-status buffer."))
788 (let ((buffer (get-buffer-create "*git-commit*"))
789 (merge-heads (git-get-merge-heads))
790 (dir default-directory))
791 (with-current-buffer buffer
792 (when (eq 0 (buffer-size))
793 (cd dir)
794 (erase-buffer)
795 (insert
796 (propertize
797 (format "Author: %s <%s>\n%s"
798 (git-get-committer-name) (git-get-committer-email)
799 (if merge-heads
800 (format "Parent: %s\n%s\n"
801 (git-rev-parse "HEAD")
802 (mapconcat (lambda (str) (concat "Parent: " str)) merge-heads "\n"))
803 ""))
804 'face 'git-header-face)
805 (propertize git-log-msg-separator 'face 'git-separator-face)
806 "\n")
807 (when (and merge-heads (file-readable-p ".git/MERGE_MSG"))
808 (insert-file-contents ".git/MERGE_MSG"))))
809 (log-edit #'git-do-commit nil #'git-log-edit-files buffer)))
810
811(defun git-find-file ()
812 "Visit the current file in its own buffer."
813 (interactive)
814 (unless git-status (error "Not in git-status buffer."))
815 (let ((info (ewoc-data (ewoc-locate git-status))))
816 (find-file (git-fileinfo->name info))
817 (when (eq 'unmerged (git-fileinfo->state info))
818 (smerge-mode))))
819
820(defun git-find-file-imerge ()
821 "Visit the current file in interactive merge mode."
822 (interactive)
823 (unless git-status (error "Not in git-status buffer."))
824 (let ((info (ewoc-data (ewoc-locate git-status))))
825 (find-file (git-fileinfo->name info))
826 (smerge-ediff)))
827
828(defun git-view-file ()
829 "View the current file in its own buffer."
830 (interactive)
831 (unless git-status (error "Not in git-status buffer."))
832 (let ((info (ewoc-data (ewoc-locate git-status))))
833 (view-file (git-fileinfo->name info))))
834
835(defun git-refresh-status ()
836 "Refresh the git status buffer."
837 (interactive)
838 (let* ((status git-status)
839 (pos (ewoc-locate status))
840 (cur-name (and pos (git-fileinfo->name (ewoc-data pos)))))
841 (unless status (error "Not in git-status buffer."))
842 (git-clear-status status)
843 (git-run-command nil nil "update-index" "--info-only" "--refresh")
844 (if (git-empty-db-p)
845 ; we need some special handling for an empty db
846 (with-temp-buffer
847 (git-run-command t nil "ls-files" "-z" "-t" "-c")
848 (git-parse-ls-files status 'added))
849 (with-temp-buffer
850 (git-run-command t nil "diff-index" "-z" "-M" "HEAD")
851 (git-parse-status status)))
852 (with-temp-buffer
853 (git-run-command t nil "ls-files" "-z" "-u")
854 (git-parse-ls-unmerged status))
855 (when (file-readable-p ".git/info/exclude")
856 (with-temp-buffer
857 (git-run-command t nil "ls-files" "-z" "-t" "-o"
858 "--exclude-from=.git/info/exclude"
859 (concat "--exclude-per-directory=" git-per-dir-ignore-file))
860 (git-parse-ls-files status 'unknown)))
861 (git-refresh-files)
862 (git-refresh-ewoc-hf status)
863 ; move point to the current file name if any
864 (let ((node (and cur-name (git-find-status-file status cur-name))))
865 (when node (ewoc-goto-node status node)))))
866
867(defun git-status-quit ()
868 "Quit git-status mode."
869 (interactive)
870 (bury-buffer))
871
872;;;; Major Mode
873;;;; ------------------------------------------------------------
874
875(defvar git-status-mode-hook nil
876 "Run after `git-status-mode' is setup.")
877
878(defvar git-status-mode-map nil
879 "Keymap for git major mode.")
880
881(defvar git-status nil
882 "List of all files managed by the git-status mode.")
883
884(unless git-status-mode-map
885 (let ((map (make-keymap))
886 (diff-map (make-sparse-keymap)))
887 (suppress-keymap map)
888 (define-key map " " 'git-next-file)
889 (define-key map "a" 'git-add-file)
890 (define-key map "c" 'git-commit-file)
891 (define-key map "d" diff-map)
892 (define-key map "=" 'git-diff-file)
893 (define-key map "f" 'git-find-file)
894 (define-key map [RET] 'git-find-file)
895 (define-key map "g" 'git-refresh-status)
896 (define-key map "i" 'git-ignore-file)
897 (define-key map "l" 'git-log-file)
898 (define-key map "m" 'git-mark-file)
899 (define-key map "M" 'git-mark-all)
900 (define-key map "n" 'git-next-file)
901 (define-key map "p" 'git-prev-file)
902 (define-key map "q" 'git-status-quit)
903 (define-key map "r" 'git-remove-file)
904 (define-key map "R" 'git-resolve-file)
905 (define-key map "T" 'git-toggle-all-marks)
906 (define-key map "u" 'git-unmark-file)
907 (define-key map "U" 'git-revert-file)
908 (define-key map "v" 'git-view-file)
909 (define-key map "x" 'git-remove-handled)
910 (define-key map "\C-?" 'git-unmark-file-up)
911 (define-key map "\M-\C-?" 'git-unmark-all)
912 ; the diff submap
913 (define-key diff-map "b" 'git-diff-file-base)
914 (define-key diff-map "c" 'git-diff-file-combined)
915 (define-key diff-map "=" 'git-diff-file)
916 (define-key diff-map "e" 'git-diff-file-idiff)
917 (define-key diff-map "E" 'git-find-file-imerge)
918 (define-key diff-map "m" 'git-diff-file-mine)
919 (define-key diff-map "o" 'git-diff-file-other)
920 (setq git-status-mode-map map)))
921
922;; git mode should only run in the *git status* buffer
923(put 'git-status-mode 'mode-class 'special)
924
925(defun git-status-mode ()
926 "Major mode for interacting with Git.
927Commands:
928\\{git-status-mode-map}"
929 (kill-all-local-variables)
930 (buffer-disable-undo)
931 (setq mode-name "git status"
932 major-mode 'git-status-mode
933 goal-column 17
934 buffer-read-only t)
935 (use-local-map git-status-mode-map)
936 (let ((buffer-read-only nil))
937 (erase-buffer)
938 (let ((status (ewoc-create 'git-fileinfo-prettyprint "" "")))
939 (set (make-local-variable 'git-status) status))
940 (run-hooks 'git-status-mode-hook)))
941
942(defun git-status (dir)
943 "Entry point into git-status mode."
944 (interactive "DSelect directory: ")
945 (setq dir (git-get-top-dir dir))
946 (if (file-directory-p (concat (file-name-as-directory dir) ".git"))
947 (let ((buffer (create-file-buffer (expand-file-name "*git-status*" dir))))
948 (switch-to-buffer buffer)
949 (git-status-mode)
950 (cd dir)
951 (git-refresh-status)
952 (goto-char (point-min)))
953 (message "%s is not a git working tree." dir)))
954
955(provide 'git)
956;;; git.el ends here