config

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

magit-push.el (16440B)


      1 ;;; magit-push.el --- Update remote 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 push commands.
     26 
     27 ;;; Code:
     28 
     29 (require 'magit)
     30 
     31 ;;; Commands
     32 
     33 ;;;###autoload (autoload 'magit-push "magit-push" nil t)
     34 (transient-define-prefix magit-push ()
     35   "Push to another repository."
     36   :man-page "git-push"
     37   ["Arguments"
     38    ("-f" "Force with lease" (nil "--force-with-lease"))
     39    ("-F" "Force"            ("-f" "--force"))
     40    ("-h" "Disable hooks"    "--no-verify")
     41    ("-n" "Dry run"          ("-n" "--dry-run"))
     42    (5 "-u" "Set upstream"   "--set-upstream")
     43    (7 "-t" "Follow tags"    "--follow-tags")]
     44   [:if magit-get-current-branch
     45    :description (lambda ()
     46                   (format (propertize "Push %s to" 'face 'transient-heading)
     47                           (propertize (magit-get-current-branch)
     48                                       'face 'magit-branch-local)))
     49    ("p" magit-push-current-to-pushremote)
     50    ("u" magit-push-current-to-upstream)
     51    ("e" "elsewhere" magit-push-current)]
     52   ["Push"
     53    [("o" "another branch"    magit-push-other)
     54     ("r" "explicit refspecs" magit-push-refspecs)
     55     ("m" "matching branches" magit-push-matching)]
     56    [("T" "a tag"             magit-push-tag)
     57     ("t" "all tags"          magit-push-tags)
     58     (6 "n" "a note ref"      magit-push-notes-ref)]]
     59   ["Configure"
     60    ("C" "Set variables..."  magit-branch-configure)])
     61 
     62 (defun magit-push-arguments ()
     63   (transient-args 'magit-push))
     64 
     65 (defun magit-git-push (branch target args)
     66   (run-hooks 'magit-credential-hook)
     67   ;; If the remote branch already exists, then we do not have to
     68   ;; qualify the target, which we prefer to avoid doing because
     69   ;; using the default namespace is wrong in obscure cases.
     70   (pcase-let ((namespace (if (magit-get-tracked target) "" "refs/heads/"))
     71               (`(,remote . ,target)
     72                (magit-split-branch-name target)))
     73     (magit-run-git-async "push" "-v" args remote
     74                          (format "%s:%s%s" branch namespace target))))
     75 
     76 ;;;###autoload (autoload 'magit-push-current-to-pushremote "magit-push" nil t)
     77 (transient-define-suffix magit-push-current-to-pushremote (args)
     78   "Push the current branch to its push-remote.
     79 
     80 When the push-remote is not configured, then read the push-remote
     81 from the user, set it, and then push to it.  With a prefix
     82 argument the push-remote can be changed before pushed to it."
     83   :if #'magit-get-current-branch
     84   :description #'magit-push--pushbranch-description
     85   (interactive (list (magit-push-arguments)))
     86   (pcase-let ((`(,branch ,remote ,changed)
     87                (magit--select-push-remote "push there")))
     88     (when changed
     89       (magit-confirm 'set-and-push
     90         (list "Really use \"%s\" as push-remote and push \"%s\" there"
     91               remote branch)))
     92     (run-hooks 'magit-credential-hook)
     93     (magit-run-git-async "push" "-v" args remote
     94                          (format "refs/heads/%s:refs/heads/%s"
     95                                  branch branch)))) ; see #3847 and #3872
     96 
     97 (defun magit-push--pushbranch-description ()
     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, creating it"
    106               (magit--propertize-face (concat remote "/" branch)
    107                                       'magit-branch-remote)))
    108      (remote
    109       (format "%s, replacing invalid" v))
    110      (t
    111       (format "%s, setting that" v)))))
    112 
    113 ;;;###autoload (autoload 'magit-push-current-to-upstream "magit-push" nil t)
    114 (transient-define-suffix magit-push-current-to-upstream (args)
    115   "Push the current branch to its upstream branch.
    116 
    117 With a prefix argument or when the upstream is either not
    118 configured or unusable, then let the user first configure
    119 the upstream."
    120   :if #'magit-get-current-branch
    121   :description #'magit-push--upstream-description
    122   (interactive (list (magit-push-arguments)))
    123   (let* ((branch (or (magit-get-current-branch)
    124                      (user-error "No branch is checked out")))
    125          (remote (magit-get "branch" branch "remote"))
    126          (merge  (magit-get "branch" branch "merge")))
    127     (when (or current-prefix-arg
    128               (not (or (magit-get-upstream-branch branch)
    129                        (magit--unnamed-upstream-p remote merge)
    130                        (magit--valid-upstream-p remote merge))))
    131       (let* ((branches (cl-union (--map (concat it "/" branch)
    132                                         (magit-list-remotes))
    133                                  (magit-list-remote-branch-names)
    134                                  :test #'equal))
    135              (upstream (magit-completing-read
    136                         (format "Set upstream of %s and push there" branch)
    137                         branches nil nil nil 'magit-revision-history
    138                         (or (car (member (magit-remote-branch-at-point) branches))
    139                             (car (member "origin/master" branches)))))
    140              (upstream* (or (magit-get-tracked upstream)
    141                             (magit-split-branch-name upstream))))
    142         (setq remote (car upstream*))
    143         (setq merge  (cdr upstream*))
    144         (unless (string-prefix-p "refs/" merge)
    145           ;; User selected a non-existent remote-tracking branch.
    146           ;; It is very likely, but not certain, that this is the
    147           ;; correct thing to do.  It is even more likely that it
    148           ;; is what the user wants to happen.
    149           (setq merge (concat "refs/heads/" merge)))
    150         (magit-confirm 'set-and-push
    151           (list "Really use \"%s\" as upstream and push \"%s\" there"
    152                 upstream branch)))
    153       (cl-pushnew "--set-upstream" args :test #'equal))
    154     (run-hooks 'magit-credential-hook)
    155     (magit-run-git-async "push" "-v" args remote (concat branch ":" merge))))
    156 
    157 (defun magit-push--upstream-description ()
    158   (and-let* ((branch (magit-get-current-branch)))
    159     (or (magit-get-upstream-branch branch)
    160         (let ((remote (magit-get "branch" branch "remote"))
    161               (merge  (magit-get "branch" branch "merge"))
    162               (u (magit--propertize-face "@{upstream}" 'bold)))
    163           (cond
    164            ((magit--unnamed-upstream-p remote merge)
    165             (format "%s as %s"
    166                     (magit--propertize-face remote 'bold)
    167                     (magit--propertize-face merge 'magit-branch-remote)))
    168            ((magit--valid-upstream-p remote merge)
    169             (format "%s creating %s"
    170                     (magit--propertize-face remote 'magit-branch-remote)
    171                     (magit--propertize-face merge 'magit-branch-remote)))
    172            ((or remote merge)
    173             (concat u ", creating it and replacing invalid"))
    174            (t
    175             (concat u ", creating it")))))))
    176 
    177 ;;;###autoload
    178 (defun magit-push-current (target args)
    179   "Push the current branch to a branch read in the minibuffer."
    180   (interactive
    181    (if-let ((current (magit-get-current-branch)))
    182        (list (magit-read-remote-branch (format "Push %s to" current)
    183                                        nil nil current 'confirm)
    184              (magit-push-arguments))
    185      (user-error "No branch is checked out")))
    186   (magit-git-push (magit-get-current-branch) target args))
    187 
    188 ;;;###autoload
    189 (defun magit-push-other (source target args)
    190   "Push an arbitrary branch or commit somewhere.
    191 Both the source and the target are read in the minibuffer."
    192   (interactive
    193    (let ((source (magit-read-local-branch-or-commit "Push")))
    194      (list source
    195            (magit-read-remote-branch
    196             (format "Push %s to" source) nil
    197             (if (magit-local-branch-p source)
    198                 (or (magit-get-push-branch source)
    199                     (magit-get-upstream-branch source))
    200               (and (magit-rev-ancestor-p source "HEAD")
    201                    (or (magit-get-push-branch)
    202                        (magit-get-upstream-branch))))
    203             source 'confirm)
    204            (magit-push-arguments))))
    205   (magit-git-push source target args))
    206 
    207 (defvar magit-push-refspecs-history nil)
    208 
    209 ;;;###autoload
    210 (defun magit-push-refspecs (remote refspecs args)
    211   "Push one or multiple REFSPECS to a REMOTE.
    212 Both the REMOTE and the REFSPECS are read in the minibuffer.  To
    213 use multiple REFSPECS, separate them with commas.  Completion is
    214 only available for the part before the colon, or when no colon
    215 is used."
    216   (interactive
    217    (list (magit-read-remote "Push to remote")
    218          (magit-completing-read-multiple
    219           "Push refspec,s: "
    220           (cons "HEAD" (magit-list-local-branch-names))
    221           nil nil nil 'magit-push-refspecs-history)
    222          (magit-push-arguments)))
    223   (run-hooks 'magit-credential-hook)
    224   (magit-run-git-async "push" "-v" args remote refspecs))
    225 
    226 ;;;###autoload
    227 (defun magit-push-matching (remote &optional args)
    228   "Push all matching branches to another repository.
    229 If multiple remotes exist, then read one from the user.
    230 If just one exists, use that without requiring confirmation."
    231   (interactive (list (magit-read-remote "Push matching branches to" nil t)
    232                      (magit-push-arguments)))
    233   (run-hooks 'magit-credential-hook)
    234   (magit-run-git-async "push" "-v" args remote ":"))
    235 
    236 ;;;###autoload
    237 (defun magit-push-tags (remote &optional args)
    238   "Push all tags to another repository.
    239 If only one remote exists, then push to that.  Otherwise prompt
    240 for a remote, offering the remote configured for the current
    241 branch as default."
    242   (interactive (list (magit-read-remote "Push tags to remote" nil t)
    243                      (magit-push-arguments)))
    244   (run-hooks 'magit-credential-hook)
    245   (magit-run-git-async "push" remote "--tags" args))
    246 
    247 ;;;###autoload
    248 (defun magit-push-tag (tag remote &optional args)
    249   "Push a tag to another repository."
    250   (interactive
    251    (let  ((tag (magit-read-tag "Push tag")))
    252      (list tag (magit-read-remote (format "Push %s to remote" tag) nil t)
    253            (magit-push-arguments))))
    254   (run-hooks 'magit-credential-hook)
    255   (magit-run-git-async "push" remote tag args))
    256 
    257 ;;;###autoload
    258 (defun magit-push-notes-ref (ref remote &optional args)
    259   "Push a notes ref to another repository."
    260   (interactive
    261    (let ((note (magit-notes-read-ref "Push notes" nil nil)))
    262      (list note
    263            (magit-read-remote (format "Push %s to remote" note) nil t)
    264            (magit-push-arguments))))
    265   (run-hooks 'magit-credential-hook)
    266   (magit-run-git-async "push" remote ref args))
    267 
    268 ;;;###autoload (autoload 'magit-push-implicitly "magit-push" nil t)
    269 (transient-define-suffix magit-push-implicitly (args)
    270   "Push somewhere without using an explicit refspec.
    271 
    272 This command simply runs \"git push -v [ARGS]\".  ARGS are the
    273 arguments specified in the popup buffer.  No explicit refspec
    274 arguments are used.  Instead the behavior depends on at least
    275 these Git variables: `push.default', `remote.pushDefault',
    276 `branch.<branch>.pushRemote', `branch.<branch>.remote',
    277 `branch.<branch>.merge', and `remote.<remote>.push'.
    278 
    279 If you add this suffix to a transient prefix without explicitly
    280 specifying the description, then an attempt is made to predict
    281 what this command will do.  To add it use something like:
    282 
    283   (transient-insert-suffix \\='magit-push \"o\"
    284     \\='(\"i\" magit-push-implicitly))"
    285   :description #'magit-push-implicitly--desc
    286   (interactive (list (magit-push-arguments)))
    287   (run-hooks 'magit-credential-hook)
    288   (magit-run-git-async "push" "-v" args))
    289 
    290 (defun magit-push-implicitly--desc ()
    291   ;; This implements the logic for git push as documented.
    292   ;; First, we resolve a remote to use based on various remote and
    293   ;; pushRemote options.
    294   ;; Then, we resolve the refspec to use for the remote based on push
    295   ;; and pushDefault options.
    296   ;; Note that the remote and refspec to push are handled separately,
    297   ;; so it doesn't make sense to talk about "pushing to upstream".
    298   ;; Depending on the options, you could end up pushing to the
    299   ;; "upstream" remote but not the "upstream" branch, and vice versa.
    300   (let* ((branch (magit-get-current-branch))
    301          (remote (or (magit-get-push-remote branch)
    302                      ;; Note: Avoid `magit-get-remote' because it
    303                      ;; filters out the local repo case (".").
    304                      (magit-get "branch" branch "remote")
    305                      (let ((remotes (magit-list-remotes)))
    306                        (cond
    307                         ((and (magit-git-version>= "2.27")
    308                               (= (length remotes) 1))
    309                          (car remotes))
    310                         ((member "origin" remotes) "origin"))))))
    311     (if (null remote)
    312         "nothing (no remote)"
    313       (let ((refspec (magit-get "remote" remote "push")))
    314         (if refspec
    315             (format "to %s with refspecs %s"
    316                     (magit--propertize-face remote 'bold)
    317                     (magit--propertize-face refspec 'bold))
    318           (pcase (or (magit-get "push.default") "simple")
    319             ("nothing" "nothing (due to push.default)")
    320             ((or "current" "simple")
    321              (format "%s to %s"
    322                      (magit--propertize-face branch 'magit-branch-current)
    323                      (magit--propertize-face (format "%s/%s" remote branch)
    324                                              'magit-branch-remote)))
    325             ((or "upstream" "tracking")
    326              (let ((ref (magit-get "branch" branch "merge")))
    327                (if ref
    328                    (format "%s to %s"
    329                            (magit--propertize-face branch 'magit-branch-current)
    330                            (cond
    331                             ((string-prefix-p "refs/heads/" ref)
    332                              (magit--propertize-face
    333                               (format "%s/%s" remote
    334                                       (substring ref (length "refs/heads/")))
    335                               'magit-branch-remote))
    336                             ((not (string-match "/" ref))
    337                              (magit--propertize-face (format "%s/%s" remote ref)
    338                                                      'magit-branch-remote))
    339                             ((format "%s as %s"
    340                                      (magit--propertize-face remote 'bold)
    341                                      (magit--propertize-face ref 'bold)))))
    342                  "nothing (no upstream)")))
    343             ("matching" (format "all matching to %s"
    344                                 (magit--propertize-face remote 'bold)))))))))
    345 
    346 ;;;###autoload (autoload 'magit-push-to-remote "magit-push" nil t)
    347 (transient-define-suffix magit-push-to-remote (remote args)
    348   "Push to REMOTE without using an explicit refspec.
    349 The REMOTE is read in the minibuffer.
    350 
    351 This command simply runs \"git push -v [ARGS] REMOTE\".  ARGS
    352 are the arguments specified in the popup buffer.  No refspec
    353 arguments are used.  Instead the behavior depends on at least
    354 these Git variables: `push.default', `remote.pushDefault',
    355 `branch.<branch>.pushRemote', `branch.<branch>.remote',
    356 `branch.<branch>.merge', and `remote.<remote>.push'.
    357 
    358 You can add this command as a suffix using something like:
    359 
    360   (transient-insert-suffix \\='magit-push \"o\"
    361     \\='(\"x\" magit-push-to-remote))"
    362   :description #'magit-push-to-remote--desc
    363   (interactive (list (magit-read-remote "Push to remote")
    364                      (magit-push-arguments)))
    365   (run-hooks 'magit-credential-hook)
    366   (magit-run-git-async "push" "-v" args remote))
    367 
    368 (defun magit-push-to-remote--desc ()
    369   (format "using %s" (magit--propertize-face "git push <remote>" 'bold)))
    370 
    371 ;;; _
    372 (provide 'magit-push)
    373 ;;; magit-push.el ends here