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