| 1 | ;; stgit.el: An emacs mode for StGit |
| 2 | ;; |
| 3 | ;; Copyright (C) 2007 David Kågedal <davidk@lysator.liu.se> |
| 4 | ;; |
| 5 | ;; To install: put this file on the load-path and place the following |
| 6 | ;; in your .emacs file: |
| 7 | ;; |
| 8 | ;; (require 'stgit) |
| 9 | ;; |
| 10 | ;; To start: `M-x stgit' |
| 11 | |
| 12 | (when (< emacs-major-version 22) |
| 13 | (error "Emacs older than 22 is not supported by stgit.el")) |
| 14 | |
| 15 | (require 'git nil t) |
| 16 | (require 'cl) |
| 17 | (require 'comint) |
| 18 | (require 'ewoc) |
| 19 | (require 'easymenu) |
| 20 | (require 'format-spec) |
| 21 | |
| 22 | (defun stgit-set-default (symbol value) |
| 23 | "Set default value of SYMBOL to VALUE using `set-default' and |
| 24 | reload all StGit buffers." |
| 25 | (set-default symbol value) |
| 26 | (dolist (buf (buffer-list)) |
| 27 | (with-current-buffer buf |
| 28 | (when (eq major-mode 'stgit-mode) |
| 29 | (stgit-reload))))) |
| 30 | |
| 31 | (defgroup stgit nil |
| 32 | "A user interface for the StGit patch maintenance tool." |
| 33 | :group 'tools |
| 34 | :link '(function-link stgit) |
| 35 | :link '(url-link "http://www.procode.org/stgit/")) |
| 36 | |
| 37 | (defcustom stgit-abbreviate-copies-and-renames t |
| 38 | "If non-nil, abbreviate copies and renames as \"dir/{old -> new}/file\" |
| 39 | instead of \"dir/old/file -> dir/new/file\"." |
| 40 | :type 'boolean |
| 41 | :group 'stgit |
| 42 | :set 'stgit-set-default) |
| 43 | |
| 44 | (defcustom stgit-default-show-worktree t |
| 45 | "Set to non-nil to by default show the working tree in a new stgit buffer. |
| 46 | |
| 47 | Use \\<stgit-mode-map>\\[stgit-toggle-worktree] to toggle this \ |
| 48 | setting in an already-started StGit buffer." |
| 49 | :type 'boolean |
| 50 | :group 'stgit |
| 51 | :link '(variable-link stgit-show-worktree)) |
| 52 | |
| 53 | (defcustom stgit-default-show-unknown nil |
| 54 | "Set to non-nil to by default show unknown files a new stgit buffer. |
| 55 | |
| 56 | Use \\<stgit-mode-map>\\[stgit-toggle-unknown] to toggle this \ |
| 57 | setting in an already-started StGit buffer." |
| 58 | :type 'boolean |
| 59 | :group 'stgit |
| 60 | :link '(variable-link stgit-show-unknown)) |
| 61 | |
| 62 | (defcustom stgit-default-show-ignored nil |
| 63 | "Set to non-nil to by default show ignored files a new stgit buffer. |
| 64 | |
| 65 | Use \\<stgit-mode-map>\\[stgit-toggle-ignored] to toggle this \ |
| 66 | setting in an already-started StGit buffer." |
| 67 | :type 'boolean |
| 68 | :group 'stgit |
| 69 | :link '(variable-link stgit-show-ignored)) |
| 70 | |
| 71 | (defcustom stgit-default-show-svn t |
| 72 | "Set to non-nil to by default show subversion information in a |
| 73 | new stgit buffer. |
| 74 | |
| 75 | Use \\<stgit-mode-map>\\[stgit-toggle-svn] to toggle this \ |
| 76 | setting in an already-started StGit buffer." |
| 77 | :type 'boolean |
| 78 | :group 'stgit |
| 79 | :link '(variable-link stgit-show-worktree)) |
| 80 | |
| 81 | (defcustom stgit-find-copies-harder nil |
| 82 | "Try harder to find copied files when listing patches. |
| 83 | |
| 84 | When not nil, runs git diff-tree with the --find-copies-harder |
| 85 | flag, which reduces performance." |
| 86 | :type 'boolean |
| 87 | :group 'stgit |
| 88 | :set 'stgit-set-default) |
| 89 | |
| 90 | (defcustom stgit-show-worktree-mode 'center |
| 91 | "This variable controls where the \"Index\" and \"Work tree\" |
| 92 | will be shown on in the buffer. |
| 93 | |
| 94 | It can be set to 'top (above all patches), 'center (show between |
| 95 | applied and unapplied patches), and 'bottom (below all patches)." |
| 96 | :type '(radio (const :tag "above all patches (top)" top) |
| 97 | (const :tag "between applied and unapplied patches (center)" |
| 98 | center) |
| 99 | (const :tag "below all patches (bottom)" bottom)) |
| 100 | :group 'stgit |
| 101 | :link '(variable-link stgit-show-worktree) |
| 102 | :set 'stgit-set-default) |
| 103 | |
| 104 | (defcustom stgit-patch-line-format "%s%m%-30n %e%d" |
| 105 | "The format string used to format patch lines. |
| 106 | The format string is passed to `format-spec' and the following |
| 107 | format characters are recognized: |
| 108 | |
| 109 | %s - A '+', '-', '>' or space, depending on whether the patch is |
| 110 | applied, unapplied, top, or something else. |
| 111 | |
| 112 | %m - An asterisk if the patch is marked, and a space otherwise. |
| 113 | |
| 114 | %n - The patch name. |
| 115 | |
| 116 | %e - The string \"(empty) \" if the patch is empty. |
| 117 | |
| 118 | %d - The short patch description. |
| 119 | |
| 120 | %D - The short patch description, or the patch name. |
| 121 | |
| 122 | When `stgit-show-patch-names' is non-nil, the `stgit-noname-patch-line-format' |
| 123 | variable is used instead." |
| 124 | :type 'string |
| 125 | :group 'stgit |
| 126 | :set 'stgit-set-default) |
| 127 | |
| 128 | (defcustom stgit-noname-patch-line-format "%s%m%e%D" |
| 129 | "The alternate format string used to format patch lines. |
| 130 | It has the same semantics as `stgit-patch-line-format', and the |
| 131 | display can be toggled between the two formats using \ |
| 132 | \\<stgit-mode-map>\\[stgit-toggle-patch-names]. |
| 133 | |
| 134 | The alternate form is used when the patch name is hidden." |
| 135 | :type 'string |
| 136 | :group 'stgit |
| 137 | :set 'stgit-set-default) |
| 138 | |
| 139 | (defcustom stgit-default-show-committed nil |
| 140 | "Set to nil to inhibit showing of historical git commits by default. |
| 141 | |
| 142 | Use \\<stgit-mode-map>\\[stgit-toggle-committed] \ |
| 143 | to toggle this setting and to control how many commits are |
| 144 | shown." |
| 145 | :type 'boolean |
| 146 | :group 'stgit |
| 147 | :link '(variable-link stgit-show-committed)) |
| 148 | |
| 149 | (defcustom stgit-default-committed-count 5 |
| 150 | "The number of historical commits to show when `stgit-show-committed' |
| 151 | is enabled." |
| 152 | :type 'number |
| 153 | :link '(variable-link stgit-committed-count)) |
| 154 | |
| 155 | (defcustom stgit-default-show-patch-names t |
| 156 | "If non-nil, default to showing patch names in a new stgit buffer. |
| 157 | |
| 158 | Use \\<stgit-mode-map>\\[stgit-toggle-patch-names] \ |
| 159 | to toggle the this setting in an already-started StGit buffer." |
| 160 | :type 'boolean |
| 161 | :group 'stgit |
| 162 | :link '(variable-link stgit-show-patch-names)) |
| 163 | |
| 164 | (defcustom stgit-file-line-format " %-11s %-2m %n %c" |
| 165 | "The format string used to format file lines. |
| 166 | The format string is passed to `format-spec' and the following |
| 167 | format characters are recognized: |
| 168 | |
| 169 | %s - A string describing the status of the file. |
| 170 | |
| 171 | %m - Mode change information |
| 172 | |
| 173 | %n - The file name. |
| 174 | |
| 175 | %c - A description of file changes." |
| 176 | :type 'string |
| 177 | :group 'stgit |
| 178 | :set 'stgit-set-default) |
| 179 | |
| 180 | (defface stgit-branch-name-face |
| 181 | '((t :inherit bold)) |
| 182 | "The face used for the StGit branch name" |
| 183 | :group 'stgit) |
| 184 | |
| 185 | (defface stgit-top-patch-face |
| 186 | '((((background dark)) (:weight bold :foreground "yellow")) |
| 187 | (((background light)) (:weight bold :foreground "purple")) |
| 188 | (t (:weight bold))) |
| 189 | "The face used for the top patch names" |
| 190 | :group 'stgit) |
| 191 | |
| 192 | (defface stgit-applied-patch-face |
| 193 | '((((background dark)) (:foreground "light yellow")) |
| 194 | (((background light)) (:foreground "purple")) |
| 195 | (t ())) |
| 196 | "The face used for applied patch names" |
| 197 | :group 'stgit) |
| 198 | |
| 199 | (defface stgit-unapplied-patch-face |
| 200 | '((((background dark)) (:foreground "gray80")) |
| 201 | (((background light)) (:foreground "orchid")) |
| 202 | (t ())) |
| 203 | "The face used for unapplied patch names" |
| 204 | :group 'stgit) |
| 205 | |
| 206 | (defface stgit-committed-patch-face |
| 207 | '((((background dark)) (:foreground "gray50")) |
| 208 | (((background light)) (:foreground "gray50")) |
| 209 | (t ())) |
| 210 | "The face used for already committed patch names" |
| 211 | :group 'stgit) |
| 212 | |
| 213 | (defface stgit-description-face |
| 214 | '((((background dark)) (:foreground "tan")) |
| 215 | (((background light)) (:foreground "dark red"))) |
| 216 | "The face used for StGit descriptions" |
| 217 | :group 'stgit) |
| 218 | |
| 219 | (defface stgit-index-work-tree-title-face |
| 220 | '((((supports :slant italic)) :slant italic) |
| 221 | (t :inherit bold)) |
| 222 | "StGit mode face used for the \"Index\" and \"Work tree\" titles" |
| 223 | :group 'stgit) |
| 224 | |
| 225 | (defface stgit-unmerged-file-face |
| 226 | '((((class color) (background light)) (:foreground "red" :bold t)) |
| 227 | (((class color) (background dark)) (:foreground "red" :bold t))) |
| 228 | "StGit mode face used for unmerged file status" |
| 229 | :group 'stgit) |
| 230 | |
| 231 | (defface stgit-unknown-file-face |
| 232 | '((((class color) (background light)) (:foreground "goldenrod" :bold t)) |
| 233 | (((class color) (background dark)) (:foreground "goldenrod" :bold t))) |
| 234 | "StGit mode face used for unknown file status" |
| 235 | :group 'stgit) |
| 236 | |
| 237 | (defface stgit-ignored-file-face |
| 238 | '((((class color) (background light)) (:foreground "grey60")) |
| 239 | (((class color) (background dark)) (:foreground "grey40"))) |
| 240 | "StGit mode face used for ignored files") |
| 241 | |
| 242 | (defface stgit-file-permission-face |
| 243 | '((((class color) (background light)) (:foreground "green" :bold t)) |
| 244 | (((class color) (background dark)) (:foreground "green" :bold t))) |
| 245 | "StGit mode face used for permission changes." |
| 246 | :group 'stgit) |
| 247 | |
| 248 | (defface stgit-modified-file-face |
| 249 | '((((class color) (background light)) (:foreground "purple")) |
| 250 | (((class color) (background dark)) (:foreground "salmon"))) |
| 251 | "StGit mode face used for modified file status" |
| 252 | :group 'stgit) |
| 253 | |
| 254 | (defun stgit (dir) |
| 255 | "Manage StGit patches for the tree in DIR. |
| 256 | |
| 257 | See `stgit-mode' for commands available." |
| 258 | (interactive "DDirectory: \n") |
| 259 | (switch-to-stgit-buffer (git-get-top-dir dir)) |
| 260 | (stgit-reload)) |
| 261 | |
| 262 | (defun stgit-assert-mode () |
| 263 | "Signal an error if not in an StGit buffer." |
| 264 | (assert (derived-mode-p 'stgit-mode) nil "Not an StGit buffer")) |
| 265 | |
| 266 | (unless (fboundp 'git-get-top-dir) |
| 267 | (defun git-get-top-dir (dir) |
| 268 | "Retrieve the top-level directory of a git tree." |
| 269 | (let ((cdup (with-output-to-string |
| 270 | (with-current-buffer standard-output |
| 271 | (cd dir) |
| 272 | (unless (eq 0 (call-process "git" nil t nil |
| 273 | "rev-parse" "--show-cdup")) |
| 274 | (error "Cannot find top-level git tree for %s" dir)))))) |
| 275 | (expand-file-name (concat (file-name-as-directory dir) |
| 276 | (car (split-string cdup "\n"))))))) |
| 277 | |
| 278 | (defun stgit-refresh-git-status (&optional dir) |
| 279 | "If it exists, refresh the `git-status' buffer belonging to |
| 280 | directory DIR or `default-directory'" |
| 281 | (when (and (fboundp 'git-find-status-buffer) |
| 282 | (fboundp 'git-refresh-status)) |
| 283 | (let* ((top-dir (git-get-top-dir (or dir default-directory))) |
| 284 | (git-status-buffer (and top-dir (git-find-status-buffer top-dir)))) |
| 285 | (when git-status-buffer |
| 286 | (with-current-buffer git-status-buffer |
| 287 | (git-refresh-status)))))) |
| 288 | |
| 289 | (defun stgit-find-buffer (dir) |
| 290 | "Return the buffer displaying StGit patches for DIR, or nil if none." |
| 291 | (setq dir (file-name-as-directory dir)) |
| 292 | (let ((buffers (buffer-list))) |
| 293 | (while (and buffers |
| 294 | (not (with-current-buffer (car buffers) |
| 295 | (and (eq major-mode 'stgit-mode) |
| 296 | (string= default-directory dir))))) |
| 297 | (setq buffers (cdr buffers))) |
| 298 | (and buffers (car buffers)))) |
| 299 | |
| 300 | (defun switch-to-stgit-buffer (dir) |
| 301 | "Switch to a (possibly new) buffer displaying StGit patches for DIR." |
| 302 | (setq dir (file-name-as-directory dir)) |
| 303 | (let ((buffer (stgit-find-buffer dir))) |
| 304 | (switch-to-buffer (or buffer |
| 305 | (create-stgit-buffer dir))))) |
| 306 | |
| 307 | (defstruct (stgit-patch |
| 308 | (:conc-name stgit-patch->)) |
| 309 | status name desc empty files-ewoc) |
| 310 | |
| 311 | (defun stgit-patch-display-name (patch) |
| 312 | (let ((name (stgit-patch->name patch))) |
| 313 | (case name |
| 314 | (:index "Index") |
| 315 | (:work "Work Tree") |
| 316 | (t (symbol-name name))))) |
| 317 | |
| 318 | (defun stgit-insert-without-trailing-whitespace (text) |
| 319 | "Insert TEXT in buffer using `insert', without trailing whitespace. |
| 320 | A newline is appended." |
| 321 | (unless (string-match "\\(.*?\\) *$" text) |
| 322 | (error)) |
| 323 | (insert (match-string 1 text) ?\n)) |
| 324 | |
| 325 | (defun stgit-line-format () |
| 326 | "Return the current line format; one of |
| 327 | `stgit-patch-line-format' and `stgit-noname-patch-line-format'" |
| 328 | (if stgit-show-patch-names |
| 329 | stgit-patch-line-format |
| 330 | stgit-noname-patch-line-format)) |
| 331 | |
| 332 | (defun stgit-patch-pp (patch) |
| 333 | (let* ((status (stgit-patch->status patch)) |
| 334 | (start (point)) |
| 335 | (name (stgit-patch->name patch)) |
| 336 | (face (cdr (assq status stgit-patch-status-face-alist))) |
| 337 | (fmt (stgit-line-format)) |
| 338 | (spec (format-spec-make |
| 339 | ?s (case status |
| 340 | ('applied "+") |
| 341 | ('top ">") |
| 342 | ('unapplied "-") |
| 343 | (t " ")) |
| 344 | ?m (if (memq name stgit-marked-patches) |
| 345 | "*" " ") |
| 346 | ?n (propertize (stgit-patch-display-name patch) |
| 347 | 'face face |
| 348 | 'syntax-table (string-to-syntax "w")) |
| 349 | ?e (if (stgit-patch->empty patch) "(empty) " "") |
| 350 | ?d (propertize (or (stgit-patch->desc patch) "") |
| 351 | 'face 'stgit-description-face) |
| 352 | ?D (propertize (let ((desc (stgit-patch->desc patch))) |
| 353 | (if (zerop (length desc)) |
| 354 | (stgit-patch-display-name patch) |
| 355 | desc)) |
| 356 | 'face face))) |
| 357 | (text (format-spec fmt spec))) |
| 358 | |
| 359 | (stgit-insert-without-trailing-whitespace text) |
| 360 | (put-text-property start (point) 'entry-type 'patch) |
| 361 | (when (memq name stgit-expanded-patches) |
| 362 | (stgit-insert-patch-files patch)) |
| 363 | (put-text-property start (point) 'patch-data patch))) |
| 364 | |
| 365 | (defun create-stgit-buffer (dir) |
| 366 | "Create a buffer for showing StGit patches. |
| 367 | Argument DIR is the repository path." |
| 368 | (let ((buf (create-file-buffer (concat dir "*stgit*"))) |
| 369 | (inhibit-read-only t)) |
| 370 | (with-current-buffer buf |
| 371 | (setq default-directory dir) |
| 372 | (stgit-mode) |
| 373 | (set (make-local-variable 'stgit-ewoc) |
| 374 | (ewoc-create #'stgit-patch-pp "Branch:\n\n" "--\n" t)) |
| 375 | (setq buffer-read-only t)) |
| 376 | buf)) |
| 377 | |
| 378 | (def-edebug-spec stgit-capture-output |
| 379 | (form body)) |
| 380 | (defmacro stgit-capture-output (name &rest body) |
| 381 | "Capture StGit output and, if there was any output, show it in a window |
| 382 | at the end. |
| 383 | Returns nil if there was no output." |
| 384 | (declare (debug ([&or stringp null] body)) |
| 385 | (indent 1)) |
| 386 | `(let ((output-buf (get-buffer-create ,(or name "*StGit output*"))) |
| 387 | (stgit-dir default-directory) |
| 388 | (inhibit-read-only t)) |
| 389 | (with-current-buffer output-buf |
| 390 | (buffer-disable-undo) |
| 391 | (erase-buffer) |
| 392 | (setq default-directory stgit-dir) |
| 393 | (setq buffer-read-only t)) |
| 394 | (let ((standard-output output-buf)) |
| 395 | ,@body) |
| 396 | (with-current-buffer output-buf |
| 397 | (set-buffer-modified-p nil) |
| 398 | (setq buffer-read-only t) |
| 399 | (if (< (point-min) (point-max)) |
| 400 | (display-buffer output-buf t))))) |
| 401 | |
| 402 | (defun stgit-make-run-args (args) |
| 403 | "Return a copy of ARGS with its elements converted to strings." |
| 404 | (mapcar (lambda (x) |
| 405 | ;; don't use (format "%s" ...) to limit type errors |
| 406 | (cond ((stringp x) x) |
| 407 | ((integerp x) (number-to-string x)) |
| 408 | ((symbolp x) (symbol-name x)) |
| 409 | (t |
| 410 | (error "Bad element in stgit-make-run-args args: %S" x)))) |
| 411 | args)) |
| 412 | |
| 413 | (defvar stgit-inhibit-messages nil |
| 414 | "Set to non-nil to inhibit messages when running `stg' commands. |
| 415 | See also `stgit-message'.") |
| 416 | (defun stgit-message (format-spec &rest args) |
| 417 | "Call `message' on the arguments unless `stgit-inhibit-messages' is non-nil." |
| 418 | (unless stgit-inhibit-messages |
| 419 | (apply 'message format-spec args))) |
| 420 | |
| 421 | (defun stgit-run (&rest args) |
| 422 | (setq args (stgit-make-run-args args)) |
| 423 | (let ((msgcmd (mapconcat #'identity args " "))) |
| 424 | (stgit-message "Running stg %s..." msgcmd) |
| 425 | (prog1 |
| 426 | (apply 'call-process "stg" nil standard-output nil args) |
| 427 | (stgit-message "Running stg %s...done" msgcmd)))) |
| 428 | |
| 429 | (defun stgit-run-silent (&rest args) |
| 430 | (let ((stgit-inhibit-messages t)) |
| 431 | (apply 'stgit-run args))) |
| 432 | |
| 433 | (defun stgit-run-git (&rest args) |
| 434 | (setq args (stgit-make-run-args args)) |
| 435 | (let ((msgcmd (mapconcat #'identity args " "))) |
| 436 | (stgit-message "Running git %s..." msgcmd) |
| 437 | (prog1 |
| 438 | (apply 'call-process "git" nil standard-output nil args) |
| 439 | (stgit-message "Running git %s...done" msgcmd)))) |
| 440 | |
| 441 | (defun stgit-run-git-silent (&rest args) |
| 442 | (let ((stgit-inhibit-messages t)) |
| 443 | (apply 'stgit-run-git args))) |
| 444 | |
| 445 | (defun stgit-index-empty-p () |
| 446 | "Returns non-nil if the index contains no changes from HEAD." |
| 447 | (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD"))) |
| 448 | |
| 449 | (defun stgit-work-tree-empty-p () |
| 450 | "Returns non-nil if the work tree contains no changes from index." |
| 451 | (zerop (stgit-run-git-silent "diff-files" "--quiet"))) |
| 452 | |
| 453 | (defvar stgit-did-advise nil |
| 454 | "Set to non-nil if appropriate (non-stgit) git functions have |
| 455 | been advised to update the stgit status when necessary.") |
| 456 | |
| 457 | (defconst stgit-allowed-branch-name-re |
| 458 | ;; Disallow control characters, space, del, and "/:@^{}~" in |
| 459 | ;; "/"-separated parts; parts may not start with a period (.) |
| 460 | "^[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\ |
| 461 | \\(/[^\0- ./:@^{}~\177][^\0- /:@^{}~\177]*\\)*$" |
| 462 | "Regular expression that (new) branch names must match.") |
| 463 | |
| 464 | (defun stgit-refresh-index () |
| 465 | (when stgit-index-node |
| 466 | (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node)))) |
| 467 | |
| 468 | (defun stgit-refresh-worktree () |
| 469 | (when stgit-worktree-node |
| 470 | (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node)))) |
| 471 | |
| 472 | (defun stgit-run-series-insert-index (ewoc) |
| 473 | (setq index-node (cons ewoc (ewoc-enter-last ewoc |
| 474 | (make-stgit-patch |
| 475 | :status 'index |
| 476 | :name :index |
| 477 | :desc nil |
| 478 | :empty nil))) |
| 479 | worktree-node (cons ewoc (ewoc-enter-last ewoc |
| 480 | (make-stgit-patch |
| 481 | :status 'work |
| 482 | :name :work |
| 483 | :desc nil |
| 484 | :empty nil))))) |
| 485 | |
| 486 | (defun stgit-svn-find-rev (sha1 hash) |
| 487 | "Return the subversion revision corresponding to SHA1 as |
| 488 | reported by git svn. |
| 489 | |
| 490 | Cached data is stored in HASH, which must have been created |
| 491 | using (make-hash-table :test 'equal)." |
| 492 | (let ((result (gethash sha1 hash t))) |
| 493 | (when (eq result t) |
| 494 | (let ((svn-rev (with-output-to-string |
| 495 | (stgit-run-git-silent "svn" "find-rev" |
| 496 | "--" sha1)))) |
| 497 | (setq result (when (string-match "\\`[0-9]+" svn-rev) |
| 498 | (string-to-number (match-string 0 svn-rev)))) |
| 499 | (puthash sha1 result hash))) |
| 500 | result)) |
| 501 | |
| 502 | (defun stgit-run-series (ewoc) |
| 503 | (setq stgit-index-node nil |
| 504 | stgit-worktree-node nil) |
| 505 | (let (all-patchsyms) |
| 506 | (when stgit-show-committed |
| 507 | (let* ((show-svn stgit-show-svn) |
| 508 | (svn-hash stgit-svn-find-rev-hash) |
| 509 | (base (stgit-id "{base}")) |
| 510 | (range (format "%s~%d..%s" base stgit-committed-count base))) |
| 511 | (with-temp-buffer |
| 512 | (let* ((standard-output (current-buffer)) |
| 513 | (fmt (stgit-line-format)) |
| 514 | (commit-abbrev (when (string-match "%-\\([0-9]+\\)n" fmt) |
| 515 | (string-to-number (match-string 1 fmt)))) |
| 516 | (exit-status (stgit-run-git-silent "--no-pager" "log" |
| 517 | "--reverse" |
| 518 | "--pretty=oneline" |
| 519 | range))) |
| 520 | (goto-char (point-min)) |
| 521 | (if (not (zerop exit-status)) |
| 522 | (message "Failed to run git log") |
| 523 | (while (not (eobp)) |
| 524 | (unless (looking-at |
| 525 | "\\([0-9a-f]+\\)\\(\\.\\.\\.\\)? \\(.*\\)") |
| 526 | (error "Syntax error in output from git log")) |
| 527 | (let* ((state 'committed) |
| 528 | (name (match-string 1)) |
| 529 | (desc (match-string 3)) |
| 530 | (empty nil)) |
| 531 | |
| 532 | (when show-svn |
| 533 | (let ((svn-rev (stgit-svn-find-rev name svn-hash))) |
| 534 | (when svn-rev |
| 535 | (setq desc (format "(r%s) %s" svn-rev desc))))) |
| 536 | |
| 537 | (and commit-abbrev |
| 538 | (< commit-abbrev (length name)) |
| 539 | (setq name (substring name 0 commit-abbrev))) |
| 540 | |
| 541 | (setq name (intern name)) |
| 542 | |
| 543 | (setq all-patchsyms (cons name all-patchsyms)) |
| 544 | (ewoc-enter-last ewoc |
| 545 | (make-stgit-patch |
| 546 | :status state |
| 547 | :name name |
| 548 | :desc desc |
| 549 | :empty empty))) |
| 550 | (forward-line 1))))))) |
| 551 | (let ((inserted-index (not stgit-show-worktree)) |
| 552 | index-node |
| 553 | worktree-node) |
| 554 | (with-temp-buffer |
| 555 | (let* ((standard-output (current-buffer)) |
| 556 | (exit-status (stgit-run-silent "series" |
| 557 | "--description" "--empty"))) |
| 558 | (goto-char (point-min)) |
| 559 | (if (not (zerop exit-status)) |
| 560 | (cond ((looking-at "stg series: \\(.*\\)") |
| 561 | (setq inserted-index t) |
| 562 | (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc)) |
| 563 | (substitute-command-keys |
| 564 | "-- not initialized; run \\[stgit-init]"))) |
| 565 | ((looking-at ".*") |
| 566 | (error "Error running stg: %s" |
| 567 | (match-string 0)))) |
| 568 | (while (not (eobp)) |
| 569 | (unless (looking-at |
| 570 | "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)") |
| 571 | (error "Syntax error in output from stg series")) |
| 572 | (let* ((state-str (match-string 2)) |
| 573 | (state (cond ((string= state-str ">") 'top) |
| 574 | ((string= state-str "+") 'applied) |
| 575 | ((string= state-str "-") 'unapplied))) |
| 576 | (name (intern (match-string 4))) |
| 577 | (desc (match-string 5)) |
| 578 | (empty (string= (match-string 1) "0"))) |
| 579 | (unless inserted-index |
| 580 | (when (or (eq stgit-show-worktree-mode 'top) |
| 581 | (and (eq stgit-show-worktree-mode 'center) |
| 582 | (eq state 'unapplied))) |
| 583 | (setq inserted-index t) |
| 584 | (stgit-run-series-insert-index ewoc))) |
| 585 | (setq all-patchsyms (cons name all-patchsyms)) |
| 586 | (ewoc-enter-last ewoc |
| 587 | (make-stgit-patch |
| 588 | :status state |
| 589 | :name name |
| 590 | :desc desc |
| 591 | :empty empty))) |
| 592 | (forward-line 1))))) |
| 593 | (unless inserted-index |
| 594 | (stgit-run-series-insert-index ewoc)) |
| 595 | (setq stgit-index-node index-node |
| 596 | stgit-worktree-node worktree-node |
| 597 | stgit-marked-patches (intersection stgit-marked-patches |
| 598 | all-patchsyms))))) |
| 599 | |
| 600 | (defun stgit-current-branch () |
| 601 | "Return the name of the current branch." |
| 602 | (substring (with-output-to-string |
| 603 | (stgit-run-silent "branch")) |
| 604 | 0 -1)) |
| 605 | |
| 606 | (defun stgit-reload (&optional description) |
| 607 | "Update the contents of the StGit buffer. |
| 608 | |
| 609 | If DESCRIPTION is non-nil, it is displayed as a status message |
| 610 | during the operation." |
| 611 | (interactive) |
| 612 | (stgit-assert-mode) |
| 613 | (when description |
| 614 | (message "%s..." description)) |
| 615 | (let ((inhibit-read-only t) |
| 616 | (curline (line-number-at-pos)) |
| 617 | (curpatch (stgit-patch-name-at-point)) |
| 618 | (curfile (stgit-patched-file-at-point)) |
| 619 | (stgit-inhibit-messages description)) |
| 620 | (ewoc-filter stgit-ewoc #'(lambda (x) nil)) |
| 621 | (ewoc-set-hf stgit-ewoc |
| 622 | (concat "Branch: " |
| 623 | (propertize (stgit-current-branch) |
| 624 | 'face 'stgit-branch-name-face) |
| 625 | "\n\n") |
| 626 | (if stgit-show-worktree |
| 627 | "--" |
| 628 | (propertize |
| 629 | (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\ |
| 630 | shows the working tree\n") |
| 631 | 'face 'stgit-description-face))) |
| 632 | (stgit-run-series stgit-ewoc) |
| 633 | (unless (and curpatch |
| 634 | (stgit-goto-patch curpatch |
| 635 | (and curfile (stgit-file->file curfile)))) |
| 636 | (goto-char (point-min)) |
| 637 | (forward-line (1- curline)) |
| 638 | (move-to-column (stgit-goal-column))) |
| 639 | (stgit-refresh-git-status)) |
| 640 | (when description |
| 641 | (message "%s...done" description))) |
| 642 | |
| 643 | (defconst stgit-file-status-code-strings |
| 644 | (mapcar (lambda (arg) |
| 645 | (cons (car arg) |
| 646 | (propertize (cadr arg) 'face (car (cddr arg))))) |
| 647 | '((add "Added" stgit-modified-file-face) |
| 648 | (copy "Copied" stgit-modified-file-face) |
| 649 | (delete "Deleted" stgit-modified-file-face) |
| 650 | (modify "Modified" stgit-modified-file-face) |
| 651 | (rename "Renamed" stgit-modified-file-face) |
| 652 | (mode-change "Mode change" stgit-modified-file-face) |
| 653 | (unmerged "Unmerged" stgit-unmerged-file-face) |
| 654 | (unknown "Unknown" stgit-unknown-file-face) |
| 655 | (ignore "Ignored" stgit-ignored-file-face))) |
| 656 | "Alist of code symbols to description strings") |
| 657 | |
| 658 | (defconst stgit-patch-status-face-alist |
| 659 | '((applied . stgit-applied-patch-face) |
| 660 | (top . stgit-top-patch-face) |
| 661 | (unapplied . stgit-unapplied-patch-face) |
| 662 | (committed . stgit-committed-patch-face) |
| 663 | (index . stgit-index-work-tree-title-face) |
| 664 | (work . stgit-index-work-tree-title-face)) |
| 665 | "Alist of face to use for a given patch status") |
| 666 | |
| 667 | (defun stgit-file-status-code-as-string (file) |
| 668 | "Return stgit status code for FILE as a string" |
| 669 | (let* ((code (assq (stgit-file->status file) |
| 670 | stgit-file-status-code-strings)) |
| 671 | (score (stgit-file->cr-score file))) |
| 672 | (when code |
| 673 | (if (and score (/= score 100)) |
| 674 | (format "%s %s" (cdr code) |
| 675 | (propertize (format "%d%%" score) |
| 676 | 'face 'stgit-description-face)) |
| 677 | (cdr code))))) |
| 678 | |
| 679 | (defun stgit-file-status-code (str &optional score) |
| 680 | "Return stgit status code from git status string" |
| 681 | (let ((code (assoc str '(("A" . add) |
| 682 | ("C" . copy) |
| 683 | ("D" . delete) |
| 684 | ("I" . ignore) |
| 685 | ("M" . modify) |
| 686 | ("R" . rename) |
| 687 | ("T" . mode-change) |
| 688 | ("U" . unmerged) |
| 689 | ("X" . unknown))))) |
| 690 | (setq code (if code (cdr code) 'unknown)) |
| 691 | (when (stringp score) |
| 692 | (if (> (length score) 0) |
| 693 | (setq score (string-to-number score)) |
| 694 | (setq score nil))) |
| 695 | (if score (cons code score) code))) |
| 696 | |
| 697 | (defconst stgit-file-type-strings |
| 698 | '((#o100 . "file") |
| 699 | (#o120 . "symlink") |
| 700 | (#o160 . "subproject")) |
| 701 | "Alist of names of file types") |
| 702 | |
| 703 | (defun stgit-file-type-string (type) |
| 704 | "Return string describing file type TYPE (the high bits of file permission). |
| 705 | Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'." |
| 706 | (let ((type-str (assoc type stgit-file-type-strings))) |
| 707 | (or (and type-str (cdr type-str)) |
| 708 | (format "unknown type %o" type)))) |
| 709 | |
| 710 | (defun stgit-file-type-change-string (old-perm new-perm) |
| 711 | "Return string describing file type change from OLD-PERM to NEW-PERM. |
| 712 | Cf. `stgit-file-type-string'." |
| 713 | (let ((old-type (lsh old-perm -9)) |
| 714 | (new-type (lsh new-perm -9))) |
| 715 | (cond ((= old-type new-type) "") |
| 716 | ((zerop new-type) "") |
| 717 | ((zerop old-type) |
| 718 | (if (= new-type #o100) |
| 719 | "" |
| 720 | (format "(%s)" (stgit-file-type-string new-type)))) |
| 721 | (t (format "(%s -> %s)" |
| 722 | (stgit-file-type-string old-type) |
| 723 | (stgit-file-type-string new-type)))))) |
| 724 | |
| 725 | (defun stgit-file-mode-change-string (old-perm new-perm) |
| 726 | "Return string describing file mode change from OLD-PERM to NEW-PERM. |
| 727 | Cf. `stgit-file-type-change-string'." |
| 728 | (setq old-perm (logand old-perm #o777) |
| 729 | new-perm (logand new-perm #o777)) |
| 730 | (if (or (= old-perm new-perm) |
| 731 | (zerop old-perm) |
| 732 | (zerop new-perm)) |
| 733 | "" |
| 734 | (let* ((modified (logxor old-perm new-perm)) |
| 735 | (not-x-modified (logand (logxor old-perm new-perm) #o666))) |
| 736 | (cond ((zerop modified) "") |
| 737 | ((and (zerop not-x-modified) |
| 738 | (or (and (eq #o111 (logand old-perm #o111)) |
| 739 | (propertize "-x" 'face 'stgit-file-permission-face)) |
| 740 | (and (eq #o111 (logand new-perm #o111)) |
| 741 | (propertize "+x" 'face |
| 742 | 'stgit-file-permission-face))))) |
| 743 | (t (concat (propertize (format "%o" old-perm) |
| 744 | 'face 'stgit-file-permission-face) |
| 745 | (propertize " -> " |
| 746 | 'face 'stgit-description-face) |
| 747 | (propertize (format "%o" new-perm) |
| 748 | 'face 'stgit-file-permission-face))))))) |
| 749 | |
| 750 | (defstruct (stgit-file |
| 751 | (:conc-name stgit-file->)) |
| 752 | old-perm new-perm copy-or-rename cr-score cr-from cr-to status file) |
| 753 | |
| 754 | (defun stgit-describe-copy-or-rename (file) |
| 755 | (let ((arrow (concat " " (propertize "->" 'face 'stgit-description-face) " ")) |
| 756 | from to common-head common-tail) |
| 757 | |
| 758 | (when stgit-abbreviate-copies-and-renames |
| 759 | (setq from (split-string (stgit-file->cr-from file) "/") |
| 760 | to (split-string (stgit-file->cr-to file) "/")) |
| 761 | |
| 762 | (while (and from to (cdr from) (cdr to) |
| 763 | (string-equal (car from) (car to))) |
| 764 | (setq common-head (cons (car from) common-head) |
| 765 | from (cdr from) |
| 766 | to (cdr to))) |
| 767 | (setq common-head (nreverse common-head) |
| 768 | from (nreverse from) |
| 769 | to (nreverse to)) |
| 770 | (while (and from to (cdr from) (cdr to) |
| 771 | (string-equal (car from) (car to))) |
| 772 | (setq common-tail (cons (car from) common-tail) |
| 773 | from (cdr from) |
| 774 | to (cdr to))) |
| 775 | (setq from (nreverse from) |
| 776 | to (nreverse to))) |
| 777 | |
| 778 | (if (or common-head common-tail) |
| 779 | (concat (if common-head |
| 780 | (mapconcat #'identity common-head "/") |
| 781 | "") |
| 782 | (if common-head "/" "") |
| 783 | (propertize "{" 'face 'stgit-description-face) |
| 784 | (mapconcat #'identity from "/") |
| 785 | arrow |
| 786 | (mapconcat #'identity to "/") |
| 787 | (propertize "}" 'face 'stgit-description-face) |
| 788 | (if common-tail "/" "") |
| 789 | (if common-tail |
| 790 | (mapconcat #'identity common-tail "/") |
| 791 | "")) |
| 792 | (concat (stgit-file->cr-from file) arrow (stgit-file->cr-to file))))) |
| 793 | |
| 794 | (defun stgit-file-pp (file) |
| 795 | (let ((start (point)) |
| 796 | (spec (format-spec-make |
| 797 | ?s (stgit-file-status-code-as-string file) |
| 798 | ?m (stgit-file-mode-change-string |
| 799 | (stgit-file->old-perm file) |
| 800 | (stgit-file->new-perm file)) |
| 801 | ?n (if (stgit-file->copy-or-rename file) |
| 802 | (stgit-describe-copy-or-rename file) |
| 803 | (stgit-file->file file)) |
| 804 | ?c (propertize (stgit-file-type-change-string |
| 805 | (stgit-file->old-perm file) |
| 806 | (stgit-file->new-perm file)) |
| 807 | 'face 'stgit-description-face)))) |
| 808 | (stgit-insert-without-trailing-whitespace |
| 809 | (format-spec stgit-file-line-format spec)) |
| 810 | (add-text-properties start (point) |
| 811 | (list 'entry-type 'file |
| 812 | 'file-data file)))) |
| 813 | |
| 814 | (defun stgit-find-copies-harder-diff-arg () |
| 815 | "Return the flag to use with `git-diff' depending on the |
| 816 | `stgit-find-copies-harder' flag." |
| 817 | (if stgit-find-copies-harder "--find-copies-harder" "-C")) |
| 818 | |
| 819 | (defun stgit-insert-ls-files (args file-flag) |
| 820 | (let ((start (point))) |
| 821 | (apply 'stgit-run-git |
| 822 | (append '("ls-files" "--exclude-standard" "-z") args)) |
| 823 | (goto-char start) |
| 824 | (while (looking-at "\\([^\0]*\\)\0") |
| 825 | (let ((name-len (- (match-end 0) (match-beginning 0)))) |
| 826 | (insert ":0 0 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 " file-flag "\0") |
| 827 | (forward-char name-len))))) |
| 828 | |
| 829 | (defun stgit-process-files (callback) |
| 830 | (goto-char (point-min)) |
| 831 | (when (looking-at "[0-9A-Fa-f]\\{40\\}\0") |
| 832 | (goto-char (match-end 0))) |
| 833 | (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ") |
| 834 | (let ((old-perm (string-to-number (match-string 1) 8)) |
| 835 | (new-perm (string-to-number (match-string 2) 8))) |
| 836 | (goto-char (match-end 0)) |
| 837 | (let ((file |
| 838 | (cond ((looking-at |
| 839 | "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0") |
| 840 | (let* ((patch-status (stgit-patch->status patch)) |
| 841 | (file-subexp (if (eq patch-status 'unapplied) |
| 842 | 3 |
| 843 | 4)) |
| 844 | (file (match-string file-subexp))) |
| 845 | (make-stgit-file |
| 846 | :old-perm old-perm |
| 847 | :new-perm new-perm |
| 848 | :copy-or-rename t |
| 849 | :cr-score (string-to-number (match-string 2)) |
| 850 | :cr-from (match-string 3) |
| 851 | :cr-to (match-string 4) |
| 852 | :status (stgit-file-status-code |
| 853 | (match-string 1)) |
| 854 | :file file))) |
| 855 | ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0") |
| 856 | (make-stgit-file |
| 857 | :old-perm old-perm |
| 858 | :new-perm new-perm |
| 859 | :copy-or-rename nil |
| 860 | :cr-score nil |
| 861 | :cr-from nil |
| 862 | :cr-to nil |
| 863 | :status (stgit-file-status-code |
| 864 | (match-string 1)) |
| 865 | :file (match-string 2)))))) |
| 866 | (goto-char (match-end 0)) |
| 867 | (funcall callback file))))) |
| 868 | |
| 869 | |
| 870 | (defun stgit-insert-patch-files (patch) |
| 871 | "Expand (show modification of) the patch PATCH after the line |
| 872 | at point." |
| 873 | (let* ((patchsym (stgit-patch->name patch)) |
| 874 | (end (point-marker)) |
| 875 | (args (list "-z" (stgit-find-copies-harder-diff-arg))) |
| 876 | (ewoc (ewoc-create #'stgit-file-pp nil nil t)) |
| 877 | (show-ignored stgit-show-ignored) |
| 878 | (show-unknown stgit-show-unknown)) |
| 879 | (set-marker-insertion-type end t) |
| 880 | (setf (stgit-patch->files-ewoc patch) ewoc) |
| 881 | (with-temp-buffer |
| 882 | (let ((standard-output (current-buffer))) |
| 883 | (apply 'stgit-run-git |
| 884 | (cond ((eq patchsym :work) |
| 885 | (let (standard-output) |
| 886 | (stgit-run-git "update-index" "--refresh")) |
| 887 | `("diff-files" "-0" ,@args)) |
| 888 | ((eq patchsym :index) |
| 889 | `("diff-index" ,@args "--cached" "HEAD")) |
| 890 | (t |
| 891 | `("diff-tree" ,@args "-r" ,(stgit-id patchsym))))) |
| 892 | |
| 893 | (when (and (eq patchsym :work)) |
| 894 | (when show-ignored |
| 895 | (stgit-insert-ls-files '("--ignored" "--others") "I")) |
| 896 | (when show-unknown |
| 897 | (stgit-insert-ls-files '("--directory" "--no-empty-directory" |
| 898 | "--others") |
| 899 | "X")) |
| 900 | (sort-regexp-fields nil ":[^\0]*\0\\([^\0]*\\)\0" "\\1" |
| 901 | (point-min) (point-max))) |
| 902 | |
| 903 | (stgit-process-files (lambda (file) (ewoc-enter-last ewoc file))) |
| 904 | |
| 905 | (unless (ewoc-nth ewoc 0) |
| 906 | (ewoc-set-hf ewoc "" |
| 907 | (concat " " |
| 908 | (propertize "<no files>" |
| 909 | 'face 'stgit-description-face) |
| 910 | "\n"))))) |
| 911 | (goto-char end))) |
| 912 | |
| 913 | (defun stgit-find-file (&optional other-window) |
| 914 | (let* ((file (or (stgit-patched-file-at-point) |
| 915 | (error "No file at point"))) |
| 916 | (filename (expand-file-name (stgit-file->file file)))) |
| 917 | (unless (file-exists-p filename) |
| 918 | (error "File does not exist")) |
| 919 | (funcall (if other-window 'find-file-other-window 'find-file) |
| 920 | filename) |
| 921 | (when (eq (stgit-file->status file) 'unmerged) |
| 922 | (smerge-mode 1)))) |
| 923 | |
| 924 | (defun stgit-expand (&optional patches collapse) |
| 925 | "Show the contents of marked patches, or the patch at point. |
| 926 | |
| 927 | See also `stgit-collapse'. |
| 928 | |
| 929 | Non-interactively, operate on PATCHES, and collapse instead of |
| 930 | expand if COLLAPSE is not nil." |
| 931 | (interactive (list (stgit-patches-marked-or-at-point t))) |
| 932 | (stgit-assert-mode) |
| 933 | (let ((patches-diff (funcall (if collapse #'intersection #'set-difference) |
| 934 | patches stgit-expanded-patches))) |
| 935 | (setq stgit-expanded-patches |
| 936 | (if collapse |
| 937 | (set-difference stgit-expanded-patches patches-diff) |
| 938 | (append stgit-expanded-patches patches-diff))) |
| 939 | (ewoc-map #'(lambda (patch) |
| 940 | (memq (stgit-patch->name patch) patches-diff)) |
| 941 | stgit-ewoc)) |
| 942 | (move-to-column (stgit-goal-column))) |
| 943 | |
| 944 | (defun stgit-collapse (&optional patches) |
| 945 | "Hide the contents of marked patches, or the patch at point. |
| 946 | |
| 947 | See also `stgit-expand'." |
| 948 | (interactive (list (stgit-patches-marked-or-at-point t))) |
| 949 | (stgit-assert-mode) |
| 950 | (stgit-expand patches t)) |
| 951 | |
| 952 | (defun stgit-select-patch () |
| 953 | (let ((patchname (stgit-patch-name-at-point))) |
| 954 | (stgit-expand (list patchname) |
| 955 | (memq patchname stgit-expanded-patches)))) |
| 956 | |
| 957 | (defun stgit-expand-directory (file) |
| 958 | (let* ((patch (stgit-patch-at-point)) |
| 959 | (ewoc (stgit-patch->files-ewoc patch)) |
| 960 | (node (ewoc-locate ewoc)) |
| 961 | (filename (stgit-file->file file)) |
| 962 | (start (make-marker)) |
| 963 | (end (make-marker))) |
| 964 | |
| 965 | (save-excursion |
| 966 | (forward-line 1) |
| 967 | (set-marker start (point)) |
| 968 | (set-marker end (point)) |
| 969 | (set-marker-insertion-type end t)) |
| 970 | |
| 971 | (assert (string-match "/$" filename)) |
| 972 | ;; remove trailing "/" |
| 973 | (setf (stgit-file->file file) (substring filename 0 -1)) |
| 974 | (ewoc-invalidate ewoc node) |
| 975 | |
| 976 | (with-temp-buffer |
| 977 | (let ((standard-output (current-buffer))) |
| 978 | (stgit-insert-ls-files (list "--directory" "--others" |
| 979 | "--no-empty-directory" "--" |
| 980 | filename) |
| 981 | "X") |
| 982 | (stgit-process-files (lambda (f) |
| 983 | (setq node (ewoc-enter-after ewoc node f)))))) |
| 984 | |
| 985 | (move-to-column (stgit-goal-column)) |
| 986 | |
| 987 | (let ((inhibit-read-only t)) |
| 988 | (put-text-property start end 'patch-data patch)))) |
| 989 | |
| 990 | (defun stgit-select-file () |
| 991 | (let* ((file (or (stgit-patched-file-at-point) |
| 992 | (error "No file at point"))) |
| 993 | (filename (stgit-file->file file))) |
| 994 | (if (string-match "/$" filename) |
| 995 | (stgit-expand-directory file) |
| 996 | (stgit-find-file)))) |
| 997 | |
| 998 | (defun stgit-select () |
| 999 | "With point on a patch, toggle showing files in the patch. |
| 1000 | |
| 1001 | With point on a file, open the associated file. Opens the target |
| 1002 | file for (applied) copies and renames." |
| 1003 | (interactive) |
| 1004 | (stgit-assert-mode) |
| 1005 | (case (get-text-property (point) 'entry-type) |
| 1006 | ('patch |
| 1007 | (stgit-select-patch)) |
| 1008 | ('file |
| 1009 | (stgit-select-file)) |
| 1010 | (t |
| 1011 | (error "No patch or file on line")))) |
| 1012 | |
| 1013 | (defun stgit-find-file-other-window () |
| 1014 | "Open file at point in other window" |
| 1015 | (interactive) |
| 1016 | (stgit-assert-mode) |
| 1017 | (stgit-find-file t)) |
| 1018 | |
| 1019 | (defun stgit-find-file-merge () |
| 1020 | "Open file at point and merge it using `smerge-ediff'." |
| 1021 | (interactive) |
| 1022 | (stgit-assert-mode) |
| 1023 | (stgit-find-file t) |
| 1024 | (smerge-ediff)) |
| 1025 | |
| 1026 | (defun stgit-quit () |
| 1027 | "Hide the stgit buffer." |
| 1028 | (interactive) |
| 1029 | (stgit-assert-mode) |
| 1030 | (bury-buffer)) |
| 1031 | |
| 1032 | (defun stgit-git-status () |
| 1033 | "Show status using `git-status'." |
| 1034 | (interactive) |
| 1035 | (stgit-assert-mode) |
| 1036 | (unless (fboundp 'git-status) |
| 1037 | (error "The stgit-git-status command requires git-status")) |
| 1038 | (let ((dir default-directory)) |
| 1039 | (save-selected-window |
| 1040 | (pop-to-buffer nil) |
| 1041 | (git-status dir)))) |
| 1042 | |
| 1043 | (defun stgit-goal-column () |
| 1044 | "Return goal column for the current line" |
| 1045 | (case (get-text-property (point) 'entry-type) |
| 1046 | ('patch 2) |
| 1047 | ('file 4) |
| 1048 | (t 0))) |
| 1049 | |
| 1050 | (defun stgit-next-line (&optional arg) |
| 1051 | "Move cursor vertically down ARG lines" |
| 1052 | (interactive "p") |
| 1053 | (stgit-assert-mode) |
| 1054 | (next-line arg) |
| 1055 | (move-to-column (stgit-goal-column))) |
| 1056 | |
| 1057 | (defun stgit-previous-line (&optional arg) |
| 1058 | "Move cursor vertically up ARG lines" |
| 1059 | (interactive "p") |
| 1060 | (stgit-assert-mode) |
| 1061 | (previous-line arg) |
| 1062 | (move-to-column (stgit-goal-column))) |
| 1063 | |
| 1064 | (defun stgit-next-patch (&optional arg) |
| 1065 | "Move cursor down ARG patches." |
| 1066 | (interactive "p") |
| 1067 | (stgit-assert-mode) |
| 1068 | (ewoc-goto-next stgit-ewoc (or arg 1)) |
| 1069 | (move-to-column goal-column)) |
| 1070 | |
| 1071 | (defun stgit-previous-patch (&optional arg) |
| 1072 | "Move cursor up ARG patches." |
| 1073 | (interactive "p") |
| 1074 | (stgit-assert-mode) |
| 1075 | (ewoc-goto-prev stgit-ewoc (or arg 1)) |
| 1076 | (move-to-column goal-column)) |
| 1077 | |
| 1078 | (defvar stgit-mode-hook nil |
| 1079 | "Run after `stgit-mode' is setup.") |
| 1080 | |
| 1081 | (defvar stgit-mode-map nil |
| 1082 | "Keymap for StGit major mode.") |
| 1083 | |
| 1084 | (unless stgit-mode-map |
| 1085 | (let ((diff-map (make-sparse-keymap)) |
| 1086 | (toggle-map (make-sparse-keymap))) |
| 1087 | (mapc (lambda (arg) (define-key diff-map (car arg) (cdr arg))) |
| 1088 | '(("b" . stgit-diff-base) |
| 1089 | ("c" . stgit-diff-combined) |
| 1090 | ("m" . stgit-find-file-merge) |
| 1091 | ("o" . stgit-diff-ours) |
| 1092 | ("r" . stgit-diff-range) |
| 1093 | ("t" . stgit-diff-theirs))) |
| 1094 | (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg))) |
| 1095 | '(("n" . stgit-toggle-patch-names) |
| 1096 | ("t" . stgit-toggle-worktree) |
| 1097 | ("h" . stgit-toggle-committed) |
| 1098 | ("i" . stgit-toggle-ignored) |
| 1099 | ("u" . stgit-toggle-unknown) |
| 1100 | ("s" . stgit-toggle-svn))) |
| 1101 | (setq stgit-mode-map (make-keymap)) |
| 1102 | (suppress-keymap stgit-mode-map) |
| 1103 | (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg))) |
| 1104 | `((" " . stgit-mark-down) |
| 1105 | ("m" . stgit-mark-down) |
| 1106 | ("\d" . stgit-unmark-up) |
| 1107 | ("u" . stgit-unmark-down) |
| 1108 | ("?" . stgit-help) |
| 1109 | ("h" . stgit-help) |
| 1110 | ("\C-p" . stgit-previous-line) |
| 1111 | ("\C-n" . stgit-next-line) |
| 1112 | ([up] . stgit-previous-line) |
| 1113 | ([down] . stgit-next-line) |
| 1114 | ("p" . stgit-previous-patch) |
| 1115 | ("n" . stgit-next-patch) |
| 1116 | ("\M-{" . stgit-previous-patch) |
| 1117 | ("\M-}" . stgit-next-patch) |
| 1118 | ("s" . stgit-git-status) |
| 1119 | ("g" . stgit-reload-or-repair) |
| 1120 | ("r" . stgit-refresh) |
| 1121 | ("\C-c\C-r" . stgit-rename) |
| 1122 | ("e" . stgit-edit) |
| 1123 | ("M" . stgit-move-patches) |
| 1124 | ("S" . stgit-squash) |
| 1125 | ("N" . stgit-new) |
| 1126 | ("c" . stgit-new-and-refresh) |
| 1127 | ("\C-c\C-c" . stgit-commit) |
| 1128 | ("\C-c\C-u" . stgit-uncommit) |
| 1129 | ("U" . stgit-revert) |
| 1130 | ("R" . stgit-resolve-file) |
| 1131 | ("\r" . stgit-select) |
| 1132 | ("+" . stgit-expand) |
| 1133 | ("-" . stgit-collapse) |
| 1134 | ("o" . stgit-find-file-other-window) |
| 1135 | ("i" . stgit-toggle-index) |
| 1136 | (">" . stgit-push-next) |
| 1137 | ("<" . stgit-pop-next) |
| 1138 | ("P" . stgit-push-or-pop) |
| 1139 | ("G" . stgit-goto) |
| 1140 | ("=" . stgit-diff) |
| 1141 | ("D" . stgit-delete) |
| 1142 | ([?\C-/] . stgit-undo) |
| 1143 | ("\C-_" . stgit-undo) |
| 1144 | ([?\C-c ?\C-/] . stgit-redo) |
| 1145 | ("\C-c\C-_" . stgit-redo) |
| 1146 | ("B" . stgit-branch) |
| 1147 | ("\C-c\C-b" . stgit-rebase) |
| 1148 | ("t" . ,toggle-map) |
| 1149 | ("d" . ,diff-map) |
| 1150 | ("q" . stgit-quit) |
| 1151 | ("!" . stgit-execute)))) |
| 1152 | |
| 1153 | (let ((at-unmerged-file '(let ((file (stgit-patched-file-at-point))) |
| 1154 | (and file (eq (stgit-file->status file) |
| 1155 | 'unmerged)))) |
| 1156 | (patch-collapsed-p '(lambda (p) (not (memq p stgit-expanded-patches))))) |
| 1157 | (easy-menu-define stgit-menu stgit-mode-map |
| 1158 | "StGit Menu" |
| 1159 | `("StGit" |
| 1160 | ["Reload" stgit-reload-or-repair |
| 1161 | :help "Reload StGit status from disk"] |
| 1162 | ["Repair" stgit-repair |
| 1163 | :keys "\\[universal-argument] \\[stgit-reload-or-repair]" |
| 1164 | :help "Repair StGit metadata"] |
| 1165 | "-" |
| 1166 | ["Undo" stgit-undo t] |
| 1167 | ["Redo" stgit-redo t] |
| 1168 | "-" |
| 1169 | ["Git status" stgit-git-status :active (fboundp 'git-status)] |
| 1170 | "-" |
| 1171 | ["New patch" stgit-new-and-refresh |
| 1172 | :help "Create a new patch from changes in index or work tree" |
| 1173 | :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p)))] |
| 1174 | ["New empty patch" stgit-new |
| 1175 | :help "Create a new, empty patch"] |
| 1176 | ["(Un)mark patch" stgit-toggle-mark |
| 1177 | :label (if (memq (stgit-patch-name-at-point nil t) |
| 1178 | stgit-marked-patches) |
| 1179 | "Unmark patch" "Mark patch") |
| 1180 | :active (stgit-patch-name-at-point nil t)] |
| 1181 | ["Expand/collapse patch" |
| 1182 | (let ((patches (stgit-patches-marked-or-at-point))) |
| 1183 | (if (member-if ,patch-collapsed-p patches) |
| 1184 | (stgit-expand patches) |
| 1185 | (stgit-collapse patches))) |
| 1186 | :label (if (member-if ,patch-collapsed-p |
| 1187 | (stgit-patches-marked-or-at-point)) |
| 1188 | "Expand patches" |
| 1189 | "Collapse patches") |
| 1190 | :active (stgit-patches-marked-or-at-point)] |
| 1191 | ["Edit patch" stgit-edit |
| 1192 | :help "Edit patch comment" |
| 1193 | :active (stgit-patch-name-at-point nil t)] |
| 1194 | ["Rename patch" stgit-rename :active (stgit-patch-name-at-point nil t)] |
| 1195 | ["Push/pop patch" stgit-push-or-pop |
| 1196 | :label (if (subsetp (stgit-patches-marked-or-at-point nil t) |
| 1197 | (stgit-applied-patchsyms t)) |
| 1198 | "Pop patches" "Push patches")] |
| 1199 | ["Delete patches" stgit-delete |
| 1200 | :active (stgit-patches-marked-or-at-point nil t)] |
| 1201 | "-" |
| 1202 | ["Move patches" stgit-move-patches |
| 1203 | :active stgit-marked-patches |
| 1204 | :help "Move marked patch(es) to point"] |
| 1205 | ["Squash patches" stgit-squash |
| 1206 | :active (> (length stgit-marked-patches) 1) |
| 1207 | :help "Merge marked patches into one"] |
| 1208 | "-" |
| 1209 | ["Refresh top patch" stgit-refresh |
| 1210 | :active (not (and (stgit-index-empty-p) (stgit-work-tree-empty-p))) |
| 1211 | :help "Refresh the top patch with changes in index or work tree"] |
| 1212 | ["Refresh this patch" (stgit-refresh t) |
| 1213 | :keys "\\[universal-argument] \\[stgit-refresh]" |
| 1214 | :help "Refresh marked patch with changes in index or work tree" |
| 1215 | :active (and (not (and (stgit-index-empty-p) |
| 1216 | (stgit-work-tree-empty-p))) |
| 1217 | (stgit-patch-name-at-point nil t))] |
| 1218 | "-" |
| 1219 | ["Find file" stgit-select |
| 1220 | :active (eq (get-text-property (point) 'entry-type) 'file)] |
| 1221 | ["Open file" stgit-find-file-other-window |
| 1222 | :active (eq (get-text-property (point) 'entry-type) 'file)] |
| 1223 | ["Toggle file index" stgit-toggle-index |
| 1224 | :active (and (eq (get-text-property (point) 'entry-type) 'file) |
| 1225 | (memq (stgit-patch-name-at-point) '(:work :index))) |
| 1226 | :label (if (eq (stgit-patch-name-at-point) :work) |
| 1227 | "Move change to index" |
| 1228 | "Move change to work tree")] |
| 1229 | "-" |
| 1230 | ["Show diff" stgit-diff |
| 1231 | :active (get-text-property (point) 'entry-type)] |
| 1232 | ["Show diff for range of applied patches" stgit-diff-range |
| 1233 | :active (= (length stgit-marked-patches) 1)] |
| 1234 | ("Merge" |
| 1235 | :active (stgit-git-index-unmerged-p) |
| 1236 | ["Combined diff" stgit-diff-combined |
| 1237 | :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))] |
| 1238 | ["Diff against base" stgit-diff-base |
| 1239 | :help "Show diff against the common base" |
| 1240 | :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))] |
| 1241 | ["Diff against ours" stgit-diff-ours |
| 1242 | :help "Show diff against our branch" |
| 1243 | :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))] |
| 1244 | ["Diff against theirs" stgit-diff-theirs |
| 1245 | :help "Show diff against their branch" |
| 1246 | :active (memq (stgit-patch-name-at-point nil nil) '(:work :index))] |
| 1247 | "-" |
| 1248 | ["Interactive merge" stgit-find-file-merge |
| 1249 | :help "Interactively merge the file" |
| 1250 | :active ,at-unmerged-file] |
| 1251 | ["Resolve file" stgit-resolve-file |
| 1252 | :help "Mark file conflict as resolved" |
| 1253 | :active ,at-unmerged-file] |
| 1254 | ) |
| 1255 | "-" |
| 1256 | ["Show index & work tree" stgit-toggle-worktree :style toggle |
| 1257 | :selected stgit-show-worktree] |
| 1258 | ["Show unknown files" stgit-toggle-unknown :style toggle |
| 1259 | :selected stgit-show-unknown :active stgit-show-worktree] |
| 1260 | ["Show ignored files" stgit-toggle-ignored :style toggle |
| 1261 | :selected stgit-show-ignored :active stgit-show-worktree] |
| 1262 | ["Show patch names" stgit-toggle-patch-names :style toggle |
| 1263 | :selected stgit-show-patch-names] |
| 1264 | ["Show recent commits" stgit-toggle-committed :style toggle |
| 1265 | :selected stgit-show-committed] |
| 1266 | ["Show subversion info" stgit-toggle-svn :style toggle |
| 1267 | :selected stgit-show-svn] |
| 1268 | "-" |
| 1269 | ["Switch branches" stgit-branch t |
| 1270 | :help "Switch to or create another branch"] |
| 1271 | ["Rebase branch" stgit-rebase t |
| 1272 | :help "Rebase the current branch"] |
| 1273 | )))) |
| 1274 | |
| 1275 | ;; disable tool bar editing buttons |
| 1276 | (put 'stgit-mode 'mode-class 'special) |
| 1277 | |
| 1278 | (defun stgit-mode () |
| 1279 | "Major mode for interacting with StGit. |
| 1280 | |
| 1281 | Start StGit using \\[stgit]. |
| 1282 | |
| 1283 | Basic commands: |
| 1284 | \\<stgit-mode-map>\ |
| 1285 | \\[stgit-help] Show this help text |
| 1286 | \\[stgit-quit] Hide the StGit buffer |
| 1287 | \\[describe-bindings] Show all key bindings |
| 1288 | |
| 1289 | \\[stgit-reload-or-repair] Reload the StGit buffer |
| 1290 | \\[universal-argument] \\[stgit-reload-or-repair] Repair StGit metadata |
| 1291 | |
| 1292 | \\[stgit-undo] Undo most recent StGit operation |
| 1293 | \\[stgit-redo] Undo recent undo |
| 1294 | |
| 1295 | \\[stgit-git-status] Run `git-status' (if available) |
| 1296 | |
| 1297 | \\[stgit-execute] Run an stg shell command |
| 1298 | |
| 1299 | Movement commands: |
| 1300 | \\[stgit-previous-line] Move to previous line |
| 1301 | \\[stgit-next-line] Move to next line |
| 1302 | \\[stgit-previous-patch] Move to previous patch |
| 1303 | \\[stgit-next-patch] Move to next patch |
| 1304 | |
| 1305 | \\[stgit-mark-down] Mark patch and move down |
| 1306 | \\[stgit-unmark-up] Unmark patch and move up |
| 1307 | \\[stgit-unmark-down] Unmark patch and move down |
| 1308 | |
| 1309 | Commands for patches: |
| 1310 | \\[stgit-select] Toggle showing changed files in patch |
| 1311 | \\[stgit-refresh] Refresh patch with changes in index or work tree |
| 1312 | \\[stgit-diff] Show the patch log and diff |
| 1313 | |
| 1314 | \\[stgit-expand] Show changes in marked patches |
| 1315 | \\[stgit-collapse] Hide changes in marked patches |
| 1316 | |
| 1317 | \\[stgit-new-and-refresh] Create a new patch from index or work tree |
| 1318 | \\[stgit-new] Create a new, empty patch |
| 1319 | |
| 1320 | \\[stgit-rename] Rename patch |
| 1321 | \\[stgit-edit] Edit patch description |
| 1322 | \\[stgit-delete] Delete patch(es) |
| 1323 | |
| 1324 | \\[stgit-revert] Revert all changes in index or work tree |
| 1325 | \\[stgit-toggle-index] Toggle all changes between index and work tree |
| 1326 | |
| 1327 | \\[stgit-push-next] Push next patch onto stack |
| 1328 | \\[stgit-pop-next] Pop current patch from stack |
| 1329 | \\[stgit-push-or-pop] Push or pop marked patches |
| 1330 | \\[stgit-goto] Make patch at point current by popping or pushing |
| 1331 | |
| 1332 | \\[stgit-squash] Squash (meld together) patches |
| 1333 | \\[stgit-move-patches] Move marked patches to point |
| 1334 | |
| 1335 | \\[stgit-commit] Commit patch(es) |
| 1336 | \\[stgit-uncommit] Uncommit patch(es) |
| 1337 | |
| 1338 | Commands for files: |
| 1339 | \\[stgit-select] Open the file in this window |
| 1340 | \\[stgit-find-file-other-window] Open the file in another window |
| 1341 | \\[stgit-diff] Show the file's diff |
| 1342 | |
| 1343 | \\[stgit-toggle-index] Toggle change between index and work tree |
| 1344 | \\[stgit-revert] Revert changes to file |
| 1345 | |
| 1346 | Display commands: |
| 1347 | \\[stgit-toggle-patch-names] Toggle showing patch names |
| 1348 | \\[stgit-toggle-worktree] Toggle showing index and work tree |
| 1349 | \\[stgit-toggle-unknown] Toggle showing unknown files |
| 1350 | \\[stgit-toggle-ignored] Toggle showing ignored files |
| 1351 | \\[stgit-toggle-committed] Toggle showing recent commits |
| 1352 | \\[stgit-toggle-svn] Toggle showing subversion information |
| 1353 | |
| 1354 | Commands for diffs: |
| 1355 | \\[stgit-diff] Show diff of patch or file |
| 1356 | \\[stgit-diff-range] Show diff for range of patches |
| 1357 | \\[stgit-diff-base] Show diff against the merge base |
| 1358 | \\[stgit-diff-ours] Show diff against our branch |
| 1359 | \\[stgit-diff-theirs] Show diff against their branch |
| 1360 | |
| 1361 | With one prefix argument (e.g., \\[universal-argument] \\[stgit-diff]), \ |
| 1362 | ignore space changes. |
| 1363 | With two prefix arguments (e.g., \\[universal-argument] \ |
| 1364 | \\[universal-argument] \\[stgit-diff]), ignore all space changes. |
| 1365 | |
| 1366 | Commands for merge conflicts: |
| 1367 | \\[stgit-find-file-merge] Resolve conflicts using `smerge-ediff' |
| 1368 | \\[stgit-resolve-file] Mark unmerged file as resolved |
| 1369 | |
| 1370 | Commands for branches: |
| 1371 | \\[stgit-branch] Switch to or create another branch |
| 1372 | \\[stgit-rebase] Rebase the current branch |
| 1373 | |
| 1374 | Customization variables: |
| 1375 | `stgit-abbreviate-copies-and-renames' |
| 1376 | `stgit-default-show-ignored' |
| 1377 | `stgit-default-show-patch-names' |
| 1378 | `stgit-default-show-unknown' |
| 1379 | `stgit-default-show-worktree' |
| 1380 | `stgit-default-show-committed' |
| 1381 | `stgit-default-show-svn' |
| 1382 | `stgit-default-committed-count' |
| 1383 | `stgit-find-copies-harder' |
| 1384 | `stgit-show-worktree-mode' |
| 1385 | |
| 1386 | See also \\[customize-group] for the \"stgit\" group." |
| 1387 | (kill-all-local-variables) |
| 1388 | (buffer-disable-undo) |
| 1389 | (setq mode-name "StGit" |
| 1390 | major-mode 'stgit-mode |
| 1391 | goal-column 2) |
| 1392 | (use-local-map stgit-mode-map) |
| 1393 | (mapc (lambda (x) (set (make-local-variable (car x)) (cdr x))) |
| 1394 | `((list-buffers-directory . ,default-directory) |
| 1395 | (parse-sexp-lookup-properties . t) |
| 1396 | (stgit-expanded-patches . (:work :index)) |
| 1397 | (stgit-index-node . nil) |
| 1398 | (stgit-worktree-node . nil) |
| 1399 | (stgit-marked-patches . nil) |
| 1400 | (stgit-svn-find-rev-hash . ,(make-hash-table :test 'equal)) |
| 1401 | (stgit-committed-count . ,stgit-default-committed-count) |
| 1402 | (stgit-show-committed . ,stgit-default-show-committed) |
| 1403 | (stgit-show-ignored . ,stgit-default-show-ignored) |
| 1404 | (stgit-show-patch-names . ,stgit-default-show-patch-names) |
| 1405 | (stgit-show-svn . ,stgit-default-show-svn) |
| 1406 | (stgit-show-unknown . ,stgit-default-show-unknown) |
| 1407 | (stgit-show-worktree . ,stgit-default-show-worktree))) |
| 1408 | (set-variable 'truncate-lines 't) |
| 1409 | (add-hook 'after-save-hook 'stgit-update-stgit-for-buffer) |
| 1410 | (unless stgit-did-advise |
| 1411 | (stgit-advise) |
| 1412 | (setq stgit-did-advise t)) |
| 1413 | (run-hooks 'stgit-mode-hook)) |
| 1414 | |
| 1415 | (defun stgit-advise-funlist (funlist) |
| 1416 | "Add advice to the functions in FUNLIST so we can refresh the |
| 1417 | stgit buffers as the git status of files change." |
| 1418 | (mapc (lambda (sym) |
| 1419 | (when (fboundp sym) |
| 1420 | (eval `(defadvice ,sym (after stgit-update-stgit-for-buffer) |
| 1421 | (stgit-update-stgit-for-buffer t))) |
| 1422 | (ad-activate sym))) |
| 1423 | funlist)) |
| 1424 | |
| 1425 | (defun stgit-advise () |
| 1426 | "Add advice to appropriate (non-stgit) git functions so we can |
| 1427 | refresh the stgit buffers as the git status of files change." |
| 1428 | (mapc (lambda (arg) |
| 1429 | (let ((feature (car arg)) |
| 1430 | (funlist (cdr arg))) |
| 1431 | (if (featurep feature) |
| 1432 | (stgit-advise-funlist funlist) |
| 1433 | (add-to-list 'after-load-alist |
| 1434 | `(,feature (stgit-advise-funlist |
| 1435 | (quote ,funlist))))))) |
| 1436 | ;; lists of (<feature> <function> <function> ...) to be advised |
| 1437 | '((vc-git vc-git-rename-file vc-git-revert vc-git-register) |
| 1438 | (git git-add-file git-checkout git-revert-file git-remove-file) |
| 1439 | (dired dired-delete-file)))) |
| 1440 | |
| 1441 | (defvar stgit-pending-refresh-buffers nil |
| 1442 | "Alist of (cons `buffer' `refresh-index') of buffers that need |
| 1443 | to be refreshed. `refresh-index' is non-nil if both work tree |
| 1444 | and index need to be refreshed.") |
| 1445 | |
| 1446 | (defun stgit-run-pending-refreshs () |
| 1447 | "Run all pending stgit buffer updates as posted by `stgit-post-refresh'." |
| 1448 | (let ((buffers stgit-pending-refresh-buffers) |
| 1449 | (stgit-inhibit-messages t)) |
| 1450 | (setq stgit-pending-refresh-buffers nil) |
| 1451 | (while buffers |
| 1452 | (let* ((elem (car buffers)) |
| 1453 | (buffer (car elem)) |
| 1454 | (refresh-index (cdr elem))) |
| 1455 | (when (buffer-name buffer) |
| 1456 | (with-current-buffer buffer |
| 1457 | (stgit-refresh-worktree) |
| 1458 | (when refresh-index (stgit-refresh-index))))) |
| 1459 | (setq buffers (cdr buffers))))) |
| 1460 | |
| 1461 | (defun stgit-post-refresh (buffer refresh-index) |
| 1462 | "Update worktree status in BUFFER when Emacs becomes idle. If |
| 1463 | REFRESH-INDEX is non-nil, also update the index." |
| 1464 | (unless stgit-pending-refresh-buffers |
| 1465 | (run-with-idle-timer 0.1 nil 'stgit-run-pending-refreshs)) |
| 1466 | (let ((elem (assq buffer stgit-pending-refresh-buffers))) |
| 1467 | (if elem |
| 1468 | ;; if buffer is already present, set its refresh-index flag if |
| 1469 | ;; necessary |
| 1470 | (when refresh-index |
| 1471 | (setcdr elem t)) |
| 1472 | ;; new entry |
| 1473 | (setq stgit-pending-refresh-buffers |
| 1474 | (cons (cons buffer refresh-index) |
| 1475 | stgit-pending-refresh-buffers))))) |
| 1476 | |
| 1477 | (defun stgit-update-stgit-for-buffer (&optional refresh-index) |
| 1478 | "When Emacs becomes idle, refresh worktree status in any |
| 1479 | `stgit-mode' buffer that shows the status of the current buffer. |
| 1480 | |
| 1481 | If REFRESH-INDEX is non-nil, also update the index." |
| 1482 | (let* ((dir (cond ((derived-mode-p 'stgit-status-mode 'dired-mode) |
| 1483 | default-directory) |
| 1484 | (buffer-file-name |
| 1485 | (file-name-directory |
| 1486 | (expand-file-name buffer-file-name))))) |
| 1487 | (gitdir (and dir (condition-case nil (git-get-top-dir dir) |
| 1488 | (error nil)))) |
| 1489 | (buffer (and gitdir (stgit-find-buffer gitdir)))) |
| 1490 | (when buffer |
| 1491 | (stgit-post-refresh buffer refresh-index)))) |
| 1492 | |
| 1493 | (defun stgit-add-mark (patchsym) |
| 1494 | "Mark the patch PATCHSYM." |
| 1495 | (setq stgit-marked-patches (cons patchsym stgit-marked-patches))) |
| 1496 | |
| 1497 | (defun stgit-remove-mark (patchsym) |
| 1498 | "Unmark the patch PATCHSYM." |
| 1499 | (setq stgit-marked-patches (delq patchsym stgit-marked-patches))) |
| 1500 | |
| 1501 | (defun stgit-clear-marks () |
| 1502 | "Unmark all patches." |
| 1503 | (setq stgit-marked-patches '())) |
| 1504 | |
| 1505 | (defun stgit-patch-at-point (&optional cause-error) |
| 1506 | (get-text-property (point) 'patch-data)) |
| 1507 | |
| 1508 | (defun stgit-patch-name-at-point (&optional cause-error types) |
| 1509 | "Return the patch name on the current line as a symbol. |
| 1510 | If CAUSE-ERROR is not nil, signal an error if none found. |
| 1511 | |
| 1512 | TYPES controls which types of commits and patches can be returned. |
| 1513 | If it is t, only allow stgit patches; if 'allow-committed, also |
| 1514 | allow historical commits; if nil, also allow work tree and index." |
| 1515 | (let ((patch (stgit-patch-at-point))) |
| 1516 | (and patch |
| 1517 | (memq (stgit-patch->status patch) |
| 1518 | (case types |
| 1519 | ((nil) nil) |
| 1520 | ((allow-committed) '(work index)) |
| 1521 | ((t) '(work index committed)) |
| 1522 | (t (error "Bad value")))) |
| 1523 | (setq patch nil)) |
| 1524 | (cond (patch |
| 1525 | (stgit-patch->name patch)) |
| 1526 | (cause-error |
| 1527 | (error "No patch on this line"))))) |
| 1528 | |
| 1529 | (defun stgit-patched-file-at-point () |
| 1530 | (get-text-property (point) 'file-data)) |
| 1531 | |
| 1532 | (defun stgit-patches-marked-or-at-point (&optional cause-error types) |
| 1533 | "Return the symbols of the marked patches, or the patch on the current line. |
| 1534 | If CAUSE-ERRROR is not nil, signal an error if none found. |
| 1535 | |
| 1536 | TYPES controls which types of commits and patches can be returned. |
| 1537 | If it is t, only allow stgit patches; if 'allow-committed, also |
| 1538 | allow historical commits; if nil, also allow work tree and index." |
| 1539 | (if stgit-marked-patches |
| 1540 | stgit-marked-patches |
| 1541 | (let ((patch (stgit-patch-name-at-point nil types))) |
| 1542 | (cond (patch (list patch)) |
| 1543 | (cause-error (error "No patches marked or at this line")) |
| 1544 | (t nil))))) |
| 1545 | |
| 1546 | (defun stgit-goto-patch (patchsym &optional file) |
| 1547 | "Move point to the line containing patch PATCHSYM and return non-nil. |
| 1548 | If that patch cannot be found, do nothing and return nil. |
| 1549 | |
| 1550 | If the patch was found and FILE is not nil, instead move to that |
| 1551 | file's line. If FILE cannot be found, stay on the line of |
| 1552 | PATCHSYM." |
| 1553 | (let ((node (ewoc-nth stgit-ewoc 0))) |
| 1554 | (while (and node (not (eq (stgit-patch->name (ewoc-data node)) |
| 1555 | patchsym))) |
| 1556 | (setq node (ewoc-next stgit-ewoc node))) |
| 1557 | (when (and node file) |
| 1558 | (let* ((file-ewoc (stgit-patch->files-ewoc (ewoc-data node))) |
| 1559 | (file-node (ewoc-nth file-ewoc 0))) |
| 1560 | (while (and file-node |
| 1561 | (not (equal (stgit-file->file (ewoc-data file-node)) |
| 1562 | file))) |
| 1563 | (setq file-node (ewoc-next file-ewoc file-node))) |
| 1564 | (when file-node |
| 1565 | (ewoc-goto-node file-ewoc file-node) |
| 1566 | (move-to-column (stgit-goal-column)) |
| 1567 | (setq node nil)))) |
| 1568 | (when node |
| 1569 | (ewoc-goto-node stgit-ewoc node) |
| 1570 | (move-to-column goal-column)))) |
| 1571 | |
| 1572 | (defun stgit-init () |
| 1573 | "Run stg init." |
| 1574 | (interactive) |
| 1575 | (stgit-assert-mode) |
| 1576 | (stgit-capture-output nil |
| 1577 | (stgit-run "init")) |
| 1578 | (stgit-reload)) |
| 1579 | |
| 1580 | (defun stgit-toggle-mark () |
| 1581 | "Toggle mark on the patch under point." |
| 1582 | (interactive) |
| 1583 | (stgit-assert-mode) |
| 1584 | (if (memq (stgit-patch-name-at-point t t) stgit-marked-patches) |
| 1585 | (stgit-unmark) |
| 1586 | (stgit-mark))) |
| 1587 | |
| 1588 | (defun stgit-mark () |
| 1589 | "Mark the patch under point." |
| 1590 | (interactive) |
| 1591 | (stgit-assert-mode) |
| 1592 | (let* ((node (ewoc-locate stgit-ewoc)) |
| 1593 | (patch (ewoc-data node))) |
| 1594 | (case (stgit-patch->status patch) |
| 1595 | (work (error "Cannot mark the work tree")) |
| 1596 | (index (error "Cannot mark the index")) |
| 1597 | (committed (error "Cannot mark a committed patch"))) |
| 1598 | (stgit-add-mark (stgit-patch->name patch)) |
| 1599 | (let ((column (current-column))) |
| 1600 | (ewoc-invalidate stgit-ewoc node) |
| 1601 | (move-to-column column)))) |
| 1602 | |
| 1603 | (defun stgit-mark-down () |
| 1604 | "Mark the patch under point and move to the next patch." |
| 1605 | (interactive) |
| 1606 | (stgit-mark) |
| 1607 | (stgit-next-patch)) |
| 1608 | |
| 1609 | (defun stgit-unmark () |
| 1610 | "Remove mark from the patch on the current line." |
| 1611 | (interactive) |
| 1612 | (stgit-assert-mode) |
| 1613 | (let* ((node (ewoc-locate stgit-ewoc)) |
| 1614 | (patch (ewoc-data node))) |
| 1615 | (stgit-remove-mark (stgit-patch->name patch)) |
| 1616 | (let ((column (current-column))) |
| 1617 | (ewoc-invalidate stgit-ewoc node) |
| 1618 | (move-to-column column)))) |
| 1619 | |
| 1620 | (defun stgit-unmark-up () |
| 1621 | "Remove mark from the patch on the previous line." |
| 1622 | (interactive) |
| 1623 | (stgit-assert-mode) |
| 1624 | (stgit-previous-patch) |
| 1625 | (stgit-unmark)) |
| 1626 | |
| 1627 | (defun stgit-unmark-down () |
| 1628 | "Remove mark from the patch on the current line." |
| 1629 | (interactive) |
| 1630 | (stgit-assert-mode) |
| 1631 | (stgit-unmark) |
| 1632 | (stgit-next-patch)) |
| 1633 | |
| 1634 | (defun stgit-rename (name) |
| 1635 | "Rename the patch under point to NAME." |
| 1636 | (interactive (list |
| 1637 | (read-string "Patch name: " |
| 1638 | (symbol-name (stgit-patch-name-at-point t t))))) |
| 1639 | (stgit-assert-mode) |
| 1640 | (let ((old-patchsym (stgit-patch-name-at-point t t))) |
| 1641 | (stgit-capture-output nil |
| 1642 | (stgit-run "rename" "--" old-patchsym name)) |
| 1643 | (let ((name-sym (intern name))) |
| 1644 | (when (memq old-patchsym stgit-expanded-patches) |
| 1645 | (setq stgit-expanded-patches |
| 1646 | (cons name-sym (delq old-patchsym stgit-expanded-patches)))) |
| 1647 | (when (memq old-patchsym stgit-marked-patches) |
| 1648 | (setq stgit-marked-patches |
| 1649 | (cons name-sym (delq old-patchsym stgit-marked-patches)))) |
| 1650 | (stgit-reload) |
| 1651 | (stgit-goto-patch name-sym)))) |
| 1652 | |
| 1653 | (defun stgit-reload-or-repair (repair) |
| 1654 | "Update the contents of the StGit buffer (`stgit-reload'). |
| 1655 | |
| 1656 | With a prefix argument, repair the StGit metadata if the branch |
| 1657 | was modified with git commands (`stgit-repair')." |
| 1658 | (interactive "P") |
| 1659 | (stgit-assert-mode) |
| 1660 | (if repair |
| 1661 | (stgit-repair) |
| 1662 | (stgit-reload))) |
| 1663 | |
| 1664 | (defun stgit-repair () |
| 1665 | "Run stg repair." |
| 1666 | (interactive) |
| 1667 | (stgit-assert-mode) |
| 1668 | (stgit-capture-output nil |
| 1669 | (stgit-run "repair")) |
| 1670 | (stgit-reload)) |
| 1671 | |
| 1672 | (defun stgit-available-branches (&optional all) |
| 1673 | "Returns a list of the names of the available stg branches as strings. |
| 1674 | |
| 1675 | If ALL is not nil, also return non-stgit branches." |
| 1676 | (let ((output (with-output-to-string |
| 1677 | (stgit-run "branch" "--list"))) |
| 1678 | (pattern (format "^>?\\s-+%c\\s-+\\(\\S-+\\)" |
| 1679 | (if all ?. ?s))) |
| 1680 | (start 0) |
| 1681 | result) |
| 1682 | (while (string-match pattern output start) |
| 1683 | (setq result (cons (match-string 1 output) result)) |
| 1684 | (setq start (match-end 0))) |
| 1685 | result)) |
| 1686 | |
| 1687 | (defun stgit-branch (branch) |
| 1688 | "Switch to or create branch BRANCH." |
| 1689 | (interactive (list (completing-read "Switch to branch: " |
| 1690 | (stgit-available-branches)))) |
| 1691 | (stgit-assert-mode) |
| 1692 | (when (cond ((equal branch (stgit-current-branch)) |
| 1693 | (error "Branch is already current")) |
| 1694 | ((member branch (stgit-available-branches t)) |
| 1695 | (stgit-capture-output nil (stgit-run "branch" "--" branch)) |
| 1696 | t) |
| 1697 | ((not (string-match stgit-allowed-branch-name-re branch)) |
| 1698 | (error "Invalid branch name")) |
| 1699 | ((yes-or-no-p (format "Create branch \"%s\"? " branch)) |
| 1700 | (let ((branch-point (completing-read |
| 1701 | "Branch from (default current branch): " |
| 1702 | (stgit-available-branches)))) |
| 1703 | (stgit-capture-output nil |
| 1704 | (apply 'stgit-run |
| 1705 | `("branch" "--create" "--" |
| 1706 | ,branch |
| 1707 | ,@(unless (zerop (length branch-point)) |
| 1708 | (list branch-point))))) |
| 1709 | t))) |
| 1710 | (stgit-reload))) |
| 1711 | |
| 1712 | (defun stgit-available-refs (&optional omit-stgit) |
| 1713 | "Returns a list of the available git refs. |
| 1714 | If OMIT-STGIT is not nil, filter out \"resf/heads/*.stgit\"." |
| 1715 | (let* ((output (with-output-to-string |
| 1716 | (stgit-run-git-silent "for-each-ref" "--format=%(refname)" |
| 1717 | "refs/tags" "refs/heads" |
| 1718 | "refs/remotes"))) |
| 1719 | (result (split-string output "\n" t))) |
| 1720 | (mapcar (lambda (s) |
| 1721 | (if (string-match "^refs/\\(heads\\|tags\\|remotes\\)/" s) |
| 1722 | (substring s (match-end 0)) |
| 1723 | s)) |
| 1724 | (if omit-stgit |
| 1725 | (delete-if (lambda (s) |
| 1726 | (string-match "^refs/heads/.*\\.stgit$" s)) |
| 1727 | result) |
| 1728 | result)))) |
| 1729 | |
| 1730 | (defun stgit-parent-branch () |
| 1731 | "Return the parent branch of the current stg branch as per |
| 1732 | git-config setting branch.<branch>.stgit.parentbranch." |
| 1733 | (let ((output (with-output-to-string |
| 1734 | (stgit-run-git-silent "config" |
| 1735 | (format "branch.%s.stgit.parentbranch" |
| 1736 | (stgit-current-branch)))))) |
| 1737 | (when (string-match ".*" output) |
| 1738 | (match-string 0 output)))) |
| 1739 | |
| 1740 | (defun stgit-rebase (new-base) |
| 1741 | "Rebase the current branch to NEW-BASE. |
| 1742 | |
| 1743 | Interactively, first ask which branch to rebase to. Defaults to |
| 1744 | what git-config branch.<branch>.stgit.parentbranch is set to." |
| 1745 | (interactive (list (completing-read "Rebase to: " |
| 1746 | (stgit-available-refs t) |
| 1747 | nil nil |
| 1748 | (stgit-parent-branch)))) |
| 1749 | (stgit-assert-mode) |
| 1750 | (stgit-capture-output nil (stgit-run "rebase" "--" new-base)) |
| 1751 | (stgit-reload)) |
| 1752 | |
| 1753 | (defun stgit-commit (count) |
| 1754 | "Run stg commit on COUNT commits. |
| 1755 | Interactively, the prefix argument is used as COUNT. |
| 1756 | A negative COUNT will uncommit instead." |
| 1757 | (interactive "p") |
| 1758 | (stgit-assert-mode) |
| 1759 | (if (< count 0) |
| 1760 | (stgit-uncommit (- count)) |
| 1761 | (stgit-capture-output nil (stgit-run "commit" "-n" count)) |
| 1762 | (stgit-reload))) |
| 1763 | |
| 1764 | (defun stgit-uncommit (count) |
| 1765 | "Run stg uncommit on COUNT commits. |
| 1766 | Interactively, the prefix argument is used as COUNT. |
| 1767 | A negative COUNT will commit instead." |
| 1768 | (interactive "p") |
| 1769 | (stgit-assert-mode) |
| 1770 | (if (< count 0) |
| 1771 | (stgit-commit (- count)) |
| 1772 | (stgit-capture-output nil (stgit-run "uncommit" "-n" count)) |
| 1773 | (stgit-reload))) |
| 1774 | |
| 1775 | (defun stgit-neighbour-file () |
| 1776 | "Return the file name of the next file after point, or the |
| 1777 | previous file if point is at the last file within a patch." |
| 1778 | (let ((old-point (point)) |
| 1779 | neighbour-file) |
| 1780 | (and (zerop (forward-line 1)) |
| 1781 | (let ((f (stgit-patched-file-at-point))) |
| 1782 | (and f (setq neighbour-file (stgit-file->file f))))) |
| 1783 | (goto-char old-point) |
| 1784 | (unless neighbour-file |
| 1785 | (and (zerop (forward-line -1)) |
| 1786 | (let ((f (stgit-patched-file-at-point))) |
| 1787 | (and f (setq neighbour-file (stgit-file->file f))))) |
| 1788 | (goto-char old-point)) |
| 1789 | neighbour-file)) |
| 1790 | |
| 1791 | (defun stgit-revert-file () |
| 1792 | "Revert the file at point, which must be in the index or the |
| 1793 | working tree." |
| 1794 | (interactive) |
| 1795 | (stgit-assert-mode) |
| 1796 | (let* ((patched-file (or (stgit-patched-file-at-point) |
| 1797 | (error "No file on the current line"))) |
| 1798 | (patch-name (stgit-patch-name-at-point)) |
| 1799 | (file-status (stgit-file->status patched-file)) |
| 1800 | (rm-file (cond ((stgit-file->copy-or-rename patched-file) |
| 1801 | (stgit-file->cr-to patched-file)) |
| 1802 | ((eq file-status 'add) |
| 1803 | (stgit-file->file patched-file)))) |
| 1804 | (co-file (cond ((eq file-status 'rename) |
| 1805 | (stgit-file->cr-from patched-file)) |
| 1806 | ((not (memq file-status '(copy add))) |
| 1807 | (stgit-file->file patched-file)))) |
| 1808 | (next-file (stgit-neighbour-file))) |
| 1809 | |
| 1810 | (unless (memq patch-name '(:work :index)) |
| 1811 | (error "No index or working tree file on this line")) |
| 1812 | |
| 1813 | (when (eq file-status 'ignore) |
| 1814 | (error "Cannot revert ignored files")) |
| 1815 | |
| 1816 | (when (eq file-status 'unknown) |
| 1817 | (error "Cannot revert unknown files")) |
| 1818 | |
| 1819 | (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0)))) |
| 1820 | (when (yes-or-no-p (format "Revert %d file%s? " |
| 1821 | nfiles |
| 1822 | (if (= nfiles 1) "" "s"))) |
| 1823 | (stgit-capture-output nil |
| 1824 | (when rm-file |
| 1825 | (stgit-run-git "rm" "-f" "-q" "--" rm-file)) |
| 1826 | (when co-file |
| 1827 | (stgit-run-git "checkout" "HEAD" co-file))) |
| 1828 | (stgit-reload) |
| 1829 | (stgit-goto-patch patch-name next-file))))) |
| 1830 | |
| 1831 | (defun stgit-revert () |
| 1832 | "Revert the change at point, which must be the index, the work |
| 1833 | tree, or a single change in either." |
| 1834 | (interactive) |
| 1835 | (stgit-assert-mode) |
| 1836 | (let ((patched-file (stgit-patched-file-at-point))) |
| 1837 | (if patched-file |
| 1838 | (stgit-revert-file) |
| 1839 | (let* ((patch-name (or (stgit-patch-name-at-point) |
| 1840 | (error "No patch or file at point"))) |
| 1841 | (patch-desc (case patch-name |
| 1842 | (:index "index") |
| 1843 | (:work "work tree") |
| 1844 | (t (error (substitute-command-keys |
| 1845 | "Use \\[stgit-delete] to delete a patch")))))) |
| 1846 | (when (if (eq patch-name :work) |
| 1847 | (stgit-work-tree-empty-p) |
| 1848 | (stgit-index-empty-p)) |
| 1849 | (error (format "There are no changes in the %s to revert" |
| 1850 | patch-desc))) |
| 1851 | (and (eq patch-name :index) |
| 1852 | (not (stgit-work-tree-empty-p)) |
| 1853 | (error "Cannot revert index as work tree contains unstaged changes")) |
| 1854 | |
| 1855 | (when (yes-or-no-p (format "Revert all changes in the %s? " |
| 1856 | patch-desc)) |
| 1857 | (if (eq patch-name :index) |
| 1858 | (stgit-run-git-silent "reset" "--hard" "-q") |
| 1859 | (stgit-run-git-silent "checkout" "--" ".")) |
| 1860 | (stgit-refresh-index) |
| 1861 | (stgit-refresh-worktree) |
| 1862 | (stgit-goto-patch patch-name)))))) |
| 1863 | |
| 1864 | (defun stgit-resolve-file () |
| 1865 | "Resolve conflict in the file at point." |
| 1866 | (interactive) |
| 1867 | (stgit-assert-mode) |
| 1868 | (let* ((patched-file (stgit-patched-file-at-point)) |
| 1869 | (patch (stgit-patch-at-point)) |
| 1870 | (patch-name (and patch (stgit-patch->name patch))) |
| 1871 | (status (and patched-file (stgit-file->status patched-file)))) |
| 1872 | |
| 1873 | (unless (memq patch-name '(:work :index)) |
| 1874 | (error "No index or working tree file on this line")) |
| 1875 | |
| 1876 | (unless (eq status 'unmerged) |
| 1877 | (error "No conflict to resolve at the current line")) |
| 1878 | |
| 1879 | (stgit-capture-output nil |
| 1880 | (stgit-move-change-to-index (stgit-file->file patched-file))) |
| 1881 | |
| 1882 | (stgit-reload))) |
| 1883 | |
| 1884 | (defun stgit-push-or-pop-patches (do-push npatches) |
| 1885 | "Push (if DO-PUSH is not nil) or pop (if DO-PUSH is nil) |
| 1886 | NPATCHES patches, or all patches if NPATCHES is t." |
| 1887 | (stgit-assert-mode) |
| 1888 | (stgit-capture-output nil |
| 1889 | (apply 'stgit-run |
| 1890 | (if do-push "push" "pop") |
| 1891 | (if (eq npatches t) |
| 1892 | '("--all") |
| 1893 | (list "-n" npatches)))) |
| 1894 | (stgit-reload) |
| 1895 | (stgit-refresh-git-status)) |
| 1896 | |
| 1897 | (defun stgit-push-next (npatches) |
| 1898 | "Push the first unapplied patch. |
| 1899 | With numeric prefix argument, push that many patches." |
| 1900 | (interactive "p") |
| 1901 | (stgit-push-or-pop-patches t npatches)) |
| 1902 | |
| 1903 | (defun stgit-pop-next (npatches) |
| 1904 | "Pop the topmost applied patch. |
| 1905 | With numeric prefix argument, pop that many patches. |
| 1906 | |
| 1907 | If NPATCHES is t, pop all patches." |
| 1908 | (interactive "p") |
| 1909 | (stgit-push-or-pop-patches nil npatches)) |
| 1910 | |
| 1911 | (defun stgit-applied-patches (&optional only-patches) |
| 1912 | "Return a list of the applied patches. |
| 1913 | |
| 1914 | If ONLY-PATCHES is not nil, exclude index and work tree." |
| 1915 | (let ((states (if only-patches |
| 1916 | '(applied top) |
| 1917 | '(applied top index work))) |
| 1918 | result) |
| 1919 | (ewoc-map (lambda (patch) |
| 1920 | (when (memq (stgit-patch->status patch) states) |
| 1921 | (setq result (cons patch result))) |
| 1922 | nil) |
| 1923 | stgit-ewoc) |
| 1924 | result)) |
| 1925 | |
| 1926 | (defun stgit-applied-patchsyms (&optional only-patches) |
| 1927 | "Return a list of the symbols of the applied patches. |
| 1928 | |
| 1929 | If ONLY-PATCHES is not nil, exclude index and work tree." |
| 1930 | (mapcar #'stgit-patch->name (stgit-applied-patches only-patches))) |
| 1931 | |
| 1932 | (defun stgit-push-or-pop () |
| 1933 | "Push or pop the marked patches." |
| 1934 | (interactive) |
| 1935 | (stgit-assert-mode) |
| 1936 | (let* ((patchsyms (stgit-patches-marked-or-at-point t t)) |
| 1937 | (applied-syms (stgit-applied-patchsyms t)) |
| 1938 | (unapplied (set-difference patchsyms applied-syms))) |
| 1939 | (stgit-capture-output nil |
| 1940 | (apply 'stgit-run |
| 1941 | (if unapplied "push" "pop") |
| 1942 | "--" |
| 1943 | (stgit-sort-patches (if unapplied unapplied patchsyms))))) |
| 1944 | (stgit-reload)) |
| 1945 | |
| 1946 | (defun stgit-goto-target () |
| 1947 | "Return the goto target at point: a patchsym, :top, |
| 1948 | or :bottom." |
| 1949 | (let ((patch (stgit-patch-at-point))) |
| 1950 | (cond (patch |
| 1951 | (case (stgit-patch->status patch) |
| 1952 | ((work index) nil) |
| 1953 | ((committed) :bottom) |
| 1954 | (t (stgit-patch->name patch)))) |
| 1955 | ((not (next-single-property-change (point) 'patch-data)) |
| 1956 | :top) |
| 1957 | ((not (previous-single-property-change (point) 'patch-data)) |
| 1958 | :bottom)))) |
| 1959 | |
| 1960 | (defun stgit-goto () |
| 1961 | "Go to the patch on the current line. |
| 1962 | |
| 1963 | Push or pop patches to make this patch topmost. Push or pop all |
| 1964 | patches if used on a line after or before all patches." |
| 1965 | (interactive) |
| 1966 | (stgit-assert-mode) |
| 1967 | (let ((patchsym (stgit-goto-target))) |
| 1968 | (unless patchsym |
| 1969 | (error "No patch to go to on this line")) |
| 1970 | (case patchsym |
| 1971 | (:top (stgit-push-or-pop-patches t t)) |
| 1972 | (:bottom (stgit-push-or-pop-patches nil t)) |
| 1973 | (t (stgit-capture-output nil |
| 1974 | (stgit-run "goto" "--" patchsym)) |
| 1975 | (stgit-reload))))) |
| 1976 | |
| 1977 | (defun stgit-id (patchsym) |
| 1978 | "Return the git commit id for PATCHSYM. |
| 1979 | If PATCHSYM is a keyword, returns PATCHSYM unmodified." |
| 1980 | (if (keywordp patchsym) |
| 1981 | patchsym |
| 1982 | (let ((result (with-output-to-string |
| 1983 | (stgit-run-silent "id" "--" patchsym)))) |
| 1984 | (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result) |
| 1985 | (error "Cannot find commit id for %s" patchsym)) |
| 1986 | (match-string 1 result)))) |
| 1987 | |
| 1988 | (defun stgit-whitespace-diff-arg (arg) |
| 1989 | (when (numberp arg) |
| 1990 | (cond ((> arg 4) "--ignore-all-space") |
| 1991 | ((> arg 1) "--ignore-space-change")))) |
| 1992 | |
| 1993 | (defun stgit-show-patch (unmerged-stage ignore-whitespace) |
| 1994 | "Show the patch on the current line. |
| 1995 | |
| 1996 | UNMERGED-STAGE is the argument to `git-diff' that that selects |
| 1997 | which stage to diff against in the case of unmerged files." |
| 1998 | (let ((space-arg (stgit-whitespace-diff-arg ignore-whitespace)) |
| 1999 | (patch-name (stgit-patch-name-at-point t))) |
| 2000 | (stgit-capture-output "*StGit patch*" |
| 2001 | (case (get-text-property (point) 'entry-type) |
| 2002 | ('file |
| 2003 | (let* ((patched-file (stgit-patched-file-at-point)) |
| 2004 | (patch-id (let ((id (stgit-id patch-name))) |
| 2005 | (if (and (eq id :index) |
| 2006 | (eq (stgit-file->status patched-file) |
| 2007 | 'unmerged)) |
| 2008 | :work |
| 2009 | id))) |
| 2010 | (args (append (and space-arg (list space-arg)) |
| 2011 | (and (stgit-file->cr-from patched-file) |
| 2012 | (list (stgit-find-copies-harder-diff-arg))) |
| 2013 | (cond ((eq patch-id :index) |
| 2014 | '("--cached")) |
| 2015 | ((eq patch-id :work) |
| 2016 | (list unmerged-stage)) |
| 2017 | (t |
| 2018 | (list (concat patch-id "^") patch-id))) |
| 2019 | '("--") |
| 2020 | (if (stgit-file->copy-or-rename patched-file) |
| 2021 | (list (stgit-file->cr-from patched-file) |
| 2022 | (stgit-file->cr-to patched-file)) |
| 2023 | (list (stgit-file->file patched-file)))))) |
| 2024 | (apply 'stgit-run-git "diff" args))) |
| 2025 | ('patch |
| 2026 | (let* ((patch-id (stgit-id patch-name))) |
| 2027 | (if (or (eq patch-id :index) (eq patch-id :work)) |
| 2028 | (apply 'stgit-run-git "diff" |
| 2029 | (stgit-find-copies-harder-diff-arg) |
| 2030 | (append (and space-arg (list space-arg)) |
| 2031 | (if (eq patch-id :index) |
| 2032 | '("--cached") |
| 2033 | (list unmerged-stage)))) |
| 2034 | (let ((args (append '("show" "-O" "--patch-with-stat" "-O" "-M") |
| 2035 | (and space-arg (list "-O" space-arg)) |
| 2036 | '("--") |
| 2037 | (list (stgit-patch-name-at-point))))) |
| 2038 | (apply 'stgit-run args))))) |
| 2039 | (t |
| 2040 | (error "No patch or file at point"))) |
| 2041 | (with-current-buffer standard-output |
| 2042 | (goto-char (point-min)) |
| 2043 | (diff-mode))))) |
| 2044 | |
| 2045 | (defmacro stgit-define-diff (name diff-arg &optional unmerged-action) |
| 2046 | `(defun ,name (&optional ignore-whitespace) |
| 2047 | ,(format "Show the patch on the current line. |
| 2048 | |
| 2049 | %sWith a prefix argument, ignore whitespace. With a prefix argument |
| 2050 | greater than four (e.g., \\[universal-argument] \ |
| 2051 | \\[universal-argument] \\[%s]), ignore all whitespace." |
| 2052 | (if unmerged-action |
| 2053 | (format "For unmerged files, %s.\n\n" unmerged-action) |
| 2054 | "") |
| 2055 | name) |
| 2056 | (interactive "p") |
| 2057 | (stgit-assert-mode) |
| 2058 | (stgit-show-patch ,diff-arg ignore-whitespace))) |
| 2059 | |
| 2060 | (stgit-define-diff stgit-diff |
| 2061 | "--ours" nil) |
| 2062 | (stgit-define-diff stgit-diff-ours |
| 2063 | "--ours" |
| 2064 | "diff against our branch") |
| 2065 | (stgit-define-diff stgit-diff-theirs |
| 2066 | "--theirs" |
| 2067 | "diff against their branch") |
| 2068 | (stgit-define-diff stgit-diff-base |
| 2069 | "--base" |
| 2070 | "diff against the merge base") |
| 2071 | (stgit-define-diff stgit-diff-combined |
| 2072 | "--cc" |
| 2073 | "show a combined diff") |
| 2074 | |
| 2075 | (defun stgit-diff-range (&optional ignore-whitespace) |
| 2076 | "Show diff for the range of patches between point and the marked patch. |
| 2077 | |
| 2078 | With a prefix argument, ignore whitespace. With a prefix argument |
| 2079 | greater than four (e.g., \\[universal-argument] \ |
| 2080 | \\[universal-argument] \\[stgit-diff-range]), ignore all whitespace." |
| 2081 | (interactive "p") |
| 2082 | (stgit-assert-mode) |
| 2083 | (unless (= (length stgit-marked-patches) 1) |
| 2084 | (error "Need exactly one patch marked")) |
| 2085 | (let* ((patches (stgit-sort-patches |
| 2086 | (cons (stgit-patch-name-at-point t 'allow-committed) |
| 2087 | stgit-marked-patches) |
| 2088 | t)) |
| 2089 | (first-patch (car patches)) |
| 2090 | (second-patch (if (cdr patches) (cadr patches) first-patch)) |
| 2091 | (whitespace-arg (stgit-whitespace-diff-arg ignore-whitespace)) |
| 2092 | (applied (stgit-applied-patchsyms t))) |
| 2093 | (unless (and (memq first-patch applied) (memq second-patch applied)) |
| 2094 | (error "Can only show diff range for applied patches")) |
| 2095 | (stgit-capture-output (format "*StGit diff %s..%s*" |
| 2096 | first-patch second-patch) |
| 2097 | (apply 'stgit-run-git (append '("diff" "--patch-with-stat") |
| 2098 | (and whitespace-arg (list whitespace-arg)) |
| 2099 | (list (format "%s^" (stgit-id first-patch)) |
| 2100 | (stgit-id second-patch)))) |
| 2101 | (with-current-buffer standard-output |
| 2102 | (goto-char (point-min)) |
| 2103 | (diff-mode))))) |
| 2104 | |
| 2105 | (defun stgit-move-change-to-index (file &optional force) |
| 2106 | "Copies the work tree state of FILE to index, using git add or git rm. |
| 2107 | |
| 2108 | If FORCE is not nil, use --force." |
| 2109 | (let ((op (if (or (file-exists-p file) (file-symlink-p file)) |
| 2110 | '("add") '("rm" "-q")))) |
| 2111 | (stgit-capture-output "*git output*" |
| 2112 | (apply 'stgit-run-git (append op (and force '("--force")) |
| 2113 | '("--") (list file)))))) |
| 2114 | |
| 2115 | (defun stgit-remove-change-from-index (file) |
| 2116 | "Unstages the change in FILE from the index" |
| 2117 | (stgit-capture-output "*git output*" |
| 2118 | (stgit-run-git "reset" "-q" "--" file))) |
| 2119 | |
| 2120 | (defun stgit-git-index-unmerged-p () |
| 2121 | (let (result) |
| 2122 | (with-output-to-string |
| 2123 | (setq result (not (zerop (stgit-run-git-silent "diff-index" "--cached" |
| 2124 | "--diff-filter=U" |
| 2125 | "--quiet" "HEAD"))))) |
| 2126 | result)) |
| 2127 | |
| 2128 | (defun stgit-file-toggle-index () |
| 2129 | "Move modified file in or out of the index. |
| 2130 | |
| 2131 | Leaves the point where it is, but moves the mark to where the |
| 2132 | file ended up. You can then jump to the file with \ |
| 2133 | \\[exchange-point-and-mark]." |
| 2134 | (interactive) |
| 2135 | (stgit-assert-mode) |
| 2136 | (let* ((patched-file (or (stgit-patched-file-at-point) |
| 2137 | (error "No file on the current line"))) |
| 2138 | (patched-status (stgit-file->status patched-file))) |
| 2139 | (when (eq patched-status 'unmerged) |
| 2140 | (error (substitute-command-keys "Use \\[stgit-resolve-file] to move an unmerged file to the index"))) |
| 2141 | (let* ((patch (stgit-patch-at-point)) |
| 2142 | (patch-name (stgit-patch->name patch)) |
| 2143 | (mark-file (if (eq patched-status 'rename) |
| 2144 | (stgit-file->cr-to patched-file) |
| 2145 | (stgit-file->file patched-file))) |
| 2146 | (point-file (if (eq patched-status 'rename) |
| 2147 | (stgit-file->cr-from patched-file) |
| 2148 | (stgit-neighbour-file)))) |
| 2149 | |
| 2150 | (cond ((eq patch-name :work) |
| 2151 | (stgit-move-change-to-index (stgit-file->file patched-file) |
| 2152 | (eq patched-status 'ignore))) |
| 2153 | ((eq patch-name :index) |
| 2154 | (stgit-remove-change-from-index (stgit-file->file patched-file))) |
| 2155 | (t |
| 2156 | (error "Can only move files between working tree and index"))) |
| 2157 | (stgit-refresh-worktree) |
| 2158 | (stgit-refresh-index) |
| 2159 | (stgit-goto-patch (if (eq patch-name :index) :work :index) mark-file) |
| 2160 | (push-mark nil t t) |
| 2161 | (stgit-goto-patch patch-name point-file)))) |
| 2162 | |
| 2163 | (defun stgit-toggle-index () |
| 2164 | "Move change in or out of the index. |
| 2165 | |
| 2166 | Works on index and work tree, as well as files in either. |
| 2167 | |
| 2168 | Leaves the point where it is, but moves the mark to where the |
| 2169 | file ended up. You can then jump to the file with \ |
| 2170 | \\[exchange-point-and-mark]." |
| 2171 | (interactive) |
| 2172 | (stgit-assert-mode) |
| 2173 | (if (stgit-patched-file-at-point) |
| 2174 | (stgit-file-toggle-index) |
| 2175 | (let ((patch-name (stgit-patch-name-at-point))) |
| 2176 | (unless (memq patch-name '(:index :work)) |
| 2177 | (error "Can only move changes between working tree and index")) |
| 2178 | (when (stgit-git-index-unmerged-p) |
| 2179 | (error "Resolve unmerged changes with \\[stgit-resolve-file] first")) |
| 2180 | (if (if (eq patch-name :index) |
| 2181 | (stgit-index-empty-p) |
| 2182 | (stgit-work-tree-empty-p)) |
| 2183 | (message "No changes to be moved") |
| 2184 | (stgit-capture-output nil |
| 2185 | (if (eq patch-name :work) |
| 2186 | (stgit-run-git "add" "--update") |
| 2187 | (stgit-run-git "reset" "--mixed" "-q"))) |
| 2188 | (stgit-refresh-worktree) |
| 2189 | (stgit-refresh-index)) |
| 2190 | (stgit-goto-patch (if (eq patch-name :index) :work :index))))) |
| 2191 | |
| 2192 | (defun stgit-edit () |
| 2193 | "Edit the patch on the current line." |
| 2194 | (interactive) |
| 2195 | (stgit-assert-mode) |
| 2196 | (let ((patchsym (stgit-patch-name-at-point t t)) |
| 2197 | (edit-buf (get-buffer-create "*StGit edit*")) |
| 2198 | (dir default-directory)) |
| 2199 | (log-edit 'stgit-confirm-edit t nil edit-buf) |
| 2200 | (set (make-local-variable 'stgit-edit-patchsym) patchsym) |
| 2201 | (setq default-directory dir) |
| 2202 | (let ((standard-output edit-buf)) |
| 2203 | (save-excursion |
| 2204 | (stgit-run-silent "edit" "--save-template=-" "--" patchsym))))) |
| 2205 | |
| 2206 | (defun stgit-confirm-edit () |
| 2207 | (interactive) |
| 2208 | (let ((file (make-temp-file "stgit-edit-"))) |
| 2209 | (write-region (point-min) (point-max) file) |
| 2210 | (stgit-capture-output nil |
| 2211 | (stgit-run "edit" "-f" file "--" stgit-edit-patchsym)) |
| 2212 | (with-current-buffer log-edit-parent-buffer |
| 2213 | (stgit-reload)))) |
| 2214 | |
| 2215 | (defun stgit-new (add-sign &optional refresh) |
| 2216 | "Create a new patch. |
| 2217 | With a prefix argument, include a \"Signed-off-by:\" line at the |
| 2218 | end of the patch." |
| 2219 | (interactive "P") |
| 2220 | (stgit-assert-mode) |
| 2221 | (let ((edit-buf (get-buffer-create "*StGit edit*")) |
| 2222 | (dir default-directory)) |
| 2223 | (log-edit 'stgit-confirm-new t nil edit-buf) |
| 2224 | (setq default-directory dir) |
| 2225 | (set (make-local-variable 'stgit-refresh-after-new) refresh) |
| 2226 | (when add-sign |
| 2227 | (save-excursion |
| 2228 | (let ((standard-output (current-buffer))) |
| 2229 | (stgit-run-silent "new" "--sign" "--save-template=-")))))) |
| 2230 | |
| 2231 | (defun stgit-confirm-new () |
| 2232 | (interactive) |
| 2233 | (let ((file (make-temp-file "stgit-edit-")) |
| 2234 | (refresh stgit-refresh-after-new)) |
| 2235 | (write-region (point-min) (point-max) file) |
| 2236 | (stgit-capture-output nil |
| 2237 | (stgit-run "new" "-f" file)) |
| 2238 | (with-current-buffer log-edit-parent-buffer |
| 2239 | (if refresh |
| 2240 | (stgit-refresh) |
| 2241 | (stgit-reload))))) |
| 2242 | |
| 2243 | (defun stgit-new-and-refresh (add-sign) |
| 2244 | "Create a new patch and refresh it with the current changes. |
| 2245 | |
| 2246 | With a prefix argument, include a \"Signed-off-by:\" line at the |
| 2247 | end of the patch. |
| 2248 | |
| 2249 | This works just like running `stgit-new' followed by `stgit-refresh'." |
| 2250 | (interactive "P") |
| 2251 | (stgit-assert-mode) |
| 2252 | (stgit-new add-sign t)) |
| 2253 | |
| 2254 | (defun stgit-create-patch-name (description) |
| 2255 | "Create a patch name from a long description" |
| 2256 | (let ((patch "")) |
| 2257 | (while (> (length description) 0) |
| 2258 | (cond ((string-match "\\`[a-zA-Z_-]+" description) |
| 2259 | (setq patch (downcase (concat patch |
| 2260 | (match-string 0 description)))) |
| 2261 | (setq description (substring description (match-end 0)))) |
| 2262 | ((string-match "\\` +" description) |
| 2263 | (setq patch (concat patch "-")) |
| 2264 | (setq description (substring description (match-end 0)))) |
| 2265 | ((string-match "\\`[^a-zA-Z_-]+" description) |
| 2266 | (setq description (substring description (match-end 0)))))) |
| 2267 | (cond ((= (length patch) 0) |
| 2268 | "patch") |
| 2269 | ((> (length patch) 20) |
| 2270 | (substring patch 0 20)) |
| 2271 | (t patch)))) |
| 2272 | |
| 2273 | (defun stgit-delete (patchsyms &optional spill-p) |
| 2274 | "Delete the patches in PATCHSYMS. |
| 2275 | Interactively, delete the marked patches, or the patch at point. |
| 2276 | |
| 2277 | With a prefix argument, or SPILL-P, spill the patch contents to |
| 2278 | the work tree and index." |
| 2279 | (interactive (list (stgit-patches-marked-or-at-point t t) |
| 2280 | current-prefix-arg)) |
| 2281 | (stgit-assert-mode) |
| 2282 | (unless patchsyms |
| 2283 | (error "No patches to delete")) |
| 2284 | (when (memq :index patchsyms) |
| 2285 | (error "Cannot delete the index")) |
| 2286 | (when (memq :work patchsyms) |
| 2287 | (error "Cannot delete the work tree")) |
| 2288 | |
| 2289 | (let ((npatches (length patchsyms))) |
| 2290 | (when (yes-or-no-p (format "Really delete %d patch%s%s? " |
| 2291 | npatches |
| 2292 | (if (= 1 npatches) "" "es") |
| 2293 | (if spill-p |
| 2294 | " (spilling contents to index)" |
| 2295 | ""))) |
| 2296 | (let ((args (append (when spill-p '("--spill")) |
| 2297 | '("--") |
| 2298 | patchsyms))) |
| 2299 | (stgit-capture-output nil |
| 2300 | (apply 'stgit-run "delete" args)) |
| 2301 | (stgit-reload))))) |
| 2302 | |
| 2303 | (defun stgit-move-patches-target () |
| 2304 | "Return the patchsym indicating a target patch for |
| 2305 | `stgit-move-patches'. |
| 2306 | |
| 2307 | This is either the first unmarked patch at or after point, or one |
| 2308 | of :top and :bottom if the point is after or before the applied |
| 2309 | patches." |
| 2310 | |
| 2311 | (save-excursion |
| 2312 | (let (result) |
| 2313 | (while (not result) |
| 2314 | (let ((patchsym (stgit-patch-name-at-point))) |
| 2315 | (cond ((memq patchsym '(:work :index)) (setq result :top)) |
| 2316 | (patchsym (if (memq patchsym stgit-marked-patches) |
| 2317 | (stgit-next-patch) |
| 2318 | (setq result patchsym))) |
| 2319 | ((re-search-backward "^>" nil t) (setq result :top)) |
| 2320 | (t (setq result :bottom))))) |
| 2321 | result))) |
| 2322 | |
| 2323 | (defun stgit-sort-patches (patchsyms &optional allow-duplicates) |
| 2324 | "Returns the list of patches in PATCHSYMS sorted according to |
| 2325 | their position in the patch series, bottommost first. |
| 2326 | |
| 2327 | PATCHSYMS must not contain duplicate entries, unless |
| 2328 | ALLOW-DUPLICATES is not nil." |
| 2329 | (let (sorted-patchsyms |
| 2330 | (series (with-output-to-string |
| 2331 | (with-current-buffer standard-output |
| 2332 | (stgit-run-silent "series" "--noprefix")))) |
| 2333 | start) |
| 2334 | (while (string-match "^\\(.+\\)" series start) |
| 2335 | (let ((patchsym (intern (match-string 1 series)))) |
| 2336 | (when (memq patchsym patchsyms) |
| 2337 | (setq sorted-patchsyms (cons patchsym sorted-patchsyms)))) |
| 2338 | (setq start (match-end 0))) |
| 2339 | (setq sorted-patchsyms (nreverse sorted-patchsyms)) |
| 2340 | |
| 2341 | (unless allow-duplicates |
| 2342 | (unless (= (length patchsyms) (length sorted-patchsyms)) |
| 2343 | (error "Internal error"))) |
| 2344 | |
| 2345 | sorted-patchsyms)) |
| 2346 | |
| 2347 | (defun stgit-move-patches (patchsyms target-patch) |
| 2348 | "Move the patches in PATCHSYMS to below TARGET-PATCH. |
| 2349 | If TARGET-PATCH is :bottom or :top, move the patches to the |
| 2350 | bottom or top of the stack, respectively. |
| 2351 | |
| 2352 | Interactively, move the marked patches to where the point is." |
| 2353 | (interactive (list stgit-marked-patches |
| 2354 | (stgit-move-patches-target))) |
| 2355 | (stgit-assert-mode) |
| 2356 | (unless patchsyms |
| 2357 | (error "Need at least one patch to move")) |
| 2358 | |
| 2359 | (unless target-patch |
| 2360 | (error "Point not at a patch")) |
| 2361 | |
| 2362 | ;; need to have patchsyms sorted by position in the stack |
| 2363 | (let ((sorted-patchsyms (stgit-sort-patches patchsyms))) |
| 2364 | (stgit-capture-output nil |
| 2365 | (if (eq target-patch :top) |
| 2366 | (apply 'stgit-run "float" "--" sorted-patchsyms) |
| 2367 | (apply 'stgit-run |
| 2368 | "sink" |
| 2369 | (append (unless (eq target-patch :bottom) |
| 2370 | (list "--to" target-patch)) |
| 2371 | '("--") |
| 2372 | sorted-patchsyms))))) |
| 2373 | (stgit-reload)) |
| 2374 | |
| 2375 | (defun stgit-squash (patchsyms) |
| 2376 | "Squash the patches in PATCHSYMS. |
| 2377 | Interactively, squash the marked patches. |
| 2378 | |
| 2379 | Unless there are any conflicts, the patches will be merged into |
| 2380 | one patch, which will occupy the same spot in the series as the |
| 2381 | deepest patch had before the squash." |
| 2382 | (interactive (list stgit-marked-patches)) |
| 2383 | (stgit-assert-mode) |
| 2384 | (when (< (length patchsyms) 2) |
| 2385 | (error "Need at least two patches to squash")) |
| 2386 | (let ((stgit-buffer (current-buffer)) |
| 2387 | (edit-buf (get-buffer-create "*StGit edit*")) |
| 2388 | (dir default-directory) |
| 2389 | (sorted-patchsyms (stgit-sort-patches patchsyms))) |
| 2390 | (log-edit 'stgit-confirm-squash t nil edit-buf) |
| 2391 | (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms) |
| 2392 | (setq default-directory dir) |
| 2393 | (let ((result (let ((standard-output edit-buf)) |
| 2394 | (save-excursion |
| 2395 | (apply 'stgit-run-silent "squash" |
| 2396 | "--save-template=-" "--" sorted-patchsyms))))) |
| 2397 | |
| 2398 | ;; stg squash may have reordered the patches or caused conflicts |
| 2399 | (with-current-buffer stgit-buffer |
| 2400 | (stgit-reload)) |
| 2401 | |
| 2402 | (unless (eq 0 result) |
| 2403 | (fundamental-mode) |
| 2404 | (rename-buffer "*StGit error*") |
| 2405 | (resize-temp-buffer-window) |
| 2406 | (switch-to-buffer-other-window stgit-buffer) |
| 2407 | (error "stg squash failed"))))) |
| 2408 | |
| 2409 | (defun stgit-confirm-squash () |
| 2410 | (interactive) |
| 2411 | (let ((file (make-temp-file "stgit-edit-"))) |
| 2412 | (write-region (point-min) (point-max) file) |
| 2413 | (stgit-capture-output nil |
| 2414 | (apply 'stgit-run "squash" "-f" file "--" stgit-patchsyms)) |
| 2415 | (with-current-buffer log-edit-parent-buffer |
| 2416 | (stgit-clear-marks) |
| 2417 | ;; Go to first marked patch and stay there |
| 2418 | (goto-char (point-min)) |
| 2419 | (re-search-forward (concat "^[>+-]\\*") nil t) |
| 2420 | (move-to-column goal-column) |
| 2421 | (let ((pos (point))) |
| 2422 | (stgit-reload) |
| 2423 | (goto-char pos))))) |
| 2424 | |
| 2425 | (defun stgit-help () |
| 2426 | "Display help for the StGit mode." |
| 2427 | (interactive) |
| 2428 | (describe-function 'stgit-mode)) |
| 2429 | |
| 2430 | (defun stgit-execute-process-sentinel (process sentinel) |
| 2431 | (let (old-sentinel stgit-buf) |
| 2432 | (with-current-buffer (process-buffer process) |
| 2433 | (setq old-sentinel old-process-sentinel |
| 2434 | stgit-buf stgit-buffer)) |
| 2435 | (and (memq (process-status process) '(exit signal)) |
| 2436 | (buffer-live-p stgit-buf) |
| 2437 | (with-current-buffer stgit-buf |
| 2438 | (stgit-reload))) |
| 2439 | (funcall old-sentinel process sentinel))) |
| 2440 | |
| 2441 | (defun stgit-execute-process-filter (process output) |
| 2442 | (with-current-buffer (process-buffer process) |
| 2443 | (let* ((old-point (point)) |
| 2444 | (pmark (process-mark process)) |
| 2445 | (insert-at (marker-position pmark)) |
| 2446 | (at-pmark (= insert-at old-point))) |
| 2447 | (goto-char insert-at) |
| 2448 | (insert-before-markers output) |
| 2449 | (comint-carriage-motion insert-at (point)) |
| 2450 | (set-marker pmark (point)) |
| 2451 | (unless at-pmark |
| 2452 | (goto-char old-point))))) |
| 2453 | |
| 2454 | (defun stgit-execute () |
| 2455 | "Prompt for an stg command to execute in a shell. |
| 2456 | |
| 2457 | The names of any marked patches or the patch at point are |
| 2458 | inserted in the command to be executed. |
| 2459 | |
| 2460 | If the command ends in an ampersand, run it asynchronously. |
| 2461 | |
| 2462 | When the command has finished, reload the stgit buffer." |
| 2463 | (interactive) |
| 2464 | (stgit-assert-mode) |
| 2465 | (let* ((patches (stgit-sort-patches |
| 2466 | (stgit-patches-marked-or-at-point nil 'allow-committed))) |
| 2467 | (patch-names (mapcar 'symbol-name patches)) |
| 2468 | (hyphens (find-if (lambda (s) (string-match "^-" s)) patch-names)) |
| 2469 | (defaultcmd (if patches |
| 2470 | (concat "stg " |
| 2471 | (and hyphens "-- ") |
| 2472 | (mapconcat 'identity patch-names " ")) |
| 2473 | "stg ")) |
| 2474 | (cmd (read-from-minibuffer "Shell command: " (cons defaultcmd 5) |
| 2475 | nil nil 'shell-command-history)) |
| 2476 | (async (string-match "&[ \t]*\\'" cmd)) |
| 2477 | (buffer (get-buffer-create |
| 2478 | (if async |
| 2479 | "*Async Shell Command*" |
| 2480 | "*Shell Command Output*")))) |
| 2481 | ;; cannot use minibuffer as stgit-reload would overwrite it; if we |
| 2482 | ;; show the buffer, shell-command will not use the minibuffer |
| 2483 | (display-buffer buffer) |
| 2484 | (shell-command cmd) |
| 2485 | (if async |
| 2486 | (let ((old-buffer (current-buffer))) |
| 2487 | (with-current-buffer buffer |
| 2488 | (let ((process (get-buffer-process buffer))) |
| 2489 | (set (make-local-variable 'old-process-sentinel) |
| 2490 | (process-sentinel process)) |
| 2491 | (set (make-local-variable 'stgit-buffer) |
| 2492 | old-buffer) |
| 2493 | (set-process-filter process 'stgit-execute-process-filter) |
| 2494 | (set-process-sentinel process 'stgit-execute-process-sentinel)))) |
| 2495 | (with-current-buffer buffer |
| 2496 | (comint-carriage-motion (point-min) (point-max))) |
| 2497 | (shrink-window-if-larger-than-buffer (get-buffer-window buffer)) |
| 2498 | (stgit-reload)))) |
| 2499 | |
| 2500 | (defun stgit-undo-or-redo (redo hard) |
| 2501 | "Run stg undo or, if REDO is non-nil, stg redo. |
| 2502 | |
| 2503 | If HARD is non-nil, use the --hard flag." |
| 2504 | (stgit-assert-mode) |
| 2505 | (let ((cmd (if redo "redo" "undo"))) |
| 2506 | (stgit-capture-output nil |
| 2507 | (if arg |
| 2508 | (when (or (and (stgit-index-empty-p) |
| 2509 | (stgit-work-tree-empty-p)) |
| 2510 | (y-or-n-p (format "Hard %s may overwrite index/work tree changes. Continue? " |
| 2511 | cmd))) |
| 2512 | (stgit-run cmd "--hard")) |
| 2513 | (stgit-run cmd)))) |
| 2514 | (stgit-reload)) |
| 2515 | |
| 2516 | (defun stgit-undo (&optional arg) |
| 2517 | "Run stg undo. |
| 2518 | With prefix argument, run it with the --hard flag. |
| 2519 | |
| 2520 | See also `stgit-redo'." |
| 2521 | (interactive "P") |
| 2522 | (stgit-undo-or-redo nil arg)) |
| 2523 | |
| 2524 | (defun stgit-redo (&optional arg) |
| 2525 | "Run stg redo. |
| 2526 | With prefix argument, run it with the --hard flag. |
| 2527 | |
| 2528 | See also `stgit-undo'." |
| 2529 | (interactive "P") |
| 2530 | (stgit-undo-or-redo t arg)) |
| 2531 | |
| 2532 | (defun stgit-refresh (&optional arg) |
| 2533 | "Run stg refresh. |
| 2534 | If the index contains any changes, only refresh from index. |
| 2535 | |
| 2536 | With prefix argument, refresh the marked patch or the patch under point." |
| 2537 | (interactive "P") |
| 2538 | (stgit-assert-mode) |
| 2539 | (let ((patchargs (if arg |
| 2540 | (let ((patches (stgit-patches-marked-or-at-point t t))) |
| 2541 | (when (> (length patches) 1) |
| 2542 | (error "Too many patches marked")) |
| 2543 | (cons "-p" patches)) |
| 2544 | nil))) |
| 2545 | (unless (stgit-index-empty-p) |
| 2546 | (setq patchargs (cons "--index" patchargs))) |
| 2547 | (stgit-capture-output nil |
| 2548 | (apply 'stgit-run "refresh" patchargs)) |
| 2549 | (stgit-refresh-git-status)) |
| 2550 | (stgit-reload)) |
| 2551 | |
| 2552 | (defvar stgit-show-worktree nil |
| 2553 | "If nil, inhibit showing work tree and index in the stgit buffer. |
| 2554 | |
| 2555 | See also `stgit-show-worktree-mode'.") |
| 2556 | |
| 2557 | (defvar stgit-show-ignored nil |
| 2558 | "If nil, inhibit showing files ignored by git.") |
| 2559 | |
| 2560 | (defvar stgit-show-unknown nil |
| 2561 | "If nil, inhibit showing files not registered with git.") |
| 2562 | |
| 2563 | (defvar stgit-show-patch-names t |
| 2564 | "If nil, inhibit showing patch names.") |
| 2565 | |
| 2566 | (defvar stgit-show-committed nil |
| 2567 | "If nil, inhibit showing recent commits.") |
| 2568 | |
| 2569 | (defvar stgit-show-svn nil |
| 2570 | "If nil, inhibit showing git svn information.") |
| 2571 | |
| 2572 | (defvar stgit-committed-count nil |
| 2573 | "The number of recent commits to show.") |
| 2574 | |
| 2575 | (defmacro stgit-define-toggle-view (sym desc help) |
| 2576 | (declare (indent 1)) |
| 2577 | (let* ((name (symbol-name sym)) |
| 2578 | (fun (intern (concat "stgit-toggle-" name))) |
| 2579 | (flag (intern (concat "stgit-show-" name)))) |
| 2580 | ;; make help-follow find the correct function |
| 2581 | `(put (quote ,fun) 'definition-name 'stgit-define-toggle-view) |
| 2582 | `(defun ,fun (&optional arg) |
| 2583 | ,help |
| 2584 | (interactive "P") |
| 2585 | (stgit-assert-mode) |
| 2586 | (setq ,flag (if arg |
| 2587 | (> (prefix-numeric-value arg) 0) |
| 2588 | (not ,flag))) |
| 2589 | (stgit-reload (format "%s %s" (if ,flag "Showing" "Hiding") ,desc))))) |
| 2590 | |
| 2591 | (stgit-define-toggle-view worktree |
| 2592 | "work tree and index" |
| 2593 | "Toggle the visibility of the work tree. |
| 2594 | With ARG, show the work tree if ARG is positive. |
| 2595 | |
| 2596 | Its initial setting is controlled by `stgit-default-show-worktree'. |
| 2597 | |
| 2598 | `stgit-show-worktree-mode' controls where on screen the index and |
| 2599 | work tree will show up.") |
| 2600 | |
| 2601 | (stgit-define-toggle-view ignored |
| 2602 | "ignored files" |
| 2603 | "Toggle the visibility of files ignored by git in the work |
| 2604 | tree. With ARG, show these files if ARG is positive. |
| 2605 | |
| 2606 | Its initial setting is controlled by `stgit-default-show-ignored'. |
| 2607 | |
| 2608 | Use \\[stgit-toggle-worktree] to show the work tree.") |
| 2609 | |
| 2610 | (stgit-define-toggle-view unknown |
| 2611 | "unknown files" |
| 2612 | "Toggle the visibility of files not registered with git in the |
| 2613 | work tree. With ARG, show these files if ARG is positive. |
| 2614 | |
| 2615 | Its initial setting is controlled by `stgit-default-show-unknown'. |
| 2616 | |
| 2617 | Use \\[stgit-toggle-worktree] to show the work tree.") |
| 2618 | |
| 2619 | (stgit-define-toggle-view patch-names |
| 2620 | "patch names" |
| 2621 | "Toggle the visibility of patch names. With ARG, show patch names |
| 2622 | if ARG is positive. |
| 2623 | |
| 2624 | The initial setting is controlled by `stgit-default-show-patch-names'.") |
| 2625 | |
| 2626 | (stgit-define-toggle-view svn |
| 2627 | "subversion revisions" |
| 2628 | "Toggle showing subversion information from git svn. With ARG, |
| 2629 | show svn information if ARG is positive. |
| 2630 | |
| 2631 | The initial setting is controlled by `stgit-default-show-svn'.") |
| 2632 | |
| 2633 | (defun stgit-toggle-committed (&optional arg) |
| 2634 | "Toggle the visibility of historical git commits. |
| 2635 | With ARG, set the number of commits to show to ARG, and disable |
| 2636 | them if ARG is zero. |
| 2637 | |
| 2638 | The initial setting is controlled by `stgit-default-show-committed'." |
| 2639 | (interactive "P") |
| 2640 | (stgit-assert-mode) |
| 2641 | (if (null arg) |
| 2642 | (setq stgit-show-committed (not stgit-show-committed)) |
| 2643 | (let ((n (prefix-numeric-value arg))) |
| 2644 | (setq stgit-show-committed (> n 0)) |
| 2645 | (setq stgit-committed-count n))) |
| 2646 | (stgit-reload (format "%s historical commits" |
| 2647 | (if (and stgit-show-committed |
| 2648 | (> stgit-committed-count 0)) |
| 2649 | "Showing" |
| 2650 | "Hiding")))) |
| 2651 | |
| 2652 | (provide 'stgit) |