emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/git-commit 59833692ed: magit-update-default-branch: New co


From: ELPA Syncer
Subject: [nongnu] elpa/git-commit 59833692ed: magit-update-default-branch: New command
Date: Sun, 15 Jan 2023 10:59:30 -0500 (EST)

branch: elpa/git-commit
commit 59833692ed02a7589482c9358466c9b890530776
Author: Jonas Bernoulli <jonas@bernoul.li>
Commit: Jonas Bernoulli <jonas@bernoul.li>

    magit-update-default-branch: New command
---
 lisp/magit-branch.el |  4 +++-
 lisp/magit-git.el    | 37 +++++++++++++++++++++++++++++++++++++
 lisp/magit-remote.el | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/lisp/magit-branch.el b/lisp/magit-branch.el
index 8087208c4a..74d793aa40 100644
--- a/lisp/magit-branch.el
+++ b/lisp/magit-branch.el
@@ -817,7 +817,9 @@ and also rename the respective reflog file."
    ("p"   magit-branch.<branch>.pushRemote)]
   ["Configure repository defaults"
    ("R" magit-pull.rebase)
-   ("P" magit-remote.pushDefault)]
+   ("P" magit-remote.pushDefault)
+   ("b" "Update default branch" magit-update-default-branch
+    :inapt-if-not magit-get-some-remote)]
   ["Configure branch creation"
    ("a m" magit-branch.autoSetupMerge)
    ("a r" magit-branch.autoSetupRebase)]
diff --git a/lisp/magit-git.el b/lisp/magit-git.el
index d5677a379d..d2937dcfeb 100644
--- a/lisp/magit-git.el
+++ b/lisp/magit-git.el
@@ -50,6 +50,7 @@
 
 ;; From `magit-process'.
 (declare-function magit-call-git "magit-process" (&rest args))
+(declare-function magit-git "magit-process" (&rest args))
 (declare-function magit-process-buffer "magit-process" (&optional nodisplay))
 (declare-function magit-process-file "magit-process"
                   (process &optional infile buffer display &rest args))
@@ -1605,6 +1606,42 @@ The amount of time spent searching is limited by
       (cl-incf i))
     prev))
 
+(defun magit--set-default-branch (newname oldname)
+  (let ((remote (or (magit-primary-remote)
+                    (user-error "Cannot determine primary remote")))
+        (branches (mapcar (lambda (line) (split-string line "\t"))
+                          (magit-git-lines
+                           "for-each-ref" "refs/heads"
+                           "--format=%(refname:short)\t%(upstream:short)"))))
+    (when-let ((old (assoc oldname branches)))
+      (unless (assoc newname branches)
+        (magit-call-git "branch" "-m" oldname newname)
+        (setcar old newname)))
+    (let ((new (if (magit-branch-p newname)
+                   newname
+                 (concat remote "/" newname))))
+      (pcase-dolist (`(,branch ,upstream) branches)
+        (cond
+         ((equal upstream oldname)
+          (magit-set-upstream-branch branch new))
+         ((equal upstream (concat remote "/" oldname))
+          (magit-set-upstream-branch branch (concat remote "/" newname))))))))
+
+(defun magit--get-default-branch (&optional update)
+  (let ((remote (magit-primary-remote)))
+    (when update
+      (if (not remote)
+          (user-error "Cannot determine primary remote")
+        (message "Determining default branch...")
+        (magit-git "fetch" "--prune")
+        (magit-git "remote" "set-head" "--auto" remote)
+        (message "Determining default branch...done")))
+    (let ((branch (magit-git-string "symbolic-ref" "--short"
+                                    (format "refs/remotes/%s/HEAD" remote))))
+      (when (and update (not branch))
+        (error "Cannot determine new default branch"))
+      (list remote (and branch (cdr (magit-split-branch-name branch)))))))
+
 (defun magit-set-upstream-branch (branch upstream)
   "Set UPSTREAM as the upstream of BRANCH.
 If UPSTREAM is nil, then unset BRANCH's upstream.
diff --git a/lisp/magit-remote.el b/lisp/magit-remote.el
index 3b6689a14a..75958c4dfa 100644
--- a/lisp/magit-remote.el
+++ b/lisp/magit-remote.el
@@ -85,6 +85,7 @@ has to be used to view and change remote related variables."
    [("C" "Configure..."         magit-remote-configure)
     ("p" "Prune stale branches" magit-remote-prune)
     ("P" "Prune stale refspecs" magit-remote-prune-refspecs)
+    ("b" magit-update-default-branch)
     (7 "z" "Unshallow remote"   magit-remote-unshallow)]]
   (interactive (list (magit-get-current-remote)))
   (transient-setup 'magit-remote nil nil :scope remote))
@@ -255,6 +256,37 @@ 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"))
 
+;;;###autoload (autoload 'magit-update-default-branch "magit-remote" nil t)
+(transient-define-suffix magit-update-default-branch ()
+  "Update name of the default branch after upstream changed it."
+  :description "Update default branch"
+  :inapt-if-not #'magit-get-some-remote
+  (interactive)
+  (pcase-let ((`(,_remote ,oldname) (magit--get-default-branch))
+              (`( ,remote ,newname) (magit--get-default-branch t)))
+    (cond
+     ((equal oldname newname)
+      (setq oldname
+            (read-string
+             (format "Name of default branch is still `%s', %s\n%s" oldname
+                     "but some upstreams might need updating."
+                     "Name of upstream branches to update: ")))
+      (magit--set-default-branch newname oldname)
+      (magit-refresh))
+     (t
+      (unless oldname
+        (setq oldname
+              (magit-read-other-local-branch
+               (format "Name of old default branch to be renamed to `%s'"
+                       newname)
+               newname "master")))
+      (cond
+       ((y-or-n-p (format "Default branch changed from `%s' to `%s' on %s.%s"
+                          oldname newname remote "  Do the same locally? "))
+        (magit--set-default-branch newname oldname)
+        (magit-refresh))
+       ((user-error "Abort")))))))
+
 ;;;###autoload
 (defun magit-remote-unshallow (remote)
   "Convert a shallow remote into a full one.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]