cleanup fetch-all.sh

This commit is contained in:
2024-06-06 07:32:48 +02:00
parent 431baa43d4
commit 06904f8a77

View File

@@ -1,53 +1,44 @@
#!/usr/bin/env bash #!/usr/bin/env bash
#
# Fetch all branches from remotes, and track missing ones.
#
# The value of variable ORIGIN is used for repository origin. If
# not set, the default is "origin".
origin=origin default_origin=origin
declare -a remotes local_b origin=${ORIGIN:-$default_origin}
#declare -A aremotes
declare -a local_b
declare -A alocal_b declare -A alocal_b
# get remotes list
readarray -t remotes < <(git remote)
# fetch all remotes # fetch all remotes
git fetch --all git fetch --all --tags
# fill associative array with remote # get local branches, and build reverse associative array
#for remote in "${remotes[@]}"; do
# aremotes["$remote"]=1
# echo doing git fetch -a "$remote"
#done
# get local branches
readarray -t local_b < <(git for-each-ref --format='%(refname:short)' refs/heads/) readarray -t local_b < <(git for-each-ref --format='%(refname:short)' refs/heads/)
# build local ref array
for ref in "${local_b[@]}"; do for ref in "${local_b[@]}"; do
alocal_b[$ref]=1 alocal_b[$ref]=1
done done
# get origin branches # get "origin" branches
readarray -t orig_b < <(git for-each-ref --format='%(refname:short)' \ readarray -t orig_b < <(git for-each-ref --format='%(refname:short)' \
refs/remotes/"$origin"/) refs/remotes/"$origin"/)
declare -p remotes # find-out missing local branches and track them.
declare -p local_b orig_b # bugs:
# - We only check local branch existence, not tracking information correctness.
# find-out missing local branches and track them # - What about sub-branches ? Like remote/a and remote/a/b not being tracked ?
for remote_b in "${orig_b[@]}"; do for remote_b in "${orig_b[@]}"; do
short=${remote_b#"$origin"/}; short=${remote_b#"$origin"/};
#echo "$remote_b -> $short ${alocal_b[$short]}" # OR (??): short=${remote_b##*/}
if ! [[ -v alocal_b[$short] ]]; then if ! [[ -v alocal_b[$short] ]]; then
echo git branch --track "$short" "$remote_b" printf "local branch %s set to track %s.\n" "$short" "$remote_b"
# echo git branch: "$remote_b" git branch --track "$short" "$remote_b"
else
printf "skipping %s.\n" "$remote_b"
fi fi
done done
echo git pull --all git pull -a
#git remote | xargs -n 1 git fetch -a