mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2018-11-18 21067e7cbe6d7a0f65ff5c317a96b5c337b0b3d8
commit | author | age
5cb5f7 1 ;;; magit-subtree.el --- subtree support for Magit  -*- lexical-binding: t -*-
C 2
3 ;; Copyright (C) 2011-2018  The Magit Project Contributors
4 ;;
5 ;; You should have received a copy of the AUTHORS.md file which
6 ;; lists all contributors.  If not, see http://magit.vc/authors.
7
8 ;; Author: Jonas Bernoulli <jonas@bernoul.li>
9 ;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
10
11 ;; Magit is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15 ;;
16 ;; Magit is distributed in the hope that it will be useful, but WITHOUT
17 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 ;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
19 ;; License for more details.
20 ;;
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with Magit.  If not, see http://www.gnu.org/licenses.
23
24 ;;; Code:
25
26 (require 'magit)
27
28 ;;; Popup
29
30 ;;;###autoload (autoload 'magit-subtree-popup "magit-subtree" nil t)
31 (magit-define-popup magit-subtree-popup
32   "Popup console for subtree commands."
33   :man-page "git-subtree"
34   :switches '("Switches for add, merge, push, and pull"
35               (?s "Squash" "--squash")
36               "Switches for split"
37               (?i "Ignore joins" "--ignore-joins")
38               (?j "Rejoin"       "--rejoin"))
39   :options  '("Options"
40               (?p "Prefix" "--prefix=" magit-subtree-read-prefix)
41               "Options for add, merge, and pull"
42               (?m "Message" "--message=")
43               "Options for split"
44               (?a "Annotate" "--annotate=")
45               (?b "Branch"   "--branch=")
46               (?o "Onto"     "--onto=" magit-read-branch-or-commit))
47   :actions  '((?a "Add"        magit-subtree-add)
48               (?m "Merge"      magit-subtree-merge)
49               (?p "Push"       magit-subtree-push)
50               (?c "Add commit" magit-subtree-add-commit)
51               (?f "Pull"       magit-subtree-pull)
52               (?s "Split"      magit-subtree-split))
53   :max-action-columns 3)
54
55 (defun magit-subtree-read-prefix (prompt &optional default)
56   (let* ((insert-default-directory nil)
57          (topdir (magit-toplevel))
58          (prefix (read-directory-name (concat prompt ": ") topdir default)))
59     (if (file-name-absolute-p prefix)
60         ;; At least `ido-mode's variant is not compatible.
61         (if (string-prefix-p topdir prefix)
62             (file-relative-name prefix topdir)
63           (user-error "%s isn't inside the repository at %s" prefix topdir))
64       prefix)))
65
66 ;;; Commands
67
68 (defun magit-subtree-prefix (prompt)
69   (--if-let (--first (string-prefix-p "--prefix=" it)
70                      (magit-subtree-arguments))
71       (substring it 9)
72     (magit-subtree-read-prefix prompt)))
73
74 (defun magit-subtree-args ()
75   (-filter (lambda (arg)
76              (if (eq this-command 'magit-subtree-split)
77                  (or (equal arg "--ignore-joins")
78                      (equal arg "--rejoin")
79                      (string-prefix-p "--annotate=" arg)
80                      (string-prefix-p "--branch=" arg)
81                      (string-prefix-p "--onto=" arg))
82                (or (equal arg "--squash")
83                    (and (string-prefix-p "--message=" arg)
84                         (not (eq this-command 'magit-subtree-push))))))
85            (magit-subtree-arguments)))
86
87 (defun magit-git-subtree (subcmd prefix &rest args)
88   (magit-run-git-async "subtree" subcmd (concat "--prefix=" prefix) args))
89
90 ;;;###autoload
91 (defun magit-subtree-add (prefix repository ref args)
92   "Add REF from REPOSITORY as a new subtree at PREFIX."
93   (interactive
94    (cons (magit-subtree-prefix "Add subtree")
95          (let ((remote (magit-read-remote-or-url "From repository")))
96            (list remote
97                  (magit-read-refspec "Ref" remote)
98                  (magit-subtree-args)))))
99   (magit-git-subtree "add" prefix args repository ref))
100
101 ;;;###autoload
102 (defun magit-subtree-add-commit (prefix commit args)
103   "Add COMMIT as a new subtree at PREFIX."
104   (interactive (list (magit-subtree-prefix "Add subtree")
105                      (magit-read-string-ns "Commit")
106                      (magit-subtree-args)))
107   (magit-git-subtree "add" prefix args commit))
108
109 ;;;###autoload
110 (defun magit-subtree-merge (prefix commit args)
111   "Merge COMMIT into the PREFIX subtree."
112   (interactive (list (magit-subtree-prefix "Merge into subtree")
113                      (magit-read-string-ns "Commit")
114                      (magit-subtree-args)))
115   (magit-git-subtree "merge" prefix args commit))
116
117 ;;;###autoload
118 (defun magit-subtree-pull (prefix repository ref args)
119   "Pull REF from REPOSITORY into the PREFIX subtree."
120   (interactive
121    (cons (magit-subtree-prefix "Pull into subtree")
122          (let ((remote (magit-read-remote-or-url "From repository")))
123            (list remote
124                  (magit-read-refspec "Ref" remote)
125                  (magit-subtree-args)))))
126   (magit-git-subtree "pull" prefix args repository ref))
127
128 ;;;###autoload
129 (defun magit-subtree-push (prefix repository ref args)
130   "Extract the history of the subtree PREFIX and push it to REF on REPOSITORY."
131   (interactive (list (magit-subtree-prefix "Push subtree")
132                      (magit-read-remote-or-url "To repository")
133                      (magit-read-string-ns "To reference")
134                      (magit-subtree-args)))
135   (magit-git-subtree "push" prefix args repository ref))
136
137 ;;;###autoload
138 (defun magit-subtree-split (prefix commit args)
139   "Extract the history of the subtree PREFIX."
140   (interactive (list (magit-subtree-prefix "Split subtree")
141                      (magit-read-string-ns "Commit")
142                      (magit-subtree-args)))
143   (magit-git-subtree "split" prefix args commit))
144
145 ;;; _
146 (provide 'magit-subtree)
147 ;;; magit-subtree.el ends here