config

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

magit-pull.el (6334B)


      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   [:description
     52    (lambda ()
     53      (if-let ((branch (magit-get-current-branch)))
     54          (concat
     55           (propertize "Pull into " 'face 'transient-heading)
     56           (propertize branch       'face 'magit-branch-local)
     57           (propertize " from"      'face 'transient-heading))
     58        (propertize "Pull from" 'face 'transient-heading)))
     59    ("p" magit-pull-from-pushremote)
     60    ("u" magit-pull-from-upstream)
     61    ("e" "elsewhere"         magit-pull-branch)]
     62   ["Fetch from"
     63    :if-non-nil magit-pull-or-fetch
     64    ("f" "remotes"           magit-fetch-all-no-prune)
     65    ("F" "remotes and prune" magit-fetch-all-prune)]
     66   ["Fetch"
     67    :if-non-nil magit-pull-or-fetch
     68    ("o" "another branch"    magit-fetch-branch)
     69    ("s" "explicit refspec"  magit-fetch-refspec)
     70    ("m" "submodules"        magit-fetch-modules)]
     71   ["Configure"
     72    ("r" magit-branch.<branch>.rebase :if magit-get-current-branch)
     73    ("C" "variables..." magit-branch-configure)]
     74   (interactive)
     75   (transient-setup 'magit-pull nil nil :scope (magit-get-current-branch)))
     76 
     77 (defun magit-pull-arguments ()
     78   (transient-args 'magit-pull))
     79 
     80 ;;;###autoload (autoload 'magit-pull-from-pushremote "magit-pull" nil t)
     81 (transient-define-suffix magit-pull-from-pushremote (args)
     82   "Pull from the push-remote of the current branch.
     83 
     84 With a prefix argument or when the push-remote is either not
     85 configured or unusable, then let the user first configure the
     86 push-remote."
     87   :if #'magit-get-current-branch
     88   :description #'magit-pull--pushbranch-description
     89   (interactive (list (magit-pull-arguments)))
     90   (pcase-let ((`(,branch ,remote)
     91                (magit--select-push-remote "pull from there")))
     92     (run-hooks 'magit-credential-hook)
     93     (magit-run-git-with-editor "pull" args remote branch)))
     94 
     95 (defun magit-pull--pushbranch-description ()
     96   ;; Also used by `magit-rebase-onto-pushremote'.
     97   (let* ((branch (magit-get-current-branch))
     98          (target (magit-get-push-branch branch t))
     99          (remote (magit-get-push-remote branch))
    100          (v (magit--push-remote-variable branch t)))
    101     (cond
    102      (target)
    103      ((member remote (magit-list-remotes))
    104       (format "%s, replacing non-existent" v))
    105      (remote
    106       (format "%s, replacing invalid" v))
    107      (t
    108       (format "%s, setting that" v)))))
    109 
    110 ;;;###autoload (autoload 'magit-pull-from-upstream "magit-pull" nil t)
    111 (transient-define-suffix magit-pull-from-upstream (args)
    112   "Pull from the upstream of the current branch.
    113 
    114 With a prefix argument or when the upstream is either not
    115 configured or unusable, then let the user first configure
    116 the upstream."
    117   :if #'magit-get-current-branch
    118   :description #'magit-pull--upstream-description
    119   (interactive (list (magit-pull-arguments)))
    120   (let* ((branch (or (magit-get-current-branch)
    121                      (user-error "No branch is checked out")))
    122          (remote (magit-get "branch" branch "remote"))
    123          (merge  (magit-get "branch" branch "merge")))
    124     (when (or current-prefix-arg
    125               (not (or (magit-get-upstream-branch branch)
    126                        (magit--unnamed-upstream-p remote merge))))
    127       (magit-set-upstream-branch
    128        branch (magit-read-upstream-branch
    129                branch (format "Set upstream of %s and pull from there" branch)))
    130       (setq remote (magit-get "branch" branch "remote"))
    131       (setq merge  (magit-get "branch" branch "merge")))
    132     (run-hooks 'magit-credential-hook)
    133     (magit-run-git-with-editor "pull" args remote merge)))
    134 
    135 (defun magit-pull--upstream-description ()
    136   (and-let* ((branch (magit-get-current-branch)))
    137     (or (magit-get-upstream-branch branch)
    138         (let ((remote (magit-get "branch" branch "remote"))
    139               (merge  (magit-get "branch" branch "merge"))
    140               (u (magit--propertize-face "@{upstream}" 'bold)))
    141           (cond
    142            ((magit--unnamed-upstream-p remote merge)
    143             (format "%s of %s"
    144                     (magit--propertize-face merge 'magit-branch-remote)
    145                     (magit--propertize-face remote 'bold)))
    146            ((magit--valid-upstream-p remote merge)
    147             (concat u ", replacing non-existent"))
    148            ((or remote merge)
    149             (concat u ", replacing invalid"))
    150            (t
    151             (concat u ", setting that")))))))
    152 
    153 ;;;###autoload
    154 (defun magit-pull-branch (source args)
    155   "Pull from a branch read in the minibuffer."
    156   (interactive (list (magit-read-remote-branch "Pull" nil nil nil t)
    157                      (magit-pull-arguments)))
    158   (run-hooks 'magit-credential-hook)
    159   (pcase-let ((`(,remote . ,branch)
    160                (magit-get-tracked source)))
    161     (magit-run-git-with-editor "pull" args remote branch)))
    162 
    163 ;;; _
    164 (provide 'magit-pull)
    165 ;;; magit-pull.el ends here