Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

884 lines
38 KiB

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