Merge pull request #293260 from r-ryantm/auto-update/odin
[NixPkgs.git] / pkgs / common-updater / unstable-updater.nix
blob29b7fcf196795d2c910105e9967d0eecf84c0d16
1 { lib
2 , writeShellScript
3 , coreutils
4 , git
5 , nix
6 , common-updater-scripts
7 }:
9 # This is an updater for unstable packages that should always use the latest
10 # commit.
11 { url ? null # The git url, if empty it will be set to src.gitRepoUrl
12 , branch ? null
13 , stableVersion ? false # Use version format according to RFC 107 (i.e. LAST_TAG+date=YYYY-MM-DD)
14 , tagPrefix ? "" # strip this prefix from a tag name when using stable version
15 , shallowClone ? true
18 let
19   updateScript = writeShellScript "unstable-update-script.sh" ''
20     set -ex
22     url=""
23     branch=""
24     use_stable_version=""
25     tag_prefix=""
26     shallow_clone=""
28     while (( $# > 0 )); do
29         flag="$1"
30         shift 1
31         case "$flag" in
32           --url=*)
33             url="''${flag#*=}"
34             ;;
35           --branch=*)
36             branch="''${flag#*=}"
37             ;;
38           --use-stable-version)
39             use_stable_version=1
40             ;;
41           --tag-prefix=*)
42             tag_prefix="''${flag#*=}"
43             ;;
44           --shallow-clone)
45             shallow_clone=1
46             ;;
47           *)
48             echo "$0: unknown option ‘''${flag}’"
49             exit 1
50             ;;
51         esac
52     done
54     # By default we set url to src.gitRepoUrl
55     if [[ -z "$url" ]]; then
56         url="$(${nix}/bin/nix-instantiate $systemArg --eval -E \
57                    "with import ./. {}; $UPDATE_NIX_ATTR_PATH.src.gitRepoUrl" \
58             | tr -d '"')"
59     fi
61     # Get info about HEAD from a shallow git clone
62     tmpdir="$(${coreutils}/bin/mktemp -d)"
64     cloneArgs=(
65       --bare
66     )
68     if [[ "$shallow_clone" == "1" ]]; then
69         cloneArgs+=(--depth=1)
70     fi
72     if [[ -n "$branch" ]]; then
73         cloneArgs+=(--branch="$branch")
74     fi
76     ${git}/bin/git clone "''${cloneArgs[@]}" "$url" "$tmpdir"
78     pushd "$tmpdir"
79     commit_date="$(${git}/bin/git show -s --pretty='format:%cs')"
80     commit_sha="$(${git}/bin/git show -s --pretty='format:%H')"
81     if [[ -z "$use_stable_version" ]]; then
82         new_version="unstable-$commit_date"
83     else
84         depth=100
85         while (( $depth < 10000 )); do
86             last_tag="$(${git}/bin/git describe --tags --abbrev=0 2> /dev/null || true)"
87             if [[ -n "$last_tag" ]]; then
88                 break
89             fi
90             ${git}/bin/git fetch --depth="$depth" --tags
91             depth=$(( $depth * 2 ))
92         done
93         if [[ -z "$last_tag" ]]; then
94             echo "Cound not found a tag within last 10000 commits" > /dev/stderr
95             exit 1
96         fi
97         if [[ -n "$tag_prefix" ]]; then
98           last_tag="''${last_tag#$tag_prefix}"
99         fi
100         new_version="$last_tag+date=$commit_date"
101     fi
102     popd
103     # ${coreutils}/bin/rm -rf "$tmpdir"
105     # update the nix expression
106     ${common-updater-scripts}/bin/update-source-version \
107         "$UPDATE_NIX_ATTR_PATH" \
108         "$new_version" \
109         --rev="$commit_sha"
110   '';
114   updateScript
115   "--url=${builtins.toString url}"
116 ] ++ lib.optionals (branch != null) [
117   "--branch=${branch}"
118 ] ++ lib.optionals stableVersion [
119   "--use-stable-version"
120   "--tag-prefix=${tagPrefix}"
121 ] ++ lib.optionals shallowClone [
122   "--shallow-clone"