+ "The face used for unapplied patch names"
+ :group 'stgit)
+
+(defface stgit-modified-file-face
+ '((((class color) (background light)) (:foreground "purple"))
+ (((class color) (background dark)) (:foreground "salmon")))
+ "StGit mode face used for modified file status"
+ :group 'stgit)
+
+(defface stgit-unmerged-file-face
+ '((((class color) (background light)) (:foreground "red" :bold t))
+ (((class color) (background dark)) (:foreground "red" :bold t)))
+ "StGit mode face used for unmerged file status"
+ :group 'stgit)
+
+(defface stgit-unknown-file-face
+ '((((class color) (background light)) (:foreground "goldenrod" :bold t))
+ (((class color) (background dark)) (:foreground "goldenrod" :bold t)))
+ "StGit mode face used for unknown file status"
+ :group 'stgit)
+
+(defcustom stgit-expand-find-copies-harder
+ nil
+ "Try harder to find copied files when listing patches.
+
+When not nil, runs git diff-tree with the --find-copies-harder
+flag, which reduces performance."
+ :type 'boolean
+ :group 'stgit)
+
+(defconst stgit-file-status-code-strings
+ (mapcar (lambda (arg)
+ (cons (car arg)
+ (format "%-12s"
+ (propertize (cadr arg) 'face (car (cddr arg))))))
+ '((add "Added" stgit-modified-file-face)
+ (copy "Copied" stgit-modified-file-face)
+ (delete "Deleted" stgit-modified-file-face)
+ (modify "Modified" stgit-modified-file-face)
+ (rename "Renamed" stgit-modified-file-face)
+ (mode-change "Mode changed" stgit-modified-file-face)
+ (unmerged "Unmerged" stgit-unmerged-file-face)
+ (unknown "Unknown" stgit-unknown-file-face)))
+ "Alist of code symbols to description strings")
+
+(defun stgit-file-status-code-as-string (code)
+ "Return stgit status code as string"
+ (let ((str (assq code stgit-file-status-code-strings)))
+ (and str (cdr str))))
+
+(defun stgit-file-status-code (str)
+ "Return stgit status code from git status string"
+ (let ((code (assoc str '(("A" . add)
+ ("C" . copy)
+ ("D" . delete)
+ ("M" . modify)
+ ("R" . rename)
+ ("T" . mode-change)
+ ("U" . unmerged)
+ ("X" . unknown)))))
+ (if code (cdr code) 'unknown)))
+
+(defun stgit-expand-patch (patchsym)
+ (save-excursion
+ (forward-line)
+ (let* ((start (point))
+ (result (with-output-to-string
+ (stgit-run-git "diff-tree" "-r" "-z"
+ (if stgit-expand-find-copies-harder
+ "--find-copies-harder"
+ "-C")
+ (stgit-id (symbol-name patchsym))))))
+ (let (mstart)
+ (while (string-match "\0:[0-7]+ [0-7]+ [0-9A-Fa-f]\\{40\\} [0-9A-Fa-f]\\{40\\} \\(\\([CR]\\)\\([0-9]*\\)\0\\([^\0]*\\)\0\\([^\0]*\\)\\|\\([ABD-QS-Z]\\)\0\\([^\0]*\\)\\)"
+ result mstart)
+ (let ((copy-or-rename (match-string 2 result))
+ (line-start (point))
+ properties)
+ (insert " ")
+ (if copy-or-rename
+ (let ((cr-score (match-string 3 result))
+ (cr-from-file (match-string 4 result))
+ (cr-to-file (match-string 5 result)))
+ (setq properties (list 'stgit-old-file cr-from-file
+ 'stgit-new-file cr-to-file))
+ (insert (stgit-file-status-code-as-string
+ (if (equal "C" copy-or-rename) 'copy 'rename))
+ cr-from-file
+ (propertize " -> " 'face 'stgit-description-face)
+ cr-to-file))
+ (let ((status (stgit-file-status-code (match-string 6 result)))
+ (file (match-string 7 result)))
+ (setq properties (list 'stgit-file file))
+ (insert (stgit-file-status-code-as-string status) file)))
+ (insert ?\n)
+ (add-text-properties line-start (point) properties))
+ (setq mstart (match-end 0))))
+ (when (= start (point))
+ (insert " <no files>\n"))
+ (put-text-property start (point) 'stgit-patchsym patchsym))))