Skip to content
This repository
branch: master
file 270 lines (221 sloc) 8.895 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
;;; magit-stgit.el --- StGit plug-in for Magit

;; Copyright (C) 2011-2014 The Magit Project Developers
;;
;; For a full list of contributors, see the AUTHORS.md file
;; at the top-level directory of this distribution and at
;; https://raw.github.com/magit/magit/master/AUTHORS.md

;; Author: Lluís Vilanova <vilanova@ac.upc.edu>
;; Keywords: vc tools
;; Package: magit-stgit
;; Package-Requires: ((cl-lib "0.3") (magit "1.3.0"))

;; Magit is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; Magit is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
;; License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Magit. If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This package provides very basic support for StGit.

;; When `magit-stgit-mode' is turned on the current patch series is
;; displayed in the status buffer. Additionally a few Emacs commands
;; are defined that wrap around StGit commands. These commands are
;; also available as "section actions".

;; If you are looking for full fledged StGit support in Emacs, then
;; have a look at `stgit.el' which is distributed with StGit.

;;; Code:

(require 'magit)

;;; Options
;;;; Variables

(defgroup magit-stgit nil
  "StGit support for Magit."
  :group 'magit-extensions)

(defcustom magit-stgit-executable "stg"
  "The name of the StGit executable."
  :group 'magit-stgit
  :type 'string)

(defcustom magit-stgit-show-patch-name t
  "Whether to prefix patch messages with the patch name, in patch series."
  :group 'magit-stgit
  :type 'boolean)

;;;; Faces

(defgroup magit-stgit-faces nil
  "Faces used by Magit-StGit."
  :group 'magit-stgit
  :group 'magit-faces)

(defface magit-stgit-patch
  '((t :inherit magit-log-sha1))
  "Face for name of a stgit patch."
  :group 'magit-stgit-faces)

(defface magit-stgit-current
  '((t :inherit magit-log-sha1))
  "Face for the current stgit patch."
  :group 'magit-stgit-faces)

(defface magit-stgit-applied
  '((t :inherit magit-cherry-equivalent))
  "Face for an applied stgit patch."
  :group 'magit-stgit-faces)

(defface magit-stgit-unapplied
  '((t :inherit magit-cherry-unmatched))
  "Face for an unapplied stgit patch."
  :group 'magit-stgit-faces)

(defface magit-stgit-empty
  '((t :inherit magit-diff-del))
  "Face for an empty stgit patch."
  :group 'magit-stgit-faces)

(defface magit-stgit-hidden
  '((t :inherit magit-diff-empty))
  "Face for an hidden stgit patch."
  :group 'magit-stgit-faces)

;;; Variables

(defvar magit-stgit-patch-buffer-name "*magit-stgit-patch*"
  "Name of buffer used to display a stgit patch.")

(defvar magit-stgit-patch-history nil
  "Input history for `magit-stgit-read-patch'.")

;;; Utilities

(defun magit-run-stgit (&rest args)
  (apply #'magit-call-process magit-stgit-executable args)
  (magit-refresh))

(defun magit-stgit-lines (&rest args)
  (with-temp-buffer
    (apply 'process-file magit-stgit-executable nil (list t nil) nil args)
    (split-string (buffer-string) "\n" 'omit-nulls)))

(defun magit-stgit-read-patch (prompt &optional require-match)
  (magit-completing-read prompt (magit-stgit-lines "series" "--noprefix")
                         nil require-match
                         nil 'magit-read-rev-history))

;;; Commands

;;;###autoload
(defun magit-stgit-refresh (&optional patch)
  "Refresh a StGit patch."
  (interactive
   (list (magit-stgit-read-patch "Refresh patch (default top)")))
  (if patch
      (magit-run-stgit "refresh" "-p" patch)
    (magit-run-stgit "refresh")))

;;;###autoload
(defun magit-stgit-repair ()
  "Repair StGit metadata if branch was modified with git commands.
In the case of Git commits these will be imported as new patches
into the series."
  (interactive)
  (message "Repairing series...")
  (magit-run-stgit "repair")
  (message "Repairing series...done"))

;;;###autoload
(defun magit-stgit-rebase ()
  "Rebase a StGit patch series."
  (interactive)
  (let ((remote (magit-get-current-remote))
        (branch (magit-get-current-branch)))
    (if (not (and remote branch))
        (user-error "Branch has no upstream")
      (when (y-or-n-p "Update remote first? ")
        (message "Updating remote...")
        (magit-run-git-async "remote" "update" remote)
        (message "Updating remote...done"))
      (magit-run-stgit "rebase" (format "remotes/%s/%s" remote branch)))))

;;;###autoload
(defun magit-stgit-discard (patch)
  "Discard a StGit patch."
  (interactive (list (magit-stgit-read-patch "Discard patch" t)))
  (when (yes-or-no-p (format "Discard patch `%s'? " patch))
    (magit-run-stgit "delete" patch)))

;;;###autoload
(defun magit-stgit-goto (patch)
  "Set PATCH as target of StGit push and pop operations."
  (magit-run-stgit "goto" patch))

;;;###autoload
(defun magit-stgit-show (patch)
  "Show diff of a StGit patch."
  (interactive (list (magit-stgit-read-patch "Show patch" t)))
  (magit-mode-setup magit-stgit-patch-buffer-name
                    #'pop-to-buffer
                    #'magit-commit-mode
                    #'magit-stgit-refresh-patch-buffer
                    patch))

(defun magit-stgit-refresh-patch-buffer (patch)
  (magit-cmd-insert-section (stgit-patch)
      #'magit-wash-commit
    magit-stgit-executable "show"
    "--diff-opts=--decorate=full"
    (and magit-show-diffstat "--diff-opts=--patch-with-stat")
    patch))

;;; Mode

(defvar magit-stgit-mode-lighter " Stg")

;;;###autoload
(define-minor-mode magit-stgit-mode
  "StGit support for Magit"
  :lighter magit-stgit-mode-lighter
  :require 'magit-stgit
  (or (derived-mode-p 'magit-mode)
      (user-error "This mode only makes sense with magit"))
  (cond
   (magit-stgit-mode
    (magit-add-section-hook 'magit-status-sections-hook
'magit-insert-stgit-series
'magit-insert-stashes t t)
    (add-hook 'magit-visit-hook 'magit-stgit-show nil t)
    (add-hook 'magit-apply-hook 'magit-stgit-goto nil t)
    (add-hook 'magit-discard-hook 'magit-stgit-discard nil t))
   (t
    (remove-hook 'magit-visit-hook 'magit-stgit-show t)
    (remove-hook 'magit-apply-hook 'magit-stgit-goto t)
    (remove-hook 'magit-discard-hook 'magit-stgit-discard t))
    (remove-hook 'magit-status-sections-hook 'magit-insert-stgit-series t))
  (when (called-interactively-p 'any)
    (magit-refresh)))

(put 'magit-stgit-show 'magit-section-action-context 'stgit-patch)
(put 'magit-stgit-goto 'magit-section-action-context 'stgit-patch)
(put 'magit-stgit-discard 'magit-section-action-context 'stgit-patch)

;;;###autoload
(defun turn-on-magit-stgit ()
  "Unconditionally turn on `magit-stgit-mode'."
  (magit-stgit-mode 1))

(easy-menu-define magit-stgit-extension-menu nil
  "StGit extension menu"
  '("StGit" :visible magit-stgit-mode
    ["Refresh patch" magit-stgit-refresh
     :help "Refresh the contents of a patch in an StGit series"]
    ["Repair" magit-stgit-repair
     :help "Repair StGit metadata if branch was modified with git commands"]
    ["Rebase series" magit-stgit-rebase
     :help "Rebase an StGit patch series"]))

(easy-menu-add-item 'magit-mode-menu '("Extensions")
                    magit-stgit-extension-menu)

;;; Series Section

(defconst magit-stgit-patch-re
  "^\\(.\\)\\([-+>!]\\) \\([^ ]+\\) +# \\(.*\\)$")

(defun magit-insert-stgit-series ()
  (when magit-stgit-mode
    (magit-cmd-insert-section (series "Patch series:")
        (apply-partially 'magit-wash-sequence 'magit-stgit-wash-patch)
      magit-stgit-executable "series" "--all" "--empty" "--description")))

(defun magit-stgit-wash-patch ()
  (looking-at magit-stgit-patch-re)
  (magit-bind-match-strings (empty state patch msg)
    (delete-region (point) (point-at-eol))
    (magit-with-section (section stgit-patch patch)
      (setf (magit-section-info section) patch)
      (insert (propertize state 'face
                          (cond ((equal state ">") 'magit-stgit-current)
                                ((equal state "+") 'magit-stgit-applied)
                                ((equal state "-") 'magit-stgit-unapplied)
                                ((equal state "!") 'magit-stgit-hidden)
                                (t (user-error "Unknown stgit patch state: %s"
                                               state))))
              (propertize empty 'face 'magit-stgit-empty) " ")
      (when magit-stgit-show-patch-name
        (insert (propertize patch 'face 'magit-stgit-patch) " "))
      (insert msg)
      (forward-line))))

(provide 'magit-stgit)
;; Local Variables:
;; indent-tabs-mode: nil
;; End:
;;; magit-stgit.el ends here
Something went wrong with that request. Please try again.