Skip to content

Commit c370497

Browse files
committed
tmp
Signed-off-by: Chun-Hung Tseng <[email protected]>
1 parent 6310d6d commit c370497

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

scripts/update_dep.sh

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
# Copyright 2025 The etcd Authors
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,7 +15,7 @@
1515
#
1616
# Usage:
1717
# ./scripts/update_dep.sh module version
18-
# or ./scripts/update_dep.sh module
18+
# or ./scripts/update_dep.sh module (to update to the latest version)
1919
# e.g.
2020
# ./scripts/update_dep.sh github.com/golang/groupcache
2121
# ./scripts/update_dep.sh github.com/soheilhy/cmux v0.1.5
@@ -26,48 +26,57 @@ set -euo pipefail
2626

2727
source ./scripts/test_lib.sh
2828

29-
if [ "$#" -ne 2 ]; then
30-
echo "Illegal number of parameters"
29+
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
30+
log_error "Illegal number of parameters. Usage: $0 module [version]"
3131
exit 1
3232
fi
3333

3434
mod="$1"
35-
ver="$2"
35+
ver="${2:-}"
3636

3737
function print_current_dep_version {
38-
echo "${mod} version in all go mod files"
39-
grep --exclude-dir=.git --include=\*.mod -Ri "^.*${mod} v.*$" | grep -v sum
40-
printf "\n\n"
38+
log_info "${mod} version in all go.mod files:"
39+
find . -name go.mod -exec grep -H "^\s*${mod}\s" {} + | sed 's|:|\t|' || true
40+
printf "\n"
4141
}
4242

4343
function is_fully_indirect {
44-
# check if all lines end with "// indirect"
45-
# if grep found nothing, the error code will be non-zero
46-
ALL=$(grep --exclude-dir=.git --include=\*.mod -Ri "^.*${mod} v.*$" | grep -v sum | wc -l)
47-
ONLY_INDIRECT=$(grep --exclude-dir=.git --include=\*.mod -Ri "^.*${mod} v.*// indirect$" | grep -v sum | wc -l)
48-
if [[ "$ALL" == "$ONLY_INDIRECT" ]]; then
49-
echo "Fully indirect, we will terminate the script"
50-
exit 1
51-
else
52-
echo "Not fully indirect, we will perform dependency bump"
44+
# Returns 0 (true) if the dependency is fully indirect, 1 (false) otherwise.
45+
local all_lines
46+
all_lines=$(find . -name go.mod -exec grep -E "^\s*${mod}\s" {} + || true)
47+
if [ -z "${all_lines}" ]; then
48+
# Not a dependency anywhere.
49+
return 0
5350
fi
51+
52+
local direct_lines
53+
direct_lines=$(echo "${all_lines}" | grep -v "// indirect")
54+
55+
if [ -z "${direct_lines}" ]; then
56+
return 0 # true, fully indirect
57+
fi
58+
return 1 # false, has direct dependencies
5459
}
5560

5661
function update_module {
57-
run go mod tidy
58-
59-
deps=$(go list -f '{{if .Version}}{{.Path}},{{.Version}}{{end}}' -m all)
60-
if [[ "$deps" == *"${mod}"* ]]; then
62+
# Check if the module is a dependency.
63+
if go list -m all | grep -q -E "^\s*${mod}\s"; then
6164
if [ -z "${ver}" ]; then
65+
log_info "Updating ${mod} to latest version in $(module_subdir)..."
6266
run go get -u "${mod}"
6367
else
68+
log_info "Updating ${mod} to version ${ver} in $(module_subdir)..."
6469
run go get "${mod}@${ver}"
6570
fi
6671
fi
6772
}
6873

6974
print_current_dep_version
70-
is_fully_indirect
75+
if is_fully_indirect; then
76+
log_warning "Dependency '${mod}' is fully indirect or not used. Nothing to do."
77+
exit 0
78+
fi
79+
7180
run_for_modules update_module
7281

7382
./scripts/fix.sh

0 commit comments

Comments
 (0)