dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

magit-push.el (14627B)


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