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