h--parse-repo-identifier: actually parse the query string

This function used to only return the kind of the query string. This
is not sufficient to properly query the forges. On top of returning
the type of a query string, we want this function to actually parse
the query into a tagged union materialized as a astring.

We update the tests accordingly to this refactoring.
This commit is contained in:
Félix Baylac-Jacqué 2022-05-19 11:57:31 +02:00
parent 735e9f2206
commit 3aac15dbb0
No known key found for this signature in database
GPG key ID: EFD315F31848DBA4
2 changed files with 43 additions and 15 deletions

View file

@ -219,12 +219,20 @@ For reference: a empty test root looks like this:
;; Test repo URI parser
;;;;;;;;;;;;;;;;;
(ert-deftest h--test-parse-repo-idetifier ()
(ert-deftest h--test-parse-repo-identifier ()
"Test h--parse-repo-identifier."
(should (equal (h--parse-repo-identifier "https://github.com/Ninjatrappeur/h.el") 'full-url))
(should (equal (h--parse-repo-identifier "github.com/Ninjatrappeur/h.el") 'full-url))
(should (equal (h--parse-repo-identifier "Ninjatrappeur/h.el") 'owner-repo))
(should (equal (h--parse-repo-identifier "h.el") 'repo)))
(should (equal
(h--parse-repo-identifier "https://github.com/Ninjatrappeur/h.el")
'((tag . full-url) (full-url . "https://github.com/Ninjatrappeur/h.el"))))
(should (equal
(h--parse-repo-identifier "github.com/Ninjatrappeur/h.el")
'((tag . full-url) (full-url . "github.com/Ninjatrappeur/h.el"))))
(should (equal
(h--parse-repo-identifier "Ninjatrappeur/h.el")
'((tag . owner-repo) (owner . "Ninjatrappeur") (repo . "h.el"))))
(should (equal
(h--parse-repo-identifier "h.el")
'((tag . repo) (repo . "h.el")))))
(ert-deftest h--test-filepath-from-clone-url ()
"Test h--filepath-from-clone-url."

40
h.el
View file

@ -181,10 +181,10 @@ nil as parameter."
;; Internal: repo URI parser
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun h--parse-repo-identifier (repo-str)
"Do its best to figure out which repo the user meant by REPO-STR.
(defun h--parse-repo-identifier (query-str)
"Do its best to figure out which repo the user meant by QUERY-STR.
A valid REPO-STR is in one of the 4 following formats:
A valid QUERY-STR is in one of the 4 following formats:
1. project
Jump to the project if available, do not fetch a remote forge
@ -195,13 +195,32 @@ A valid REPO-STR is in one of the 4 following formats:
3. forge.tld/owner/project
Open the project is available, fetch it if not.
4. https://forge.tld/owner/project
Open the project is available, fetch it if not."
Open the project is available, fetch it if not.
This function will return a tagged union in the form of a alist. For
each kind of format, it'll return something along the line of:
\(('tag . 'full-url) ('full-url .\
\"https://full-url.org/path/to/git/repo/checkout\"))
or
\(('tag . 'owner-repo) ('owner . \"NinjaTrappeur\") ('repo\
. \"h.el\"))
or
\(('tag . 'repo) ('repo . \"h.el\"))"
(cond
((or (string-match "^https?://.*/.*/.*$" repo-str)
(string-match "^.*/.*/.*$" repo-str))
'full-url)
((string-match "^.*/.*$" repo-str) 'owner-repo)
(t 'repo)))
;; Full-url case
((or (string-match "^https?://.*/.*/.*$" query-str)
(string-match "^.*/.*/.*$" query-str))
`((tag . full-url) (full-url . ,query-str)))
;; owner/repo case
((string-match "^.*/.*$" query-str)
(let*
((splitted-query (split-string "Ninjatrappeur/h.el" "/"))
(owner (car splitted-query))
(repo (cadr splitted-query)))
`((tag . owner-repo) (owner . ,owner) (repo . ,repo))))
;; repo case
(t `((tag . repo) (repo . ,query-str)))))
(defun h--filepath-from-clone-url (clone-url)
"Return the relative path relative to the coderoot for CLONE-URL.
@ -230,7 +249,8 @@ not filter anything.
If QUERY-STRING is a fully qualified URL, exclusively use the relevant forge."
(let*
((query-string-type (h--parse-repo-identifier query-string)))
((query-string-type
(cdr (assoc 'tag (h--parse-repo-identifier query-string)))))
(cond
;; query-string is a full URL. Let's filter out the irrelevant forges.
((eq query-string-type 'full-url)