Implement h--parse-repo-identifier

This function is meant to identify what kind of repo a user is giving
to us.
This commit is contained in:
Félix Baylac-Jacqué 2022-05-04 20:04:20 +02:00
parent e7d04c4e27
commit db74a5794a
No known key found for this signature in database
GPG key ID: EFD315F31848DBA4
2 changed files with 36 additions and 0 deletions

View file

@ -216,5 +216,15 @@ For reference: a empty test root looks like this:
(insert-file-contents "./tests/fixtures/github-get-request-ko.txt")
(should (equal (h--fetch-github-parse-response (current-buffer)) nil))))
;; Test repo URI parser
;;;;;;;;;;;;;;;;;
(ert-deftest h--test-parse-repo-idetifier ()
"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)))
(provide 'h-tests)
;;; h-tests.el ends here

26
h.el
View file

@ -160,6 +160,32 @@ nil as parameter."
(apply 'h--query-gitlab (h--parse-query-string-for-forge query-string)))
(t (error (format "No fetcher for %s" query-string)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 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.
A valid REPO-STR is in one of the 4 following formats:
1. project
Jump to the project if available, do not fetch a remote forge
project.
2. owner/project
Open a promp with available projects + fetch all the remote
forges.
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."
(cond
((or (string-match "^https://.*/.*/.*$" repo-str)
(string-match "^.*/.*/.*$" repo-str))
'full-url)
((string-match "^.*/.*$" repo-str) 'owner-repo)
(t 'repo)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Internal: code-root management functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;