branch: elpa/magit
commit 7091f967e648f26cc274f6f13a10f37a268ddc9b
Author: Jonas Bernoulli <[email protected]>
Commit: Jonas Bernoulli <[email protected]>
magit-checkout-remote-ref: New command
---
docs/CHANGELOG.4 | 2 ++
docs/magit.org | 16 ++++++++++++++++
docs/magit.texi | 16 ++++++++++++++++
lisp/magit-branch.el | 32 +++++++++++++++++++++++++++++++-
4 files changed, 65 insertions(+), 1 deletion(-)
diff --git a/docs/CHANGELOG.4 b/docs/CHANGELOG.4
index 19c63745a0..47a40e5383 100644
--- a/docs/CHANGELOG.4
+++ b/docs/CHANGELOG.4
@@ -56,6 +56,8 @@
from the user. Previously this was only done for local branches.
#5502
+- Added new command ~magit-checkout-remote-ref~. #5410
+
Bugfixes:
- ~magit-ignore-submodules-p~ didn't return ~nil~ for ~none~.
diff --git a/docs/magit.org b/docs/magit.org
index f23cb6fc77..41540aefc8 100644
--- a/docs/magit.org
+++ b/docs/magit.org
@@ -5618,6 +5618,22 @@ The variables are described in [[*Branch Git Variables]].
minibuffer. With prefix argument the branch is renamed even if that
name conflicts with an existing branch.
+- Key: b r (magit-checkout-remote-ref) ::
+
+ This commands first reads a remote from the user. Then it queries
+ that remote for a list of its references and presents that to the
+ user. After the user has selected one of them, it fetches just that
+ ref, and finally it checks out "FETCH_HEAD", which now refers to the
+ same commit as REF does on REMOTE.
+
+ This is only useful if you usually only fetch a subset of the refs
+ from REMOTE. Otherwise it is better to use ~magit-checkout~, as that
+ avoids a round-trip.
+
+ This command is not shown in the menu by default, see
+ [[info:transient#Enabling and Disabling
+ Suffixes][transient#Enabling and Disabling Suffixes]].
+
- User Option: magit-branch-read-upstream-first ::
When creating a branch, whether to read the upstream branch before
diff --git a/docs/magit.texi b/docs/magit.texi
index 2dc5f54e45..1950ca7891 100644
--- a/docs/magit.texi
+++ b/docs/magit.texi
@@ -6616,6 +6616,22 @@ prompt is confusing.
Rename a branch. The branch and the new name are read in the
minibuffer. With prefix argument the branch is renamed even if that
name conflicts with an existing branch.
+
+@item @kbd{b r} (@code{magit-checkout-remote-ref})
+@kindex b r
+@findex magit-checkout-remote-ref
+This commands first reads a remote from the user. Then it queries
+that remote for a list of its references and presents that to the
+user. After the user has selected one of them, it fetches just that
+ref, and finally it checks out "FETCH@math{_HEAD}", which now refers to the
+same commit as REF does on REMOTE@.
+
+This is only useful if you usually only fetch a subset of the refs
+from REMOTE@. Otherwise it is better to use @code{magit-checkout}, as that
+avoids a round-trip.
+
+This command is not shown in the menu by default, see
+@ref{Enabling and Disabling Suffixes,transient#Enabling and Disabling
Suffixes,,transient,}.
@end table
@defopt magit-branch-read-upstream-first
diff --git a/lisp/magit-branch.el b/lisp/magit-branch.el
index abc00f0dc8..88e06efb3f 100644
--- a/lisp/magit-branch.el
+++ b/lisp/magit-branch.el
@@ -235,7 +235,8 @@ has to be used to view and change branch related variables."
[["Checkout"
("b" "branch/revision" magit-checkout)
("l" "local branch" magit-branch-checkout)
- (6 "o" "new orphan" magit-branch-orphan)]
+ (6 "o" "new orphan" magit-branch-orphan)
+ (0 "r" "remote ref" magit-checkout-remote-ref)]
[""
("c" "new branch" magit-branch-and-checkout)
("s" "new spin-off" magit-branch-spinoff)
@@ -426,6 +427,35 @@ when using `magit-branch-and-checkout'."
(interactive (magit-branch-read-args "Create and checkout orphan branch"))
(magit-run-git "checkout" "--orphan" branch start-point))
+;;;###autoload
+(defun magit-checkout-remote-ref (remote ref)
+ "Checkout reference REF from REMOTE.
+
+This command queries the REMOTE for a list of its references. After
+the user has selected on of them, it fetches just that, and finally
+it checks out \"FETCH_HEAD\", which now refers to the same commit as
+REF does on REMOTE.
+
+This is only useful if you usually only fetch a subset of the refs
+from REMOTE. Otherwise it is better to use `magit-checkout', as
+that avoids a round-trip."
+ (declare (interactive-only magit-call-git))
+ (interactive
+ (let ((remote (magit-read-remote "Checkout ref from remote" nil t)))
+ (list remote
+ (magit-completing-read
+ "Fetch and checkout ref"
+ (prog2 (message "Fetching list of remote refs...")
+ (magit-remote-list-refs remote)
+ (message "Fetching list of remote refs...done"))))))
+ (magit-run-git-async "fetch" remote ref)
+ (set-process-sentinel
+ magit-this-process
+ (lambda (process _event)
+ (when (memq (process-status process) '(exit signal))
+ (magit--checkout "FETCH_HEAD")
+ (magit-refresh)))))
+
(defun magit-branch-read-args (prompt &optional default-start)
(cond-let
((not magit-branch-read-upstream-first)