scripts/updatemodules

101 lines
2.7 KiB
Plaintext
Raw Normal View History

2022-11-16 15:01:09 +01:00
#!/bin/bash
2023-02-07 17:15:44 +01:00
allatonce=
2022-11-16 15:01:09 +01:00
pushurl="gitea@gitea.opensuse.org:lnussel/core.git"
prsrcusr="lnussel"
prtarget="https://gitea.opensuse.org/api/v1/repos/mold/core/pulls"
2023-02-07 17:15:44 +01:00
if [ "$1" = '--allatonce' ]; then
allatonce=1
shift
fi
2022-11-16 15:01:09 +01:00
modules=($@)
origin="$(git config --get remote.origin.url)"
2023-02-07 17:15:44 +01:00
now="$(date "+%s %z")"
2022-11-16 15:01:09 +01:00
# FIXME: hardcoded to avoid changing commits for now
2023-02-07 17:15:44 +01:00
now="$(stat -c %Y token) +0100"
2022-11-16 15:01:09 +01:00
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"
tmpfile=$(mktemp updatemodules.XXXXXX)
cleanup()
{
rm -f "$tmpfile"
}
trap cleanup EXIT
2023-02-07 17:15:44 +01:00
read -r token < token || exit 1
2022-11-16 15:01:09 +01:00
2023-02-07 17:15:44 +01:00
# read all submodules
2022-11-16 15:01:09 +01:00
declare -A revs
2023-02-07 17:15:44 +01:00
while read -r m t cid p; do
2022-11-16 15:01:09 +01:00
[ "$t" = "commit" ] || continue
revs["$p"]="$cid"
2023-02-07 17:15:44 +01:00
done < <(git cat-file -p "HEAD^{tree}")
2022-11-16 15:01:09 +01:00
if [ -z "$modules" ]; then
modules=("${!revs[@]}")
fi
2023-02-07 17:15:44 +01:00
# check remotes for updates
2022-11-16 15:01:09 +01:00
declare -A commits
2023-02-07 17:15:44 +01:00
treetext=$(git cat-file -p "HEAD^{tree}")
2022-11-16 15:01:09 +01:00
for m in "${modules[@]}"; do
path="$(git config -f .gitmodules --get "submodule.$m.path")"
url="$(git config -f .gitmodules --get "submodule.$m.url")"
branch="$(git config -f .gitmodules --get "submodule.$m.branch")"
2023-02-07 17:15:44 +01:00
if [ -z "$path" ] || [ -z "$url" ]; then
2022-11-16 15:01:09 +01:00
echo "$m unknown" >&2
continue
fi
if [ "${url:0:3}" = '../' ]; then
url="$origin/$url"
fi
2023-02-07 17:15:44 +01:00
read -r cid _d < <(git ls-remote "$url" "${branch:-HEAD}")
2022-11-16 15:01:09 +01:00
if [ -z "${revs[$path]}" ]; then
echo "$path unknown" >&2
continue
fi
2023-02-07 17:15:44 +01:00
# create a new commit for this package
2022-11-16 15:01:09 +01:00
if [ "${revs[$path]}" != "$cid" ]; then
echo "Needs update: $path@${revs[$path]} -> $cid"
2023-02-07 17:15:44 +01:00
if [ "$allatonce" = 1 ]; then
treetext="${treetext/${revs[$path]} $path/$cid $path}"
else
newtree="$(git cat-file -p "HEAD^{tree}" | sed -e "s/${revs[$path]}\t$path/$cid\t$path/" | git mktree)"
newcid="$(git commit-tree -p HEAD -m "Update $m" "$newtree")"
commits["$m"]="$newcid"
fi
2022-11-16 15:01:09 +01:00
fi
done
2023-02-07 17:15:44 +01:00
if [ "$allatonce" = 1 ]; then
newtree="$(echo "$treetext" | git mktree)"
newcid="$(git commit-tree -p HEAD -m "Update all" "$newtree")"
commits["all"]="$newcid"
fi
2022-11-16 15:01:09 +01:00
for m in "${!commits[@]}"; do
if git send-pack --force "$pushurl" "${commits[$m]}:refs/heads/update_$m"; then
#https://github.com/go-gitea/gitea/issues/18842
if curl -s -f "$prtarget" \
-H "accept: application/json" \
-H "Authorization: token $token" \
-H "Content-Type: application/json" \
-d "{ \"base\": \"main\", \"head\": \"$prsrcusr:update_$m\", \"title\": \"Update $m\"}" > "$tmpfile"; then
prid=$(jq .id < "$tmpfile")
echo "filed pr #$prid for $m"
else
echo "failed to file pr for $m"
jq .message < "$tmpfile"
fi
else
echo "failed to push $m update" >&2
jq .message < "$tmpfile"
fi
done