chromium,chromedriver: 129.0.6668.91 -> 129.0.6668.100
[NixPkgs.git] / pkgs / common-updater / scripts / update-source-version
blob626c8f8261c03a01d5a3c09c5082ad52deb3a240
1 #!/usr/bin/env bash
2 set -e
4 scriptName=update-source-version # do not use the .wrapped name
6 die() {
7 echo "$scriptName: error: $1" >&2
8 exit 1
11 usage() {
12 echo "Usage: $scriptName <attr> <version> [<new-source-hash>] [<new-source-url>]"
13 echo " [--version-key=<version-key>] [--source-key=<source-key>]"
14 echo " [--system=<system>] [--file=<file-to-update>] [--rev=<revision>]"
15 echo " [--ignore-same-hash] [--ignore-same-version] [--print-changes]"
16 echo ""
17 echo "The <version> positional argument is also optional when passing --ignore-same-version."
20 ignoreSameVersion=
21 args=()
23 for arg in "$@"; do
24 case $arg in
25 --system=*)
26 system="${arg#*=}"
27 systemArg="--system ${arg#*=}"
29 --version-key=*)
30 versionKey="${arg#*=}"
32 --source-key=*)
33 sourceKey="${arg#*=}"
35 --file=*)
36 nixFile="${arg#*=}"
37 if [[ ! -f "$nixFile" ]]; then
38 die "Could not find provided file $nixFile"
41 --rev=*)
42 newRevision="${arg#*=}"
44 --ignore-same-hash)
45 ignoreSameHash="true"
47 --ignore-same-version)
48 ignoreSameVersion="true"
50 --print-changes)
51 printChanges="true"
53 --help)
54 usage
55 exit 0
57 --*)
58 echo "$scriptName: Unknown argument: $arg"
59 usage
60 exit 1
63 args["${#args[*]}"]=$arg
65 esac
66 done
68 attr=${args[0]}
69 newVersion=${args[1]}
70 newHash=${args[2]}
71 newUrl=${args[3]}
73 # Third-party repositories might not accept arguments in their default.nix.
74 importTree="(let tree = import ./.; in if builtins.isFunction tree then tree {} else tree)"
76 if [[ -z "$ignoreSameVersion" ]]; then
77 requiredArgs=2
78 else
79 requiredArgs=1
82 if (( "${#args[*]}" < $requiredArgs )); then
83 echo "$scriptName: Too few arguments"
84 usage
85 exit 1
88 if (( "${#args[*]}" > 4 )); then
89 echo "$scriptName: Too many arguments"
90 usage
91 exit 1
94 if [[ -z "$versionKey" ]]; then
95 versionKey=version
98 if [[ -z "$sourceKey" ]]; then
99 sourceKey=src
102 # Allow finding packages among flake outputs in repos using flake-compat.
103 pname=$(nix-instantiate $systemArg --eval --strict -A "$attr.name" || echo)
104 if [[ -z "$pname" ]]; then
105 if [[ -z "$system" ]]; then
106 system=$(nix-instantiate --eval -E 'builtins.currentSystem' | tr -d '"')
109 pname=$(nix-instantiate $systemArg --eval --strict -A "packages.$system.$attr.name" || echo)
110 if [[ -n "$pname" ]]; then
111 attr="packages.$system.$attr"
112 else
113 pname=$(nix-instantiate $systemArg --eval --strict -A "legacyPackages.$system.$attr.name" || echo)
114 if [[ -n "$pname" ]]; then
115 attr="legacyPackages.$system.$attr"
116 else
117 die "Could not find attribute '$attr'!"
122 if [[ -z "$nixFile" ]]; then
123 nixFile=$(nix-instantiate $systemArg --eval --strict -A "$attr.meta.position" | sed -re 's/^"(.*):[0-9]+"$/\1/')
124 if [[ ! -f "$nixFile" ]]; then
125 die "Couldn't evaluate '$attr.meta.position' to locate the .nix file!"
128 # flake-compat will return paths in the Nix store, we need to correct for that.
129 possiblyOutPath=$(nix-instantiate $systemArg --eval -E "with $importTree; outPath" 2>/dev/null | tr -d '"')
130 if [[ -n "$possiblyOutPath" ]]; then
131 outPathEscaped=$(echo "$possiblyOutPath" | sed 's#[$^*\\.[|]#\\&#g')
132 pwdEscaped=$(echo "$PWD" | sed 's#[$^*\\.[|]#\\&#g')
133 nixFile=$(echo "$nixFile" | sed "s|^$outPathEscaped|$pwdEscaped|")
137 oldHashAlgo=$(nix-instantiate $systemArg --eval --strict -E "let pkgs = $importTree; in pkgs.$attr.$sourceKey.drvAttrs.outputHashAlgo or pkgs.$attr.$sourceKey.drvAttrs.outputHash" | tr -d '"' | sed "s/-.*//")
138 oldHash=$(nix-instantiate $systemArg --eval --strict -A "$attr.$sourceKey.drvAttrs.outputHash" | tr -d '"')
140 if [[ -z "$oldHashAlgo" || -z "$oldHash" ]]; then
141 die "Couldn't evaluate old source hash from '$attr.$sourceKey'!"
144 if [[ $(grep --count "$oldHash" "$nixFile") != 1 ]]; then
145 die "Couldn't locate old source hash '$oldHash' (or it appeared more than once) in '$nixFile'!"
148 oldVersion=$(nix-instantiate $systemArg --eval -E "with $importTree; $attr.${versionKey} or (builtins.parseDrvName $attr.name).version" | tr -d '"')
150 if [[ -z "$oldVersion" ]]; then
151 die "Couldn't find out the old version of '$attr'!"
154 if [[ -n "$ignoreSameVersion" && -z "$newVersion" ]]; then
155 newVersion="$oldVersion"
158 if [[ -z "$ignoreSameVersion" && "$oldVersion" = "$newVersion" ]]; then
159 echo "$scriptName: New version same as old version, nothing to do." >&2
160 if [ -n "$printChanges" ]; then
161 printf '[]\n'
163 exit 0
166 if [[ -n "$newRevision" ]]; then
167 oldRevision=$(nix-instantiate $systemArg --eval -E "with $importTree; $attr.$sourceKey.rev" | tr -d '"')
168 if [[ -z "$oldRevision" ]]; then
169 die "Couldn't evaluate source revision from '$attr.$sourceKey'!"
173 # Escape regex metacharacter that are allowed in store path names
174 oldVersionEscaped=$(echo "$oldVersion" | sed -re 's|[.+]|\\&|g')
176 if [[ $(grep --count --extended-regexp "^\s*(let\b)?\s*$versionKey\s*=\s*\"$oldVersionEscaped\"" "$nixFile") = 1 ]]; then
177 pattern="/\b$versionKey\b\s*=/ s|\"$oldVersionEscaped\"|\"$newVersion\"|"
178 elif [[ $(grep --count --extended-regexp "^\s*(let\b)?\s*name\s*=\s*\"[^\"]+-$oldVersionEscaped\"" "$nixFile") = 1 ]]; then
179 pattern="/\bname\b\s*=/ s|-$oldVersionEscaped\"|-$newVersion\"|"
180 else
181 die "Couldn't figure out where out where to patch in new version in '$attr'!"
184 if [[ "$oldHash" =~ ^(sha256|sha512)[:-] ]]; then
185 # Handle the possible SRI-style hash attribute (in the form ${type}${separator}${hash})
186 # True SRI uses dash as a separator and only supports base64, whereas Nix’s SRI-style format uses a colon and supports all the same encodings like regular hashes (16/32/64).
187 # To keep this program reasonably simple, we will upgrade Nix’s format to SRI.
188 oldHashAlgo="${BASH_REMATCH[1]}"
189 sri=true
190 elif [[ "$oldHashAlgo" = "null" ]]; then
191 # Some fetcher functions support SRI-style `hash` attribute in addition to legacy type-specific attributes. When `hash` is used `outputHashAlgo` is null so let’s complain when SRI-style hash value was not detected.
192 die "Unable to figure out hashing scheme from '$oldHash' in '$attr'!"
195 case "$oldHashAlgo" in
196 # Choose a temporary hash for given algorithm.
197 # Not using all-zeroes hash, since that is sometimes
198 # used for clean-up when updating multi-source packages.
199 # Created by hashing “update-source-version” string.
200 sha256) tempHash=AzH1rZFqEH8sovZZfJykvsEmCedEZWigQFHWHl6/PdE= ;;
201 sha512) tempHash=KFj9Fvco4AuCgLJIGRnVzyssRf7VGP2oi5CkH6ADvj75ow3am3h8pxefOgQlO+i33Q/BBnG/ST/F7B/0BvWHxw== ;;
202 *) die "Unhandled hash algorithm '$oldHashAlgo' in '$attr'!" ;;
203 esac
205 if [[ -n "$sri" ]]; then
206 # SRI hashes only support base64
207 # SRI hashes need to declare the hash type as part of the hash
208 tempHash="$(nix --extra-experimental-features nix-command hash to-sri --type "$oldHashAlgo" "$tempHash" 2>/dev/null \
209 || nix to-sri --type "$oldHashAlgo" "$tempHash" 2>/dev/null)" \
210 || die "Failed to convert hash to SRI representation!"
213 # Escape regex metacharacter that are allowed in hashes (+)
214 oldHashEscaped=$(echo "$oldHash" | sed -re 's|[+]|\\&|g')
215 tempHashEscaped=$(echo "$tempHash" | sed -re 's|[+]|\\&|g')
217 if [[ "$oldVersion" != "$newVersion" ]]; then
218 # Replace new version
219 sed -i.cmp "$nixFile" -re "$pattern"
220 if cmp -s "$nixFile" "$nixFile.cmp"; then
221 die "Failed to replace version '$oldVersion' to '$newVersion' in '$attr'!"
225 # Replace new URL
226 if [[ -n "$newUrl" ]]; then
227 oldUrl=$(nix-instantiate $systemArg --eval -E "with $importTree; builtins.elemAt ($attr.$sourceKey.drvAttrs.urls or [ $attr.$sourceKey.url ]) 0" | tr -d '"')
228 if [[ -z "$oldUrl" ]]; then
229 die "Couldn't evaluate source url from '$attr.$sourceKey'!"
232 # Escape regex metacharacter that are allowed in store path names
233 oldUrlEscaped=$(echo "$oldUrl" | sed -re 's|[${}.+]|\\&|g')
235 sed -i.cmp "$nixFile" -re "s|\"$oldUrlEscaped\"|\"$newUrl\"|"
236 if cmp -s "$nixFile" "$nixFile.cmp"; then
237 die "Failed to replace source URL '$oldUrl' to '$newUrl' in '$attr'!"
241 sed -i.cmp "$nixFile" -re "s|\"$oldHashEscaped\"|\"$tempHash\"|"
242 if cmp -s "$nixFile" "$nixFile.cmp"; then
243 die "Failed to replace source hash of '$attr' to a temporary hash!"
246 # Replace new revision, if given
247 if [[ -n "$newRevision" ]]; then
248 sed -i.cmp "$nixFile" -re "s|\"$oldRevision\"|\"$newRevision\"|"
249 if cmp -s "$nixFile" "$nixFile.cmp"; then
250 die "Failed to replace source revision '$oldRevision' to '$newRevision' in '$attr'!"
254 # If new hash not given on the command line, recalculate it ourselves.
255 if [[ -z "$newHash" ]]; then
256 nix-build $systemArg --no-out-link -A "$attr.$sourceKey" 2>"$attr.fetchlog" >/dev/null || true
257 # FIXME: use nix-build --hash here once https://github.com/NixOS/nix/issues/1172 is fixed
258 newHash=$(
259 sed '1,/hash mismatch in fixed-output derivation/d' "$attr.fetchlog" \
260 | grep --perl-regexp --only-matching 'got: +.+[:-]\K.+' \
261 || true # handled below
264 if [[ -n "$newHash" && -n "$sri" ]]; then
265 # nix-build preserves the hashing scheme so we can just convert the result to SRI using the old type
266 newHash="$(nix --extra-experimental-features nix-command hash to-sri --type "$oldHashAlgo" "$newHash" 2>/dev/null \
267 || nix to-sri --type "$oldHashAlgo" "$newHash" 2>/dev/null)" \
268 || die "Failed to convert hash to SRI representation!"
272 if [[ -z "$newHash" ]]; then
273 cat "$attr.fetchlog" >&2
274 die "Couldn't figure out new hash of '$attr.$sourceKey'!"
277 if [[ -z "${ignoreSameHash}" && "$oldVersion" != "$newVersion" && "$oldHash" = "$newHash" ]]; then
278 die "Both the old and new source hashes of '$attr.$sourceKey' were equivalent. Please fix the package's source URL to be dependent on '\${version}'!"
281 sed -i.cmp "$nixFile" -re "s|\"$tempHashEscaped\"|\"$newHash\"|"
282 if cmp -s "$nixFile" "$nixFile.cmp"; then
283 die "Failed to replace temporary source hash of '$attr' to the final source hash!"
286 rm -f "$nixFile.cmp"
287 rm -f "$attr.fetchlog"
289 if [ -n "$printChanges" ]; then
290 printf '[{"attrPath":"%s","oldVersion":"%s","newVersion":"%s","files":["%s"]}]\n' "$attr" "$oldVersion" "$newVersion" "$nixFile"