Skip to content

Commit 28a253c

Browse files
committed
check: In flutter_version, verify version bound matches commit
1 parent 1201e38 commit 28a253c

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

tools/check

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ run_test() {
220220
}
221221

222222
# Check the Flutter version in pubspec.yaml is commented with a commit ID,
223-
# and the commit is from upstream main.
223+
# which agrees with the version, and the commit is from upstream main.
224224
run_flutter_version() {
225225
# Omitted from this files check:
226226
# tools/check
@@ -231,14 +231,49 @@ run_flutter_version() {
231231
flutter_tree=$(flutter_tree)
232232
flutter_git=( git --git-dir="${flutter_tree}"/.git )
233233

234-
# Parse our Flutter version spec with its commit-ID comment.
235-
local flutter_commit
236-
flutter_commit=$(
234+
# Parse our Flutter version spec and its commit-ID comment.
235+
local parsed flutter_version flutter_commit
236+
# shellcheck disable=SC2207 # output has controlled whitespace
237+
parsed=( $(
237238
perl <pubspec.yaml -0ne '
238-
print $1 if (/^ sdk: .*\n flutter: \S+\s*# ([0-9a-f]{40})$/m)'
239+
print "$1 $2" if (
240+
/^ sdk: .*\n flutter: '\''>=(\S+)'\''\s*# ([0-9a-f]{40})$/m)'
241+
) ) || return
242+
if [ -z "${#parsed[@]}" ]; then
243+
echo >&2 "error: Flutter version spec not recognized in pubspec.yaml"
244+
return 1
245+
fi
246+
flutter_version="${parsed[0]}"
247+
flutter_commit="${parsed[1]}"
248+
249+
# Check the version name matches the commit ID.
250+
local commit_described predicted_version
251+
commit_described=$(
252+
"${flutter_git[@]}" describe --tags "${flutter_commit}"
239253
) || return
240-
if [ -z "${flutter_commit}" ]; then
241-
echo >&2 "error: Flutter commit spec not found in pubspec.yaml"
254+
predicted_version=$(
255+
echo "${commit_described}" \
256+
| perl -lne 'print if (s
257+
# This transformation is ad hoc.
258+
# If we find cases where it fails, we can study
259+
# how the `flutter` tool actually decides the version name.
260+
<^(\d+\.\d+\.\d+-) (\d+) (\.\d+\.pre) -(\d+) -g[0-9a-f]+$>
261+
<$1 . ($2 + 1) . $3 . "." . $4>xe)'
262+
) || return
263+
if [ -z "${predicted_version}" ]; then
264+
cat >&2 <<EOF
265+
error: unexpected 'git describe' result on Flutter commit in pubspec.yaml
266+
Commit ${flutter_commit} was described as: ${commit_described}
267+
EOF
268+
return 1
269+
fi
270+
if [ "${flutter_version}" != "${predicted_version}" ]; then
271+
cat >&2 <<EOF
272+
error: Flutter commit in pubspec.yaml seems to differ from version bound
273+
Commit ${flutter_commit} was described as: ${commit_described}
274+
We therefore expect the Flutter version name to be: ${predicted_version}
275+
But the Flutter version bound in pubspec.yaml is: ${flutter_version}
276+
EOF
242277
return 1
243278
fi
244279

@@ -257,7 +292,7 @@ EOF
257292
return 1
258293
fi
259294

260-
if_verbose echo "OK Flutter commit ${flutter_commit}"
295+
if_verbose echo "OK Flutter ${flutter_version} aka ${flutter_commit}"
261296

262297
return 0
263298
}

0 commit comments

Comments
 (0)