This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support all possible "lib to url" cases
- Loading branch information
Showing
2 changed files
with
59 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,11 @@ | |
[babashka.fs :as fs] | ||
[babashka.process :refer [sh]])) | ||
|
||
(defn- ensure-git-dir [client & args] | ||
(apply (:ensure-git-dir client) args)) | ||
(defn- ensure-git-dir [client git-url] | ||
(binding [*err* (java.io.StringWriter.)] | ||
(let [path ((:ensure-git-dir client) git-url)] | ||
((:git-fetch client) (fs/file path)) | ||
path))) | ||
|
||
(defn default-branch [client git-url] | ||
(let [lib-dir (ensure-git-dir client git-url) | ||
|
@@ -25,21 +28,59 @@ | |
|
||
(defn find-git-tag [client git-url tag] | ||
(let [lib-dir (ensure-git-dir client git-url) | ||
tag-result (sh ["git" "rev-parse" tag] {:dir lib-dir})] | ||
tag-result (sh ["git" "log" "-n" "1" tag "--pretty=format:\"%H\""] | ||
{:dir lib-dir})] | ||
{:name (str tag) | ||
:commit {:sha (str/trim (:out tag-result))}})) | ||
:commit {:sha (edn/read-string (:out tag-result))}})) | ||
|
||
(defn latest-git-tag [client git-url] | ||
(let [lib-dir (ensure-git-dir client git-url) | ||
log-result (sh "git describe --tags --abbrev=0" {:dir lib-dir}) | ||
tag (edn/read-string (:out log-result))] | ||
(find-git-tag client git-url tag))) | ||
|
||
(defn- clean-github-lib [lib] | ||
(let [lib (str/replace lib "com.github." "") | ||
lib (str/replace lib "io.github." "") | ||
lib (symbol lib)] | ||
lib)) | ||
(def providers | ||
{#"^(com|io)\.github\." :github | ||
#"^(com|io)\.gitlab\." :gitlab | ||
#"^(org|io)\.bitbucket\." :bitbucket | ||
#"^(com|io)\.beanstalkapp\." :beanstalk | ||
#"^ht\.sr\." :sourcehut}) | ||
|
||
(defn- clean-lib-str [lib] | ||
(->> (reduce #(str/replace %1 %2 "") lib (keys providers)) | ||
symbol)) | ||
|
||
(defn git-http-url [lib] | ||
(let [provider (some #(when (re-seq (key %) (str lib)) %) providers) | ||
s (clean-lib-str (str lib))] | ||
(case (val provider) | ||
:github (str "https://github.com/" s ".git") | ||
:gitlab (str "https://gitlab.com/" s ".git") | ||
:bitbucket (let [[u] (str/split (str s) #"/")] | ||
(str "https://" u "@bitbucket.org/" s ".git")) | ||
:beanstalk (let [[u] (str/split (str s) #"/")] | ||
(str "https://" u ".git.beanstalkapp.com/" (name lib) ".git")) | ||
:sourcehut (str "https://git.sr.ht/~" s)))) | ||
|
||
(defn git-ssh-url [lib] | ||
(let [provider (some #(when (re-seq (key %) (str lib)) %) providers) | ||
s (clean-lib-str (str lib))] | ||
(case (val provider) | ||
:github (str "[email protected]:" s ".git") | ||
:gitlab (str "[email protected]:" s ".git") | ||
:bitbucket (str "[email protected]:" s ".git") | ||
:beanstalk (let [[u] (str/split (str s) #"/")] | ||
(str "git@" u ".git.beanstalkapp.com:/" s ".git")) | ||
:sourcehut (str "[email protected]:~" s)))) | ||
|
||
(defn github-repo-ssh-url [lib] | ||
(str "[email protected]:" (clean-github-lib lib) ".git")) | ||
(defn git-repo-url [client lib] | ||
(try | ||
(let [url (git-http-url lib)] | ||
(ensure-git-dir client url) | ||
url) | ||
(catch Exception e | ||
(if (re-seq #"^Unable to clone " (ex-message e)) | ||
(let [url (git-ssh-url lib)] | ||
(ensure-git-dir client url) | ||
url) | ||
(throw e))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters