Skip to content

Commit 4ff9158

Browse files
committed
Automatically resolve repo default branch
This removes the need to specify the :ref if the default branch is not `master`
1 parent fc9d1fb commit 4ff9158

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

docs/module-spec.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ containers | map? | A docker-compose [`services`](https://docs.docker.com/compos
1414
Field | Type | Description
1515
-|-|-
1616
url | string | The github repo identifier of a remote module in the form `<owner>/<repo>`. Currently this is the only supported value but in future this will be expanded to support additional remote sources.
17-
ref | string? | A git ref that the remote module should be resolved against. Defaults to `master`
17+
ref | string? | A git ref that the remote module should be resolved against. Defaults to the default branch of the repo
18+
or`master`
1819
sha | string? | An exact sha that should be used instead of dynamically resolving it from a ref. Exclusive with ref.
1920
subdir | string? | A directory path within the remote repository that the `module` config file should be resolved from. Defaults to `.kl`
2021

src/k16/kl/api/resolver.clj

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,23 @@
2323

2424
(:sha data)))
2525

26+
(defn- get-default-branch [identifier]
27+
(let [res (api.github/request {:path (str "/repos/" identifier)})
28+
data (json/read-value (:body res) json/keyword-keys-object-mapper)]
29+
30+
(when (not= 200 (:status res))
31+
(log/info (str "Failed to resolve default branch for " identifier))
32+
(cli.util/exit! (:message data) 1))
33+
34+
(or (:default_branch data)
35+
"master")))
36+
2637
(defn- resolve-module-ref [{:keys [url sha ref subdir]}]
2738
(when-not sha
2839
(log/debug (str "Resolving " url (if subdir (str "/" subdir) ""))))
2940

30-
(let [sha (if sha sha (get-commit-for-ref url ref))]
41+
(let [ref (if ref ref (get-default-branch url))
42+
sha (if sha sha (get-commit-for-ref url ref))]
3143
(cond-> {:url url :sha sha :ref ref}
3244
subdir (assoc :subdir subdir))))
3345

@@ -45,12 +57,6 @@
4557
(let [lock-entry (get lock submodule-name)
4658
current-reference (:ref lock-entry)
4759

48-
ref (when (and (not (:sha partial-ref))
49-
(not (:ref partial-ref)))
50-
"master")
51-
partial-ref (cond-> partial-ref
52-
ref (assoc :ref ref))
53-
5460
should-resolve?
5561
(or (not (:sha current-reference))
5662

0 commit comments

Comments
 (0)