scripts/updatemodules
Ludwig Nussel ecfcb53fd3 Split
- move pusher to separate script
- add getopt
2023-02-20 14:27:54 +01:00

109 lines
2.8 KiB
Bash
Executable File

#!/bin/bash
set -e
allatonce=
helpandquit()
{
cat <<-EOF
Usage: $0 [OPTIONS] [<module> ...]
OPTIONS:
-allatonce create dir from filename before extracting
-h help screen
EOF
exit 0
}
getopttmp=$(getopt -o h --long help,allatonce -n "${0##*/}" -- "$@")
eval set -- "$getopttmp"
while true ; do
case "$1" in
-h|--help) helpandquit; shift ;;
--allatonce) allatonce=1; shift ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
. .settings
modules=("$@")
origin="$(git config --get remote.origin.url)"
now="$(date "+%s %z")"
# FIXME: hardcoded to avoid changing commits for now
now="$(stat -c %Y token) +0100"
export GIT_AUTHOR_NAME="Auto"
export GIT_AUTHOR_EMAIL="auto@suse.de"
export GIT_AUTHOR_DATE="$now"
export GIT_COMMITTER_NAME="Auto"
export GIT_COMMITTER_EMAIL="auto@suse.de"
export GIT_COMMITTER_DATE="$now"
commit_base="origin/main"
tmpfile=$(mktemp updatemodules.XXXXXX)
cleanup()
{
rm -f "$tmpfile"
}
trap cleanup EXIT
# read all submodules
declare -A revs
while read -r m t cid p; do
[ "$t" = "commit" ] || continue
revs["$p"]="$cid"
done < <(git cat-file -p "$commit_base^{tree}")
if [ -z "$modules" ]; then
modules=("${!revs[@]}")
fi
# check remotes for updates
declare -A commits
treetext=$(git cat-file -p "$commit_base^{tree}")
gitmodules=$(git cat-file -p "$commit_base":.gitmodules)
for m in "${modules[@]}"; do
path="$(echo "$gitmodules" | git config -f /dev/stdin --get "submodule.$m.path")"
url="$(echo "$gitmodules" | git config -f /dev/stdin --get "submodule.$m.url")"
branch="$(echo "$gitmodules" | git config -f /dev/stdin --get "submodule.$m.branch")"
if [ -z "$path" ] || [ -z "$url" ]; then
echo "$m unknown" >&2
continue
fi
if [ "${url:0:3}" = '../' ]; then
url="$origin/$url"
fi
read -r cid _d < <(git ls-remote "$url" "${branch:-HEAD}")
if [ -z "${revs[$path]}" ]; then
echo "$path unknown" >&2
continue
fi
# create a new commit for this package
if [ "${revs[$path]}" != "$cid" ]; then
echo "Needs update: $path@${revs[$path]} -> $cid"
if [ "$allatonce" = 1 ]; then
treetext="${treetext/${revs[$path]} $path/$cid $path}"
else
newtree="$(git cat-file -p "$commit_base^{tree}" | sed -e "s/${revs[$path]}\t$path/$cid\t$path/" | git mktree)"
newcid="$(git commit-tree -p "$commit_base" -m "Update $m" "$newtree")"
commits["$m"]="$newcid"
fi
fi
done
if [ "$allatonce" = 1 ]; then
newtree="$(echo "$treetext" | git mktree)"
newcid="$(git commit-tree -p "$commit_base" -m "Update all" "$newtree")"
commits["all"]="$newcid"
fi
for m in "${!commits[@]}"; do
mkdir -p ".scripts/push"
if [ -e ".scripts/push/$m" ]; then
read -r cid < ".scripts/push/$m"
[ "$cid" = "${commits[$m]}" ] || echo "Warning: previous commit $cid for $m" >&2
fi
echo "${commits[$m]}" > ".scripts/push/$m"
done