summaryrefslogtreecommitdiff
path: root/util/scripts/update_submodules
blob: 9f6c14f5b00067c77683f170e1a272caa80f5829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env bash
#
# SPDX-License-Identifier: GPL-2.0-only

# Description:
# Check all submodules for updates.  If there are more than a minimum
# number of changes, create a commit to update the submodule to the
# new version.

set -eu -o pipefail

VERSION="1.01"
PROGRAM=$0
PROGNAME="$(basename "${PROGRAM}")"

export LANG=C
export LC_ALL=C
export TZ=UTC0

min_commits=10
TOP=${PWD}
SUBMODULES_WITH_UPDATES=0
submodule_dirs=()
skip_sync=""
max_commits_to_list=65

show_version() {
	echo "${PROGNAME} version ${VERSION}"
	echo
}

usage() {
	echo "Usage: ${PROGNAME} [options]"
	echo
	echo "Options:"
	echo " -c | --changes <#>     Specify the minimum number of changes to update a repo"
	echo " -h | --help            Print usage and exit"
	echo " -R | --repo <dir>      Specify a single repo directory to update"
	echo " -s | --skipsync        Assume that repos are already synced"
	echo " -V | --version         Print the version and exit"
	echo
}

get_args() {
	args=$(getopt -l changes:,help,repo:,skipsync,version -o c:hR:sV -- "$@")
	getopt_ret=$?
	eval set -- "${args}"

	if [ ${getopt_ret} != 0 ]; then
		usage
		exit 1
	fi

	while true; do
		local opt
		opt="$1"
		shift
		case "${opt}" in
		-c | --changes)
			min_commits="${1}"
			shift
			;;
		-h | --help)
			usage
			exit 0
			;;
		-R | --repo)
			submodule_dirs=("$(readlink -f "${1}")")
			shift
			if [[ ! -d "${submodule_dirs[0]}" ]]; then
				echo "Error: ${submodule_dirs[0]} is not valid."
				usage
				exit 1
			fi
			;;
		-s | --skipsync)
			skip_sync=1
			;;
		-V | --version)
			exit 0
			;;
		*)
			break
			;;
		esac
	done
}


main() {
	show_version
	get_args "$@"

	if (( ${#submodule_dirs[@]} == 0 )); then
		readarray -t submodule_dirs < <(git submodule foreach pwd | grep -v "Entering")
	fi

	for submodule in "${submodule_dirs[@]}"; do
		echo "Checking submodule ${submodule}"
		if ! cd "$submodule"; then
 			echo "Error: could not cd to $submodule"
			exit 1
		fi

		initial_commit_id="$(git log --pretty='%h' -n 1)"
		initial_commit_description="$(git log --pretty='%ci - (%s)' -n 1)"
		if [[ ${skip_sync} != "1" ]]; then
			git fetch 2>/dev/null
		fi

		declare -a branches=("origin/main" "origin/master" "origin/trunk")
		for branch in "${branches[@]}"; do
			if git branch -a | grep "${branch}" > /dev/null; then
				branch_name="${branch}"
				break
			fi
		done

		updated_commit_id="$(git log --pretty='%h' -n 1 "${branch_name}" -- )"
		updated_commit_description="$(git log --pretty='%ci - (%s)' -n 1 "${updated_commit_id}")"
		if [ "${initial_commit_id}" = "${updated_commit_id}" ]; then
			echo "No updates for ${submodule}"
			continue
		fi
		SUBMODULES_WITH_UPDATES+=1
		update_log="$(git log --oneline "${initial_commit_id}..${updated_commit_id}")"
		update_count="$(echo "${update_log}" | wc -l)"
		if [[ "${update_count}" -gt "${max_commits_to_list}" ]]; then
			update_log=""
			new_commit_terminator="."
		else
			new_commit_terminator=":"
		fi
		echo "${update_count} new commits for ${submodule}"
		if [ "${update_count}" -ge "${min_commits}" ]; then
			echo "Creating commit to update ${submodule##*/} submodule"
			git checkout "${updated_commit_id}" > /dev/null 2>&1
			cd "${TOP}" || exit 1
			git add "${submodule}" > /dev/null 2>&1 || exit 1
			git commit -s -F- > /dev/null 2>&1 <<-EOF
	Update ${submodule##*/} submodule to upstream ${branch##*/}

	Updating from commit id ${initial_commit_id}:
	$initial_commit_description

	to commit id ${updated_commit_id}:
	${updated_commit_description}

	This brings in ${update_count} new commits${new_commit_terminator}
	${update_log}
	EOF
		fi
	done

	if [ "${SUBMODULES_WITH_UPDATES}" = "0" ]; then
		echo "No submodules with any updates."
	fi
}

main "$@"