utils/index-repository: support calling on existing repository

Make utils/index-repository idempotent, meaning we can call it multiple
times on the same repo and same remotes without issues.

Also allow adding new remotes to an existing repo.

Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
This commit is contained in:
Théo Lebrun 2024-11-08 10:13:11 +01:00
parent a34a6c692a
commit 9d90b07201

View file

@ -14,13 +14,26 @@ mkdir -p $dir/data $dir/repo
git="git -C $dir/repo"
# This doesn't fail if repo already exists
$git init --bare
git config --system --add safe.directory $dir/repo
existing_remotes="$($git remote | xargs -L1 -r $git remote get-url | sort -u)"
shift
i=0
i="$($git remote | awk '
BEGIN { n=-1; }
$0 ~ /^remote[0-9]+$/ { i=substr($0, length("remote")+1);
if (i>n) n=i; }
END { print n+1; }')"
for remote
do
# Don't `git remote add` remotes that already exist, which is not an error.
if echo "$existing_remotes" | grep -qF "$remote"; then
continue;
fi
$git remote add remote$i $remote
i=$(($i+1))
done