diff options
| author | tv <tv@krebsco.de> | 2026-11-13 14:01:19 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-13 15:06:32 +0100 |
| commit | 36cfd6b9fa8d729172f90e8fdd507349905d4f11 (patch) | |
| tree | 93424f012ce3b538eab7550429cc654331476d25 /pkgs | |
| parent | 494b3127d82ea8139a6937a1064ea58f62bfe4bf (diff) | |
populate: follow branches and tags of git sources
A ref that consists of 4 to 40 hex digits is a commit, which populate
fetches only if the work tree lacks it, unless fetchAlways is set. Any
other ref is a branch or tag, which populate fetches on every run,
resetting the work tree to the commit fetched. Resetting to the name
itself resolves to a local branch, which fetching doesn't move.
Diffstat (limited to 'pkgs')
| -rw-r--r-- | pkgs/populate/default.nix | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/pkgs/populate/default.nix b/pkgs/populate/default.nix index a5daee8..1787b65 100644 --- a/pkgs/populate/default.nix +++ b/pkgs/populate/default.nix @@ -1209,25 +1209,32 @@ let git remote set-url origin ${quote source.url} fi - # TODO resolve git_ref to commit hash - hash=${quote source.ref} - - if ! test "$(git log --format=%H -1)" = "$hash"; then - ${if source.fetchAlways then /* sh */ '' - ${if source.shallow then /* sh */ '' - git fetch --depth=1 origin "$hash" - '' else /* sh */ '' - git fetch origin - ''} + # A commit, given by its hash or a prefix thereof, gets fetched only if + # the work tree lacks it, unless fetchAlways is set. A branch or tag + # gets fetched on every run, so the work tree follows it: resetting to + # its name would reset to a local branch, which fetching doesn't move. + ref=${quote source.ref} + fetch() { + ${if source.shallow then /* sh */ '' + git fetch --depth=1 origin "$1" '' else /* sh */ '' - if ! git log -1 "$hash" >/dev/null 2>&1; then - ${if source.shallow then /* sh */ '' - git fetch --depth=1 origin "$hash" - '' else /* sh */ '' - git fetch origin - ''} - fi + git fetch origin "$1" ''} + } + if printf %s "$ref" | grep -Eqx '[0-9a-f]{4,40}'; then + hash=$ref + if ! test "$(git log --format=%H -1)" = "$hash"; then + ${if source.fetchAlways then /* sh */ '' + ${if source.shallow then ''fetch "$hash"'' else "git fetch origin"} + '' else /* sh */ '' + if ! git log -1 "$hash" >/dev/null 2>&1; then + ${if source.shallow then ''fetch "$hash"'' else "git fetch origin"} + fi + ''} + fi + else + fetch "$ref" + hash=$(git rev-parse --verify 'FETCH_HEAD^{commit}') fi # Also if HEAD is there already: rewriting a lock file in place, e.g., # changes the work tree, and stages the lock file if Nix created it. |
