magit-remote.el 41.9 KB
Newer Older
1
;;; magit-remote.el --- transfer Git commits  -*- lexical-binding: t -*-
2

Jonas Bernoulli's avatar
Jonas Bernoulli committed
3
;; Copyright (C) 2008-2018  The Magit Project Contributors
4
;;
5 6
;; You should have received a copy of the AUTHORS.md file which
;; lists all contributors.  If not, see http://magit.vc/authors.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

;; Author: Jonas Bernoulli <jonas@bernoul.li>
;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>

;; Magit is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; Magit is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
;; License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Magit.  If not, see http://www.gnu.org/licenses.

24 25 26 27 28 29
;;; Commentary:

;; This library implements support for interacting with remote
;; repositories.  Commands for cloning, fetching, pulling, and
;; pushing are defined here.

30 31 32 33
;;; Code:

(require 'magit)

34 35 36 37 38 39 40 41 42
;;; Options

(defcustom magit-fetch-modules-jobs 4
  "Number of submodules to fetch in parallel.
Ignored for Git versions before v2.8.0."
  :package-version '(magit . "2.12.0")
  :group 'magit-commands
  :type '(choice (const :tag "one at a time" nil) number))

43 44
;;; Clone

45 46 47 48 49 50
(defcustom magit-clone-set-remote-head nil
  "Whether cloning creates the symbolic-ref `<remote>/HEAD'."
  :package-version '(magit . "2.4.2")
  :group 'magit-commands
  :type 'boolean)

51 52
(defcustom magit-clone-set-remote.pushDefault 'ask
  "Whether to set the value of `remote.pushDefault' after cloning.
53 54 55 56 57

If t, then set without asking.  If nil, then don't set.  If
`ask', then ask."
  :package-version '(magit . "2.4.0")
  :group 'magit-commands
58 59 60
  :type '(choice (const :tag "set" t)
                 (const :tag "ask" ask)
                 (const :tag "don't set" nil)))
61

62 63
;;;###autoload
(defun magit-clone (repository directory)
64 65
  "Clone the REPOSITORY to DIRECTORY.
Then show the status buffer for the new repository."
66
  (interactive
67
   (let  ((url (magit-read-string-ns "Clone repository")))
68 69
     (list url (read-directory-name
                "Clone to: " nil nil nil
70
                (and (string-match "\\([^/:]+?\\)\\(/?\\.git\\)?$" url)
71 72
                     (match-string 1 url))))))
  (setq directory (file-name-as-directory (expand-file-name directory)))
73
  (magit-run-git-async "clone" repository
74
                       (magit-convert-filename-for-git directory))
75 76 77 78 79 80 81 82 83 84
  ;; Don't refresh the buffer we're calling from.
  (process-put magit-this-process 'inhibit-refresh t)
  (set-process-sentinel
   magit-this-process
   (lambda (process event)
     (when (memq (process-status process) '(exit signal))
       (let ((magit-process-raise-error t))
         (magit-process-sentinel process event)))
     (when (and (eq (process-status process) 'exit)
                (= (process-exit-status process) 0))
85 86 87 88
       (let ((default-directory directory))
         (when (or (eq  magit-clone-set-remote.pushDefault t)
                   (and magit-clone-set-remote.pushDefault
                        (y-or-n-p "Set `remote.pushDefault' to \"origin\"? ")))
89
           (setf (magit-get "remote.pushDefault") "origin"))
90 91
         (unless magit-clone-set-remote-head
           (magit-remote-unset-head "origin")))
92 93
       (with-current-buffer (process-get process 'command-buf)
         (magit-status-internal directory))))))
94

95 96
;;; Remote
;;;; Options
97

98 99
(defcustom magit-remote-add-set-remote.pushDefault 'ask-if-unset
  "Whether to set the value of `remote.pushDefault' after adding a remote.
100 101 102

If `ask', then always ask.  If `ask-if-unset', then ask, but only
if the variable isn't set already.  If nil, then don't ever set.
103 104 105
If the value is a string, then set without asking, provided that
the name of the added remote is equal to that string and the
variable isn't already set."
106 107
  :package-version '(magit . "2.4.0")
  :group 'magit-commands
108 109 110 111
  :type '(choice (const  :tag "ask if unset" ask-if-unset)
                 (const  :tag "always ask" ask)
                 (string :tag "set if named")
                 (const  :tag "don't set")))
112

113 114 115 116 117 118 119 120 121
(defcustom magit-remote-popup-show-variables t
  "Whether the `magit-remote-popup' shows Git variables.
When set to nil, no variables are displayed directly in this
popup, instead the sub-popup `magit-remote-config-popup' has
to be used to view and change remote related variables."
  :package-version '(magit . "2.12.0")
  :group 'magit-commands
  :type 'boolean)

122 123
;;;; Popup

124 125
(defvar magit-remote-config-variables)

Jonas Bernoulli's avatar
Jonas Bernoulli committed
126
;;;###autoload (autoload 'magit-remote-popup "magit-remote" nil t)
127 128 129
(magit-define-popup magit-remote-popup
  "Popup console for remote commands."
  :man-page "git-remote"
130
  :default-arguments '("-f")
131 132 133
  :variables (lambda ()
               (and magit-remote-popup-show-variables
                    magit-remote-config-variables))
134 135
  :switches '("Switches for add"
              (?f "Fetch after add" "-f"))
136 137 138 139 140
  :actions  '((?a "Add"                  magit-remote-add)
              (?C "Configure..."         magit-remote-config-popup)
              (?r "Rename"               magit-remote-rename)
              (?p "Prune stale branches" magit-remote-prune)
              (?k "Remove"               magit-remote-remove)
141
              (?P "Prune stale refspecs" magit-remote-prune-refspecs))
142
  :max-action-columns 2)
143

144 145
;;;; Commands

146 147 148 149 150 151
(defun magit-read-url (prompt &optional initial-input)
  (let ((url (magit-read-string-ns prompt initial-input)))
    (if (string-prefix-p "~" url)
        (expand-file-name url)
      url)))

152
;;;###autoload
153
(defun magit-remote-add (remote url &optional args)
154
  "Add a remote named REMOTE and fetch it."
155
  (interactive (list (magit-read-string-ns "Remote name")
156 157
                     (magit-read-url "Remote url")
                     (magit-remote-arguments)))
158
  (if (pcase (list magit-remote-add-set-remote.pushDefault
159
                   (magit-get "remote.pushDefault"))
160
        (`(,(pred stringp) ,_) t)
161
        ((or `(ask ,_) `(ask-if-unset nil))
162
         (y-or-n-p (format "Set `remote.pushDefault' to \"%s\"? " remote))))
163
      (progn (magit-call-git "remote" "add" args remote url)
164
             (setf (magit-get "remote.pushDefault") remote)
165
             (magit-refresh))
166
    (magit-run-git-async "remote" "add" args remote url)))
167 168 169

;;;###autoload
(defun magit-remote-rename (old new)
170
  "Rename the remote named OLD to NEW."
171 172
  (interactive
   (let  ((remote (magit-read-remote "Rename remote")))
173
     (list remote (magit-read-string-ns (format "Rename %s to" remote)))))
174
  (unless (string= old new)
175 176 177
    (magit-call-git "remote" "rename" old new)
    (magit-remote--cleanup-push-variables old new)
    (magit-refresh)))
178

179 180 181 182
;;;###autoload
(defun magit-remote-remove (remote)
  "Delete the remote named REMOTE."
  (interactive (list (magit-read-remote "Delete remote")))
183 184 185 186 187 188 189 190 191 192 193 194
  (magit-call-git "remote" "rm" remote)
  (magit-remote--cleanup-push-variables remote)
  (magit-refresh))

(defun magit-remote--cleanup-push-variables (remote &optional new-name)
  (magit-with-toplevel
    (when (equal (magit-get "remote.pushDefault") remote)
      (magit-set new-name "remote.pushDefault"))
    (dolist (var (magit-git-lines "config" "--name-only"
                                  "--get-regexp" "^branch\.[^.]*\.pushRemote"
                                  (format "^%s$" remote)))
      (magit-call-git "config" (and (not new-name) "--unset") var new-name))))
195

196 197
(defconst magit--refspec-re "\\`\\(\\+\\)?\\([^:]+\\):\\(.*\\)\\'")

198 199 200 201 202 203
;;;###autoload
(defun magit-remote-prune (remote)
  "Remove stale remote-tracking branches for REMOTE."
  (interactive (list (magit-read-remote "Prune stale branches of remote")))
  (magit-run-git-async "remote" "prune" remote))

204 205
;;;###autoload
(defun magit-remote-prune-refspecs (remote)
206 207 208 209 210 211 212 213 214 215 216 217
  "Remove stale refspecs for REMOTE.

A refspec is stale if there no longer exists at least one branch
on the remote that would be fetched due to that refspec.  A stale
refspec is problematic because its existence causes Git to refuse
to fetch according to the remaining non-stale refspecs.

If only stale refspecs remain, then offer to either delete the
remote or to replace the stale refspecs with the default refspec.

Also remove the remote-tracking branches that were created due to
the now stale refspecs.  Other stale branches are not removed."
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
  (interactive (list (magit-read-remote "Prune refspecs of remote")))
  (let* ((tracking-refs (magit-list-remote-branches remote))
         (remote-refs (magit-remote-list-refs remote))
         (variable (format "remote.%s.fetch" remote))
         (refspecs (magit-get-all variable))
         stale)
    (dolist (refspec refspecs)
      (when (string-match magit--refspec-re refspec)
        (let ((theirs (match-string 2 refspec))
              (ours   (match-string 3 refspec)))
          (unless (if (string-match "\\*" theirs)
                      (let ((re (replace-match ".*" t t theirs)))
                        (--some (string-match-p re it) remote-refs))
                    (member theirs remote-refs))
            (push (cons refspec
                        (if (string-match "\\*" ours)
                            (let ((re (replace-match ".*" t t ours)))
                              (--filter (string-match-p re it) tracking-refs))
                          (list (car (member ours tracking-refs)))))
                  stale)))))
    (if (not stale)
        (message "No stale refspecs for remote %S" remote)
      (if (= (length stale)
             (length refspecs))
          (magit-read-char-case
              (format "All of %s's refspecs are stale.  " remote) nil
            (?s "replace with [d]efault refspec"
                (magit-set-all
                 (list (format "+refs/heads/*:refs/remotes/%s/*" remote))
                 variable))
            (?r "[r]emove remote"
                (magit-call-git "remote" "rm" remote))
            (?a "or [a]abort"
                (user-error "Abort")))
        (if (if (= (length stale) 1)
                (pcase-let ((`(,refspec . ,refs) (car stale)))
                  (magit-confirm 'prune-stale-refspecs
                    (format "Prune stale refspec %s and branch %%s" refspec)
                    (format "Prune stale refspec %s and %%i branches" refspec)
257
                    nil refs))
258 259
              (magit-confirm 'prune-stale-refspecs nil
                (format "Prune %%i stale refspecs and %i branches"
260 261
                        (length (cl-mapcan (lambda (s) (copy-sequence (cdr s)))
                                           stale)))
262
                nil
Jonas Bernoulli's avatar
Jonas Bernoulli committed
263 264 265 266 267
                (--map (pcase-let ((`(,refspec . ,refs) it))
                         (concat refspec "\n"
                                 (mapconcat (lambda (b) (concat "  " b))
                                            refs "\n")))
                       stale)))
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
            (pcase-dolist (`(,refspec . ,refs) stale)
              (magit-call-git "config" "--unset" variable
                              (regexp-quote refspec))
              (magit--log-action
               (lambda (refs)
                 (format "Deleting %i branches" (length refs)))
               (lambda (ref)
                 (format "Deleting branch %s (was %s)" ref
                         (magit-rev-parse "--short" ref)))
               refs)
              (dolist (ref refs)
                (magit-call-git "update-ref" "-d" ref)))
          (user-error "Abort")))
      (magit-refresh))))

283 284 285 286 287 288 289 290 291 292 293
;;;###autoload
(defun magit-remote-set-head (remote &optional branch)
  "Set the local representation of REMOTE's default branch.
Query REMOTE and set the symbolic-ref refs/remotes/<remote>/HEAD
accordingly.  With a prefix argument query for the branch to be
used, which allows you to select an incorrect value if you fancy
doing that."
  (interactive
   (let  ((remote (magit-read-remote "Set HEAD for remote")))
     (list remote
           (and current-prefix-arg
294 295
                (magit-read-remote-branch (format "Set %s/HEAD to" remote)
                                          remote nil nil t)))))
296 297 298 299 300 301 302 303 304
  (magit-run-git "remote" "set-head" remote (or branch "--auto")))

;;;###autoload
(defun magit-remote-unset-head (remote)
  "Unset the local representation of REMOTE's default branch.
Delete the symbolic-ref \"refs/remotes/<remote>/HEAD\"."
  (interactive (list (magit-read-remote "Unset HEAD for remote")))
  (magit-run-git "remote" "set-head" remote "--delete"))

305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
;;;; Config Popup

(defvar magit-remote-config--remote nil)

;;;###autoload
(defun magit-remote-config-popup (remote)
  "Popup console for setting remote variables."
  (interactive
   (list (if (or current-prefix-arg
                 (and (eq magit-current-popup 'magit-remote-popup)
                      magit-remote-popup-show-variables))
             (magit-read-remote "Configure remote")
           (magit-remote-config--remote-1))))
  (let ((magit-remote-config--remote remote))
    (magit-invoke-popup 'magit-remote-config-popup nil nil)))

(defvar magit-remote-config-variables
  '((lambda ()
      (concat
       (propertize "Configure " 'face 'magit-popup-heading)
       (propertize (magit-remote-config--remote) 'face 'magit-branch-remote)))
    (?u "remote.%s.url"
        magit-set-remote*url
        magit-format-remote*url)
    (?U "remote.%s.fetch"
        magit-set-remote*fetch
        magit-format-remote*fetch)
    (?s "remote.%s.pushurl"
        magit-set-remote*pushurl
        magit-format-remote*pushurl)
    (?S "remote.%s.push"
        magit-set-remote*push
        magit-format-remote*push)
    (?O "remote.%s.tagOpt"
        magit-cycle-remote*tagOpt
        magit-format-remote*tagOpt)))

(defvar magit-remote-config-popup
  `(:man-page "git-remote"
    :variables ,magit-remote-config-variables
    :setup-function magit-remote-config-popup-setup))

(defun magit-remote-config-popup-setup (val def)
  (magit-popup-default-setup val def)
  (setq-local magit-remote-config--remote magit-remote-config--remote))

(defun magit-remote-config--remote (&optional prompt)
  (if prompt
      (or (and (not current-prefix-arg)
               (or magit-remote-config--remote
                   (magit-remote-config--remote-1)))
          (magit-read-remote prompt))
    (or magit-remote-config--remote
        (magit-remote-config--remote-1)
        "<name>")))

(defun magit-remote-config--remote-1 ()
  (let ((remote (magit-get-upstream-remote)))
    (if (or (not remote)
            (equal remote "."))
        (and (magit-remote-p "origin") "origin")
      remote)))

;;;; Config Commands and Inserters

(defun magit-set-remote*url (remote urls)
  "Set the variable `url' for the remote named REMOTE to URLS."
  (interactive (magit-remote-config--read-args "url" "Urls: "))
  (magit-remote-config--set-url remote "url" urls))

375 376
(defun magit-set-remote*fetch (remote values)
  "Set the variable `fetch' for the remote named REMOTE to VALUES."
377
  (interactive (magit-remote-config--read-args "fetch" "Fetch specs: "))
378
  (magit-set-all values "remote" remote "fetch")
379
  (magit-refresh))
380 381 382 383 384 385

(defun magit-set-remote*pushurl (remote urls)
  "Set the variable `pushurl' for the remote named REMOTE to URLS."
  (interactive (magit-remote-config--read-args "pushurl" "Urls: "))
  (magit-remote-config--set-url remote "pushurl" urls "--push"))

386 387
(defun magit-set-remote*push (remote values)
  "Set the variable `push' for the remote named REMOTE to VALUES."
388
  (interactive (magit-remote-config--read-args "push" "Push specs: "))
389
  (magit-set-all values "remote" remote "push")
390
  (magit-refresh))
391 392 393

(defun magit-cycle-remote*tagOpt (remote)
  (interactive (list (magit-remote-config--remote)))
394
  (magit--set-popup-variable (format "remote.%s.tagOpt" remote)
395
                             '("--no-tags" "--tags") nil))
396 397 398 399 400 401 402 403 404 405 406 407 408 409

(defun magit-format-remote*url ()
  (magit-remote-config--format-variable "url"))

(defun magit-format-remote*fetch ()
  (magit-remote-config--format-variable "fetch"))

(defun magit-format-remote*pushurl ()
  (magit-remote-config--format-variable "pushurl"))

(defun magit-format-remote*push ()
  (magit-remote-config--format-variable "push"))

(defun magit-format-remote*tagOpt ()
410
  (let ((remote (magit-remote-config--remote)))
411 412 413 414
    (magit--format-popup-variable:choices
     (format "remote.%s.tagOpts" remote)
     '("--no-tags" "--tags") nil nil
     (+ (length remote) 16))))
415 416 417 418 419 420 421 422 423 424 425 426 427

(defun magit-remote-config--read-args (var prompt)
  (let* ((remote (magit-remote-config--remote (format "Set `%s' of remote" var)))
         (value (magit-get-all "remote" remote var)))
    (list remote
          (mapcar (lambda (url)
                    (if (string-prefix-p "~" url)
                        (expand-file-name url)
                      url))
                  (completing-read-multiple
                   prompt nil nil nil
                   (and value (mapconcat #'identity value ",")))))))

428
(defun magit-remote-config--set-url (remote var values &optional arg)
429
  (let ((old (magit-get-all "remote" remote var)))
430
    (dolist (v (-difference values old))
431
      (magit-call-git "remote" "set-url" arg "--add" remote v))
432
    (dolist (v (-difference old values))
433 434 435 436 437
      (magit-call-git "remote" "set-url" arg "--delete" remote
                      (concat "^" (regexp-quote v) "$"))))
  (magit-refresh))

(defun magit-remote-config--format-variable (variable)
438 439 440
  (magit--format-popup-variable:values
   (format "remote.%s.%s" (magit-remote-config--remote) variable)
   25))
441

442 443
;;; Fetch

Jonas Bernoulli's avatar
Jonas Bernoulli committed
444
;;;###autoload (autoload 'magit-fetch-popup "magit-remote" nil t)
445 446 447
(magit-define-popup magit-fetch-popup
  "Popup console for fetch commands."
  :man-page "git-fetch"
448
  :switches '((?p "Prune deleted branches" "--prune"))
449 450 451
  :actions  '("Configure"
              (?C "variables..."           magit-branch-config-popup)
              "Fetch from"
452
              (?p magit-get-push-remote    magit-fetch-from-pushremote)
453
              (?u magit-get-remote         magit-fetch-from-upstream)
454 455 456
              (?e "elsewhere"              magit-fetch)
              (?a "all remotes"            magit-fetch-all)
              "Fetch"
457 458
              (?o "another branch"         magit-fetch-branch)
              (?r "explicit refspec"       magit-fetch-refspec)
459
              (?m "submodules"             magit-fetch-modules))
460 461
  :default-action 'magit-fetch
  :max-action-columns 1)
462

463 464
(defun magit-git-fetch (remote args)
  (run-hooks 'magit-credential-hook)
465
  (magit-run-git-async "fetch" remote args))
466

467
;;;###autoload
468 469 470
(defun magit-fetch-from-pushremote (args)
  "Fetch from the push-remote of the current branch."
  (interactive (list (magit-fetch-arguments)))
471
  (--if-let (magit-get-push-remote)
472
      (magit-git-fetch it args)
473
    (--if-let (magit-get-current-branch)
474 475 476 477 478 479 480 481 482
        (user-error "No push-remote is configured for %s" it)
      (user-error "No branch is checked out"))))

;;;###autoload
(defun magit-fetch-from-upstream (args)
  "Fetch from the upstream repository of the current branch."
  (interactive (list (magit-fetch-arguments)))
  (--if-let (magit-get-remote)
      (magit-git-fetch it args)
483
    (--if-let (magit-get-current-branch)
484 485
        (user-error "No upstream is configured for %s" it)
      (user-error "No branch is checked out"))))
486

487
;;;###autoload
488
(defun magit-fetch (remote args)
489
  "Fetch from another repository."
490
  (interactive (list (magit-read-remote "Fetch remote")
491
                     (magit-fetch-arguments)))
492
  (magit-git-fetch remote args))
493

494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
;;;###autoload
(defun magit-fetch-branch (remote branch args)
  "Fetch a BRANCH from a REMOTE."
  (interactive
   (let ((remote (magit-read-remote-or-url "Fetch from remote or url")))
     (list remote
           (magit-read-remote-branch "Fetch branch" remote)
           (magit-fetch-arguments))))
  (magit-git-fetch remote (cons branch args)))

;;;###autoload
(defun magit-fetch-refspec (remote refspec args)
  "Fetch a REFSPEC from a REMOTE."
  (interactive
   (let ((remote (magit-read-remote-or-url "Fetch from remote or url")))
     (list remote
           (magit-read-refspec "Fetch using refspec" remote)
           (magit-fetch-arguments))))
  (magit-git-fetch remote (cons refspec args)))

514
;;;###autoload
515
(defun magit-fetch-all (args)
516
  "Fetch from all remotes."
517 518 519
  (interactive (list (cl-intersection (magit-fetch-arguments)
                                      (list "--verbose" "--prune")
                                      :test #'equal)))
520
  (run-hooks 'magit-credential-hook)
521
  (magit-run-git-async "remote" "update" args))
522

523 524 525 526 527 528 529
;;;###autoload
(defun magit-fetch-all-prune ()
  "Fetch from all remotes, and prune.
Prune remote tracking branches for branches that have been
removed on the respective remote."
  (interactive)
  (run-hooks 'magit-credential-hook)
530
  (magit-run-git-async "remote" "update" "--prune"))
531 532 533 534 535 536

;;;###autoload
(defun magit-fetch-all-no-prune ()
  "Fetch from all remotes."
  (interactive)
  (run-hooks 'magit-credential-hook)
537
  (magit-run-git-async "remote" "update"))
538

539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
;;;###autoload
(defun magit-fetch-modules (&optional all)
  "Fetch all submodules.

Option `magit-fetch-modules-jobs' controls how many submodules
are being fetched in parallel.  Also fetch the super-repository,
because `git-fetch' does not support not doing that.  With a
prefix argument fetch all remotes."
  (interactive "P")
  (magit-with-toplevel
    (magit-run-git-async
     "fetch" "--verbose" "--recurse-submodules"
     (and magit-fetch-modules-jobs
          (version<= "2.8.0" (magit-git-version))
          (list "-j" (number-to-string magit-fetch-modules-jobs)))
     (and all "--all"))))

556 557
;;; Pull

Jonas Bernoulli's avatar
Jonas Bernoulli committed
558
;;;###autoload (autoload 'magit-pull-popup "magit-remote" nil t)
559 560 561
(magit-define-popup magit-pull-popup
  "Popup console for pull commands."
  :man-page "git-pull"
562 563
  :variables '("Configure"
               (?r "branch.%s.rebase"
564
                   magit-cycle-branch*rebase
565 566
                   magit-pull-format-branch*rebase)
               (?C "variables..." magit-branch-config-popup))
567 568 569 570 571 572 573
  :actions '((lambda ()
               (--if-let (magit-get-current-branch)
                   (concat
                    (propertize "Pull into " 'face 'magit-popup-heading)
                    (propertize it           'face 'magit-branch-local)
                    (propertize " from"      'face 'magit-popup-heading))
                 (propertize "Pull from" 'face 'magit-popup-heading)))
574 575 576
             (?p magit-get-push-branch     magit-pull-from-pushremote)
             (?u magit-get-upstream-branch magit-pull-from-upstream)
             (?e "elsewhere"               magit-pull))
577 578
  :default-action 'magit-pull
  :max-action-columns 1)
579

580 581 582 583 584 585
;;;###autoload (autoload 'magit-pull-and-fetch-popup "magit-remote" nil t)
(magit-define-popup magit-pull-and-fetch-popup
  "Popup console for pull and fetch commands.

This popup is intended as a replacement for the separate popups
`magit-pull-popup' and `magit-fetch-popup'.  To use it, add this
586
to your init file:
587

588 589
  (with-eval-after-load \\='magit-remote
    (define-key magit-mode-map \"f\" \\='magit-pull-and-fetch-popup)
590 591 592 593 594 595 596 597 598
    (define-key magit-mode-map \"F\" nil))

The combined popup does not offer all commands and arguments
available from the individual popups.  Instead of the argument
`--prune' and the command `magit-fetch-all' it uses two commands
`magit-fetch-prune' and `magit-fetch-no-prune'.  And the commands
`magit-fetch-from-pushremote' and `magit-fetch-from-upstream' are
missing.  To add them use something like:

599 600 601 602 603 604 605
  (with-eval-after-load \\='magit-remote
    (magit-define-popup-action \\='magit-pull-and-fetch-popup ?U
      \\='magit-get-upstream-branch
      \\='magit-fetch-from-upstream-remote ?F)
    (magit-define-popup-action \\='magit-pull-and-fetch-popup ?P
      \\='magit-get-push-branch
      \\='magit-fetch-from-push-remote ?F))"
606
  :man-page "git-pull"
607
  :variables '("Configure"
608 609
               (?r "branch.%s.rebase"
                   magit-cycle-branch*rebase
610 611
                   magit-pull-format-branch*rebase)
               (?C "variables..." magit-branch-config-popup))
612 613 614 615 616 617 618 619 620 621 622 623 624 625
  :actions '((lambda ()
               (--if-let (magit-get-current-branch)
                   (concat
                    (propertize "Pull into " 'face 'magit-popup-heading)
                    (propertize it           'face 'magit-branch-local)
                    (propertize " from"      'face 'magit-popup-heading))
                 (propertize "Pull from" 'face 'magit-popup-heading)))
             (?p magit-get-push-branch     magit-pull-from-pushremote)
             (?u magit-get-upstream-branch magit-pull-from-upstream)
             (?e "elsewhere"               magit-pull)
             "Fetch from"
             (?f "remotes"           magit-fetch-all-no-prune)
             (?F "remotes and prune" magit-fetch-all-prune)
             "Fetch"
626
             (?o "another branch"    magit-fetch-branch)
627
             (?s "explicit refspec"  magit-fetch-refspec)
628
             (?m "submodules"        magit-fetch-modules))
629 630 631
  :default-action 'magit-fetch
  :max-action-columns 1)

632
(defun magit-pull-format-branch*rebase ()
633 634 635 636
  (magit--format-popup-variable:choices
   (format "branch.%s.rebase" (or (magit-get-current-branch) "<name>"))
   '("true" "false")
   "false" "pull.rebase"))
637

638
(defun magit-git-pull (source args)
639
  (run-hooks 'magit-credential-hook)
640 641 642
  (-let [(remote . branch)
         (magit-split-branch-name source)]
    (magit-run-git-with-editor "pull" args remote branch)))
643

644
;;;###autoload
645 646 647 648 649 650 651 652 653 654 655 656 657
(defun magit-pull-from-pushremote (args)
  "Pull from the push-remote of the current branch."
  (interactive (list (magit-pull-arguments)))
  (--if-let (magit-get-push-branch)
      (magit-git-pull it args)
    (--if-let (magit-get-current-branch)
        (user-error "No push-remote is configured for %s" it)
      (user-error "No branch is checked out"))))

;;;###autoload
(defun magit-pull-from-upstream (args)
  "Pull from the upstream of the current branch."
  (interactive (list (magit-pull-arguments)))
658
  (--if-let (magit-get-upstream-branch)
659 660 661
      (progn (run-hooks 'magit-credential-hook)
             (magit-run-git-with-editor
              "pull" args (car (magit-split-branch-name it))))
662
    (--if-let (magit-get-current-branch)
663 664
        (user-error "No upstream is configured for %s" it)
      (user-error "No branch is checked out"))))
665 666

;;;###autoload
667
(defun magit-pull (source args)
668
  "Pull from a branch read in the minibuffer."
669 670
  (interactive (list (magit-read-remote-branch "Pull" nil nil nil t)
                     (magit-pull-arguments)))
671
  (magit-git-pull source args))
672

673 674
;;; Push

675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
(defcustom magit-push-current-set-remote-if-missing t
  "Whether to configure missing remotes before pushing.

When nil, then the command `magit-push-current-to-pushremote' and
`magit-push-current-to-upstream' do not appear in the push popup
if the push-remote resp. upstream is not configured.  If the user
invokes one of these commands anyway, then it raises an error.

When non-nil, then these commands always appear in the push
popup.  But if the required configuration is missing, then they
do appear in a way that indicates that this is the case.  If the
user invokes one of them, then it asks for the necessary
configuration, stores the configuration, and then uses it to push
a first time.

This option also affects whether the argument `--set-upstream' is
available in the popup.  If the value is t, then that argument is
692 693 694
redundant.  But note that changing the value of this option does
not take affect immediately, the argument will only be added or
removed after restarting Emacs."
695
  :package-version '(magit . "2.6.0")
696
  :group 'magit-commands
697 698 699
  :type '(choice (const :tag "don't set" nil)
                 (const :tag "set branch.<name>.pushRemote" t)
                 (const :tag "set remote.pushDefault" default)))
700

Jonas Bernoulli's avatar
Jonas Bernoulli committed
701
;;;###autoload (autoload 'magit-push-popup "magit-remote" nil t)
702 703 704
(magit-define-popup magit-push-popup
  "Popup console for push commands."
  :man-page "git-push"
705 706 707 708
  :switches `((?f "Force with lease" "--force-with-lease")
              (?F "Force"            "--force")
              (?h "Disable hooks"    "--no-verify")
              (?d "Dry run"          "--dry-run")
709 710
              ,@(and (not magit-push-current-set-remote-if-missing)
                     '((?u "Set upstream"  "--set-upstream"))))
711 712 713
  :actions '("Configure"
             (?C "variables..."      magit-branch-config-popup)
             (lambda ()
714 715 716 717
               (--when-let (magit-get-current-branch)
                 (concat (propertize "Push " 'face 'magit-popup-heading)
                         (propertize it      'face 'magit-branch-local)
                         (propertize " to"   'face 'magit-popup-heading))))
718 719 720 721 722
             (?p magit--push-current-to-pushremote-desc
                 magit-push-current-to-pushremote)
             (?u magit--push-current-to-upstream-desc
                 magit-push-current-to-upstream)
             (?e "elsewhere\n"       magit-push-current)
723
             "Push"
724 725
             (?o "another branch"    magit-push)
             (?T "a tag"             magit-push-tag)
726 727 728
             (?r "explicit refspecs" magit-push-refspecs)
             (?t "all tags"          magit-push-tags)
             (?m "matching branches" magit-push-matching))
729
  :max-action-columns 2)
730

731
(defun magit-git-push (branch target args)
732
  (run-hooks 'magit-credential-hook)
733 734
  (-let [(remote . target)
         (magit-split-branch-name target)]
735 736
    (magit-run-git-async "push" "-v" args remote
                         (format "%s:refs/heads/%s" branch target))))
737

738
;;;###autoload
739
(defun magit-push-current-to-pushremote (args &optional push-remote)
740
  "Push the current branch to `branch.<name>.pushRemote'.
741 742 743 744 745 746 747 748 749
If that variable is unset, then push to `remote.pushDefault'.

When `magit-push-current-set-remote-if-missing' is non-nil and
the push-remote is not configured, then read the push-remote from
the user, set it, and then push to it.  With a prefix argument
the push-remote can be changed before pushed to it."
  (interactive
   (list (magit-push-arguments)
         (and (magit--push-current-set-pushremote-p current-prefix-arg)
750 751 752 753 754
              (magit-read-remote
               (if (eq magit-push-current-set-remote-if-missing 'default)
                   "Set `remote.pushDefault' and push there"
                 (format "Set `branch.%s.pushRemote' and push there"
                         (magit-get-current-branch)))))))
755
  (--if-let (magit-get-current-branch)
756
      (progn (when push-remote
757 758 759
               (setf (magit-get
                      (if (eq magit-push-current-set-remote-if-missing 'default)
                          "remote.pushDefault"
760
                        (format "branch.%s.pushRemote" it)))
761
                     push-remote))
762 763 764 765 766
             (-if-let (remote (magit-get-push-remote it))
                 (if (member remote (magit-list-remotes))
                     (magit-git-push it (concat remote "/" it) args)
                   (user-error "Remote `%s' doesn't exist" remote))
               (user-error "No push-remote is configured for %s" it)))
767 768
    (user-error "No branch is checked out")))

769
(defun magit--push-current-set-pushremote-p (&optional change)
770 771 772 773
  (and (or change
           (and magit-push-current-set-remote-if-missing
                (not (magit-get-push-remote))))
       (magit-get-current-branch)))
774 775 776 777 778

(defun magit--push-current-to-pushremote-desc ()
  (--if-let (magit-get-push-branch)
      (concat (magit-branch-set-face it) "\n")
    (and (magit--push-current-set-pushremote-p)
779 780 781 782 783 784
         (concat
          (propertize (if (eq magit-push-current-set-remote-if-missing 'default)
                          "pushDefault"
                        "pushRemote")
                      'face 'bold)
          ", after setting that\n"))))
785

786
;;;###autoload
787 788 789 790 791 792 793 794 795 796
(defun magit-push-current-to-upstream (args &optional upstream)
  "Push the current branch to its upstream branch.

When `magit-push-current-set-remote-if-missing' is non-nil and
the upstream is not configured, then read the upstream from the
user, set it, and then push to it.  With a prefix argument the
upstream can be changed before pushed to it."
  (interactive
   (list (magit-push-arguments)
         (and (magit--push-current-set-upstream-p current-prefix-arg)
797
              (magit-read-upstream-branch))))
798
  (--if-let (magit-get-current-branch)
799 800
      (progn
        (when upstream
801
          (magit-set-branch*merge/remote it upstream))
802 803 804
        (-if-let (target (magit-get-upstream-branch it))
            (magit-git-push it target args)
          (user-error "No upstream is configured for %s" it)))
805
    (user-error "No branch is checked out")))
806

807
(defun magit--push-current-set-upstream-p (&optional change)
808 809 810 811
  (and (or change
           (and magit-push-current-set-remote-if-missing
                (not (magit-get-upstream-branch))))
       (magit-get-current-branch)))
812 813 814 815 816 817 818 819

(defun magit--push-current-to-upstream-desc ()
  (--if-let (magit-get-upstream-branch)
      (concat (magit-branch-set-face it) "\n")
    (and (magit--push-current-set-upstream-p)
         (concat (propertize "@{upstream}" 'face 'bold)
                 ", after setting that\n"))))

820
;;;###autoload
821
(defun magit-push-current (target args)
822 823
  "Push the current branch to a branch read in the minibuffer."
  (interactive
824 825 826
   (--if-let (magit-get-current-branch)
       (list (magit-read-remote-branch (format "Push %s to" it)
                                       nil nil it 'confirm)
827
             (magit-push-arguments))
828 829
     (user-error "No branch is checked out")))
  (magit-git-push (magit-get-current-branch) target args))
830

831
;;;###autoload
832 833 834 835 836 837
(defun magit-push (source target args)
  "Push an arbitrary branch or commit somewhere.
Both the source and the target are read in the minibuffer."
  (interactive
   (let ((source (magit-read-local-branch-or-commit "Push")))
     (list source
838 839 840 841 842 843 844 845 846
           (magit-read-remote-branch
            (format "Push %s to" source) nil
            (if (magit-local-branch-p source)
                (or (magit-get-push-branch source)
                    (magit-get-upstream-branch source))
              (and (magit-rev-ancestor-p source "HEAD")
                   (or (magit-get-push-branch)
                       (magit-get-upstream-branch))))
            source 'confirm)
847 848
           (magit-push-arguments))))
  (magit-git-push source target args))
849

850 851
(defvar magit-push-refspecs-history nil)

852 853 854 855 856 857 858 859 860
;;;###autoload
(defun magit-push-refspecs (remote refspecs args)
  "Push one or multiple REFSPECS to a REMOTE.
Both the REMOTE and the REFSPECS are read in the minibuffer.  To
use multiple REFSPECS, separate them with commas.  Completion is
only available for the part before the colon, or when no colon
is used."
  (interactive
   (list (magit-read-remote "Push to remote")
861 862
         (split-string (magit-completing-read-multiple
                        "Push refspec,s"
863 864
                        (cons "HEAD" (magit-list-local-branch-names))
                        nil nil 'magit-push-refspecs-history)
865
                       crm-default-separator t)
866 867 868 869
         (magit-push-arguments)))
  (run-hooks 'magit-credential-hook)
  (magit-run-git-async "push" "-v" args remote refspecs))

870
;;;###autoload
871 872
(defun magit-push-matching (remote &optional args)
  "Push all matching branches to another repository.
873
If multiple remotes exist, then read one from the user.
874
If just one exists, use that without requiring confirmation."
875 876
  (interactive (list (magit-read-remote "Push matching branches to" nil t)
                     (magit-push-arguments)))
877
  (run-hooks 'magit-credential-hook)
878
  (magit-run-git-async "push" "-v" args remote ":"))
879

880
;;;###autoload
881
(defun magit-push-tags (remote &optional args)
882 883 884 885
  "Push all tags to another repository.
If only one remote exists, then push to that.  Otherwise prompt
for a remote, offering the remote configured for the current
branch as default."
886
  (interactive (list (magit-read-remote "Push tags to remote" nil t)
887
                     (magit-push-arguments)))
888
  (run-hooks 'magit-credential-hook)
889
  (magit-run-git-async "push" remote "--tags" args))
890

891 892 893 894 895
;;;###autoload
(defun magit-push-tag (tag remote &optional args)
  "Push a tag to another repository."
  (interactive
   (let  ((tag (magit-read-tag "Push tag")))
896 897
     (list tag (magit-read-remote (format "Push %s to remote" tag) nil t)
           (magit-push-arguments))))
898
  (run-hooks 'magit-credential-hook)
899
  (magit-run-git-async "push" remote tag args))
900

901 902 903 904 905 906 907
;;;###autoload
(defun magit-push-implicitly (args)
  "Push somewhere without using an explicit refspec.

This command simply runs \"git push -v [ARGS]\".  ARGS are the
arguments specified in the popup buffer.  No explicit refspec
arguments are used.  Instead the behavior depends on at least
908
these Git variables: `push.default', `remote.pushDefault',
909 910 911 912 913 914 915
`branch.<branch>.pushRemote', `branch.<branch>.remote',
`branch.<branch>.merge', and `remote.<remote>.push'.

To add this command to the push popup add this to your init file:

  (with-eval-after-load \\='magit-remote
    (magit-define-popup-action \\='magit-push-popup ?P
916 917
      \\='magit-push-implicitly--desc
      \\='magit-push-implicitly ?p t))
918 919

The function `magit-push-implicitly--desc' attempts to predict
920
what this command will do.  The value it returns is displayed in
921 922 923
the popup buffer."
  (interactive (list (magit-push-arguments)))
  (run-hooks 'magit-credential-hook)
924
  (magit-run-git-async "push" "-v" args))
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952

(defun magit-push-implicitly--desc ()
  (let ((default (magit-get "push.default")))
    (unless (equal default "nothing")
      (or (-when-let* ((remote (or (magit-get-remote)
                                   (magit-remote-p "origin")))
                       (refspec (magit-get "remote" remote "push")))
            (format "%s using %s"
                    (propertize remote  'face 'magit-branch-remote)
                    (propertize refspec 'face 'bold)))
          (--when-let (and (not (magit-get-push-branch))
                           (magit-get-upstream-branch))
            (format "%s aka %s\n"
                    (magit-branch-set-face it)
                    (propertize "@{upstream}" 'face 'bold)))
          (--when-let (magit-get-push-branch)
            (format "%s aka %s\n"
                    (magit-branch-set-face it)
                    (propertize "pushRemote" 'face 'bold)))
          (--when-let (magit-get-@{push}-branch)
            (format "%s aka %s\n"
                    (magit-branch-set-face it)
                    (propertize "@{push}" 'face 'bold)))
          (format "using %s (%s is %s)\n"
                  (propertize "git push"     'face 'bold)
                  (propertize "push.default" 'face 'bold)
                  (propertize default        'face 'bold))))))

953 954 955 956 957 958 959 960
;;;###autoload
(defun magit-push-to-remote (remote args)
  "Push to REMOTE without using an explicit refspec.
The REMOTE is read in the minibuffer.

This command simply runs \"git push -v [ARGS] REMOTE\".  ARGS
are the arguments specified in the popup buffer.  No refspec
arguments are used.  Instead the behavior depends on at least
961
these Git variables: `push.default', `remote.pushDefault',
962 963 964 965 966 967 968
`branch.<branch>.pushRemote', `branch.<branch>.remote',
`branch.<branch>.merge', and `remote.<remote>.push'.

To add this command to the push popup add this to your init file:

  (with-eval-after-load \\='magit-remote
    (magit-define-popup-action \\='magit-push-popup ?r
969 970
      \\='magit-push-to-remote--desc
      \\='magit-push-to-remote ?p t))"
971 972 973
  (interactive (list (magit-read-remote "Push to remote")
                     (magit-push-arguments)))
  (run-hooks 'magit-credential-hook)
974
  (magit-run-git-async "push" "-v" args remote))
975 976 977 978

(defun magit-push-to-remote--desc ()
  (format "using %s\n" (propertize "git push <remote>" 'face 'bold)))

979 980
;;; Email

Jonas Bernoulli's avatar
Jonas Bernoulli committed
981
;;;###autoload (autoload 'magit-patch-popup "magit-remote" nil t)
982 983 984
(magit-define-popup magit-patch-popup
  "Popup console for patch commands."
  :man-page "git-format-patch"
985 986
  :switches '("Switches for formatting patches"
              (?l "Add cover letter" "--cover-letter"))
987 988
  :options  '("Options for formatting patches"
              (?f "From"             "--from=")
989 990 991
              (?t "To"               "--to=")
              (?c "CC"               "--cc=")
              (?r "In reply to"      "--in-reply-to=")
992
              (?P "Subject Prefix"   "--subject-prefix=")
993 994 995 996 997 998 999 1000 1001 1002 1003 1004
              (?v "Reroll count"     "--reroll-count=")
              (?s "Thread style"     "--thread=")
              (?U "Context lines"    "-U")
              (?M "Detect renames"   "-M")
              (?C "Detect copies"    "-C")
              (?A "Diff algorithm"   "--diff-algorithm="
                  magit-diff-select-algorithm)
              (?o "Output directory" "--output-directory="))
  :actions  '((?p "Format patches"   magit-format-patch)
              (?r "Request pull"     magit-request-pull))
  :default-action 'magit-format-patch)

1005
;;;###autoload
1006
(defun magit-format-patch (range args)
1007 1008 1009 1010
  "Create patches for the commits in RANGE.
When a single commit is given for RANGE, create a patch for the
changes introduced by that commit (unlike 'git format-patch'
which creates patches for all commits that are reachable from
1011
`HEAD' but not from the specified commit)."
1012
  (interactive
1013
   (list (-if-let (revs (magit-region-values 'commit t))
1014
             (concat (car (last revs)) "^.." (car revs))
1015 1016 1017
           (let ((range (magit-read-range-or-commit "Format range or commit")))
             (if (string-match-p "\\.\\." range)
                 range
1018 1019
               (format "%s~..%s" range range))))
         (magit-patch-arguments)))
1020 1021
  (magit-call-git "format-patch" range args)
  (when (member "--cover-letter" args)
1022 1023 1024 1025 1026 1027 1028 1029
    (find-file
     (expand-file-name
      "0000-cover-letter.patch"
      (let ((topdir (magit-toplevel)))
        (or (--some (and (string-match "--output-directory=\\(.+\\)" it)
                         (expand-file-name (match-string 1 it) topdir))
                    args)
            topdir))))))
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040

;;;###autoload
(defun magit-request-pull (url start end)
  "Request upstream to pull from you public repository.

URL is the url of your publically accessible repository.
START is a commit that already is in the upstream repository.
END is the last commit, usually a branch name, which upstream
is asked to pull.  START has to be reachable from that commit."
  (interactive
   (list (magit-get "remote" (magit-read-remote "Remote") "url")
1041
         (magit-read-branch-or-commit "Start" (magit-get-upstream-branch))
1042 1043 1044 1045 1046 1047
         (magit-read-branch-or-commit "End")))
  (let ((dir default-directory))
    ;; mu4e changes default-directory
    (compose-mail)
    (setq default-directory dir))
  (message-goto-body)
1048
  (magit-git-insert "request-pull" start url end)
1049 1050
  (set-buffer-modified-p nil))

1051 1052
(provide 'magit-remote)
;;; magit-remote.el ends here