dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

magit-subtree.el (6607B)


      1 ;;; magit-subtree.el --- subtree support for Magit  -*- lexical-binding: t -*-
      2 
      3 ;; Copyright (C) 2011-2021  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 ;; SPDX-License-Identifier: GPL-3.0-or-later
     12 
     13 ;; Magit is free software; you can redistribute it and/or modify it
     14 ;; under the terms of the GNU General Public License as published by
     15 ;; the Free Software Foundation; either version 3, or (at your option)
     16 ;; any later version.
     17 ;;
     18 ;; Magit is distributed in the hope that it will be useful, but WITHOUT
     19 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     20 ;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
     21 ;; License for more details.
     22 ;;
     23 ;; You should have received a copy of the GNU General Public License
     24 ;; along with Magit.  If not, see http://www.gnu.org/licenses.
     25 
     26 ;;; Code:
     27 
     28 (require 'magit)
     29 
     30 ;;; Commands
     31 
     32 ;;;###autoload (autoload 'magit-subtree "magit-subtree" nil t)
     33 (transient-define-prefix magit-subtree ()
     34   "Import or export subtrees."
     35   :man-page "git-subtree"
     36   ["Actions"
     37    ("i" "Import" magit-subtree-import)
     38    ("e" "Export" magit-subtree-export)])
     39 
     40 ;;;###autoload (autoload 'magit-subtree-import "magit-subtree" nil t)
     41 (transient-define-prefix magit-subtree-import ()
     42   "Import subtrees."
     43   :man-page "git-subtree"
     44   ["Arguments"
     45    (magit-subtree:--prefix)
     46    (magit-subtree:--message)
     47    ("-s" "Squash" "--squash")]
     48   ["Actions"
     49    [("a" "Add"        magit-subtree-add)
     50     ("c" "Add commit" magit-subtree-add-commit)]
     51    [("m" "Merge"      magit-subtree-merge)
     52     ("f" "Pull"       magit-subtree-pull)]])
     53 
     54 ;;;###autoload (autoload 'magit-subtree-export "magit-subtree" nil t)
     55 (transient-define-prefix magit-subtree-export ()
     56   "Export subtrees."
     57   :man-page "git-subtree"
     58   ["Arguments"
     59    (magit-subtree:--prefix)
     60    (magit-subtree:--annotate)
     61    (magit-subtree:--branch)
     62    (magit-subtree:--onto)
     63    ("-i" "Ignore joins" "--ignore-joins")
     64    ("-j" "Rejoin"       "--rejoin")]
     65   ["Actions"
     66    ("p" "Push"          magit-subtree-push)
     67    ("s" "Split"         magit-subtree-split)])
     68 
     69 (transient-define-argument magit-subtree:--prefix ()
     70   :description "Prefix"
     71   :class 'transient-option
     72   :shortarg "-P"
     73   :argument "--prefix="
     74   :reader 'magit-subtree-read-prefix)
     75 
     76 (defun magit-subtree-read-prefix (prompt &optional default _history)
     77   (let* ((insert-default-directory nil)
     78          (topdir (magit-toplevel))
     79          (prefix (read-directory-name (concat prompt ": ") topdir default)))
     80     (if (file-name-absolute-p prefix)
     81         ;; At least `ido-mode's variant is not compatible.
     82         (if (string-prefix-p topdir prefix)
     83             (file-relative-name prefix topdir)
     84           (user-error "%s isn't inside the repository at %s" prefix topdir))
     85       prefix)))
     86 
     87 (transient-define-argument magit-subtree:--message ()
     88   :description "Message"
     89   :class 'transient-option
     90   :shortarg "-m"
     91   :argument "--message=")
     92 
     93 (transient-define-argument magit-subtree:--annotate ()
     94   :description "Annotate"
     95   :class 'transient-option
     96   :key "-a"
     97   :argument "--annotate=")
     98 
     99 (transient-define-argument magit-subtree:--branch ()
    100   :description "Branch"
    101   :class 'transient-option
    102   :shortarg "-b"
    103   :argument "--branch=")
    104 
    105 (transient-define-argument magit-subtree:--onto ()
    106   :description "Onto"
    107   :class 'transient-option
    108   :key "-o"
    109   :argument "--onto="
    110   :reader 'magit-transient-read-revision)
    111 
    112 (defun magit-subtree-prefix (transient prompt)
    113   (--if-let (--first (string-prefix-p "--prefix=" it)
    114                      (transient-args transient))
    115       (substring it 9)
    116     (magit-subtree-read-prefix prompt)))
    117 
    118 (defun magit-subtree-arguments (transient)
    119   (--remove (string-prefix-p "--prefix=" it)
    120             (transient-args transient)))
    121 
    122 (defun magit-git-subtree (subcmd prefix &rest args)
    123   (magit-run-git-async "subtree" subcmd (concat "--prefix=" prefix) args))
    124 
    125 ;;;###autoload
    126 (defun magit-subtree-add (prefix repository ref args)
    127   "Add REF from REPOSITORY as a new subtree at PREFIX."
    128   (interactive
    129    (cons (magit-subtree-prefix 'magit-subtree-import "Add subtree")
    130          (let ((remote (magit-read-remote-or-url "From repository")))
    131            (list remote
    132                  (magit-read-refspec "Ref" remote)
    133                  (magit-subtree-arguments 'magit-subtree-import)))))
    134   (magit-git-subtree "add" prefix args repository ref))
    135 
    136 ;;;###autoload
    137 (defun magit-subtree-add-commit (prefix commit args)
    138   "Add COMMIT as a new subtree at PREFIX."
    139   (interactive
    140    (list (magit-subtree-prefix 'magit-subtree-import "Add subtree")
    141          (magit-read-string-ns "Commit")
    142          (magit-subtree-arguments 'magit-subtree-import)))
    143   (magit-git-subtree "add" prefix args commit))
    144 
    145 ;;;###autoload
    146 (defun magit-subtree-merge (prefix commit args)
    147   "Merge COMMIT into the PREFIX subtree."
    148   (interactive
    149    (list (magit-subtree-prefix 'magit-subtree-import "Merge into subtree")
    150          (magit-read-string-ns "Commit")
    151          (magit-subtree-arguments 'magit-subtree-import)))
    152   (magit-git-subtree "merge" prefix args commit))
    153 
    154 ;;;###autoload
    155 (defun magit-subtree-pull (prefix repository ref args)
    156   "Pull REF from REPOSITORY into the PREFIX subtree."
    157   (interactive
    158    (cons (magit-subtree-prefix 'magit-subtree-import "Pull into subtree")
    159          (let ((remote (magit-read-remote-or-url "From repository")))
    160            (list remote
    161                  (magit-read-refspec "Ref" remote)
    162                  (magit-subtree-arguments 'magit-subtree-import)))))
    163   (magit-git-subtree "pull" prefix args repository ref))
    164 
    165 ;;;###autoload
    166 (defun magit-subtree-push (prefix repository ref args)
    167   "Extract the history of the subtree PREFIX and push it to REF on REPOSITORY."
    168   (interactive (list (magit-subtree-prefix 'magit-subtree-export "Push subtree")
    169                      (magit-read-remote-or-url "To repository")
    170                      (magit-read-string-ns "To reference")
    171                      (magit-subtree-arguments 'magit-subtree-export)))
    172   (magit-git-subtree "push" prefix args repository ref))
    173 
    174 ;;;###autoload
    175 (defun magit-subtree-split (prefix commit args)
    176   "Extract the history of the subtree PREFIX."
    177   (interactive (list (magit-subtree-prefix 'magit-subtree-export "Split subtree")
    178                      (magit-read-string-ns "Commit")
    179                      (magit-subtree-arguments 'magit-subtree-export)))
    180   (magit-git-subtree "split" prefix args commit))
    181 
    182 ;;; _
    183 (provide 'magit-subtree)
    184 ;;; magit-subtree.el ends here