Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Support all possible "lib to url" cases
Browse files Browse the repository at this point in the history
  • Loading branch information
rads committed Jan 16, 2023
1 parent 140dfd5 commit 365ca11
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 17 deletions.
63 changes: 52 additions & 11 deletions infer/src/rads/deps_info/git.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)))))
13 changes: 7 additions & 6 deletions infer/src/rads/deps_info/infer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@

[#{} #{:git/url}]
(fn [client lib-sym lib-opts]
(let [url (or (:git/url lib-opts) (git/github-repo-ssh-url lib-sym))
(let [url (or (:git/url lib-opts) (git/git-repo-url client lib-sym))
{:keys [name commit]} (git/latest-git-tag client url)]
{lib-sym {:git/url url :git/tag name :git/sha (:sha commit)}}))

[#{:git/tag} #{:git/url :git/tag}]
(fn [client lib-sym lib-opts]
(let [url (or (:git/url lib-opts) (git/github-repo-ssh-url lib-sym))
(let [url (or (:git/url lib-opts) (git/git-repo-url client lib-sym))
tag (:git/tag lib-opts)
{:keys [commit]} (git/find-git-tag client url tag)]
{lib-sym {:git/url url :git/tag tag :git/sha (:sha commit)}}))

[#{:git/sha} #{:git/url :git/sha}]
(fn [_ lib-sym lib-opts]
(let [url (or (:git/url lib-opts) (git/github-repo-ssh-url lib-sym))
(fn [client lib-sym lib-opts]
(let [url (or (:git/url lib-opts) (git/git-repo-url client lib-sym))
sha (:git/sha lib-opts)]
{lib-sym {:git/url url :git/sha sha}}))

[#{:latest-sha} #{:git/url :latest-sha}]
(fn [client lib-sym lib-opts]
(let [url (or (:git/url lib-opts) (git/github-repo-ssh-url lib-sym))
(let [url (or (:git/url lib-opts) (git/git-repo-url client lib-sym))
sha (git/latest-git-sha client url)]
{lib-sym {:git/url url :git/sha sha}}))

Expand Down Expand Up @@ -66,7 +66,8 @@
:valid-combinations valid-lib-opts}))

(def ^:private default-deps-info-client
{:ensure-git-dir gitlibs-impl/ensure-git-dir})
{:ensure-git-dir gitlibs-impl/ensure-git-dir
:git-fetch gitlibs-impl/git-fetch})

(defn infer
"Returns a tools.deps lib map for the given CLI opts."
Expand Down

0 comments on commit 365ca11

Please sign in to comment.