config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

magit-branch.el (43049B)


      1 ;;; magit-branch.el --- Branch support  -*- lexical-binding:t -*-
      2 
      3 ;; Copyright (C) 2008-2024 The Magit Project Contributors
      4 
      5 ;; Author: Jonas Bernoulli <emacs.magit@jonas.bernoulli.dev>
      6 ;; Maintainer: Jonas Bernoulli <emacs.magit@jonas.bernoulli.dev>
      7 
      8 ;; SPDX-License-Identifier: GPL-3.0-or-later
      9 
     10 ;; Magit is free software: you can redistribute it and/or modify it
     11 ;; under the terms of the GNU General Public License as published by
     12 ;; the Free Software Foundation, either version 3 of the License, or
     13 ;; (at your option) any later version.
     14 ;;
     15 ;; Magit is distributed in the hope that it will be useful, but WITHOUT
     16 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     17 ;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
     18 ;; License for more details.
     19 ;;
     20 ;; You should have received a copy of the GNU General Public License
     21 ;; along with Magit.  If not, see <https://www.gnu.org/licenses/>.
     22 
     23 ;;; Commentary:
     24 
     25 ;; This library implements support for branches.  It defines commands
     26 ;; for creating, checking out, manipulating, and configuring branches.
     27 ;; Commands defined here are mainly concerned with branches as
     28 ;; pointers, commands that deal with what a branch points at, are
     29 ;; defined elsewhere.
     30 
     31 ;;; Code:
     32 
     33 (require 'magit)
     34 (require 'magit-reset)
     35 
     36 ;;; Options
     37 
     38 (defcustom magit-branch-read-upstream-first t
     39   "Whether to read upstream before name of new branch when creating a branch.
     40 
     41 `nil'      Read the branch name first.
     42 `t'        Read the upstream first.
     43 `fallback' Read the upstream first, but if it turns out that the chosen
     44            value is not a valid upstream (because it cannot be resolved
     45            as an existing revision), then treat it as the name of the
     46            new branch and continue by reading the upstream next."
     47   :package-version '(magit . "2.2.0")
     48   :group 'magit-commands
     49   :type '(choice (const :tag "read branch name first" nil)
     50                  (const :tag "read upstream first" t)
     51                  (const :tag "read upstream first, with fallback" fallback)))
     52 
     53 (defcustom magit-branch-prefer-remote-upstream nil
     54   "Whether to favor remote upstreams when creating new branches.
     55 
     56 When a new branch is created, then the branch, commit, or stash
     57 at point is suggested as the default starting point of the new
     58 branch, or if there is no such revision at point the current
     59 branch.  In either case the user may choose another starting
     60 point.
     61 
     62 If the chosen starting point is a branch, then it may also be set
     63 as the upstream of the new branch, depending on the value of the
     64 Git variable `branch.autoSetupMerge'.  By default this is done
     65 for remote branches, but not for local branches.
     66 
     67 You might prefer to always use some remote branch as upstream.
     68 If the chosen starting point is (1) a local branch, (2) whose
     69 name matches a member of the value of this option, (3) the
     70 upstream of that local branch is a remote branch with the same
     71 name, and (4) that remote branch can be fast-forwarded to the
     72 local branch, then the chosen branch is used as starting point,
     73 but its own upstream is used as the upstream of the new branch.
     74 
     75 Members of this option's value are treated as branch names that
     76 have to match exactly unless they contain a character that makes
     77 them invalid as a branch name.  Recommended characters to use
     78 to trigger interpretation as a regexp are \"*\" and \"^\".  Some
     79 other characters which you might expect to be invalid, actually
     80 are not, e.g., \".+$\" are all perfectly valid.  More precisely,
     81 if `git check-ref-format --branch STRING' exits with a non-zero
     82 status, then treat STRING as a regexp.
     83 
     84 Assuming the chosen branch matches these conditions you would end
     85 up with with e.g.:
     86 
     87   feature --upstream--> origin/master
     88 
     89 instead of
     90 
     91   feature --upstream--> master --upstream--> origin/master
     92 
     93 Which you prefer is a matter of personal preference.  If you do
     94 prefer the former, then you should add branches such as \"master\",
     95 \"next\", and \"maint\" to the value of this options."
     96   :package-version '(magit . "2.4.0")
     97   :group 'magit-commands
     98   :type '(repeat string))
     99 
    100 (defcustom magit-branch-adjust-remote-upstream-alist nil
    101   "Alist of upstreams to be used when branching from remote branches.
    102 
    103 When creating a local branch from an ephemeral branch located
    104 on a remote, e.g., a feature or hotfix branch, then that remote
    105 branch should usually not be used as the upstream branch, since
    106 the push-remote already allows accessing it and having both the
    107 upstream and the push-remote reference the same related branch
    108 would be wasteful.  Instead a branch like \"maint\" or \"master\"
    109 should be used as the upstream.
    110 
    111 This option allows specifying the branch that should be used as
    112 the upstream when branching certain remote branches.  The value
    113 is an alist of the form ((UPSTREAM . RULE)...).  The first
    114 element is used whose UPSTREAM exists and whose RULE matches
    115 the name of the new branch.  Subsequent elements are ignored.
    116 
    117 UPSTREAM is the branch to be used as the upstream for branches
    118 specified by RULE.  It can be a local or a remote branch.
    119 
    120 RULE can either be a regular expression, matching branches whose
    121 upstream should be the one specified by UPSTREAM.  Or it can be
    122 a list of the only branches that should *not* use UPSTREAM; all
    123 other branches will.  Matching is done after stripping the remote
    124 part of the name of the branch that is being branched from.
    125 
    126 If you use a finite set of non-ephemeral branches across all your
    127 repositories, then you might use something like:
    128 
    129   ((\"origin/master\" . (\"master\" \"next\" \"maint\")))
    130 
    131 Or if the names of all your ephemeral branches contain a slash,
    132 at least in some repositories, then a good value could be:
    133 
    134   ((\"origin/master\" . \"/\"))
    135 
    136 Of course you can also fine-tune:
    137 
    138   ((\"origin/maint\" . \"\\\\\\=`hotfix/\")
    139    (\"origin/master\" . \"\\\\\\=`feature/\"))
    140 
    141 UPSTREAM can be a local branch:
    142 
    143   ((\"master\" . (\"master\" \"next\" \"maint\")))
    144 
    145 Because the main branch is no longer almost always named \"master\"
    146 you should also account for other common names:
    147 
    148   ((\"main\"  . (\"main\" \"master\" \"next\" \"maint\"))
    149    (\"master\" . (\"main\" \"master\" \"next\" \"maint\")))
    150 
    151 If you use remote branches as UPSTREAM, then you might also want
    152 to set `magit-branch-prefer-remote-upstream' to a non-nil value.
    153 However, I recommend that you use local branches as UPSTREAM."
    154   :package-version '(magit . "2.9.0")
    155   :group 'magit-commands
    156   :type '(repeat (cons (string :tag "Use upstream")
    157                        (choice :tag "for branches"
    158                                (regexp :tag "matching")
    159                                (repeat :tag "except"
    160                                        (string :tag "branch"))))))
    161 
    162 (defcustom magit-branch-rename-push-target t
    163   "Whether the push-remote setup is preserved when renaming a branch.
    164 
    165 The command `magit-branch-rename' renames a branch named OLD to
    166 NEW.  This option controls how much of the push-remote setup is
    167 preserved when doing so.
    168 
    169 When nil, then preserve nothing and unset `branch.OLD.pushRemote'.
    170 
    171 When `local-only', then first set `branch.NEW.pushRemote' to the
    172   same value as `branch.OLD.pushRemote', provided the latter is
    173   actually set and unless the former already has another value.
    174 
    175 When t, then rename the branch named OLD on the remote specified
    176   by `branch.OLD.pushRemote' to NEW, provided OLD exists on that
    177   remote and unless NEW already exists on the remote.
    178 
    179 When `forge-only' and the `forge' package is available, then
    180   behave like `t' if the remote points to a repository on a forge
    181   (currently Github or Gitlab), otherwise like `local-only'."
    182   :package-version '(magit . "2.90.0")
    183   :group 'magit-commands
    184   :type '(choice
    185           (const :tag "Don't preserve push-remote setup" nil)
    186           (const :tag "Preserve push-remote setup" local-only)
    187           (const :tag "... and rename corresponding branch on remote" t)
    188           (const :tag "... but only if remote is on a forge" forge-only)))
    189 
    190 (defcustom magit-branch-direct-configure t
    191   "Whether the command `magit-branch' shows Git variables.
    192 When set to nil, no variables are displayed by this transient
    193 command, instead the sub-transient `magit-branch-configure'
    194 has to be used to view and change branch related variables."
    195   :package-version '(magit . "2.7.0")
    196   :group 'magit-commands
    197   :type 'boolean)
    198 
    199 (defcustom magit-published-branches '("origin/master")
    200   "List of branches that are considered to be published."
    201   :package-version '(magit . "2.13.0")
    202   :group 'magit-commands
    203   :type '(repeat string))
    204 
    205 ;;; Commands
    206 
    207 ;;;###autoload (autoload 'magit-branch "magit" nil t)
    208 (transient-define-prefix magit-branch (branch)
    209   "Add, configure or remove a branch."
    210   :man-page "git-branch"
    211   [:if (lambda ()
    212          (and magit-branch-direct-configure
    213               (oref (transient-prefix-object) scope)))
    214    :description
    215    (lambda ()
    216      (concat (propertize "Configure " 'face 'transient-heading)
    217              (propertize (oref (transient-prefix-object) scope)
    218                          'face 'magit-branch-local)))
    219    ("d" magit-branch.<branch>.description)
    220    ("u" magit-branch.<branch>.merge/remote)
    221    ("r" magit-branch.<branch>.rebase)
    222    ("p" magit-branch.<branch>.pushRemote)]
    223   [:if-non-nil magit-branch-direct-configure
    224    :description "Configure repository defaults"
    225    ("R" magit-pull.rebase)
    226    ("P" magit-remote.pushDefault)
    227    ("B" "Update default branch" magit-update-default-branch
    228     :inapt-if-not magit-get-some-remote)]
    229   ["Arguments"
    230    (7 "-r" "Recurse submodules when checking out an existing branch"
    231       "--recurse-submodules"
    232       :if (lambda () (magit-git-version>= "2.13")))]
    233   [["Checkout"
    234     ("b" "branch/revision"   magit-checkout)
    235     ("l" "local branch"      magit-branch-checkout)
    236     (6 "o" "new orphan"      magit-branch-orphan)]
    237    [""
    238     ("c" "new branch"        magit-branch-and-checkout)
    239     ("s" "new spin-off"      magit-branch-spinoff)
    240     (5 "w" "new worktree"    magit-worktree-checkout)]
    241    ["Create"
    242     ("n" "new branch"        magit-branch-create)
    243     ("S" "new spin-out"      magit-branch-spinout)
    244     (5 "W" "new worktree"    magit-worktree-branch)]
    245    ["Do"
    246     ("C" "configure..."      magit-branch-configure)
    247     ("m" "rename"            magit-branch-rename)
    248     ("x" "reset"             magit-branch-reset)
    249     ("k" "delete"            magit-branch-delete)]
    250    [""
    251     (7 "h" "shelve"          magit-branch-shelve)
    252     (7 "H" "unshelve"        magit-branch-unshelve)]]
    253   (interactive (list (magit-get-current-branch)))
    254   (transient-setup 'magit-branch nil nil :scope branch))
    255 
    256 (defun magit-branch-arguments ()
    257   (transient-args 'magit-branch))
    258 
    259 ;;;###autoload
    260 (defun magit-checkout (revision &optional args)
    261   "Checkout REVISION, updating the index and the working tree.
    262 If REVISION is a local branch, then that becomes the current
    263 branch.  If it is something else, then `HEAD' becomes detached.
    264 Checkout fails if the working tree or the staging area contain
    265 changes.
    266 \n(git checkout REVISION)."
    267   (declare (interactive-only magit--checkout))
    268   (interactive (list (magit-read-other-branch-or-commit "Checkout")
    269                      (magit-branch-arguments)))
    270   (when (string-match "\\`heads/\\(.+\\)" revision)
    271     (setq revision (match-string 1 revision)))
    272   (magit-run-git-async "checkout" args revision))
    273 
    274 (defun magit--checkout (revision &optional args)
    275   (when (string-match "\\`heads/\\(.+\\)" revision)
    276     (setq revision (match-string 1 revision)))
    277   (magit-call-git "checkout" args revision))
    278 
    279 ;;;###autoload
    280 (defun magit-branch-create (branch start-point)
    281   "Create BRANCH at branch or revision START-POINT."
    282   (declare (interactive-only magit-call-git))
    283   (interactive (magit-branch-read-args "Create branch"))
    284   (magit-run-git-async "branch" branch start-point)
    285   (set-process-sentinel
    286    magit-this-process
    287    (lambda (process event)
    288      (when (memq (process-status process) '(exit signal))
    289        (magit-branch-maybe-adjust-upstream branch start-point)
    290        (magit-process-sentinel process event)))))
    291 
    292 ;;;###autoload
    293 (defun magit-branch-and-checkout (branch start-point &optional args)
    294   "Create and checkout BRANCH at branch or revision START-POINT."
    295   (declare (interactive-only magit-call-git))
    296   (interactive (append (magit-branch-read-args "Create and checkout branch")
    297                        (list (magit-branch-arguments))))
    298   (if (string-match-p "^stash@{[0-9]+}$" start-point)
    299       (magit-run-git "stash" "branch" branch start-point)
    300     (magit-run-git-async "checkout" args "-b" branch start-point)
    301     (set-process-sentinel
    302      magit-this-process
    303      (lambda (process event)
    304        (when (memq (process-status process) '(exit signal))
    305          (magit-branch-maybe-adjust-upstream branch start-point)
    306          (magit-process-sentinel process event))))))
    307 
    308 ;;;###autoload
    309 (defun magit-branch-or-checkout (arg &optional start-point)
    310   "Hybrid between `magit-checkout' and `magit-branch-and-checkout'.
    311 
    312 Ask the user for an existing branch or revision.  If the user
    313 input actually can be resolved as a branch or revision, then
    314 check that out, just like `magit-checkout' would.
    315 
    316 Otherwise create and checkout a new branch using the input as
    317 its name.  Before doing so read the starting-point for the new
    318 branch.  This is similar to what `magit-branch-and-checkout'
    319 does."
    320   (declare (interactive-only magit-call-git))
    321   (interactive
    322    (let ((arg (magit-read-other-branch-or-commit "Checkout")))
    323      (list arg
    324            (and (not (magit-commit-p arg))
    325                 (magit-read-starting-point "Create and checkout branch" arg)))))
    326   (when (string-match "\\`heads/\\(.+\\)" arg)
    327     (setq arg (match-string 1 arg)))
    328   (if start-point
    329       (with-suppressed-warnings ((interactive-only magit-branch-and-checkout))
    330         (magit-branch-and-checkout arg start-point))
    331     (magit--checkout arg)
    332     (magit-refresh)))
    333 
    334 ;;;###autoload
    335 (defun magit-branch-checkout (branch &optional start-point)
    336   "Checkout an existing or new local branch.
    337 
    338 Read a branch name from the user offering all local branches and
    339 a subset of remote branches as candidates.  Omit remote branches
    340 for which a local branch by the same name exists from the list
    341 of candidates.  The user can also enter a completely new branch
    342 name.
    343 
    344 - If the user selects an existing local branch, then check that
    345   out.
    346 
    347 - If the user selects a remote branch, then create and checkout
    348   a new local branch with the same name.  Configure the selected
    349   remote branch as push target.
    350 
    351 - If the user enters a new branch name, then create and check
    352   that out, after also reading the starting-point from the user.
    353 
    354 In the latter two cases the upstream is also set.  Whether it is
    355 set to the chosen START-POINT or something else depends on the
    356 value of `magit-branch-adjust-remote-upstream-alist', just like
    357 when using `magit-branch-and-checkout'."
    358   (declare (interactive-only magit-call-git))
    359   (interactive
    360    (let* ((current (magit-get-current-branch))
    361           (local   (magit-list-local-branch-names))
    362           (remote  (--filter (and (string-match "[^/]+/" it)
    363                                   (not (member (substring it (match-end 0))
    364                                                (cons "HEAD" local))))
    365                              (magit-list-remote-branch-names)))
    366           (choices (nconc (delete current local) remote))
    367           (atpoint (magit-branch-at-point))
    368           (choice  (magit-completing-read
    369                     "Checkout branch" choices
    370                     nil nil nil 'magit-revision-history
    371                     (or (car (member atpoint choices))
    372                         (and atpoint
    373                              (car (member (and (string-match "[^/]+/" atpoint)
    374                                                (substring atpoint (match-end 0)))
    375                                           choices)))))))
    376      (cond ((member choice remote)
    377             (list (and (string-match "[^/]+/" choice)
    378                        (substring choice (match-end 0)))
    379                   choice))
    380            ((member choice local)
    381             (list choice))
    382            (t
    383             (list choice (magit-read-starting-point "Create" choice))))))
    384   (cond
    385    ((not start-point)
    386     (magit--checkout branch (magit-branch-arguments))
    387     (magit-refresh))
    388    (t
    389     (when (magit-anything-modified-p t)
    390       (user-error "Cannot checkout when there are uncommitted changes"))
    391     (magit-run-git-async "checkout" (magit-branch-arguments)
    392                          "-b" branch start-point)
    393     (set-process-sentinel
    394      magit-this-process
    395      (lambda (process event)
    396        (when (memq (process-status process) '(exit signal))
    397          (magit-branch-maybe-adjust-upstream branch start-point)
    398          (when (magit-remote-branch-p start-point)
    399            (pcase-let ((`(,remote . ,remote-branch)
    400                         (magit-split-branch-name start-point)))
    401              (when (and (equal branch remote-branch)
    402                         (not (equal remote (magit-get "remote.pushDefault"))))
    403                (magit-set remote "branch" branch "pushRemote"))))
    404          (magit-process-sentinel process event)))))))
    405 
    406 (defun magit-branch-maybe-adjust-upstream (branch start-point)
    407   (when-let ((upstream
    408               (or (and (magit-get-upstream-branch branch)
    409                        (magit-get-indirect-upstream-branch start-point))
    410                   (and (magit-remote-branch-p start-point)
    411                        (let ((name (cdr (magit-split-branch-name start-point))))
    412                          (seq-some
    413                           (pcase-lambda (`(,upstream . ,rule))
    414                             (and (magit-branch-p upstream)
    415                                  (if (listp rule)
    416                                      (not (member name rule))
    417                                    (string-match-p rule name))
    418                                  upstream))
    419                           magit-branch-adjust-remote-upstream-alist))))))
    420     (magit-call-git "branch" (concat "--set-upstream-to=" upstream) branch)))
    421 
    422 ;;;###autoload
    423 (defun magit-branch-orphan (branch start-point)
    424   "Create and checkout an orphan BRANCH with contents from revision START-POINT."
    425   (interactive (magit-branch-read-args "Create and checkout orphan branch"))
    426   (magit-run-git "checkout" "--orphan" branch start-point))
    427 
    428 (defun magit-branch-read-args (prompt &optional default-start)
    429   (if magit-branch-read-upstream-first
    430       (let ((choice (magit-read-starting-point prompt nil default-start)))
    431         (cond
    432          ((magit-rev-verify choice)
    433           (list (magit-read-string-ns
    434                  (if magit-completing-read--silent-default
    435                      (format "%s (starting at `%s')" prompt choice)
    436                    "Name for new branch")
    437                  (let ((def (string-join (cdr (split-string choice "/")) "/")))
    438                    (and (member choice (magit-list-remote-branch-names))
    439                         (not (member def (magit-list-local-branch-names)))
    440                         def)))
    441                 choice))
    442          ((eq magit-branch-read-upstream-first 'fallback)
    443           (list choice
    444                 (magit-read-starting-point prompt choice default-start)))
    445          ((user-error "Not a valid starting-point: %s" choice))))
    446     (let ((branch (magit-read-string-ns (concat prompt " named"))))
    447       (if (magit-branch-p branch)
    448           (magit-branch-read-args
    449            (format "Branch `%s' already exists; pick another name" branch)
    450            default-start)
    451         (list branch (magit-read-starting-point prompt branch default-start))))))
    452 
    453 ;;;###autoload
    454 (defun magit-branch-spinout (branch &optional from)
    455   "Create new branch from the unpushed commits.
    456 Like `magit-branch-spinoff' but remain on the current branch.
    457 If there are any uncommitted changes, then behave exactly like
    458 `magit-branch-spinoff'."
    459   (interactive (list (magit-read-string-ns "Spin out branch")
    460                      (car (last (magit-region-values 'commit)))))
    461   (magit--branch-spinoff branch from nil))
    462 
    463 ;;;###autoload
    464 (defun magit-branch-spinoff (branch &optional from)
    465   "Create new branch from the unpushed commits.
    466 
    467 Create and checkout a new branch starting at and tracking the
    468 current branch.  That branch in turn is reset to the last commit
    469 it shares with its upstream.  If the current branch has no
    470 upstream or no unpushed commits, then the new branch is created
    471 anyway and the previously current branch is not touched.
    472 
    473 This is useful to create a feature branch after work has already
    474 began on the old branch (likely but not necessarily \"master\").
    475 
    476 If the current branch is a member of the value of option
    477 `magit-branch-prefer-remote-upstream' (which see), then the
    478 current branch will be used as the starting point as usual, but
    479 the upstream of the starting-point may be used as the upstream
    480 of the new branch, instead of the starting-point itself.
    481 
    482 If optional FROM is non-nil, then the source branch is reset
    483 to `FROM~', instead of to the last commit it shares with its
    484 upstream.  Interactively, FROM is only ever non-nil, if the
    485 region selects some commits, and among those commits, FROM is
    486 the commit that is the fewest commits ahead of the source
    487 branch.
    488 
    489 The commit at the other end of the selection actually does not
    490 matter, all commits between FROM and `HEAD' are moved to the new
    491 branch.  If FROM is not reachable from `HEAD' or is reachable
    492 from the source branch's upstream, then an error is raised."
    493   (interactive (list (magit-read-string-ns "Spin off branch")
    494                      (car (last (magit-region-values 'commit)))))
    495   (magit--branch-spinoff branch from t))
    496 
    497 (defun magit--branch-spinoff (branch from checkout)
    498   (when (magit-branch-p branch)
    499     (user-error "Cannot spin off %s.  It already exists" branch))
    500   (when (and (not checkout)
    501              (magit-anything-modified-p))
    502     (message "Staying on HEAD due to uncommitted changes")
    503     (setq checkout t))
    504   (if-let ((current (magit-get-current-branch)))
    505       (let ((tracked (magit-get-upstream-branch current))
    506             base)
    507         (when from
    508           (unless (magit-rev-ancestor-p from current)
    509             (user-error "Cannot spin off %s.  %s is not reachable from %s"
    510                         branch from current))
    511           (when (and tracked
    512                      (magit-rev-ancestor-p from tracked))
    513             (user-error "Cannot spin off %s.  %s is ancestor of upstream %s"
    514                         branch from tracked)))
    515         (let ((magit-process-raise-error t))
    516           (if checkout
    517               (magit-call-git "checkout" "-b" branch current)
    518             (magit-call-git "branch" branch current)))
    519         (when-let ((upstream (magit-get-indirect-upstream-branch current)))
    520           (magit-call-git "branch" "--set-upstream-to" upstream branch))
    521         (when (and tracked
    522                    (setq base
    523                          (if from
    524                              (concat from "^")
    525                            (magit-git-string "merge-base" current tracked)))
    526                    (not (magit-rev-eq base current)))
    527           (if checkout
    528               (magit-call-git "update-ref" "-m"
    529                               (format "reset: moving to %s" base)
    530                               (concat "refs/heads/" current) base)
    531             (magit-call-git "reset" "--hard" base))))
    532     (if checkout
    533         (magit-call-git "checkout" "-b" branch)
    534       (magit-call-git "branch" branch)))
    535   (magit-refresh))
    536 
    537 ;;;###autoload
    538 (defun magit-branch-reset (branch to &optional set-upstream)
    539   "Reset a branch to the tip of another branch or any other commit.
    540 
    541 When the branch being reset is the current branch, then do a
    542 hard reset.  If there are any uncommitted changes, then the user
    543 has to confirm the reset because those changes would be lost.
    544 
    545 This is useful when you have started work on a feature branch but
    546 realize it's all crap and want to start over.
    547 
    548 When resetting to another branch and a prefix argument is used,
    549 then also set the target branch as the upstream of the branch
    550 that is being reset."
    551   (interactive
    552    (let* ((atpoint (magit-local-branch-at-point))
    553           (branch (magit-read-local-branch "Reset branch" atpoint))
    554           (minibuffer-default-add-function (magit--minibuf-default-add-commit)))
    555      (list branch
    556            (magit-completing-read (format "Reset %s to" branch)
    557                                   (delete branch (magit-list-branch-names))
    558                                   nil nil nil 'magit-revision-history
    559                                   (or (and (not (equal branch atpoint)) atpoint)
    560                                       (magit-get-upstream-branch branch)))
    561            current-prefix-arg)))
    562   (let ((magit-inhibit-refresh t))
    563     (if (equal branch (magit-get-current-branch))
    564         (if (and (magit-anything-modified-p)
    565                  (not (yes-or-no-p
    566                        "Uncommitted changes will be lost.  Proceed? ")))
    567             (user-error "Abort")
    568           (magit-reset-hard to))
    569       (magit-call-git "update-ref"
    570                       "-m" (format "reset: moving to %s" to)
    571                       (magit-git-string "rev-parse" "--symbolic-full-name"
    572                                         branch)
    573                       to))
    574     (when (and set-upstream (magit-branch-p to))
    575       (magit-set-upstream-branch branch to)
    576       (magit-branch-maybe-adjust-upstream branch to)))
    577   (magit-refresh))
    578 
    579 (defvar magit-branch-delete-never-verify nil
    580   "Whether `magit-branch-delete' always pushes with \"--no-verify\".")
    581 
    582 ;;;###autoload
    583 (defun magit-branch-delete (branches &optional force)
    584   "Delete one or multiple branches.
    585 
    586 If the region marks multiple branches, then offer to delete
    587 those, otherwise prompt for a single branch to be deleted,
    588 defaulting to the branch at point.
    589 
    590 Require confirmation when deleting branches is dangerous in some
    591 way.  Option `magit-no-confirm' can be customized to not require
    592 confirmation in certain cases.  See its docstring to learn why
    593 confirmation is required by default in certain cases or if a
    594 prompt is confusing."
    595   ;; One would expect this to be a command as simple as, for example,
    596   ;; `magit-branch-rename'; but it turns out everyone wants to squeeze
    597   ;; a bit of extra functionality into this one, including myself.
    598   (interactive
    599    (let ((branches (magit-region-values 'branch t))
    600          (force current-prefix-arg))
    601      (if (length> branches 1)
    602          (magit-confirm t nil "Delete %d branches" nil branches)
    603        (setq branches
    604              (list (magit-read-branch-prefer-other
    605                     (if force "Force delete branch" "Delete branch")))))
    606      (unless force
    607        (when-let ((unmerged (seq-remove #'magit-branch-merged-p branches)))
    608          (if (magit-confirm 'delete-unmerged-branch
    609                "Delete unmerged branch %s"
    610                "Delete %d unmerged branches"
    611                'noabort unmerged)
    612              (setq force branches)
    613            (or (setq branches
    614                      (cl-set-difference branches unmerged :test #'equal))
    615                (user-error "Abort")))))
    616      (list branches force)))
    617   (let* ((refs (mapcar #'magit-ref-fullname branches))
    618          (ambiguous (--remove it refs)))
    619     (when ambiguous
    620       (user-error
    621        "%s ambiguous.  Please cleanup using git directly."
    622        (let ((len (length ambiguous)))
    623          (cond
    624           ((= len 1)
    625            (format "%s is" (seq-find #'magit-ref-ambiguous-p branches)))
    626           ((= len (length refs))
    627            (format "These %s names are" len))
    628           (t
    629            (format "%s of these names are" len))))))
    630     (cond
    631      ((string-match "^refs/remotes/\\([^/]+\\)" (car refs))
    632       (let* ((remote (match-string 1 (car refs)))
    633              (offset (1+ (length remote))))
    634         (cond
    635          ((magit-confirm 'delete-branch-on-remote
    636             (format "Deleting local %s.  Also delete on %s"
    637                     (magit-ref-fullname (car branches))
    638                     remote)
    639             (format "Deleting %d local refs.  Also delete on %s"
    640                     (length refs)
    641                     remote)
    642             'noabort refs)
    643           ;; The ref may actually point at another rev on the remote,
    644           ;; but this is better than nothing.
    645           (dolist (ref refs)
    646             (message "Delete %s (was %s)" ref
    647                      (magit-rev-parse "--short" ref)))
    648           ;; Assume the branches actually still exist on the remote.
    649           (magit-run-git-async
    650            "push"
    651            (and (or force magit-branch-delete-never-verify) "--no-verify")
    652            remote
    653            (--map (concat ":" (substring it offset)) branches))
    654           ;; If that is not the case, then this deletes the tracking branches.
    655           (set-process-sentinel
    656            magit-this-process
    657            (apply-partially #'magit-delete-remote-branch-sentinel remote refs)))
    658          (t
    659           (dolist (ref refs)
    660             (message "Delete %s (was %s)" ref
    661                      (magit-rev-parse "--short" ref))
    662             (magit-call-git "update-ref" "-d" ref))
    663           (magit-refresh)))))
    664      ((length> branches 1)
    665       (setq branches (delete (magit-get-current-branch) branches))
    666       (mapc #'magit-branch-maybe-delete-pr-remote branches)
    667       (mapc #'magit-branch-unset-pushRemote branches)
    668       (magit-run-git "branch" (if force "-D" "-d") branches))
    669      (t ; And now for something completely different.
    670       (let* ((branch (car branches))
    671              (prompt (format "Branch %s is checked out.  " branch))
    672              (target (magit-get-indirect-upstream-branch branch t)))
    673         (when (equal branch (magit-get-current-branch))
    674           (when (or (equal branch target)
    675                     (not target))
    676             (setq target (magit-main-branch)))
    677           (pcase (if (or (equal branch target)
    678                          (not target))
    679                      (magit-read-char-case prompt nil
    680                        (?d "[d]etach HEAD & delete" 'detach)
    681                        (?a "[a]bort"                'abort))
    682                    (magit-read-char-case prompt nil
    683                      (?d "[d]etach HEAD & delete" 'detach)
    684                      (?c (format "[c]heckout %s & delete" target) 'target)
    685                      (?a "[a]bort" 'abort)))
    686             (`detach (unless (or (equal force '(4))
    687                                  (member branch force)
    688                                  (magit-branch-merged-p branch t))
    689                        (magit-confirm 'delete-unmerged-branch
    690                          "Delete unmerged branch %s" ""
    691                          nil (list branch)))
    692                      (magit-call-git "checkout" "--detach"))
    693             (`target (unless (or (equal force '(4))
    694                                  (member branch force)
    695                                  (magit-branch-merged-p branch target))
    696                        (magit-confirm 'delete-unmerged-branch
    697                          "Delete unmerged branch %s" ""
    698                          nil (list branch)))
    699                      (magit-call-git "checkout" target))
    700             (`abort  (user-error "Abort")))
    701           (setq force t))
    702         (magit-branch-maybe-delete-pr-remote branch)
    703         (magit-branch-unset-pushRemote branch)
    704         (magit-run-git "branch" (if force "-D" "-d") branch))))))
    705 
    706 (put 'magit-branch-delete 'interactive-only t)
    707 
    708 (defun magit-branch-maybe-delete-pr-remote (branch)
    709   (when-let ((remote (magit-get "branch" branch "pullRequestRemote")))
    710     (let* ((variable (format "remote.%s.fetch" remote))
    711            (refspecs (magit-get-all variable)))
    712       (unless (member (format "+refs/heads/*:refs/remotes/%s/*" remote)
    713                       refspecs)
    714         (let ((refspec
    715                (if (equal (magit-get "branch" branch "pushRemote") remote)
    716                    (format "+refs/heads/%s:refs/remotes/%s/%s"
    717                            branch remote branch)
    718                  (let ((merge (magit-get "branch" branch "merge")))
    719                    (and merge
    720                         (string-prefix-p "refs/heads/" merge)
    721                         (setq merge (substring merge 11))
    722                         (format "+refs/heads/%s:refs/remotes/%s/%s"
    723                                 merge remote merge))))))
    724           (when (member refspec refspecs)
    725             (if (and (length= refspecs 1)
    726                      (magit-confirm 'delete-pr-remote
    727                        (format "Also delete remote %s (%s)" remote
    728                                "no pull-request branch remains")
    729                        nil t))
    730                 (magit-call-git "remote" "rm" remote)
    731               (magit-call-git "config" "--unset-all" variable
    732                               (format "^%s$" (regexp-quote refspec))))))))))
    733 
    734 (defun magit-branch-unset-pushRemote (branch)
    735   (magit-set nil "branch" branch "pushRemote"))
    736 
    737 (defun magit-delete-remote-branch-sentinel (remote refs process event)
    738   (when (memq (process-status process) '(exit signal))
    739     (if (= (process-exit-status process) 1)
    740         (if-let ((on-remote (--map (concat "refs/remotes/" remote "/" it)
    741                                    (magit-remote-list-branches remote)))
    742                  (rest (--filter (and (not (member it on-remote))
    743                                       (magit-ref-exists-p it))
    744                                  refs)))
    745             (progn
    746               (process-put process 'inhibit-refresh t)
    747               (magit-process-sentinel process event)
    748               (setq magit-this-error nil)
    749               (message "Some remote branches no longer exist.  %s"
    750                        "Deleting just the local tracking refs instead...")
    751               (dolist (ref rest)
    752                 (magit-call-git "update-ref" "-d" ref))
    753               (magit-refresh)
    754               (message "Deleting local remote-tracking refs...done"))
    755           (magit-process-sentinel process event))
    756       (magit-process-sentinel process event))))
    757 
    758 ;;;###autoload
    759 (defun magit-branch-rename (old new &optional force)
    760   "Rename the branch named OLD to NEW.
    761 
    762 With a prefix argument FORCE, rename even if a branch named NEW
    763 already exists.
    764 
    765 If `branch.OLD.pushRemote' is set, then unset it.  Depending on
    766 the value of `magit-branch-rename-push-target' (which see) maybe
    767 set `branch.NEW.pushRemote' and maybe rename the push-target on
    768 the remote."
    769   (interactive
    770    (let ((branch (magit-read-local-branch "Rename branch")))
    771      (list branch
    772            (magit-read-string-ns (format "Rename branch '%s' to" branch)
    773                                  nil 'magit-revision-history)
    774            current-prefix-arg)))
    775   (when (string-match "\\`heads/\\(.+\\)" old)
    776     (setq old (match-string 1 old)))
    777   (when (equal old new)
    778     (user-error "Old and new branch names are the same"))
    779   (magit-call-git "branch" (if force "-M" "-m") old new)
    780   (when magit-branch-rename-push-target
    781     (let ((remote (magit-get-push-remote old))
    782           (old-specified (magit-get "branch" old "pushRemote"))
    783           (new-specified (magit-get "branch" new "pushRemote")))
    784       (when (and old-specified (or force (not new-specified)))
    785         ;; Keep the target setting branch specified, even if that is
    786         ;; redundant.  But if a branch by the same name existed before
    787         ;; and the rename isn't forced, then do not change a leftover
    788         ;; setting.  Such a leftover setting may or may not conform to
    789         ;; what we expect here...
    790         (magit-set old-specified "branch" new "pushRemote"))
    791       (when (and (equal (magit-get-push-remote new) remote)
    792                  ;; ...and if it does not, then we must abort.
    793                  (not (eq magit-branch-rename-push-target 'local-only))
    794                  (or (not (eq magit-branch-rename-push-target 'forge-only))
    795                      (and (require (quote forge) nil t)
    796                           (fboundp 'forge--split-forge-url)
    797                           (and-let* ((url (magit-git-string
    798                                            "remote" "get-url" remote)))
    799                             (forge--split-forge-url url)))))
    800         (let ((old-target (magit-get-push-branch old t))
    801               (new-target (magit-get-push-branch new t))
    802               (remote (magit-get-push-remote new)))
    803           (when (and old-target
    804                      (not new-target)
    805                      (magit-y-or-n-p (format "Also rename %S to %S on \"%s\""
    806                                              old new remote)))
    807             ;; Rename on (i.e., within) the remote, but only if the
    808             ;; destination ref doesn't exist yet.  If that ref already
    809             ;; exists, then it probably is of some value and we better
    810             ;; not touch it.  Ignore what the local ref points at,
    811             ;; i.e., if the local and the remote ref didn't point at
    812             ;; the same commit before the rename then keep it that way.
    813             (magit-call-git "push" "-v" remote
    814                             (format "%s:refs/heads/%s" old-target new)
    815                             (format ":refs/heads/%s" old)))))))
    816   (magit-branch-unset-pushRemote old)
    817   (magit-refresh))
    818 
    819 ;;;###autoload
    820 (defun magit-branch-shelve (branch)
    821   "Shelve a BRANCH.
    822 Rename \"refs/heads/BRANCH\" to \"refs/shelved/BRANCH\",
    823 and also rename the respective reflog file."
    824   (interactive (list (magit-read-other-local-branch "Shelve branch")))
    825   (let ((old (concat "refs/heads/"   branch))
    826         (new (concat "refs/shelved/" branch)))
    827     (magit-git "update-ref" new old "")
    828     (magit--rename-reflog-file old new)
    829     (magit-branch-unset-pushRemote branch)
    830     (magit-run-git "branch" "-D" branch)))
    831 
    832 ;;;###autoload
    833 (defun magit-branch-unshelve (branch)
    834   "Unshelve a BRANCH
    835 Rename \"refs/shelved/BRANCH\" to \"refs/heads/BRANCH\",
    836 and also rename the respective reflog file."
    837   (interactive
    838    (list (magit-completing-read
    839           "Unshelve branch"
    840           (--map (substring it 8)
    841                  (magit-list-refnames "refs/shelved"))
    842           nil t)))
    843   (let ((old (concat "refs/shelved/" branch))
    844         (new (concat "refs/heads/"   branch)))
    845     (magit-git "update-ref" new old "")
    846     (magit--rename-reflog-file old new)
    847     (magit-run-git "update-ref" "-d" old)))
    848 
    849 (defun magit--rename-reflog-file (old new)
    850   (let* ((dir (magit-gitdir))
    851          (old (expand-file-name (concat "logs/" old) dir))
    852          (new (expand-file-name (concat "logs/" new) dir)))
    853     (when (file-exists-p old)
    854       (make-directory (file-name-directory new) t)
    855       (rename-file old new t))))
    856 
    857 ;;; Configure
    858 
    859 ;;;###autoload (autoload 'magit-branch-configure "magit-branch" nil t)
    860 (transient-define-prefix magit-branch-configure (branch)
    861   "Configure a branch."
    862   :man-page "git-branch"
    863   [:description
    864    (lambda ()
    865      (concat (propertize "Configure " 'face 'transient-heading)
    866              (propertize (oref (transient-prefix-object) scope)
    867                          'face 'magit-branch-local)))
    868    ("d" magit-branch.<branch>.description)
    869    ("u" magit-branch.<branch>.merge/remote)
    870    ("r" magit-branch.<branch>.rebase)
    871    ("p" magit-branch.<branch>.pushRemote)]
    872   ["Configure repository defaults"
    873    ("R" magit-pull.rebase)
    874    ("P" magit-remote.pushDefault)
    875    ("B" "Update default branch" magit-update-default-branch
    876     :inapt-if-not magit-get-some-remote)]
    877   ["Configure branch creation"
    878    ("a m" magit-branch.autoSetupMerge)
    879    ("a r" magit-branch.autoSetupRebase)]
    880   (interactive
    881    (list (or (and (not current-prefix-arg)
    882                   (not (and magit-branch-direct-configure
    883                             (eq transient-current-command 'magit-branch)))
    884                   (magit-get-current-branch))
    885              (magit--read-branch-scope))))
    886   (transient-setup 'magit-branch-configure nil nil :scope branch))
    887 
    888 (defun magit--read-branch-scope (&optional obj)
    889   (magit-read-local-branch
    890    (if obj
    891        (format "Set %s for branch"
    892                (format (oref obj variable) "<name>"))
    893      "Configure branch")))
    894 
    895 (transient-define-suffix magit-branch.<branch>.description (branch)
    896   "Edit the description of BRANCH."
    897   :class 'magit--git-variable
    898   :transient nil
    899   :variable "branch.%s.description"
    900   (interactive (list (oref transient-current-prefix scope)))
    901   (magit-run-git-with-editor "branch" "--edit-description" branch))
    902 
    903 (defclass magit--git-branch:upstream (magit--git-variable)
    904   ((format :initform " %k %m %M\n   %r %R")))
    905 
    906 (transient-define-infix magit-branch.<branch>.merge/remote ()
    907   :class 'magit--git-branch:upstream)
    908 
    909 (cl-defmethod transient-init-value ((obj magit--git-branch:upstream))
    910   (when-let* ((branch (oref (transient-prefix-object) scope))
    911               (remote (magit-get "branch" branch "remote"))
    912               (merge  (magit-get "branch" branch "merge")))
    913     (oset obj value (list remote merge))))
    914 
    915 (cl-defmethod transient-infix-read ((obj magit--git-branch:upstream))
    916   (if (oref obj value)
    917       (oset obj value nil)
    918     (magit-read-upstream-branch (oref (transient-prefix-object) scope)
    919                                 "Upstream")))
    920 
    921 (cl-defmethod transient-infix-set ((obj magit--git-branch:upstream) refname)
    922   (magit-set-upstream-branch (oref (transient-prefix-object) scope) refname)
    923   (oset obj value
    924         (and-let* ((branch (oref (transient-prefix-object) scope))
    925                    (r (magit-get "branch" branch "remote"))
    926                    (m (magit-get "branch" branch "merge")))
    927           (list r m)))
    928   (magit-refresh))
    929 
    930 (cl-defmethod transient-format ((obj magit--git-branch:upstream))
    931   (let ((branch (oref (transient-prefix-object) scope)))
    932     (format-spec
    933      (oref obj format)
    934      `((?k . ,(transient-format-key obj))
    935        (?r . ,(format "branch.%s.remote" branch))
    936        (?m . ,(format "branch.%s.merge" branch))
    937        (?R . ,(transient-format-value obj #'car))
    938        (?M . ,(transient-format-value obj #'cadr))))))
    939 
    940 (cl-defmethod transient-format-value ((obj magit--git-branch:upstream) key)
    941   (if-let ((value (funcall key (oref obj value))))
    942       (propertize value 'face 'transient-argument)
    943     (propertize "unset" 'face 'transient-inactive-argument)))
    944 
    945 (transient-define-infix magit-branch.<branch>.rebase ()
    946   :class 'magit--git-variable:choices
    947   :scope #'magit--read-branch-scope
    948   :variable "branch.%s.rebase"
    949   :fallback "pull.rebase"
    950   :choices '("true" "false")
    951   :default "false")
    952 
    953 (transient-define-infix magit-branch.<branch>.pushRemote ()
    954   :class 'magit--git-variable:choices
    955   :scope #'magit--read-branch-scope
    956   :variable "branch.%s.pushRemote"
    957   :fallback "remote.pushDefault"
    958   :choices #'magit-list-remotes)
    959 
    960 (transient-define-infix magit-pull.rebase ()
    961   :class 'magit--git-variable:choices
    962   :variable "pull.rebase"
    963   :choices '("true" "false")
    964   :default "false")
    965 
    966 (transient-define-infix magit-remote.pushDefault ()
    967   :class 'magit--git-variable:choices
    968   :variable "remote.pushDefault"
    969   :choices #'magit-list-remotes)
    970 
    971 (transient-define-infix magit-branch.autoSetupMerge ()
    972   :class 'magit--git-variable:choices
    973   :variable "branch.autoSetupMerge"
    974   :choices '("always" "true" "false")
    975   :default "true")
    976 
    977 (transient-define-infix magit-branch.autoSetupRebase ()
    978   :class 'magit--git-variable:choices
    979   :variable "branch.autoSetupRebase"
    980   :choices '("always" "local" "remote" "never")
    981   :default "never")
    982 
    983 ;;; _
    984 (provide 'magit-branch)
    985 ;;; magit-branch.el ends here