magit-pull.el (6369B)
1 ;;; magit-pull.el --- Update local objects and refs -*- 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 pull commands. 26 27 ;;; Code: 28 29 (require 'magit) 30 31 ;;; Options 32 33 (defcustom magit-pull-or-fetch nil 34 "Whether `magit-pull' also offers some fetch suffixes." 35 :package-version '(magit . "3.0.0") 36 :group 'magit-commands 37 :type 'boolean) 38 39 ;;; Commands 40 41 ;;;###autoload (autoload 'magit-pull "magit-pull" nil t) 42 (transient-define-prefix magit-pull () 43 "Pull from another repository." 44 :man-page "git-pull" 45 :incompatible '(("--ff-only" "--rebase")) 46 [:description 47 (lambda () (if magit-pull-or-fetch "Pull arguments" "Arguments")) 48 ("-f" "Fast-forward only" "--ff-only") 49 ("-r" "Rebase local commits" ("-r" "--rebase")) 50 ("-A" "Autostash" "--autostash" :level 7) 51 ("-F" "Force" ("-f" "--force"))] 52 [:description 53 (lambda () 54 (if-let ((branch (magit-get-current-branch))) 55 (concat 56 (propertize "Pull into " 'face 'transient-heading) 57 (propertize branch 'face 'magit-branch-local) 58 (propertize " from" 'face 'transient-heading)) 59 (propertize "Pull from" 'face 'transient-heading))) 60 ("p" magit-pull-from-pushremote) 61 ("u" magit-pull-from-upstream) 62 ("e" "elsewhere" magit-pull-branch)] 63 ["Fetch from" 64 :if-non-nil magit-pull-or-fetch 65 ("f" "remotes" magit-fetch-all-no-prune) 66 ("F" "remotes and prune" magit-fetch-all-prune)] 67 ["Fetch" 68 :if-non-nil magit-pull-or-fetch 69 ("o" "another branch" magit-fetch-branch) 70 ("s" "explicit refspec" magit-fetch-refspec) 71 ("m" "submodules" magit-fetch-modules)] 72 ["Configure" 73 ("r" magit-branch.<branch>.rebase :if magit-get-current-branch) 74 ("C" "variables..." magit-branch-configure)] 75 (interactive) 76 (transient-setup 'magit-pull nil nil :scope (magit-get-current-branch))) 77 78 (defun magit-pull-arguments () 79 (transient-args 'magit-pull)) 80 81 ;;;###autoload (autoload 'magit-pull-from-pushremote "magit-pull" nil t) 82 (transient-define-suffix magit-pull-from-pushremote (args) 83 "Pull from the push-remote of the current branch. 84 85 With a prefix argument or when the push-remote is either not 86 configured or unusable, then let the user first configure the 87 push-remote." 88 :if #'magit-get-current-branch 89 :description #'magit-pull--pushbranch-description 90 (interactive (list (magit-pull-arguments))) 91 (pcase-let ((`(,branch ,remote) 92 (magit--select-push-remote "pull from there"))) 93 (run-hooks 'magit-credential-hook) 94 (magit-run-git-with-editor "pull" args remote branch))) 95 96 (defun magit-pull--pushbranch-description () 97 ;; Also used by `magit-rebase-onto-pushremote'. 98 (let* ((branch (magit-get-current-branch)) 99 (target (magit-get-push-branch branch t)) 100 (remote (magit-get-push-remote branch)) 101 (v (magit--push-remote-variable branch t))) 102 (cond 103 (target) 104 ((member remote (magit-list-remotes)) 105 (format "%s, replacing non-existent" v)) 106 (remote 107 (format "%s, replacing invalid" v)) 108 (t 109 (format "%s, setting that" v))))) 110 111 ;;;###autoload (autoload 'magit-pull-from-upstream "magit-pull" nil t) 112 (transient-define-suffix magit-pull-from-upstream (args) 113 "Pull from the upstream of the current branch. 114 115 With a prefix argument or when the upstream is either not 116 configured or unusable, then let the user first configure 117 the upstream." 118 :if #'magit-get-current-branch 119 :description #'magit-pull--upstream-description 120 (interactive (list (magit-pull-arguments))) 121 (let* ((branch (or (magit-get-current-branch) 122 (user-error "No branch is checked out"))) 123 (remote (magit-get "branch" branch "remote")) 124 (merge (magit-get "branch" branch "merge"))) 125 (when (or current-prefix-arg 126 (not (or (magit-get-upstream-branch branch) 127 (magit--unnamed-upstream-p remote merge)))) 128 (magit-set-upstream-branch 129 branch (magit-read-upstream-branch 130 branch (format "Set upstream of %s and pull from there" branch))) 131 (setq remote (magit-get "branch" branch "remote")) 132 (setq merge (magit-get "branch" branch "merge"))) 133 (run-hooks 'magit-credential-hook) 134 (magit-run-git-with-editor "pull" args remote merge))) 135 136 (defun magit-pull--upstream-description () 137 (and-let* ((branch (magit-get-current-branch))) 138 (or (magit-get-upstream-branch branch) 139 (let ((remote (magit-get "branch" branch "remote")) 140 (merge (magit-get "branch" branch "merge")) 141 (u (magit--propertize-face "@{upstream}" 'bold))) 142 (cond 143 ((magit--unnamed-upstream-p remote merge) 144 (format "%s of %s" 145 (magit--propertize-face merge 'magit-branch-remote) 146 (magit--propertize-face remote 'bold))) 147 ((magit--valid-upstream-p remote merge) 148 (concat u ", replacing non-existent")) 149 ((or remote merge) 150 (concat u ", replacing invalid")) 151 (t 152 (concat u ", setting that"))))))) 153 154 ;;;###autoload 155 (defun magit-pull-branch (source args) 156 "Pull from a branch read in the minibuffer." 157 (interactive (list (magit-read-remote-branch "Pull" nil nil nil t) 158 (magit-pull-arguments))) 159 (run-hooks 'magit-credential-hook) 160 (pcase-let ((`(,remote . ,branch) 161 (magit-get-tracked source))) 162 (magit-run-git-with-editor "pull" args remote branch))) 163 164 ;;; _ 165 (provide 'magit-pull) 166 ;;; magit-pull.el ends here