config

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

magit-branch.el (43136B)


      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 (mapconcat #'identity
    438                                        (cdr (split-string choice "/"))
    439                                        "/")))
    440                    (and (member choice (magit-list-remote-branch-names))
    441                         (not (member def (magit-list-local-branch-names)))
    442                         def)))
    443                 choice))
    444          ((eq magit-branch-read-upstream-first 'fallback)
    445           (list choice
    446                 (magit-read-starting-point prompt choice default-start)))
    447          ((user-error "Not a valid starting-point: %s" choice))))
    448     (let ((branch (magit-read-string-ns (concat prompt " named"))))
    449       (if (magit-branch-p branch)
    450           (magit-branch-read-args
    451            (format "Branch `%s' already exists; pick another name" branch)
    452            default-start)
    453         (list branch (magit-read-starting-point prompt branch default-start))))))
    454 
    455 ;;;###autoload
    456 (defun magit-branch-spinout (branch &optional from)
    457   "Create new branch from the unpushed commits.
    458 Like `magit-branch-spinoff' but remain on the current branch.
    459 If there are any uncommitted changes, then behave exactly like
    460 `magit-branch-spinoff'."
    461   (interactive (list (magit-read-string-ns "Spin out branch")
    462                      (car (last (magit-region-values 'commit)))))
    463   (magit--branch-spinoff branch from nil))
    464 
    465 ;;;###autoload
    466 (defun magit-branch-spinoff (branch &optional from)
    467   "Create new branch from the unpushed commits.
    468 
    469 Create and checkout a new branch starting at and tracking the
    470 current branch.  That branch in turn is reset to the last commit
    471 it shares with its upstream.  If the current branch has no
    472 upstream or no unpushed commits, then the new branch is created
    473 anyway and the previously current branch is not touched.
    474 
    475 This is useful to create a feature branch after work has already
    476 began on the old branch (likely but not necessarily \"master\").
    477 
    478 If the current branch is a member of the value of option
    479 `magit-branch-prefer-remote-upstream' (which see), then the
    480 current branch will be used as the starting point as usual, but
    481 the upstream of the starting-point may be used as the upstream
    482 of the new branch, instead of the starting-point itself.
    483 
    484 If optional FROM is non-nil, then the source branch is reset
    485 to `FROM~', instead of to the last commit it shares with its
    486 upstream.  Interactively, FROM is only ever non-nil, if the
    487 region selects some commits, and among those commits, FROM is
    488 the commit that is the fewest commits ahead of the source
    489 branch.
    490 
    491 The commit at the other end of the selection actually does not
    492 matter, all commits between FROM and `HEAD' are moved to the new
    493 branch.  If FROM is not reachable from `HEAD' or is reachable
    494 from the source branch's upstream, then an error is raised."
    495   (interactive (list (magit-read-string-ns "Spin off branch")
    496                      (car (last (magit-region-values 'commit)))))
    497   (magit--branch-spinoff branch from t))
    498 
    499 (defun magit--branch-spinoff (branch from checkout)
    500   (when (magit-branch-p branch)
    501     (user-error "Cannot spin off %s.  It already exists" branch))
    502   (when (and (not checkout)
    503              (magit-anything-modified-p))
    504     (message "Staying on HEAD due to uncommitted changes")
    505     (setq checkout t))
    506   (if-let ((current (magit-get-current-branch)))
    507       (let ((tracked (magit-get-upstream-branch current))
    508             base)
    509         (when from
    510           (unless (magit-rev-ancestor-p from current)
    511             (user-error "Cannot spin off %s.  %s is not reachable from %s"
    512                         branch from current))
    513           (when (and tracked
    514                      (magit-rev-ancestor-p from tracked))
    515             (user-error "Cannot spin off %s.  %s is ancestor of upstream %s"
    516                         branch from tracked)))
    517         (let ((magit-process-raise-error t))
    518           (if checkout
    519               (magit-call-git "checkout" "-b" branch current)
    520             (magit-call-git "branch" branch current)))
    521         (when-let ((upstream (magit-get-indirect-upstream-branch current)))
    522           (magit-call-git "branch" "--set-upstream-to" upstream branch))
    523         (when (and tracked
    524                    (setq base
    525                          (if from
    526                              (concat from "^")
    527                            (magit-git-string "merge-base" current tracked)))
    528                    (not (magit-rev-eq base current)))
    529           (if checkout
    530               (magit-call-git "update-ref" "-m"
    531                               (format "reset: moving to %s" base)
    532                               (concat "refs/heads/" current) base)
    533             (magit-call-git "reset" "--hard" base))))
    534     (if checkout
    535         (magit-call-git "checkout" "-b" branch)
    536       (magit-call-git "branch" branch)))
    537   (magit-refresh))
    538 
    539 ;;;###autoload
    540 (defun magit-branch-reset (branch to &optional set-upstream)
    541   "Reset a branch to the tip of another branch or any other commit.
    542 
    543 When the branch being reset is the current branch, then do a
    544 hard reset.  If there are any uncommitted changes, then the user
    545 has to confirm the reset because those changes would be lost.
    546 
    547 This is useful when you have started work on a feature branch but
    548 realize it's all crap and want to start over.
    549 
    550 When resetting to another branch and a prefix argument is used,
    551 then also set the target branch as the upstream of the branch
    552 that is being reset."
    553   (interactive
    554    (let* ((atpoint (magit-local-branch-at-point))
    555           (branch (magit-read-local-branch "Reset branch" atpoint))
    556           (minibuffer-default-add-function (magit--minibuf-default-add-commit)))
    557      (list branch
    558            (magit-completing-read (format "Reset %s to" branch)
    559                                   (delete branch (magit-list-branch-names))
    560                                   nil nil nil 'magit-revision-history
    561                                   (or (and (not (equal branch atpoint)) atpoint)
    562                                       (magit-get-upstream-branch branch)))
    563            current-prefix-arg)))
    564   (let ((magit-inhibit-refresh t))
    565     (if (equal branch (magit-get-current-branch))
    566         (if (and (magit-anything-modified-p)
    567                  (not (yes-or-no-p
    568                        "Uncommitted changes will be lost.  Proceed? ")))
    569             (user-error "Abort")
    570           (magit-reset-hard to))
    571       (magit-call-git "update-ref"
    572                       "-m" (format "reset: moving to %s" to)
    573                       (magit-git-string "rev-parse" "--symbolic-full-name"
    574                                         branch)
    575                       to))
    576     (when (and set-upstream (magit-branch-p to))
    577       (magit-set-upstream-branch branch to)
    578       (magit-branch-maybe-adjust-upstream branch to)))
    579   (magit-refresh))
    580 
    581 (defvar magit-branch-delete-never-verify nil
    582   "Whether `magit-branch-delete' always pushes with \"--no-verify\".")
    583 
    584 ;;;###autoload
    585 (defun magit-branch-delete (branches &optional force)
    586   "Delete one or multiple branches.
    587 
    588 If the region marks multiple branches, then offer to delete
    589 those, otherwise prompt for a single branch to be deleted,
    590 defaulting to the branch at point.
    591 
    592 Require confirmation when deleting branches is dangerous in some
    593 way.  Option `magit-no-confirm' can be customized to not require
    594 confirmation in certain cases.  See its docstring to learn why
    595 confirmation is required by default in certain cases or if a
    596 prompt is confusing."
    597   ;; One would expect this to be a command as simple as, for example,
    598   ;; `magit-branch-rename'; but it turns out everyone wants to squeeze
    599   ;; a bit of extra functionality into this one, including myself.
    600   (interactive
    601    (let ((branches (magit-region-values 'branch t))
    602          (force current-prefix-arg))
    603      (if (length> branches 1)
    604          (magit-confirm t nil "Delete %d branches" nil branches)
    605        (setq branches
    606              (list (magit-read-branch-prefer-other
    607                     (if force "Force delete branch" "Delete branch")))))
    608      (unless force
    609        (when-let ((unmerged (seq-remove #'magit-branch-merged-p branches)))
    610          (if (magit-confirm 'delete-unmerged-branch
    611                "Delete unmerged branch %s"
    612                "Delete %d unmerged branches"
    613                'noabort unmerged)
    614              (setq force branches)
    615            (or (setq branches
    616                      (cl-set-difference branches unmerged :test #'equal))
    617                (user-error "Abort")))))
    618      (list branches force)))
    619   (let* ((refs (mapcar #'magit-ref-fullname branches))
    620          (ambiguous (--remove it refs)))
    621     (when ambiguous
    622       (user-error
    623        "%s ambiguous.  Please cleanup using git directly."
    624        (let ((len (length ambiguous)))
    625          (cond
    626           ((= len 1)
    627            (format "%s is" (seq-find #'magit-ref-ambiguous-p branches)))
    628           ((= len (length refs))
    629            (format "These %s names are" len))
    630           (t
    631            (format "%s of these names are" len))))))
    632     (cond
    633      ((string-match "^refs/remotes/\\([^/]+\\)" (car refs))
    634       (let* ((remote (match-string 1 (car refs)))
    635              (offset (1+ (length remote))))
    636         (cond
    637          ((magit-confirm 'delete-branch-on-remote
    638             (format "Deleting local %s.  Also delete on %s"
    639                     (magit-ref-fullname (car branches))
    640                     remote)
    641             (format "Deleting %d local refs.  Also delete on %s"
    642                     (length refs)
    643                     remote)
    644             'noabort refs)
    645           ;; The ref may actually point at another rev on the remote,
    646           ;; but this is better than nothing.
    647           (dolist (ref refs)
    648             (message "Delete %s (was %s)" ref
    649                      (magit-rev-parse "--short" ref)))
    650           ;; Assume the branches actually still exist on the remote.
    651           (magit-run-git-async
    652            "push"
    653            (and (or force magit-branch-delete-never-verify) "--no-verify")
    654            remote
    655            (--map (concat ":" (substring it offset)) branches))
    656           ;; If that is not the case, then this deletes the tracking branches.
    657           (set-process-sentinel
    658            magit-this-process
    659            (apply-partially #'magit-delete-remote-branch-sentinel remote refs)))
    660          (t
    661           (dolist (ref refs)
    662             (message "Delete %s (was %s)" ref
    663                      (magit-rev-parse "--short" ref))
    664             (magit-call-git "update-ref" "-d" ref))
    665           (magit-refresh)))))
    666      ((length> branches 1)
    667       (setq branches (delete (magit-get-current-branch) branches))
    668       (mapc #'magit-branch-maybe-delete-pr-remote branches)
    669       (mapc #'magit-branch-unset-pushRemote branches)
    670       (magit-run-git "branch" (if force "-D" "-d") branches))
    671      (t ; And now for something completely different.
    672       (let* ((branch (car branches))
    673              (prompt (format "Branch %s is checked out.  " branch))
    674              (target (magit-get-indirect-upstream-branch branch t)))
    675         (when (equal branch (magit-get-current-branch))
    676           (when (or (equal branch target)
    677                     (not target))
    678             (setq target (magit-main-branch)))
    679           (pcase (if (or (equal branch target)
    680                          (not target))
    681                      (magit-read-char-case prompt nil
    682                        (?d "[d]etach HEAD & delete" 'detach)
    683                        (?a "[a]bort"                'abort))
    684                    (magit-read-char-case prompt nil
    685                      (?d "[d]etach HEAD & delete" 'detach)
    686                      (?c (format "[c]heckout %s & delete" target) 'target)
    687                      (?a "[a]bort" 'abort)))
    688             (`detach (unless (or (equal force '(4))
    689                                  (member branch force)
    690                                  (magit-branch-merged-p branch t))
    691                        (magit-confirm 'delete-unmerged-branch
    692                          "Delete unmerged branch %s" ""
    693                          nil (list branch)))
    694                      (magit-call-git "checkout" "--detach"))
    695             (`target (unless (or (equal force '(4))
    696                                  (member branch force)
    697                                  (magit-branch-merged-p branch target))
    698                        (magit-confirm 'delete-unmerged-branch
    699                          "Delete unmerged branch %s" ""
    700                          nil (list branch)))
    701                      (magit-call-git "checkout" target))
    702             (`abort  (user-error "Abort")))
    703           (setq force t))
    704         (magit-branch-maybe-delete-pr-remote branch)
    705         (magit-branch-unset-pushRemote branch)
    706         (magit-run-git "branch" (if force "-D" "-d") branch))))))
    707 
    708 (put 'magit-branch-delete 'interactive-only t)
    709 
    710 (defun magit-branch-maybe-delete-pr-remote (branch)
    711   (when-let ((remote (magit-get "branch" branch "pullRequestRemote")))
    712     (let* ((variable (format "remote.%s.fetch" remote))
    713            (refspecs (magit-get-all variable)))
    714       (unless (member (format "+refs/heads/*:refs/remotes/%s/*" remote)
    715                       refspecs)
    716         (let ((refspec
    717                (if (equal (magit-get "branch" branch "pushRemote") remote)
    718                    (format "+refs/heads/%s:refs/remotes/%s/%s"
    719                            branch remote branch)
    720                  (let ((merge (magit-get "branch" branch "merge")))
    721                    (and merge
    722                         (string-prefix-p "refs/heads/" merge)
    723                         (setq merge (substring merge 11))
    724                         (format "+refs/heads/%s:refs/remotes/%s/%s"
    725                                 merge remote merge))))))
    726           (when (member refspec refspecs)
    727             (if (and (length= refspecs 1)
    728                      (magit-confirm 'delete-pr-remote
    729                        (format "Also delete remote %s (%s)" remote
    730                                "no pull-request branch remains")
    731                        nil t))
    732                 (magit-call-git "remote" "rm" remote)
    733               (magit-call-git "config" "--unset-all" variable
    734                               (format "^%s$" (regexp-quote refspec))))))))))
    735 
    736 (defun magit-branch-unset-pushRemote (branch)
    737   (magit-set nil "branch" branch "pushRemote"))
    738 
    739 (defun magit-delete-remote-branch-sentinel (remote refs process event)
    740   (when (memq (process-status process) '(exit signal))
    741     (if (= (process-exit-status process) 1)
    742         (if-let ((on-remote (--map (concat "refs/remotes/" remote "/" it)
    743                                    (magit-remote-list-branches remote)))
    744                  (rest (--filter (and (not (member it on-remote))
    745                                       (magit-ref-exists-p it))
    746                                  refs)))
    747             (progn
    748               (process-put process 'inhibit-refresh t)
    749               (magit-process-sentinel process event)
    750               (setq magit-this-error nil)
    751               (message "Some remote branches no longer exist.  %s"
    752                        "Deleting just the local tracking refs instead...")
    753               (dolist (ref rest)
    754                 (magit-call-git "update-ref" "-d" ref))
    755               (magit-refresh)
    756               (message "Deleting local remote-tracking refs...done"))
    757           (magit-process-sentinel process event))
    758       (magit-process-sentinel process event))))
    759 
    760 ;;;###autoload
    761 (defun magit-branch-rename (old new &optional force)
    762   "Rename the branch named OLD to NEW.
    763 
    764 With a prefix argument FORCE, rename even if a branch named NEW
    765 already exists.
    766 
    767 If `branch.OLD.pushRemote' is set, then unset it.  Depending on
    768 the value of `magit-branch-rename-push-target' (which see) maybe
    769 set `branch.NEW.pushRemote' and maybe rename the push-target on
    770 the remote."
    771   (interactive
    772    (let ((branch (magit-read-local-branch "Rename branch")))
    773      (list branch
    774            (magit-read-string-ns (format "Rename branch '%s' to" branch)
    775                                  nil 'magit-revision-history)
    776            current-prefix-arg)))
    777   (when (string-match "\\`heads/\\(.+\\)" old)
    778     (setq old (match-string 1 old)))
    779   (when (equal old new)
    780     (user-error "Old and new branch names are the same"))
    781   (magit-call-git "branch" (if force "-M" "-m") old new)
    782   (when magit-branch-rename-push-target
    783     (let ((remote (magit-get-push-remote old))
    784           (old-specified (magit-get "branch" old "pushRemote"))
    785           (new-specified (magit-get "branch" new "pushRemote")))
    786       (when (and old-specified (or force (not new-specified)))
    787         ;; Keep the target setting branch specified, even if that is
    788         ;; redundant.  But if a branch by the same name existed before
    789         ;; and the rename isn't forced, then do not change a leftover
    790         ;; setting.  Such a leftover setting may or may not conform to
    791         ;; what we expect here...
    792         (magit-set old-specified "branch" new "pushRemote"))
    793       (when (and (equal (magit-get-push-remote new) remote)
    794                  ;; ...and if it does not, then we must abort.
    795                  (not (eq magit-branch-rename-push-target 'local-only))
    796                  (or (not (eq magit-branch-rename-push-target 'forge-only))
    797                      (and (require (quote forge) nil t)
    798                           (fboundp 'forge--split-forge-url)
    799                           (and-let* ((url (magit-git-string
    800                                            "remote" "get-url" remote)))
    801                             (forge--split-forge-url url)))))
    802         (let ((old-target (magit-get-push-branch old t))
    803               (new-target (magit-get-push-branch new t))
    804               (remote (magit-get-push-remote new)))
    805           (when (and old-target
    806                      (not new-target)
    807                      (magit-y-or-n-p (format "Also rename %S to %S on \"%s\""
    808                                              old new remote)))
    809             ;; Rename on (i.e., within) the remote, but only if the
    810             ;; destination ref doesn't exist yet.  If that ref already
    811             ;; exists, then it probably is of some value and we better
    812             ;; not touch it.  Ignore what the local ref points at,
    813             ;; i.e., if the local and the remote ref didn't point at
    814             ;; the same commit before the rename then keep it that way.
    815             (magit-call-git "push" "-v" remote
    816                             (format "%s:refs/heads/%s" old-target new)
    817                             (format ":refs/heads/%s" old)))))))
    818   (magit-branch-unset-pushRemote old)
    819   (magit-refresh))
    820 
    821 ;;;###autoload
    822 (defun magit-branch-shelve (branch)
    823   "Shelve a BRANCH.
    824 Rename \"refs/heads/BRANCH\" to \"refs/shelved/BRANCH\",
    825 and also rename the respective reflog file."
    826   (interactive (list (magit-read-other-local-branch "Shelve branch")))
    827   (let ((old (concat "refs/heads/"   branch))
    828         (new (concat "refs/shelved/" branch)))
    829     (magit-git "update-ref" new old "")
    830     (magit--rename-reflog-file old new)
    831     (magit-branch-unset-pushRemote branch)
    832     (magit-run-git "branch" "-D" branch)))
    833 
    834 ;;;###autoload
    835 (defun magit-branch-unshelve (branch)
    836   "Unshelve a BRANCH
    837 Rename \"refs/shelved/BRANCH\" to \"refs/heads/BRANCH\",
    838 and also rename the respective reflog file."
    839   (interactive
    840    (list (magit-completing-read
    841           "Unshelve branch"
    842           (--map (substring it 8)
    843                  (magit-list-refnames "refs/shelved"))
    844           nil t)))
    845   (let ((old (concat "refs/shelved/" branch))
    846         (new (concat "refs/heads/"   branch)))
    847     (magit-git "update-ref" new old "")
    848     (magit--rename-reflog-file old new)
    849     (magit-run-git "update-ref" "-d" old)))
    850 
    851 (defun magit--rename-reflog-file (old new)
    852   (let* ((dir (magit-gitdir))
    853          (old (expand-file-name (concat "logs/" old) dir))
    854          (new (expand-file-name (concat "logs/" new) dir)))
    855     (when (file-exists-p old)
    856       (make-directory (file-name-directory new) t)
    857       (rename-file old new t))))
    858 
    859 ;;; Configure
    860 
    861 ;;;###autoload (autoload 'magit-branch-configure "magit-branch" nil t)
    862 (transient-define-prefix magit-branch-configure (branch)
    863   "Configure a branch."
    864   :man-page "git-branch"
    865   [:description
    866    (lambda ()
    867      (concat (propertize "Configure " 'face 'transient-heading)
    868              (propertize (oref (transient-prefix-object) scope)
    869                          'face 'magit-branch-local)))
    870    ("d" magit-branch.<branch>.description)
    871    ("u" magit-branch.<branch>.merge/remote)
    872    ("r" magit-branch.<branch>.rebase)
    873    ("p" magit-branch.<branch>.pushRemote)]
    874   ["Configure repository defaults"
    875    ("R" magit-pull.rebase)
    876    ("P" magit-remote.pushDefault)
    877    ("B" "Update default branch" magit-update-default-branch
    878     :inapt-if-not magit-get-some-remote)]
    879   ["Configure branch creation"
    880    ("a m" magit-branch.autoSetupMerge)
    881    ("a r" magit-branch.autoSetupRebase)]
    882   (interactive
    883    (list (or (and (not current-prefix-arg)
    884                   (not (and magit-branch-direct-configure
    885                             (eq transient-current-command 'magit-branch)))
    886                   (magit-get-current-branch))
    887              (magit--read-branch-scope))))
    888   (transient-setup 'magit-branch-configure nil nil :scope branch))
    889 
    890 (defun magit--read-branch-scope (&optional obj)
    891   (magit-read-local-branch
    892    (if obj
    893        (format "Set %s for branch"
    894                (format (oref obj variable) "<name>"))
    895      "Configure branch")))
    896 
    897 (transient-define-suffix magit-branch.<branch>.description (branch)
    898   "Edit the description of BRANCH."
    899   :class 'magit--git-variable
    900   :transient nil
    901   :variable "branch.%s.description"
    902   (interactive (list (oref transient-current-prefix scope)))
    903   (magit-run-git-with-editor "branch" "--edit-description" branch))
    904 
    905 (defclass magit--git-branch:upstream (magit--git-variable)
    906   ((format :initform " %k %m %M\n   %r %R")))
    907 
    908 (transient-define-infix magit-branch.<branch>.merge/remote ()
    909   :class 'magit--git-branch:upstream)
    910 
    911 (cl-defmethod transient-init-value ((obj magit--git-branch:upstream))
    912   (when-let* ((branch (oref (transient-prefix-object) scope))
    913               (remote (magit-get "branch" branch "remote"))
    914               (merge  (magit-get "branch" branch "merge")))
    915     (oset obj value (list remote merge))))
    916 
    917 (cl-defmethod transient-infix-read ((obj magit--git-branch:upstream))
    918   (if (oref obj value)
    919       (oset obj value nil)
    920     (magit-read-upstream-branch (oref (transient-prefix-object) scope)
    921                                 "Upstream")))
    922 
    923 (cl-defmethod transient-infix-set ((obj magit--git-branch:upstream) refname)
    924   (magit-set-upstream-branch (oref (transient-prefix-object) scope) refname)
    925   (oset obj value
    926         (and-let* ((branch (oref (transient-prefix-object) scope))
    927                    (r (magit-get "branch" branch "remote"))
    928                    (m (magit-get "branch" branch "merge")))
    929           (list r m)))
    930   (magit-refresh))
    931 
    932 (cl-defmethod transient-format ((obj magit--git-branch:upstream))
    933   (let ((branch (oref (transient-prefix-object) scope)))
    934     (format-spec
    935      (oref obj format)
    936      `((?k . ,(transient-format-key obj))
    937        (?r . ,(format "branch.%s.remote" branch))
    938        (?m . ,(format "branch.%s.merge" branch))
    939        (?R . ,(transient-format-value obj #'car))
    940        (?M . ,(transient-format-value obj #'cadr))))))
    941 
    942 (cl-defmethod transient-format-value ((obj magit--git-branch:upstream) key)
    943   (if-let ((value (funcall key (oref obj value))))
    944       (propertize value 'face 'transient-argument)
    945     (propertize "unset" 'face 'transient-inactive-argument)))
    946 
    947 (transient-define-infix magit-branch.<branch>.rebase ()
    948   :class 'magit--git-variable:choices
    949   :scope #'magit--read-branch-scope
    950   :variable "branch.%s.rebase"
    951   :fallback "pull.rebase"
    952   :choices '("true" "false")
    953   :default "false")
    954 
    955 (transient-define-infix magit-branch.<branch>.pushRemote ()
    956   :class 'magit--git-variable:choices
    957   :scope #'magit--read-branch-scope
    958   :variable "branch.%s.pushRemote"
    959   :fallback "remote.pushDefault"
    960   :choices #'magit-list-remotes)
    961 
    962 (transient-define-infix magit-pull.rebase ()
    963   :class 'magit--git-variable:choices
    964   :variable "pull.rebase"
    965   :choices '("true" "false")
    966   :default "false")
    967 
    968 (transient-define-infix magit-remote.pushDefault ()
    969   :class 'magit--git-variable:choices
    970   :variable "remote.pushDefault"
    971   :choices #'magit-list-remotes)
    972 
    973 (transient-define-infix magit-branch.autoSetupMerge ()
    974   :class 'magit--git-variable:choices
    975   :variable "branch.autoSetupMerge"
    976   :choices '("always" "true" "false")
    977   :default "true")
    978 
    979 (transient-define-infix magit-branch.autoSetupRebase ()
    980   :class 'magit--git-variable:choices
    981   :variable "branch.autoSetupRebase"
    982   :choices '("always" "local" "remote" "never")
    983   :default "never")
    984 
    985 ;;; _
    986 (provide 'magit-branch)
    987 ;;; magit-branch.el ends here