config

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

magit-branch.el (42793B)


      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 ((branch (magit-read-local-branch "Reset branch"
    553                                           (magit-local-branch-at-point))))
    554      (list branch
    555            (magit-read-branch-or-commit (format "Reset %s to" branch)
    556                                         (magit-get-upstream-branch branch)
    557                                         branch)
    558            current-prefix-arg)))
    559   (let ((magit-inhibit-refresh t))
    560     (if (equal branch (magit-get-current-branch))
    561         (if (and (magit-anything-modified-p)
    562                  (not (yes-or-no-p
    563                        "Uncommitted changes will be lost.  Proceed? ")))
    564             (user-error "Abort")
    565           (magit-reset-hard to))
    566       (magit-call-git "update-ref"
    567                       "-m" (format "reset: moving to %s" to)
    568                       (magit-git-string "rev-parse" "--symbolic-full-name"
    569                                         branch)
    570                       to))
    571     (when (and set-upstream (magit-branch-p to))
    572       (magit-set-upstream-branch branch to)
    573       (magit-branch-maybe-adjust-upstream branch to)))
    574   (magit-refresh))
    575 
    576 (defvar magit-branch-delete-never-verify nil
    577   "Whether `magit-branch-delete' always pushes with \"--no-verify\".")
    578 
    579 ;;;###autoload
    580 (defun magit-branch-delete (branches &optional force)
    581   "Delete one or multiple branches.
    582 
    583 If the region marks multiple branches, then offer to delete
    584 those, otherwise prompt for a single branch to be deleted,
    585 defaulting to the branch at point.
    586 
    587 Require confirmation when deleting branches is dangerous in some
    588 way.  Option `magit-no-confirm' can be customized to not require
    589 confirmation in certain cases.  See its docstring to learn why
    590 confirmation is required by default in certain cases or if a
    591 prompt is confusing."
    592   ;; One would expect this to be a command as simple as, for example,
    593   ;; `magit-branch-rename'; but it turns out everyone wants to squeeze
    594   ;; a bit of extra functionality into this one, including myself.
    595   (interactive
    596    (let ((branches (magit-region-values 'branch t))
    597          (force current-prefix-arg))
    598      (if (length> branches 1)
    599          (magit-confirm t nil "Delete %d branches" nil branches)
    600        (setq branches
    601              (list (magit-read-branch-prefer-other
    602                     (if force "Force delete branch" "Delete branch")))))
    603      (unless force
    604        (when-let ((unmerged (seq-remove #'magit-branch-merged-p branches)))
    605          (if (magit-confirm 'delete-unmerged-branch
    606                "Delete unmerged branch %s"
    607                "Delete %d unmerged branches"
    608                'noabort unmerged)
    609              (setq force branches)
    610            (or (setq branches
    611                      (cl-set-difference branches unmerged :test #'equal))
    612                (user-error "Abort")))))
    613      (list branches force)))
    614   (let* ((refs (mapcar #'magit-ref-fullname branches))
    615          (ambiguous (--remove it refs)))
    616     (when ambiguous
    617       (user-error
    618        "%s ambiguous.  Please cleanup using git directly."
    619        (let ((len (length ambiguous)))
    620          (cond
    621           ((= len 1)
    622            (format "%s is" (seq-find #'magit-ref-ambiguous-p branches)))
    623           ((= len (length refs))
    624            (format "These %s names are" len))
    625           (t
    626            (format "%s of these names are" len))))))
    627     (cond
    628      ((string-match "^refs/remotes/\\([^/]+\\)" (car refs))
    629       (let* ((remote (match-string 1 (car refs)))
    630              (offset (1+ (length remote))))
    631         (cond
    632          ((magit-confirm 'delete-branch-on-remote
    633             (list "Deleting local %s.  Also delete on %s"
    634                   (magit-ref-fullname (car branches))
    635                   remote)
    636             (list "Deleting %d local refs.  Also delete on %s"
    637                   (length refs)
    638                   remote)
    639             'noabort refs)
    640           ;; The ref may actually point at another rev on the remote,
    641           ;; but this is better than nothing.
    642           (dolist (ref refs)
    643             (message "Delete %s (was %s)" ref
    644                      (magit-rev-parse "--short" ref)))
    645           ;; Assume the branches actually still exist on the remote.
    646           (magit-run-git-async
    647            "push"
    648            (and (or force magit-branch-delete-never-verify) "--no-verify")
    649            remote
    650            (--map (concat ":" (substring it offset)) branches))
    651           ;; If that is not the case, then this deletes the tracking branches.
    652           (set-process-sentinel
    653            magit-this-process
    654            (apply-partially #'magit-delete-remote-branch-sentinel remote refs)))
    655          (t
    656           (dolist (ref refs)
    657             (message "Delete %s (was %s)" ref
    658                      (magit-rev-parse "--short" ref))
    659             (magit-call-git "update-ref" "-d" ref))
    660           (magit-refresh)))))
    661      ((length> branches 1)
    662       (setq branches (delete (magit-get-current-branch) branches))
    663       (mapc #'magit-branch-maybe-delete-pr-remote branches)
    664       (mapc #'magit-branch-unset-pushRemote branches)
    665       (magit-run-git "branch" (if force "-D" "-d") branches))
    666      (t ; And now for something completely different.
    667       (let* ((branch (car branches))
    668              (prompt (format "Branch %s is checked out.  " branch))
    669              (target (magit-get-indirect-upstream-branch branch t)))
    670         (when (equal branch (magit-get-current-branch))
    671           (when (or (equal branch target)
    672                     (not target))
    673             (setq target (magit-main-branch)))
    674           (pcase (if (or (equal branch target)
    675                          (not target))
    676                      (magit-read-char-case prompt nil
    677                        (?d "[d]etach HEAD & delete" 'detach)
    678                        (?a "[a]bort"                'abort))
    679                    (magit-read-char-case prompt nil
    680                      (?d "[d]etach HEAD & delete" 'detach)
    681                      (?c (format "[c]heckout %s & delete" target) 'target)
    682                      (?a "[a]bort" 'abort)))
    683             (`detach (unless (or (equal force '(4))
    684                                  (member branch force)
    685                                  (magit-branch-merged-p branch t))
    686                        (magit-confirm 'delete-unmerged-branch
    687                          "Delete unmerged branch %s" ""
    688                          nil (list branch)))
    689                      (magit-call-git "checkout" "--detach"))
    690             (`target (unless (or (equal force '(4))
    691                                  (member branch force)
    692                                  (magit-branch-merged-p branch target))
    693                        (magit-confirm 'delete-unmerged-branch
    694                          "Delete unmerged branch %s" ""
    695                          nil (list branch)))
    696                      (magit-call-git "checkout" target))
    697             (`abort  (user-error "Abort")))
    698           (setq force t))
    699         (magit-branch-maybe-delete-pr-remote branch)
    700         (magit-branch-unset-pushRemote branch)
    701         (magit-run-git "branch" (if force "-D" "-d") branch))))))
    702 
    703 (put 'magit-branch-delete 'interactive-only t)
    704 
    705 (defun magit-branch-maybe-delete-pr-remote (branch)
    706   (when-let ((remote (magit-get "branch" branch "pullRequestRemote")))
    707     (let* ((variable (format "remote.%s.fetch" remote))
    708            (refspecs (magit-get-all variable)))
    709       (unless (member (format "+refs/heads/*:refs/remotes/%s/*" remote)
    710                       refspecs)
    711         (let ((refspec
    712                (if (equal (magit-get "branch" branch "pushRemote") remote)
    713                    (format "+refs/heads/%s:refs/remotes/%s/%s"
    714                            branch remote branch)
    715                  (let ((merge (magit-get "branch" branch "merge")))
    716                    (and merge
    717                         (string-prefix-p "refs/heads/" merge)
    718                         (setq merge (substring merge 11))
    719                         (format "+refs/heads/%s:refs/remotes/%s/%s"
    720                                 merge remote merge))))))
    721           (when (member refspec refspecs)
    722             (if (and (length= refspecs 1)
    723                      (magit-confirm 'delete-pr-remote
    724                        (list "Also delete remote %s (%s)" remote
    725                              "no pull-request branch remains")
    726                        nil t))
    727                 (magit-call-git "remote" "rm" remote)
    728               (magit-call-git "config" "--unset-all" variable
    729                               (format "^%s$" (regexp-quote refspec))))))))))
    730 
    731 (defun magit-branch-unset-pushRemote (branch)
    732   (magit-set nil "branch" branch "pushRemote"))
    733 
    734 (defun magit-delete-remote-branch-sentinel (remote refs process event)
    735   (when (memq (process-status process) '(exit signal))
    736     (if (= (process-exit-status process) 1)
    737         (if-let ((on-remote (--map (concat "refs/remotes/" remote "/" it)
    738                                    (magit-remote-list-branches remote)))
    739                  (rest (--filter (and (not (member it on-remote))
    740                                       (magit-ref-exists-p it))
    741                                  refs)))
    742             (progn
    743               (process-put process 'inhibit-refresh t)
    744               (magit-process-sentinel process event)
    745               (setq magit-this-error nil)
    746               (message "Some remote branches no longer exist.  %s"
    747                        "Deleting just the local tracking refs instead...")
    748               (dolist (ref rest)
    749                 (magit-call-git "update-ref" "-d" ref))
    750               (magit-refresh)
    751               (message "Deleting local remote-tracking refs...done"))
    752           (magit-process-sentinel process event))
    753       (magit-process-sentinel process event))))
    754 
    755 ;;;###autoload
    756 (defun magit-branch-rename (old new &optional force)
    757   "Rename the branch named OLD to NEW.
    758 
    759 With a prefix argument FORCE, rename even if a branch named NEW
    760 already exists.
    761 
    762 If `branch.OLD.pushRemote' is set, then unset it.  Depending on
    763 the value of `magit-branch-rename-push-target' (which see) maybe
    764 set `branch.NEW.pushRemote' and maybe rename the push-target on
    765 the remote."
    766   (interactive
    767    (let ((branch (magit-read-local-branch "Rename branch")))
    768      (list branch
    769            (magit-read-string-ns (format "Rename branch '%s' to" branch)
    770                                  nil 'magit-revision-history)
    771            current-prefix-arg)))
    772   (when (string-match "\\`heads/\\(.+\\)" old)
    773     (setq old (match-string 1 old)))
    774   (when (equal old new)
    775     (user-error "Old and new branch names are the same"))
    776   (magit-call-git "branch" (if force "-M" "-m") old new)
    777   (when magit-branch-rename-push-target
    778     (let ((remote (magit-get-push-remote old))
    779           (old-specified (magit-get "branch" old "pushRemote"))
    780           (new-specified (magit-get "branch" new "pushRemote")))
    781       (when (and old-specified (or force (not new-specified)))
    782         ;; Keep the target setting branch specified, even if that is
    783         ;; redundant.  But if a branch by the same name existed before
    784         ;; and the rename isn't forced, then do not change a leftover
    785         ;; setting.  Such a leftover setting may or may not conform to
    786         ;; what we expect here...
    787         (magit-set old-specified "branch" new "pushRemote"))
    788       (when (and (equal (magit-get-push-remote new) remote)
    789                  ;; ...and if it does not, then we must abort.
    790                  (not (eq magit-branch-rename-push-target 'local-only))
    791                  (or (not (eq magit-branch-rename-push-target 'forge-only))
    792                      (and (require (quote forge) nil t)
    793                           (fboundp 'forge--split-forge-url)
    794                           (and-let* ((url (magit-git-string
    795                                            "remote" "get-url" remote)))
    796                             (forge--split-forge-url url)))))
    797         (let ((old-target (magit-get-push-branch old t))
    798               (new-target (magit-get-push-branch new t))
    799               (remote (magit-get-push-remote new)))
    800           (when (and old-target
    801                      (not new-target)
    802                      (magit-y-or-n-p (format "Also rename %S to %S on \"%s\""
    803                                              old new remote)))
    804             ;; Rename on (i.e., within) the remote, but only if the
    805             ;; destination ref doesn't exist yet.  If that ref already
    806             ;; exists, then it probably is of some value and we better
    807             ;; not touch it.  Ignore what the local ref points at,
    808             ;; i.e., if the local and the remote ref didn't point at
    809             ;; the same commit before the rename then keep it that way.
    810             (magit-call-git "push" "-v" remote
    811                             (format "%s:refs/heads/%s" old-target new)
    812                             (format ":refs/heads/%s" old)))))))
    813   (magit-branch-unset-pushRemote old)
    814   (magit-refresh))
    815 
    816 ;;;###autoload
    817 (defun magit-branch-shelve (branch)
    818   "Shelve a BRANCH.
    819 Rename \"refs/heads/BRANCH\" to \"refs/shelved/BRANCH\",
    820 and also rename the respective reflog file."
    821   (interactive (list (magit-read-other-local-branch "Shelve branch")))
    822   (let ((old (concat "refs/heads/"   branch))
    823         (new (concat "refs/shelved/" branch)))
    824     (magit-git "update-ref" new old "")
    825     (magit--rename-reflog-file old new)
    826     (magit-branch-unset-pushRemote branch)
    827     (magit-run-git "branch" "-D" branch)))
    828 
    829 ;;;###autoload
    830 (defun magit-branch-unshelve (branch)
    831   "Unshelve a BRANCH
    832 Rename \"refs/shelved/BRANCH\" to \"refs/heads/BRANCH\",
    833 and also rename the respective reflog file."
    834   (interactive
    835    (list (magit-completing-read
    836           "Unshelve branch"
    837           (--map (substring it 8)
    838                  (magit-list-refnames "refs/shelved"))
    839           nil t)))
    840   (let ((old (concat "refs/shelved/" branch))
    841         (new (concat "refs/heads/"   branch)))
    842     (magit-git "update-ref" new old "")
    843     (magit--rename-reflog-file old new)
    844     (magit-run-git "update-ref" "-d" old)))
    845 
    846 (defun magit--rename-reflog-file (old new)
    847   (let* ((dir (magit-gitdir))
    848          (old (expand-file-name (concat "logs/" old) dir))
    849          (new (expand-file-name (concat "logs/" new) dir)))
    850     (when (file-exists-p old)
    851       (make-directory (file-name-directory new) t)
    852       (rename-file old new t))))
    853 
    854 ;;; Configure
    855 
    856 ;;;###autoload (autoload 'magit-branch-configure "magit-branch" nil t)
    857 (transient-define-prefix magit-branch-configure (branch)
    858   "Configure a branch."
    859   :man-page "git-branch"
    860   [:description
    861    (lambda ()
    862      (concat (propertize "Configure " 'face 'transient-heading)
    863              (propertize (oref (transient-prefix-object) scope)
    864                          'face 'magit-branch-local)))
    865    ("d" magit-branch.<branch>.description)
    866    ("u" magit-branch.<branch>.merge/remote)
    867    ("r" magit-branch.<branch>.rebase)
    868    ("p" magit-branch.<branch>.pushRemote)]
    869   ["Configure repository defaults"
    870    ("R" magit-pull.rebase)
    871    ("P" magit-remote.pushDefault)
    872    ("B" "Update default branch" magit-update-default-branch
    873     :inapt-if-not magit-get-some-remote)]
    874   ["Configure branch creation"
    875    ("a m" magit-branch.autoSetupMerge)
    876    ("a r" magit-branch.autoSetupRebase)]
    877   (interactive
    878    (list (or (and (not current-prefix-arg)
    879                   (not (and magit-branch-direct-configure
    880                             (eq transient-current-command 'magit-branch)))
    881                   (magit-get-current-branch))
    882              (magit--read-branch-scope))))
    883   (transient-setup 'magit-branch-configure nil nil :scope branch))
    884 
    885 (defun magit--read-branch-scope (&optional obj)
    886   (magit-read-local-branch
    887    (if obj
    888        (format "Set %s for branch"
    889                (format (oref obj variable) "<name>"))
    890      "Configure branch")))
    891 
    892 (transient-define-suffix magit-branch.<branch>.description (branch)
    893   "Edit the description of BRANCH."
    894   :class 'magit--git-variable
    895   :transient nil
    896   :variable "branch.%s.description"
    897   (interactive (list (oref transient-current-prefix scope)))
    898   (magit-run-git-with-editor "branch" "--edit-description" branch))
    899 
    900 (defclass magit--git-branch:upstream (magit--git-variable)
    901   ((format :initform " %k %m %M\n   %r %R")))
    902 
    903 (transient-define-infix magit-branch.<branch>.merge/remote ()
    904   :class 'magit--git-branch:upstream)
    905 
    906 (cl-defmethod transient-init-value ((obj magit--git-branch:upstream))
    907   (when-let* ((branch (oref (transient-prefix-object) scope))
    908               (remote (magit-get "branch" branch "remote"))
    909               (merge  (magit-get "branch" branch "merge")))
    910     (oset obj value (list remote merge))))
    911 
    912 (cl-defmethod transient-infix-read ((obj magit--git-branch:upstream))
    913   (if (oref obj value)
    914       (oset obj value nil)
    915     (magit-read-upstream-branch (oref (transient-prefix-object) scope)
    916                                 "Upstream")))
    917 
    918 (cl-defmethod transient-infix-set ((obj magit--git-branch:upstream) refname)
    919   (magit-set-upstream-branch (oref (transient-prefix-object) scope) refname)
    920   (oset obj value
    921         (and-let* ((branch (oref (transient-prefix-object) scope))
    922                    (r (magit-get "branch" branch "remote"))
    923                    (m (magit-get "branch" branch "merge")))
    924           (list r m)))
    925   (magit-refresh))
    926 
    927 (cl-defmethod transient-format ((obj magit--git-branch:upstream))
    928   (let ((branch (oref (transient-prefix-object) scope)))
    929     (format-spec
    930      (oref obj format)
    931      `((?k . ,(transient-format-key obj))
    932        (?r . ,(format "branch.%s.remote" branch))
    933        (?m . ,(format "branch.%s.merge" branch))
    934        (?R . ,(transient-format-value obj #'car))
    935        (?M . ,(transient-format-value obj #'cadr))))))
    936 
    937 (cl-defmethod transient-format-value ((obj magit--git-branch:upstream) key)
    938   (if-let ((value (funcall key (oref obj value))))
    939       (propertize value 'face 'transient-argument)
    940     (propertize "unset" 'face 'transient-inactive-argument)))
    941 
    942 (transient-define-infix magit-branch.<branch>.rebase ()
    943   :class 'magit--git-variable:choices
    944   :scope #'magit--read-branch-scope
    945   :variable "branch.%s.rebase"
    946   :fallback "pull.rebase"
    947   :choices '("true" "false")
    948   :default "false")
    949 
    950 (transient-define-infix magit-branch.<branch>.pushRemote ()
    951   :class 'magit--git-variable:choices
    952   :scope #'magit--read-branch-scope
    953   :variable "branch.%s.pushRemote"
    954   :fallback "remote.pushDefault"
    955   :choices #'magit-list-remotes)
    956 
    957 (transient-define-infix magit-pull.rebase ()
    958   :class 'magit--git-variable:choices
    959   :variable "pull.rebase"
    960   :choices '("true" "false")
    961   :default "false")
    962 
    963 (transient-define-infix magit-remote.pushDefault ()
    964   :class 'magit--git-variable:choices
    965   :variable "remote.pushDefault"
    966   :choices #'magit-list-remotes)
    967 
    968 (transient-define-infix magit-branch.autoSetupMerge ()
    969   :class 'magit--git-variable:choices
    970   :variable "branch.autoSetupMerge"
    971   :choices '("always" "true" "false")
    972   :default "true")
    973 
    974 (transient-define-infix magit-branch.autoSetupRebase ()
    975   :class 'magit--git-variable:choices
    976   :variable "branch.autoSetupRebase"
    977   :choices '("always" "local" "remote" "never")
    978   :default "never")
    979 
    980 ;;; _
    981 (provide 'magit-branch)
    982 ;;; magit-branch.el ends here