| 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 | (require 'git nil t) |
| 13 | (require 'cl) |
| 14 | (require 'ewoc) |
| 15 | |
| 16 | (defun stgit (dir) |
| 17 | "Manage StGit patches for the tree in DIR." |
| 18 | (interactive "DDirectory: \n") |
| 19 | (switch-to-stgit-buffer (git-get-top-dir dir)) |
| 20 | (stgit-reload)) |
| 21 | |
| 22 | (unless (fboundp 'git-get-top-dir) |
| 23 | (defun git-get-top-dir (dir) |
| 24 | "Retrieve the top-level directory of a git tree." |
| 25 | (let ((cdup (with-output-to-string |
| 26 | (with-current-buffer standard-output |
| 27 | (cd dir) |
| 28 | (unless (eq 0 (call-process "git" nil t nil |
| 29 | "rev-parse" "--show-cdup")) |
| 30 | (error "Cannot find top-level git tree for %s" dir)))))) |
| 31 | (expand-file-name (concat (file-name-as-directory dir) |
| 32 | (car (split-string cdup "\n"))))))) |
| 33 | |
| 34 | (defun stgit-refresh-git-status (&optional dir) |
| 35 | "If it exists, refresh the `git-status' buffer belonging to |
| 36 | directory DIR or `default-directory'" |
| 37 | (when (and (fboundp 'git-find-status-buffer) |
| 38 | (fboundp 'git-refresh-status)) |
| 39 | (let* ((top-dir (git-get-top-dir (or dir default-directory))) |
| 40 | (git-status-buffer (and top-dir (git-find-status-buffer top-dir)))) |
| 41 | (when git-status-buffer |
| 42 | (with-current-buffer git-status-buffer |
| 43 | (git-refresh-status)))))) |
| 44 | |
| 45 | (defun stgit-find-buffer (dir) |
| 46 | "Return the buffer displaying StGit patches for DIR, or nil if none." |
| 47 | (setq dir (file-name-as-directory dir)) |
| 48 | (let ((buffers (buffer-list))) |
| 49 | (while (and buffers |
| 50 | (not (with-current-buffer (car buffers) |
| 51 | (and (eq major-mode 'stgit-mode) |
| 52 | (string= default-directory dir))))) |
| 53 | (setq buffers (cdr buffers))) |
| 54 | (and buffers (car buffers)))) |
| 55 | |
| 56 | (defun switch-to-stgit-buffer (dir) |
| 57 | "Switch to a (possibly new) buffer displaying StGit patches for DIR." |
| 58 | (setq dir (file-name-as-directory dir)) |
| 59 | (let ((buffer (stgit-find-buffer dir))) |
| 60 | (switch-to-buffer (or buffer |
| 61 | (create-stgit-buffer dir))))) |
| 62 | |
| 63 | (defstruct (stgit-patch) |
| 64 | status name desc empty files-ewoc) |
| 65 | |
| 66 | (defun stgit-patch-pp (patch) |
| 67 | (let ((status (stgit-patch-status patch)) |
| 68 | (start (point)) |
| 69 | (name (stgit-patch-name patch))) |
| 70 | (case name |
| 71 | (:index (insert (propertize " Index" 'face 'italic))) |
| 72 | (:work (insert (propertize " Work tree" 'face 'italic))) |
| 73 | (t (insert (case status |
| 74 | ('applied "+") |
| 75 | ('top ">") |
| 76 | ('unapplied "-")) |
| 77 | (if (memq name stgit-marked-patches) |
| 78 | "*" " ") |
| 79 | (propertize (format "%-30s" |
| 80 | (symbol-name name)) |
| 81 | 'face (case status |
| 82 | ('applied 'stgit-applied-patch-face) |
| 83 | ('top 'stgit-top-patch-face) |
| 84 | ('unapplied 'stgit-unapplied-patch-face) |
| 85 | ('index nil) |
| 86 | ('work nil))) |
| 87 | " " |
| 88 | (if (stgit-patch-empty patch) "(empty) " "") |
| 89 | (propertize (or (stgit-patch-desc patch) "") |
| 90 | 'face 'stgit-description-face)))) |
| 91 | (put-text-property start (point) 'entry-type 'patch) |
| 92 | (when (memq name stgit-expanded-patches) |
| 93 | (stgit-insert-patch-files patch)) |
| 94 | (put-text-property start (point) 'patch-data patch))) |
| 95 | |
| 96 | (defun create-stgit-buffer (dir) |
| 97 | "Create a buffer for showing StGit patches. |
| 98 | Argument DIR is the repository path." |
| 99 | (let ((buf (create-file-buffer (concat dir "*stgit*"))) |
| 100 | (inhibit-read-only t)) |
| 101 | (with-current-buffer buf |
| 102 | (setq default-directory dir) |
| 103 | (stgit-mode) |
| 104 | (set (make-local-variable 'stgit-ewoc) |
| 105 | (ewoc-create #'stgit-patch-pp "Branch:\n" "--")) |
| 106 | (setq buffer-read-only t)) |
| 107 | buf)) |
| 108 | |
| 109 | (defmacro stgit-capture-output (name &rest body) |
| 110 | "Capture StGit output and, if there was any output, show it in a window |
| 111 | at the end. |
| 112 | Returns nil if there was no output." |
| 113 | (declare (debug ([&or stringp null] body)) |
| 114 | (indent 1)) |
| 115 | `(let ((output-buf (get-buffer-create ,(or name "*StGit output*"))) |
| 116 | (stgit-dir default-directory) |
| 117 | (inhibit-read-only t)) |
| 118 | (with-current-buffer output-buf |
| 119 | (erase-buffer) |
| 120 | (setq default-directory stgit-dir) |
| 121 | (setq buffer-read-only t)) |
| 122 | (let ((standard-output output-buf)) |
| 123 | ,@body) |
| 124 | (with-current-buffer output-buf |
| 125 | (set-buffer-modified-p nil) |
| 126 | (setq buffer-read-only t) |
| 127 | (if (< (point-min) (point-max)) |
| 128 | (display-buffer output-buf t))))) |
| 129 | |
| 130 | (defun stgit-make-run-args (args) |
| 131 | "Return a copy of ARGS with its elements converted to strings." |
| 132 | (mapcar (lambda (x) |
| 133 | ;; don't use (format "%s" ...) to limit type errors |
| 134 | (cond ((stringp x) x) |
| 135 | ((integerp x) (number-to-string x)) |
| 136 | ((symbolp x) (symbol-name x)) |
| 137 | (t |
| 138 | (error "Bad element in stgit-make-run-args args: %S" x)))) |
| 139 | args)) |
| 140 | |
| 141 | (defun stgit-run-silent (&rest args) |
| 142 | (setq args (stgit-make-run-args args)) |
| 143 | (apply 'call-process "stg" nil standard-output nil args)) |
| 144 | |
| 145 | (defun stgit-run (&rest args) |
| 146 | (setq args (stgit-make-run-args args)) |
| 147 | (let ((msgcmd (mapconcat #'identity args " "))) |
| 148 | (message "Running stg %s..." msgcmd) |
| 149 | (apply 'call-process "stg" nil standard-output nil args) |
| 150 | (message "Running stg %s...done" msgcmd))) |
| 151 | |
| 152 | (defun stgit-run-git (&rest args) |
| 153 | (setq args (stgit-make-run-args args)) |
| 154 | (let ((msgcmd (mapconcat #'identity args " "))) |
| 155 | (message "Running git %s..." msgcmd) |
| 156 | (apply 'call-process "git" nil standard-output nil args) |
| 157 | (message "Running git %s...done" msgcmd))) |
| 158 | |
| 159 | (defun stgit-run-git-silent (&rest args) |
| 160 | (setq args (stgit-make-run-args args)) |
| 161 | (apply 'call-process "git" nil standard-output nil args)) |
| 162 | |
| 163 | (defun stgit-index-empty-p () |
| 164 | "Returns non-nil if the index contains no changes from HEAD." |
| 165 | (zerop (stgit-run-git-silent "diff-index" "--cached" "--quiet" "HEAD"))) |
| 166 | |
| 167 | (defvar stgit-index-node) |
| 168 | (defvar stgit-worktree-node) |
| 169 | |
| 170 | (defun stgit-refresh-index () |
| 171 | (when stgit-index-node |
| 172 | (ewoc-invalidate (car stgit-index-node) (cdr stgit-index-node)))) |
| 173 | |
| 174 | (defun stgit-refresh-worktree () |
| 175 | (when stgit-worktree-node |
| 176 | (ewoc-invalidate (car stgit-worktree-node) (cdr stgit-worktree-node)))) |
| 177 | |
| 178 | (defun stgit-run-series (ewoc) |
| 179 | (let ((first-line t)) |
| 180 | (with-temp-buffer |
| 181 | (let ((exit-status (stgit-run-silent "series" "--description" "--empty"))) |
| 182 | (goto-char (point-min)) |
| 183 | (if (not (zerop exit-status)) |
| 184 | (cond ((looking-at "stg series: \\(.*\\)") |
| 185 | (ewoc-set-hf ewoc (car (ewoc-get-hf ewoc)) |
| 186 | "-- not initialized (run M-x stgit-init)")) |
| 187 | ((looking-at ".*") |
| 188 | (error "Error running stg: %s" |
| 189 | (match-string 0)))) |
| 190 | (while (not (eobp)) |
| 191 | (unless (looking-at |
| 192 | "\\([0 ]\\)\\([>+-]\\)\\( \\)\\([^ ]+\\) *[|#] \\(.*\\)") |
| 193 | (error "Syntax error in output from stg series")) |
| 194 | (let* ((state-str (match-string 2)) |
| 195 | (state (cond ((string= state-str ">") 'top) |
| 196 | ((string= state-str "+") 'applied) |
| 197 | ((string= state-str "-") 'unapplied)))) |
| 198 | (ewoc-enter-last ewoc |
| 199 | (make-stgit-patch |
| 200 | :status state |
| 201 | :name (intern (match-string 4)) |
| 202 | :desc (match-string 5) |
| 203 | :empty (string= (match-string 1) "0")))) |
| 204 | (setq first-line nil) |
| 205 | (forward-line 1))))) |
| 206 | (if stgit-show-worktree |
| 207 | (setq stgit-index-node (cons ewoc (ewoc-enter-last ewoc |
| 208 | (make-stgit-patch |
| 209 | :status 'index |
| 210 | :name :index |
| 211 | :desc nil |
| 212 | :empty nil))) |
| 213 | stgit-worktree-node (cons ewoc (ewoc-enter-last ewoc |
| 214 | (make-stgit-patch |
| 215 | :status 'work |
| 216 | :name :work |
| 217 | :desc nil |
| 218 | :empty nil)))) |
| 219 | (setq stgit-worktree-node nil)))) |
| 220 | |
| 221 | |
| 222 | (defun stgit-reload () |
| 223 | "Update the contents of the StGit buffer." |
| 224 | (interactive) |
| 225 | (let ((inhibit-read-only t) |
| 226 | (curline (line-number-at-pos)) |
| 227 | (curpatch (stgit-patch-name-at-point))) |
| 228 | (ewoc-filter stgit-ewoc #'(lambda (x) nil)) |
| 229 | (ewoc-set-hf stgit-ewoc |
| 230 | (concat "Branch: " |
| 231 | (propertize |
| 232 | (with-temp-buffer |
| 233 | (stgit-run-silent "branch") |
| 234 | (buffer-substring (point-min) (1- (point-max)))) |
| 235 | 'face 'stgit-branch-name-face) |
| 236 | "\n") |
| 237 | (if stgit-show-worktree |
| 238 | "--" |
| 239 | (propertize |
| 240 | (substitute-command-keys "--\n\"\\[stgit-toggle-worktree]\"\ |
| 241 | shows the working tree\n") |
| 242 | 'face 'stgit-description-face))) |
| 243 | (stgit-run-series stgit-ewoc) |
| 244 | (if curpatch |
| 245 | (stgit-goto-patch curpatch) |
| 246 | (goto-line curline))) |
| 247 | (stgit-refresh-git-status)) |
| 248 | |
| 249 | (defgroup stgit nil |
| 250 | "A user interface for the StGit patch maintenance tool." |
| 251 | :group 'tools) |
| 252 | |
| 253 | (defface stgit-description-face |
| 254 | '((((background dark)) (:foreground "tan")) |
| 255 | (((background light)) (:foreground "dark red"))) |
| 256 | "The face used for StGit descriptions" |
| 257 | :group 'stgit) |
| 258 | |
| 259 | (defface stgit-branch-name-face |
| 260 | '((t :inherit bold)) |
| 261 | "The face used for the StGit branch name" |
| 262 | :group 'stgit) |
| 263 | |
| 264 | (defface stgit-top-patch-face |
| 265 | '((((background dark)) (:weight bold :foreground "yellow")) |
| 266 | (((background light)) (:weight bold :foreground "purple")) |
| 267 | (t (:weight bold))) |
| 268 | "The face used for the top patch names" |
| 269 | :group 'stgit) |
| 270 | |
| 271 | (defface stgit-applied-patch-face |
| 272 | '((((background dark)) (:foreground "light yellow")) |
| 273 | (((background light)) (:foreground "purple")) |
| 274 | (t ())) |
| 275 | "The face used for applied patch names" |
| 276 | :group 'stgit) |
| 277 | |
| 278 | (defface stgit-unapplied-patch-face |
| 279 | '((((background dark)) (:foreground "gray80")) |
| 280 | (((background light)) (:foreground "orchid")) |
| 281 | (t ())) |
| 282 | "The face used for unapplied patch names" |
| 283 | :group 'stgit) |
| 284 | |
| 285 | (defface stgit-modified-file-face |
| 286 | '((((class color) (background light)) (:foreground "purple")) |
| 287 | (((class color) (background dark)) (:foreground "salmon"))) |
| 288 | "StGit mode face used for modified file status" |
| 289 | :group 'stgit) |
| 290 | |
| 291 | (defface stgit-unmerged-file-face |
| 292 | '((((class color) (background light)) (:foreground "red" :bold t)) |
| 293 | (((class color) (background dark)) (:foreground "red" :bold t))) |
| 294 | "StGit mode face used for unmerged file status" |
| 295 | :group 'stgit) |
| 296 | |
| 297 | (defface stgit-unknown-file-face |
| 298 | '((((class color) (background light)) (:foreground "goldenrod" :bold t)) |
| 299 | (((class color) (background dark)) (:foreground "goldenrod" :bold t))) |
| 300 | "StGit mode face used for unknown file status" |
| 301 | :group 'stgit) |
| 302 | |
| 303 | (defface stgit-file-permission-face |
| 304 | '((((class color) (background light)) (:foreground "green" :bold t)) |
| 305 | (((class color) (background dark)) (:foreground "green" :bold t))) |
| 306 | "StGit mode face used for permission changes." |
| 307 | :group 'stgit) |
| 308 | |
| 309 | (defcustom stgit-expand-find-copies-harder |
| 310 | nil |
| 311 | "Try harder to find copied files when listing patches. |
| 312 | |
| 313 | When not nil, runs git diff-tree with the --find-copies-harder |
| 314 | flag, which reduces performance." |
| 315 | :type 'boolean |
| 316 | :group 'stgit) |
| 317 | |
| 318 | (defconst stgit-file-status-code-strings |
| 319 | (mapcar (lambda (arg) |
| 320 | (cons (car arg) |
| 321 | (propertize (cadr arg) 'face (car (cddr arg))))) |
| 322 | '((add "Added" stgit-modified-file-face) |
| 323 | (copy "Copied" stgit-modified-file-face) |
| 324 | (delete "Deleted" stgit-modified-file-face) |
| 325 | (modify "Modified" stgit-modified-file-face) |
| 326 | (rename "Renamed" stgit-modified-file-face) |
| 327 | (mode-change "Mode change" stgit-modified-file-face) |
| 328 | (unmerged "Unmerged" stgit-unmerged-file-face) |
| 329 | (unknown "Unknown" stgit-unknown-file-face))) |
| 330 | "Alist of code symbols to description strings") |
| 331 | |
| 332 | (defun stgit-file-status-code-as-string (file) |
| 333 | "Return stgit status code for FILE as a string" |
| 334 | (let* ((code (assq (stgit-file-status file) |
| 335 | stgit-file-status-code-strings)) |
| 336 | (score (stgit-file-cr-score file))) |
| 337 | (when code |
| 338 | (format "%-11s " |
| 339 | (if (and score (/= score 100)) |
| 340 | (format "%s %s" (cdr code) |
| 341 | (propertize (format "%d%%" score) |
| 342 | 'face 'stgit-description-face)) |
| 343 | (cdr code)))))) |
| 344 | |
| 345 | (defun stgit-file-status-code (str &optional score) |
| 346 | "Return stgit status code from git status string" |
| 347 | (let ((code (assoc str '(("A" . add) |
| 348 | ("C" . copy) |
| 349 | ("D" . delete) |
| 350 | ("M" . modify) |
| 351 | ("R" . rename) |
| 352 | ("T" . mode-change) |
| 353 | ("U" . unmerged) |
| 354 | ("X" . unknown))))) |
| 355 | (setq code (if code (cdr code) 'unknown)) |
| 356 | (when (stringp score) |
| 357 | (if (> (length score) 0) |
| 358 | (setq score (string-to-number score)) |
| 359 | (setq score nil))) |
| 360 | (if score (cons code score) code))) |
| 361 | |
| 362 | (defconst stgit-file-type-strings |
| 363 | '((#o100 . "file") |
| 364 | (#o120 . "symlink") |
| 365 | (#o160 . "subproject")) |
| 366 | "Alist of names of file types") |
| 367 | |
| 368 | (defun stgit-file-type-string (type) |
| 369 | "Return string describing file type TYPE (the high bits of file permission). |
| 370 | Cf. `stgit-file-type-strings' and `stgit-file-type-change-string'." |
| 371 | (let ((type-str (assoc type stgit-file-type-strings))) |
| 372 | (or (and type-str (cdr type-str)) |
| 373 | (format "unknown type %o" type)))) |
| 374 | |
| 375 | (defun stgit-file-type-change-string (old-perm new-perm) |
| 376 | "Return string describing file type change from OLD-PERM to NEW-PERM. |
| 377 | Cf. `stgit-file-type-string'." |
| 378 | (let ((old-type (lsh old-perm -9)) |
| 379 | (new-type (lsh new-perm -9))) |
| 380 | (cond ((= old-type new-type) "") |
| 381 | ((zerop new-type) "") |
| 382 | ((zerop old-type) |
| 383 | (if (= new-type #o100) |
| 384 | "" |
| 385 | (format " (%s)" (stgit-file-type-string new-type)))) |
| 386 | (t (format " (%s -> %s)" |
| 387 | (stgit-file-type-string old-type) |
| 388 | (stgit-file-type-string new-type)))))) |
| 389 | |
| 390 | (defun stgit-file-mode-change-string (old-perm new-perm) |
| 391 | "Return string describing file mode change from OLD-PERM to NEW-PERM. |
| 392 | Cf. `stgit-file-type-change-string'." |
| 393 | (setq old-perm (logand old-perm #o777) |
| 394 | new-perm (logand new-perm #o777)) |
| 395 | (if (or (= old-perm new-perm) |
| 396 | (zerop old-perm) |
| 397 | (zerop new-perm)) |
| 398 | "" |
| 399 | (let* ((modified (logxor old-perm new-perm)) |
| 400 | (not-x-modified (logand (logxor old-perm new-perm) #o666))) |
| 401 | (cond ((zerop modified) "") |
| 402 | ((and (zerop not-x-modified) |
| 403 | (or (and (eq #o111 (logand old-perm #o111)) |
| 404 | (propertize "-x" 'face 'stgit-file-permission-face)) |
| 405 | (and (eq #o111 (logand new-perm #o111)) |
| 406 | (propertize "+x" 'face |
| 407 | 'stgit-file-permission-face))))) |
| 408 | (t (concat (propertize (format "%o" old-perm) |
| 409 | 'face 'stgit-file-permission-face) |
| 410 | (propertize " -> " |
| 411 | 'face 'stgit-description-face) |
| 412 | (propertize (format "%o" new-perm) |
| 413 | 'face 'stgit-file-permission-face))))))) |
| 414 | |
| 415 | (defstruct (stgit-file) |
| 416 | old-perm new-perm copy-or-rename cr-score cr-from cr-to status file) |
| 417 | |
| 418 | (defun stgit-file-pp (file) |
| 419 | (let ((status (stgit-file-status file)) |
| 420 | (name (if (stgit-file-copy-or-rename file) |
| 421 | (concat (stgit-file-cr-from file) |
| 422 | (propertize " -> " |
| 423 | 'face 'stgit-description-face) |
| 424 | (stgit-file-cr-to file)) |
| 425 | (stgit-file-file file))) |
| 426 | (mode-change (stgit-file-mode-change-string |
| 427 | (stgit-file-old-perm file) |
| 428 | (stgit-file-new-perm file))) |
| 429 | (start (point))) |
| 430 | (insert (format " %-12s%1s%s%s\n" |
| 431 | (stgit-file-status-code-as-string file) |
| 432 | mode-change |
| 433 | name |
| 434 | (propertize (stgit-file-type-change-string |
| 435 | (stgit-file-old-perm file) |
| 436 | (stgit-file-new-perm file)) |
| 437 | 'face 'stgit-description-face))) |
| 438 | (add-text-properties start (point) |
| 439 | (list 'entry-type 'file |
| 440 | 'file-data file)))) |
| 441 | |
| 442 | (defun stgit-insert-patch-files (patch) |
| 443 | "Expand (show modification of) the patch PATCH after the line |
| 444 | at point." |
| 445 | (let* ((patchsym (stgit-patch-name patch)) |
| 446 | (end (progn (insert "#") (prog1 (point-marker) (forward-char -1)))) |
| 447 | (args (list "-z" (if stgit-expand-find-copies-harder |
| 448 | "--find-copies-harder" |
| 449 | "-C"))) |
| 450 | (ewoc (ewoc-create #'stgit-file-pp nil nil t))) |
| 451 | (setf (stgit-patch-files-ewoc patch) ewoc) |
| 452 | (with-temp-buffer |
| 453 | (apply 'stgit-run-git |
| 454 | (cond ((eq patchsym :work) |
| 455 | `("diff-files" ,@args)) |
| 456 | ((eq patchsym :index) |
| 457 | `("diff-index" ,@args "--cached" "HEAD")) |
| 458 | (t |
| 459 | `("diff-tree" ,@args "-r" ,(stgit-id patchsym))))) |
| 460 | (goto-char (point-min)) |
| 461 | (unless (or (eobp) (memq patchsym '(:work :index))) |
| 462 | (forward-char 41)) |
| 463 | (while (looking-at ":\\([0-7]+\\) \\([0-7]+\\) [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} ") |
| 464 | (let ((old-perm (string-to-number (match-string 1) 8)) |
| 465 | (new-perm (string-to-number (match-string 2) 8))) |
| 466 | (goto-char (match-end 0)) |
| 467 | (let ((file |
| 468 | (cond ((looking-at |
| 469 | "\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\0") |
| 470 | (make-stgit-file |
| 471 | :old-perm old-perm |
| 472 | :new-perm new-perm |
| 473 | :copy-or-rename t |
| 474 | :cr-score (string-to-number (match-string 2)) |
| 475 | :cr-from (match-string 3) |
| 476 | :cr-to (match-string 4) |
| 477 | :status (stgit-file-status-code (match-string 1)) |
| 478 | :file (match-string 3))) |
| 479 | ((looking-at "\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\0") |
| 480 | (make-stgit-file |
| 481 | :old-perm old-perm |
| 482 | :new-perm new-perm |
| 483 | :copy-or-rename nil |
| 484 | :cr-score nil |
| 485 | :cr-from nil |
| 486 | :cr-to nil |
| 487 | :status (stgit-file-status-code (match-string 1)) |
| 488 | :file (match-string 2)))))) |
| 489 | (ewoc-enter-last ewoc file)) |
| 490 | (goto-char (match-end 0)))) |
| 491 | (unless (ewoc-nth ewoc 0) |
| 492 | (ewoc-set-hf ewoc "" (propertize " <no files>\n" |
| 493 | 'face 'stgit-description-face)))) |
| 494 | (goto-char end) |
| 495 | (delete-char -2))) |
| 496 | |
| 497 | (defun stgit-select-file () |
| 498 | (let ((filename (expand-file-name |
| 499 | (stgit-file-file (stgit-patched-file-at-point))))) |
| 500 | (unless (file-exists-p filename) |
| 501 | (error "File does not exist")) |
| 502 | (find-file filename))) |
| 503 | |
| 504 | (defun stgit-select-patch () |
| 505 | (let ((patchname (stgit-patch-name-at-point))) |
| 506 | (if (memq patchname stgit-expanded-patches) |
| 507 | (setq stgit-expanded-patches (delq patchname stgit-expanded-patches)) |
| 508 | (setq stgit-expanded-patches (cons patchname stgit-expanded-patches))) |
| 509 | (ewoc-invalidate stgit-ewoc (ewoc-locate stgit-ewoc))) |
| 510 | (move-to-column (stgit-goal-column))) |
| 511 | |
| 512 | (defun stgit-select () |
| 513 | "Expand or collapse the current entry" |
| 514 | (interactive) |
| 515 | (case (get-text-property (point) 'entry-type) |
| 516 | ('patch |
| 517 | (stgit-select-patch)) |
| 518 | ('file |
| 519 | (stgit-select-file)) |
| 520 | (t |
| 521 | (error "No patch or file on line")))) |
| 522 | |
| 523 | (defun stgit-find-file-other-window () |
| 524 | "Open file at point in other window" |
| 525 | (interactive) |
| 526 | (let ((patched-file (stgit-patched-file-at-point))) |
| 527 | (unless patched-file |
| 528 | (error "No file on the current line")) |
| 529 | (let ((filename (expand-file-name (stgit-file-file patched-file)))) |
| 530 | (unless (file-exists-p filename) |
| 531 | (error "File does not exist")) |
| 532 | (find-file-other-window filename)))) |
| 533 | |
| 534 | (defun stgit-quit () |
| 535 | "Hide the stgit buffer." |
| 536 | (interactive) |
| 537 | (bury-buffer)) |
| 538 | |
| 539 | (defun stgit-git-status () |
| 540 | "Show status using `git-status'." |
| 541 | (interactive) |
| 542 | (unless (fboundp 'git-status) |
| 543 | (error "The stgit-git-status command requires git-status")) |
| 544 | (let ((dir default-directory)) |
| 545 | (save-selected-window |
| 546 | (pop-to-buffer nil) |
| 547 | (git-status dir)))) |
| 548 | |
| 549 | (defun stgit-goal-column () |
| 550 | "Return goal column for the current line" |
| 551 | (case (get-text-property (point) 'entry-type) |
| 552 | ('patch 2) |
| 553 | ('file 4) |
| 554 | (t 0))) |
| 555 | |
| 556 | (defun stgit-next-line (&optional arg) |
| 557 | "Move cursor vertically down ARG lines" |
| 558 | (interactive "p") |
| 559 | (next-line arg) |
| 560 | (move-to-column (stgit-goal-column))) |
| 561 | |
| 562 | (defun stgit-previous-line (&optional arg) |
| 563 | "Move cursor vertically up ARG lines" |
| 564 | (interactive "p") |
| 565 | (previous-line arg) |
| 566 | (move-to-column (stgit-goal-column))) |
| 567 | |
| 568 | (defun stgit-next-patch (&optional arg) |
| 569 | "Move cursor down ARG patches." |
| 570 | (interactive "p") |
| 571 | (ewoc-goto-next stgit-ewoc (or arg 1)) |
| 572 | (move-to-column goal-column)) |
| 573 | |
| 574 | (defun stgit-previous-patch (&optional arg) |
| 575 | "Move cursor up ARG patches." |
| 576 | (interactive "p") |
| 577 | (ewoc-goto-prev stgit-ewoc (or arg 1)) |
| 578 | (move-to-column goal-column)) |
| 579 | |
| 580 | (defvar stgit-mode-hook nil |
| 581 | "Run after `stgit-mode' is setup.") |
| 582 | |
| 583 | (defvar stgit-mode-map nil |
| 584 | "Keymap for StGit major mode.") |
| 585 | |
| 586 | (unless stgit-mode-map |
| 587 | (let ((toggle-map (make-keymap))) |
| 588 | (suppress-keymap toggle-map) |
| 589 | (mapc (lambda (arg) (define-key toggle-map (car arg) (cdr arg))) |
| 590 | '(("t" . stgit-toggle-worktree))) |
| 591 | (setq stgit-mode-map (make-keymap)) |
| 592 | (suppress-keymap stgit-mode-map) |
| 593 | (mapc (lambda (arg) (define-key stgit-mode-map (car arg) (cdr arg))) |
| 594 | `((" " . stgit-mark) |
| 595 | ("m" . stgit-mark) |
| 596 | ("\d" . stgit-unmark-up) |
| 597 | ("u" . stgit-unmark-down) |
| 598 | ("?" . stgit-help) |
| 599 | ("h" . stgit-help) |
| 600 | ("\C-p" . stgit-previous-line) |
| 601 | ("\C-n" . stgit-next-line) |
| 602 | ([up] . stgit-previous-line) |
| 603 | ([down] . stgit-next-line) |
| 604 | ("p" . stgit-previous-patch) |
| 605 | ("n" . stgit-next-patch) |
| 606 | ("\M-{" . stgit-previous-patch) |
| 607 | ("\M-}" . stgit-next-patch) |
| 608 | ("s" . stgit-git-status) |
| 609 | ("g" . stgit-reload) |
| 610 | ("r" . stgit-refresh) |
| 611 | ("\C-c\C-r" . stgit-rename) |
| 612 | ("e" . stgit-edit) |
| 613 | ("M" . stgit-move-patches) |
| 614 | ("S" . stgit-squash) |
| 615 | ("N" . stgit-new) |
| 616 | ("R" . stgit-repair) |
| 617 | ("\C-c\C-c" . stgit-commit) |
| 618 | ("\C-c\C-u" . stgit-uncommit) |
| 619 | ("U" . stgit-revert-file) |
| 620 | ("\r" . stgit-select) |
| 621 | ("o" . stgit-find-file-other-window) |
| 622 | ("i" . stgit-file-toggle-index) |
| 623 | (">" . stgit-push-next) |
| 624 | ("<" . stgit-pop-next) |
| 625 | ("P" . stgit-push-or-pop) |
| 626 | ("G" . stgit-goto) |
| 627 | ("=" . stgit-show) |
| 628 | ("D" . stgit-delete) |
| 629 | ([(control ?/)] . stgit-undo) |
| 630 | ("\C-_" . stgit-undo) |
| 631 | ("B" . stgit-branch) |
| 632 | ("t" . ,toggle-map) |
| 633 | ("q" . stgit-quit))))) |
| 634 | |
| 635 | (defun stgit-mode () |
| 636 | "Major mode for interacting with StGit. |
| 637 | Commands: |
| 638 | \\{stgit-mode-map}" |
| 639 | (kill-all-local-variables) |
| 640 | (buffer-disable-undo) |
| 641 | (setq mode-name "StGit" |
| 642 | major-mode 'stgit-mode |
| 643 | goal-column 2) |
| 644 | (use-local-map stgit-mode-map) |
| 645 | (set (make-local-variable 'list-buffers-directory) default-directory) |
| 646 | (set (make-local-variable 'stgit-marked-patches) nil) |
| 647 | (set (make-local-variable 'stgit-expanded-patches) nil) |
| 648 | (set (make-local-variable 'stgit-show-worktree) stgit-default-show-worktree) |
| 649 | (set (make-local-variable 'stgit-index-node) nil) |
| 650 | (set (make-local-variable 'stgit-worktree-node) nil) |
| 651 | (set-variable 'truncate-lines 't) |
| 652 | (add-hook 'after-save-hook 'stgit-update-saved-file) |
| 653 | (run-hooks 'stgit-mode-hook)) |
| 654 | |
| 655 | (defun stgit-update-saved-file () |
| 656 | (let* ((file (expand-file-name buffer-file-name)) |
| 657 | (dir (file-name-directory file)) |
| 658 | (gitdir (condition-case nil (git-get-top-dir dir) |
| 659 | (error nil))) |
| 660 | (buffer (and gitdir (stgit-find-buffer gitdir)))) |
| 661 | (when buffer |
| 662 | (with-current-buffer buffer |
| 663 | (stgit-refresh-worktree))))) |
| 664 | |
| 665 | (defun stgit-add-mark (patchsym) |
| 666 | "Mark the patch PATCHSYM." |
| 667 | (setq stgit-marked-patches (cons patchsym stgit-marked-patches))) |
| 668 | |
| 669 | (defun stgit-remove-mark (patchsym) |
| 670 | "Unmark the patch PATCHSYM." |
| 671 | (setq stgit-marked-patches (delq patchsym stgit-marked-patches))) |
| 672 | |
| 673 | (defun stgit-clear-marks () |
| 674 | "Unmark all patches." |
| 675 | (setq stgit-marked-patches '())) |
| 676 | |
| 677 | (defun stgit-patch-at-point (&optional cause-error) |
| 678 | (get-text-property (point) 'patch-data)) |
| 679 | |
| 680 | (defun stgit-patch-name-at-point (&optional cause-error) |
| 681 | "Return the patch name on the current line as a symbol. |
| 682 | If CAUSE-ERROR is not nil, signal an error if none found." |
| 683 | (let ((patch (stgit-patch-at-point))) |
| 684 | (cond (patch |
| 685 | (stgit-patch-name patch)) |
| 686 | (cause-error |
| 687 | (error "No patch on this line"))))) |
| 688 | |
| 689 | (defun stgit-patched-file-at-point () |
| 690 | (get-text-property (point) 'file-data)) |
| 691 | |
| 692 | (defun stgit-patches-marked-or-at-point () |
| 693 | "Return the symbols of the marked patches, or the patch on the current line." |
| 694 | (if stgit-marked-patches |
| 695 | stgit-marked-patches |
| 696 | (let ((patch (stgit-patch-name-at-point))) |
| 697 | (if patch |
| 698 | (list patch) |
| 699 | '())))) |
| 700 | |
| 701 | (defun stgit-goto-patch (patchsym) |
| 702 | "Move point to the line containing patch PATCHSYM. |
| 703 | If that patch cannot be found, do nothing." |
| 704 | (let ((node (ewoc-nth stgit-ewoc 0))) |
| 705 | (while (and node (not (eq (stgit-patch-name (ewoc-data node)) |
| 706 | patchsym))) |
| 707 | (setq node (ewoc-next stgit-ewoc node))) |
| 708 | (when node |
| 709 | (ewoc-goto-node stgit-ewoc node) |
| 710 | (move-to-column goal-column)))) |
| 711 | |
| 712 | (defun stgit-init () |
| 713 | "Run stg init." |
| 714 | (interactive) |
| 715 | (stgit-capture-output nil |
| 716 | (stgit-run "init")) |
| 717 | (stgit-reload)) |
| 718 | |
| 719 | (defun stgit-mark () |
| 720 | "Mark the patch under point." |
| 721 | (interactive) |
| 722 | (let* ((node (ewoc-locate stgit-ewoc)) |
| 723 | (patch (ewoc-data node))) |
| 724 | (stgit-add-mark (stgit-patch-name patch)) |
| 725 | (ewoc-invalidate stgit-ewoc node)) |
| 726 | (stgit-next-patch)) |
| 727 | |
| 728 | (defun stgit-unmark-up () |
| 729 | "Remove mark from the patch on the previous line." |
| 730 | (interactive) |
| 731 | (stgit-previous-patch) |
| 732 | (let* ((node (ewoc-locate stgit-ewoc)) |
| 733 | (patch (ewoc-data node))) |
| 734 | (stgit-remove-mark (stgit-patch-name patch)) |
| 735 | (ewoc-invalidate stgit-ewoc node)) |
| 736 | (move-to-column (stgit-goal-column))) |
| 737 | |
| 738 | (defun stgit-unmark-down () |
| 739 | "Remove mark from the patch on the current line." |
| 740 | (interactive) |
| 741 | (let* ((node (ewoc-locate stgit-ewoc)) |
| 742 | (patch (ewoc-data node))) |
| 743 | (stgit-remove-mark (stgit-patch-name patch)) |
| 744 | (ewoc-invalidate stgit-ewoc node)) |
| 745 | (stgit-next-patch)) |
| 746 | |
| 747 | (defun stgit-rename (name) |
| 748 | "Rename the patch under point to NAME." |
| 749 | (interactive (list (read-string "Patch name: " |
| 750 | (symbol-name (stgit-patch-name-at-point t))))) |
| 751 | (let ((old-patchsym (stgit-patch-name-at-point t))) |
| 752 | (stgit-capture-output nil |
| 753 | (stgit-run "rename" old-patchsym name)) |
| 754 | (let ((name-sym (intern name))) |
| 755 | (when (memq old-patchsym stgit-expanded-patches) |
| 756 | (setq stgit-expanded-patches |
| 757 | (cons name-sym (delq old-patchsym stgit-expanded-patches)))) |
| 758 | (when (memq old-patchsym stgit-marked-patches) |
| 759 | (setq stgit-marked-patches |
| 760 | (cons name-sym (delq old-patchsym stgit-marked-patches)))) |
| 761 | (stgit-reload) |
| 762 | (stgit-goto-patch name-sym)))) |
| 763 | |
| 764 | (defun stgit-repair () |
| 765 | "Run stg repair." |
| 766 | (interactive) |
| 767 | (stgit-capture-output nil |
| 768 | (stgit-run "repair")) |
| 769 | (stgit-reload)) |
| 770 | |
| 771 | (defun stgit-available-branches () |
| 772 | "Returns a list of the available stg branches" |
| 773 | (let ((output (with-output-to-string |
| 774 | (stgit-run "branch" "--list"))) |
| 775 | (start 0) |
| 776 | result) |
| 777 | (while (string-match "^>?\\s-+s\\s-+\\(\\S-+\\)" output start) |
| 778 | (setq result (cons (match-string 1 output) result)) |
| 779 | (setq start (match-end 0))) |
| 780 | result)) |
| 781 | |
| 782 | (defun stgit-branch (branch) |
| 783 | "Switch to branch BRANCH." |
| 784 | (interactive (list (completing-read "Switch to branch: " |
| 785 | (stgit-available-branches)))) |
| 786 | (stgit-capture-output nil (stgit-run "branch" "--" branch)) |
| 787 | (stgit-reload)) |
| 788 | |
| 789 | (defun stgit-commit (count) |
| 790 | "Run stg commit on COUNT commits. |
| 791 | Interactively, the prefix argument is used as COUNT." |
| 792 | (interactive "p") |
| 793 | (stgit-capture-output nil (stgit-run "commit" "-n" count)) |
| 794 | (stgit-reload)) |
| 795 | |
| 796 | (defun stgit-revert-file () |
| 797 | "Revert the file at point, which must be in the index or the |
| 798 | working tree." |
| 799 | (interactive) |
| 800 | (let* ((patched-file (or (stgit-patched-file-at-point) |
| 801 | (error "No file on the current line"))) |
| 802 | (patch-name (stgit-patch-name-at-point)) |
| 803 | (file-status (stgit-file-status patched-file)) |
| 804 | (rm-file (cond ((stgit-file-copy-or-rename patched-file) |
| 805 | (stgit-file-cr-to patched-file)) |
| 806 | ((eq file-status 'add) |
| 807 | (stgit-file-file patched-file)))) |
| 808 | (co-file (cond ((eq file-status 'rename) |
| 809 | (stgit-file-cr-from patched-file)) |
| 810 | ((not (memq file-status '(copy add))) |
| 811 | (stgit-file-file patched-file))))) |
| 812 | |
| 813 | (unless (memq patch-name '(:work :index)) |
| 814 | (error "No index or working tree file on this line")) |
| 815 | |
| 816 | (let ((nfiles (+ (if rm-file 1 0) (if co-file 1 0)))) |
| 817 | (when (yes-or-no-p (format "Revert %d file%s? " |
| 818 | nfiles |
| 819 | (if (= nfiles 1) "" "s"))) |
| 820 | (stgit-capture-output nil |
| 821 | (when rm-file |
| 822 | (stgit-run-git "rm" "-f" "-q" "--" rm-file)) |
| 823 | (when co-file |
| 824 | (stgit-run-git "checkout" "HEAD" co-file))) |
| 825 | (stgit-reload))))) |
| 826 | |
| 827 | (defun stgit-uncommit (count) |
| 828 | "Run stg uncommit on COUNT commits. |
| 829 | Interactively, the prefix argument is used as COUNT." |
| 830 | (interactive "p") |
| 831 | (stgit-capture-output nil (stgit-run "uncommit" "-n" count)) |
| 832 | (stgit-reload)) |
| 833 | |
| 834 | (defun stgit-push-next (npatches) |
| 835 | "Push the first unapplied patch. |
| 836 | With numeric prefix argument, push that many patches." |
| 837 | (interactive "p") |
| 838 | (stgit-capture-output nil (stgit-run "push" "-n" npatches)) |
| 839 | (stgit-reload) |
| 840 | (stgit-refresh-git-status)) |
| 841 | |
| 842 | (defun stgit-pop-next (npatches) |
| 843 | "Pop the topmost applied patch. |
| 844 | With numeric prefix argument, pop that many patches." |
| 845 | (interactive "p") |
| 846 | (stgit-capture-output nil (stgit-run "pop" "-n" npatches)) |
| 847 | (stgit-reload) |
| 848 | (stgit-refresh-git-status)) |
| 849 | |
| 850 | (defun stgit-applied-at-point () |
| 851 | "Is the patch on the current line applied?" |
| 852 | (save-excursion |
| 853 | (beginning-of-line) |
| 854 | (looking-at "[>+]"))) |
| 855 | |
| 856 | (defun stgit-push-or-pop () |
| 857 | "Push or pop the patch on the current line." |
| 858 | (interactive) |
| 859 | (let ((patchsym (stgit-patch-name-at-point t)) |
| 860 | (applied (stgit-applied-at-point))) |
| 861 | (stgit-capture-output nil |
| 862 | (stgit-run (if applied "pop" "push") patchsym)) |
| 863 | (stgit-reload))) |
| 864 | |
| 865 | (defun stgit-goto () |
| 866 | "Go to the patch on the current line." |
| 867 | (interactive) |
| 868 | (let ((patchsym (stgit-patch-name-at-point t))) |
| 869 | (stgit-capture-output nil |
| 870 | (stgit-run "goto" patchsym)) |
| 871 | (stgit-reload))) |
| 872 | |
| 873 | (defun stgit-id (patchsym) |
| 874 | "Return the git commit id for PATCHSYM. |
| 875 | If PATCHSYM is a keyword, returns PATCHSYM unmodified." |
| 876 | (if (keywordp patchsym) |
| 877 | patchsym |
| 878 | (let ((result (with-output-to-string |
| 879 | (stgit-run-silent "id" patchsym)))) |
| 880 | (unless (string-match "^\\([0-9A-Fa-f]\\{40\\}\\)$" result) |
| 881 | (error "Cannot find commit id for %s" patchsym)) |
| 882 | (match-string 1 result)))) |
| 883 | |
| 884 | (defun stgit-show () |
| 885 | "Show the patch on the current line." |
| 886 | (interactive) |
| 887 | (stgit-capture-output "*StGit patch*" |
| 888 | (case (get-text-property (point) 'entry-type) |
| 889 | ('file |
| 890 | (let* ((patched-file (stgit-patched-file-at-point)) |
| 891 | (patch-name (stgit-patch-name-at-point)) |
| 892 | (patch-id (stgit-id patch-name)) |
| 893 | (args (append (and (stgit-file-cr-from patched-file) |
| 894 | (if stgit-expand-find-copies-harder |
| 895 | '("--find-copies-harder") |
| 896 | '("-C"))) |
| 897 | (cond ((eq patch-id :index) |
| 898 | '("--cached")) |
| 899 | ((eq patch-id :work) |
| 900 | nil) |
| 901 | (t |
| 902 | (list (concat patch-id "^") patch-id))) |
| 903 | '("--") |
| 904 | (if (stgit-file-copy-or-rename patched-file) |
| 905 | (list (stgit-file-cr-from patched-file) |
| 906 | (stgit-file-cr-to patched-file)) |
| 907 | (list (stgit-file-file patched-file)))))) |
| 908 | (apply 'stgit-run-git "diff" args))) |
| 909 | ('patch |
| 910 | (stgit-run "show" "-O" "--patch-with-stat" "-O" "-M" |
| 911 | (stgit-patch-name-at-point))) |
| 912 | (t |
| 913 | (error "No patch or file at point"))) |
| 914 | (with-current-buffer standard-output |
| 915 | (goto-char (point-min)) |
| 916 | (diff-mode)))) |
| 917 | |
| 918 | (defun stgit-move-change-to-index (file) |
| 919 | "Copies the workspace state of FILE to index, using git add or git rm" |
| 920 | (let ((op (if (or (file-exists-p file) (file-symlink-p file)) |
| 921 | '("add") '("rm" "-q")))) |
| 922 | (stgit-capture-output "*git output*" |
| 923 | (apply 'stgit-run-git (append op '("--") (list file)))))) |
| 924 | |
| 925 | (defun stgit-remove-change-from-index (file) |
| 926 | "Unstages the change in FILE from the index" |
| 927 | (stgit-capture-output "*git output*" |
| 928 | (stgit-run-git "reset" "-q" "--" file))) |
| 929 | |
| 930 | (defun stgit-file-toggle-index () |
| 931 | "Move modified file in or out of the index." |
| 932 | (interactive) |
| 933 | (let ((patched-file (stgit-patched-file-at-point))) |
| 934 | (unless patched-file |
| 935 | (error "No file on the current line")) |
| 936 | (let ((patch-name (stgit-patch-name-at-point))) |
| 937 | (cond ((eq patch-name :work) |
| 938 | (stgit-move-change-to-index (stgit-file-file patched-file))) |
| 939 | ((eq patch-name :index) |
| 940 | (stgit-remove-change-from-index (stgit-file-file patched-file))) |
| 941 | (t |
| 942 | (error "Can only move files in the working tree to index"))))) |
| 943 | (stgit-refresh-worktree) |
| 944 | (stgit-refresh-index)) |
| 945 | |
| 946 | (defun stgit-edit () |
| 947 | "Edit the patch on the current line." |
| 948 | (interactive) |
| 949 | (let ((patchsym (stgit-patch-name-at-point t)) |
| 950 | (edit-buf (get-buffer-create "*StGit edit*")) |
| 951 | (dir default-directory)) |
| 952 | (log-edit 'stgit-confirm-edit t nil edit-buf) |
| 953 | (set (make-local-variable 'stgit-edit-patchsym) patchsym) |
| 954 | (setq default-directory dir) |
| 955 | (let ((standard-output edit-buf)) |
| 956 | (stgit-run-silent "edit" "--save-template=-" patchsym)))) |
| 957 | |
| 958 | (defun stgit-confirm-edit () |
| 959 | (interactive) |
| 960 | (let ((file (make-temp-file "stgit-edit-"))) |
| 961 | (write-region (point-min) (point-max) file) |
| 962 | (stgit-capture-output nil |
| 963 | (stgit-run "edit" "-f" file stgit-edit-patchsym)) |
| 964 | (with-current-buffer log-edit-parent-buffer |
| 965 | (stgit-reload)))) |
| 966 | |
| 967 | (defun stgit-new (add-sign) |
| 968 | "Create a new patch. |
| 969 | With a prefix argument, include a \"Signed-off-by:\" line at the |
| 970 | end of the patch." |
| 971 | (interactive "P") |
| 972 | (let ((edit-buf (get-buffer-create "*StGit edit*")) |
| 973 | (dir default-directory)) |
| 974 | (log-edit 'stgit-confirm-new t nil edit-buf) |
| 975 | (setq default-directory dir) |
| 976 | (when add-sign |
| 977 | (save-excursion |
| 978 | (let ((standard-output (current-buffer))) |
| 979 | (stgit-run-silent "new" "--sign" "--save-template=-")))))) |
| 980 | |
| 981 | (defun stgit-confirm-new () |
| 982 | (interactive) |
| 983 | (let ((file (make-temp-file "stgit-edit-"))) |
| 984 | (write-region (point-min) (point-max) file) |
| 985 | (stgit-capture-output nil |
| 986 | (stgit-run "new" "-f" file)) |
| 987 | (with-current-buffer log-edit-parent-buffer |
| 988 | (stgit-reload)))) |
| 989 | |
| 990 | (defun stgit-create-patch-name (description) |
| 991 | "Create a patch name from a long description" |
| 992 | (let ((patch "")) |
| 993 | (while (> (length description) 0) |
| 994 | (cond ((string-match "\\`[a-zA-Z_-]+" description) |
| 995 | (setq patch (downcase (concat patch |
| 996 | (match-string 0 description)))) |
| 997 | (setq description (substring description (match-end 0)))) |
| 998 | ((string-match "\\` +" description) |
| 999 | (setq patch (concat patch "-")) |
| 1000 | (setq description (substring description (match-end 0)))) |
| 1001 | ((string-match "\\`[^a-zA-Z_-]+" description) |
| 1002 | (setq description (substring description (match-end 0)))))) |
| 1003 | (cond ((= (length patch) 0) |
| 1004 | "patch") |
| 1005 | ((> (length patch) 20) |
| 1006 | (substring patch 0 20)) |
| 1007 | (t patch)))) |
| 1008 | |
| 1009 | (defun stgit-delete (patchsyms &optional spill-p) |
| 1010 | "Delete the patches in PATCHSYMS. |
| 1011 | Interactively, delete the marked patches, or the patch at point. |
| 1012 | |
| 1013 | With a prefix argument, or SPILL-P, spill the patch contents to |
| 1014 | the work tree and index." |
| 1015 | (interactive (list (stgit-patches-marked-or-at-point) |
| 1016 | current-prefix-arg)) |
| 1017 | (unless patchsyms |
| 1018 | (error "No patches to delete")) |
| 1019 | (let ((npatches (length patchsyms))) |
| 1020 | (when (yes-or-no-p (format "Really delete %d patch%s%s? " |
| 1021 | npatches |
| 1022 | (if (= 1 npatches) "" "es") |
| 1023 | (if spill-p |
| 1024 | " (spilling contents to index)" |
| 1025 | ""))) |
| 1026 | (let ((args (if spill-p |
| 1027 | (cons "--spill" patchsyms) |
| 1028 | patchsyms))) |
| 1029 | (stgit-capture-output nil |
| 1030 | (apply 'stgit-run "delete" args)) |
| 1031 | (stgit-reload))))) |
| 1032 | |
| 1033 | (defun stgit-move-patches-target () |
| 1034 | "Return the patchsym indicating a target patch for |
| 1035 | `stgit-move-patches'. |
| 1036 | |
| 1037 | This is either the patch at point, or one of :top and :bottom, if |
| 1038 | the point is after or before the applied patches." |
| 1039 | |
| 1040 | (let ((patchsym (stgit-patch-name-at-point))) |
| 1041 | (cond (patchsym patchsym) |
| 1042 | ((save-excursion (re-search-backward "^>" nil t)) :top) |
| 1043 | (t :bottom)))) |
| 1044 | |
| 1045 | (defun stgit-sort-patches (patchsyms) |
| 1046 | "Returns the list of patches in PATCHSYMS sorted according to |
| 1047 | their position in the patch series, bottommost first. |
| 1048 | |
| 1049 | PATCHSYMS may not contain duplicate entries." |
| 1050 | (let (sorted-patchsyms |
| 1051 | (series (with-output-to-string |
| 1052 | (with-current-buffer standard-output |
| 1053 | (stgit-run-silent "series" "--noprefix")))) |
| 1054 | start) |
| 1055 | (while (string-match "^\\(.+\\)" series start) |
| 1056 | (let ((patchsym (intern (match-string 1 series)))) |
| 1057 | (when (memq patchsym patchsyms) |
| 1058 | (setq sorted-patchsyms (cons patchsym sorted-patchsyms)))) |
| 1059 | (setq start (match-end 0))) |
| 1060 | (setq sorted-patchsyms (nreverse sorted-patchsyms)) |
| 1061 | |
| 1062 | (unless (= (length patchsyms) (length sorted-patchsyms)) |
| 1063 | (error "Internal error")) |
| 1064 | |
| 1065 | sorted-patchsyms)) |
| 1066 | |
| 1067 | (defun stgit-move-patches (patchsyms target-patch) |
| 1068 | "Move the patches in PATCHSYMS to below TARGET-PATCH. |
| 1069 | If TARGET-PATCH is :bottom or :top, move the patches to the |
| 1070 | bottom or top of the stack, respectively. |
| 1071 | |
| 1072 | Interactively, move the marked patches to where the point is." |
| 1073 | (interactive (list stgit-marked-patches |
| 1074 | (stgit-move-patches-target))) |
| 1075 | (unless patchsyms |
| 1076 | (error "Need at least one patch to move")) |
| 1077 | |
| 1078 | (unless target-patch |
| 1079 | (error "Point not at a patch")) |
| 1080 | |
| 1081 | (if (eq target-patch :top) |
| 1082 | (stgit-capture-output nil |
| 1083 | (apply 'stgit-run "float" patchsyms)) |
| 1084 | |
| 1085 | ;; need to have patchsyms sorted by position in the stack |
| 1086 | (let ((sorted-patchsyms (stgit-sort-patches patchsyms))) |
| 1087 | (while sorted-patchsyms |
| 1088 | (setq sorted-patchsyms |
| 1089 | (and (stgit-capture-output nil |
| 1090 | (if (eq target-patch :bottom) |
| 1091 | (stgit-run "sink" "--" (car sorted-patchsyms)) |
| 1092 | (stgit-run "sink" "--to" target-patch "--" |
| 1093 | (car sorted-patchsyms)))) |
| 1094 | (cdr sorted-patchsyms)))))) |
| 1095 | (stgit-reload)) |
| 1096 | |
| 1097 | (defun stgit-squash (patchsyms) |
| 1098 | "Squash the patches in PATCHSYMS. |
| 1099 | Interactively, squash the marked patches. |
| 1100 | |
| 1101 | Unless there are any conflicts, the patches will be merged into |
| 1102 | one patch, which will occupy the same spot in the series as the |
| 1103 | deepest patch had before the squash." |
| 1104 | (interactive (list stgit-marked-patches)) |
| 1105 | (when (< (length patchsyms) 2) |
| 1106 | (error "Need at least two patches to squash")) |
| 1107 | (let ((stgit-buffer (current-buffer)) |
| 1108 | (edit-buf (get-buffer-create "*StGit edit*")) |
| 1109 | (dir default-directory) |
| 1110 | (sorted-patchsyms (stgit-sort-patches patchsyms))) |
| 1111 | (log-edit 'stgit-confirm-squash t nil edit-buf) |
| 1112 | (set (make-local-variable 'stgit-patchsyms) sorted-patchsyms) |
| 1113 | (setq default-directory dir) |
| 1114 | (let ((result (let ((standard-output edit-buf)) |
| 1115 | (apply 'stgit-run-silent "squash" |
| 1116 | "--save-template=-" sorted-patchsyms)))) |
| 1117 | |
| 1118 | ;; stg squash may have reordered the patches or caused conflicts |
| 1119 | (with-current-buffer stgit-buffer |
| 1120 | (stgit-reload)) |
| 1121 | |
| 1122 | (unless (eq 0 result) |
| 1123 | (fundamental-mode) |
| 1124 | (rename-buffer "*StGit error*") |
| 1125 | (resize-temp-buffer-window) |
| 1126 | (switch-to-buffer-other-window stgit-buffer) |
| 1127 | (error "stg squash failed"))))) |
| 1128 | |
| 1129 | (defun stgit-confirm-squash () |
| 1130 | (interactive) |
| 1131 | (let ((file (make-temp-file "stgit-edit-"))) |
| 1132 | (write-region (point-min) (point-max) file) |
| 1133 | (stgit-capture-output nil |
| 1134 | (apply 'stgit-run "squash" "-f" file stgit-patchsyms)) |
| 1135 | (with-current-buffer log-edit-parent-buffer |
| 1136 | (stgit-clear-marks) |
| 1137 | ;; Go to first marked patch and stay there |
| 1138 | (goto-char (point-min)) |
| 1139 | (re-search-forward (concat "^[>+-]\\*") nil t) |
| 1140 | (move-to-column goal-column) |
| 1141 | (let ((pos (point))) |
| 1142 | (stgit-reload) |
| 1143 | (goto-char pos))))) |
| 1144 | |
| 1145 | (defun stgit-help () |
| 1146 | "Display help for the StGit mode." |
| 1147 | (interactive) |
| 1148 | (describe-function 'stgit-mode)) |
| 1149 | |
| 1150 | (defun stgit-undo (&optional arg) |
| 1151 | "Run stg undo. |
| 1152 | With prefix argument, run it with the --hard flag." |
| 1153 | (interactive "P") |
| 1154 | (stgit-capture-output nil |
| 1155 | (if arg |
| 1156 | (stgit-run "undo" "--hard") |
| 1157 | (stgit-run "undo"))) |
| 1158 | (stgit-reload)) |
| 1159 | |
| 1160 | (defun stgit-refresh (&optional arg) |
| 1161 | "Run stg refresh. |
| 1162 | With prefix argument, refresh the marked patch or the patch under point." |
| 1163 | (interactive "P") |
| 1164 | (let ((patchargs (if arg |
| 1165 | (let ((patches (stgit-patches-marked-or-at-point))) |
| 1166 | (cond ((null patches) |
| 1167 | (error "No patch to update")) |
| 1168 | ((> (length patches) 1) |
| 1169 | (error "Too many patches selected")) |
| 1170 | (t |
| 1171 | (cons "-p" patches)))) |
| 1172 | nil))) |
| 1173 | (stgit-capture-output nil |
| 1174 | (apply 'stgit-run "refresh" patchargs)) |
| 1175 | (stgit-refresh-git-status)) |
| 1176 | (stgit-reload)) |
| 1177 | |
| 1178 | (defcustom stgit-default-show-worktree |
| 1179 | nil |
| 1180 | "Set to non-nil to by default show the working tree in a new stgit buffer. |
| 1181 | |
| 1182 | This value is used as the default value for `stgit-show-worktree'." |
| 1183 | :type 'boolean |
| 1184 | :group 'stgit) |
| 1185 | |
| 1186 | (defvar stgit-show-worktree nil |
| 1187 | "Show work tree and index in the stgit buffer. |
| 1188 | |
| 1189 | See `stgit-default-show-worktree' for its default value.") |
| 1190 | |
| 1191 | (defun stgit-toggle-worktree (&optional arg) |
| 1192 | "Toggle the visibility of the work tree. |
| 1193 | With arg, show the work tree if arg is positive. |
| 1194 | |
| 1195 | Its initial setting is controlled by `stgit-default-show-worktree'." |
| 1196 | (interactive) |
| 1197 | (setq stgit-show-worktree |
| 1198 | (if (numberp arg) |
| 1199 | (> arg 0) |
| 1200 | (not stgit-show-worktree))) |
| 1201 | (stgit-reload)) |
| 1202 | |
| 1203 | (provide 'stgit) |