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

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

[nongnu] elpa/git-commit ffbbfa1e2d 2/2: Allow magit-clone-url-format to


From: ELPA Syncer
Subject: [nongnu] elpa/git-commit ffbbfa1e2d 2/2: Allow magit-clone-url-format to be more dynamic
Date: Sat, 20 Aug 2022 16:58:38 -0400 (EDT)

branch: elpa/git-commit
commit ffbbfa1e2de3673703b32bb1e1246d5086eab33c
Author: Nacho Barrientos <nacho.barrientos@cern.ch>
Commit: Kyle Meyer <kyle@kyleam.com>

    Allow magit-clone-url-format to be more dynamic
    
    This patch permits `magit-clone-url-format' to be represented as an
    alist mapping hostnames to URL formats. This way Magit can be
    configured to translate repository names into URLs when using servers
    that employ different URL schemes. The current value type (string) is
    kept for backwards compatibility.
---
 docs/RelNotes/3.4.0.org |  3 +++
 docs/magit.org          | 22 ++++++++++++++++++++--
 docs/magit.texi         | 22 ++++++++++++++++++++--
 lisp/magit-clone.el     | 41 +++++++++++++++++++++++++++--------------
 test/magit-tests.el     | 42 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 112 insertions(+), 18 deletions(-)

diff --git a/docs/RelNotes/3.4.0.org b/docs/RelNotes/3.4.0.org
index 8888783c26..4d46521c9a 100644
--- a/docs/RelNotes/3.4.0.org
+++ b/docs/RelNotes/3.4.0.org
@@ -57,6 +57,9 @@ dfe3d03a14 git-commit-save-message: Report whether message 
was saved
   default) to support partial cloning, a feature that is available as
   of Git v2.17.  #4102
 
+- ~magit-clone-url-format~ can now be configured to an alist to
+  support servers that employ different URL schemes.  #4738
+
 - Added new command ~magit-stash-push~, which allows specifying a list of
   files to be stashed.  #4583
 
diff --git a/docs/magit.org b/docs/magit.org
index 9061c3617f..a297da00c2 100644
--- a/docs/magit.org
+++ b/docs/magit.org
@@ -4333,8 +4333,26 @@ The following suffixes are disabled by default. See
 - User Option: magit-clone-url-format ::
 
   The format specified by this option is used when turning repository
-  names into urls. ~%h~ is the hostname and ~%n~ is the repository name,
-  including the name of the owner.
+  names into urls.  ~%h~ is the hostname and ~%n~ is the repository
+  name, including the name of the owner.  The value can be a string
+  (representing a single static format) or an alist with elements
+  ~(HOSTNAME . FORMAT)~ mapping hostnames to formats.  When an alist
+  is used, the ~nil~ key represents the default format.
+
+  Example of a single format string:
+
+  #+BEGIN_SRC emacs-lisp
+    (setq magit-clone-url-format
+          "git@%h:%n.git")
+  #+END_SRC
+
+  Example of by-hostname format strings:
+
+  #+BEGIN_SRC emacs-lisp
+    (setq magit-clone-url-format
+          '(("git.example.com" . "git@%h:~%n")
+            (nil . "git@%h:%n.git")))
+  #+END_SRC
 
 ** Staging and Unstaging
 
diff --git a/docs/magit.texi b/docs/magit.texi
index 55726a4db4..0e6b52bada 100644
--- a/docs/magit.texi
+++ b/docs/magit.texi
@@ -5389,8 +5389,26 @@ username itself.
 
 @defopt magit-clone-url-format
 The format specified by this option is used when turning repository
-names into urls. @code{%h} is the hostname and @code{%n} is the repository 
name,
-including the name of the owner.
+names into urls.  @code{%h} is the hostname and @code{%n} is the repository
+name, including the name of the owner.  The value can be a string
+(representing a single static format) or an alist with elements
+@code{(HOSTNAME . FORMAT)} mapping hostnames to formats.  When an alist
+is used, the @code{nil} key represents the default format.
+
+Example of a single format string:
+
+@lisp
+(setq magit-clone-url-format
+      "git@@%h:%n.git")
+@end lisp
+
+Example of by-hostname format strings:
+
+@lisp
+(setq magit-clone-url-format
+      '(("git.example.com" . "git@@%h:~%n")
+        (nil . "git@@%h:%n.git")))
+@end lisp
 @end defopt
 
 @node Staging and Unstaging
diff --git a/lisp/magit-clone.el b/lisp/magit-clone.el
index d6d889c1fc..e93344b2d1 100644
--- a/lisp/magit-clone.el
+++ b/lisp/magit-clone.el
@@ -93,12 +93,17 @@ as the username itself."
                        (string :tag "user name or git variable"))))
 
 (defcustom magit-clone-url-format "git@%h:%n.git"
-  "Format used when turning repository names into urls.
-%h is the hostname and %n is the repository name, including
-the name of the owner.  Also see `magit-clone-name-alist'."
+  "Format(s) used when turning repository names into urls.
+%h is the hostname and %n is the repository name, including the
+name of the owner.  The value can be a string (representing a
+single static format) or an alist with elements (HOSTNAME
+. FORMAT) mapping hostnames to formats.  When an alist is used,
+the nil key represents the default.  Also see
+`magit-clone-name-alist'."
   :package-version '(magit . "3.0.0")
   :group 'magit-commands
-  :type 'string)
+  :type '(choice (string)
+                 (alist :key-type string :value-type string)))
 
 ;;; Commands
 
@@ -311,16 +316,24 @@ Then show the status buffer for the new repository."
                   'magit-clone-name-alist)))
 
 (defun magit-clone--format-url (host user repo)
-  (format-spec
-   magit-clone-url-format
-   `((?h . ,host)
-     (?n . ,(if (string-search "/" repo)
-                repo
-              (if (string-search "." user)
-                  (if-let ((user (magit-get user)))
-                      (concat user "/" repo)
-                    (user-error "Set %S or specify owner explicitly" user))
-                (concat user "/" repo)))))))
+  (if-let ((url-format
+            (cond ((listp magit-clone-url-format)
+                   (cdr (or (assoc host magit-clone-url-format)
+                            (assoc nil magit-clone-url-format))))
+                  ((stringp magit-clone-url-format)
+                   magit-clone-url-format))))
+      (format-spec
+       url-format
+       `((?h . ,host)
+         (?n . ,(if (string-search "/" repo)
+                    repo
+                  (if (string-search "." user)
+                      (if-let ((user (magit-get user)))
+                          (concat user "/" repo)
+                        (user-error "Set %S or specify owner explicitly" user))
+                    (concat user "/" repo))))))
+    (user-error
+     "Bogus `magit-clone-url-format' (bad type or missing default)")))
 
 ;;; _
 (provide 'magit-clone)
diff --git a/test/magit-tests.el b/test/magit-tests.el
index 316de0cd04..9f2208b93d 100644
--- a/test/magit-tests.el
+++ b/test/magit-tests.el
@@ -303,6 +303,48 @@ Enter passphrase for key '/home/user/.ssh/id_rsa': "
         (should (equal (pop sent-strings) "mypasswd\n")))
       (should (null sent-strings)))))
 
+;;; Clone
+
+(ert-deftest magit-clone:--name-to-url-format-single-string ()
+  (let ((magit-clone-url-format "bird@%h:%n.git")
+        (magit-clone-name-alist
+         '(("\\`\\(?:github:\\|gh:\\)?\\([^:]+\\)\\'" "github.com" "u")
+           ("\\`\\(?:gitlab:\\|gl:\\)\\([^:]+\\)\\'" "gitlab.com" "u"))))
+    (should (string-equal (magit-clone--name-to-url "gh:a/b")
+                          "bird@github.com:a/b.git"))
+    (should (string-equal (magit-clone--name-to-url "gl:a/b")
+                          "bird@gitlab.com:a/b.git"))
+    (should (string-equal (magit-clone--name-to-url "github:c/d")
+                          "bird@github.com:c/d.git"))
+    (should (string-equal (magit-clone--name-to-url "gitlab:c/d")
+                          "bird@gitlab.com:c/d.git"))))
+
+(ert-deftest magit-clone:--name-to-url-format-bad-type-throws-error ()
+  (let ((magit-clone-url-format 3))
+    (should-error (magit-clone--name-to-url "gh:a/b")
+                  :type 'user-error)))
+
+(ert-deftest 
magit-clone:--name-to-url-format-alist-different-urls-per-hostname ()
+  (let ((magit-clone-name-alist
+         '(("\\`\\(?:example:\\|ex:\\)\\([^:]+\\)\\'" "git.example.com" 
"foouser")
+           ("\\`\\(?:gh:\\)?\\([^:]+\\)\\'" "github.com" "u")))
+        (magit-clone-url-format
+         '(("git.example.com" . "cow@%h:~%n")
+           (nil . "git@%h:%n.git"))))
+    (should (string-equal (magit-clone--name-to-url "gh:a/b")
+                          "git@github.com:a/b.git"))
+    (should (string-equal (magit-clone--name-to-url "ex:a/b")
+                          "cow@git.example.com:~a/b"))
+    (should (string-equal (magit-clone--name-to-url "example:x/y")
+                          "cow@git.example.com:~x/y"))
+    (should (string-equal (magit-clone--name-to-url "ex:c")
+                          "cow@git.example.com:~foouser/c"))))
+
+(ert-deftest magit-clone:--name-to-url-format-alist-no-fallback-throws-error ()
+  (let ((magit-clone-url-format '(("fail.example.com" . "git@%h:~%n"))))
+    (should-error (magit-clone--name-to-url "gh:a/b")
+                  :type 'user-error)))
+
 ;;; Status
 
 (defun magit-test-get-section (list file)



reply via email to

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