Update sync-repos script
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -20,60 +20,91 @@ RED='\033[0;31m'
|
|||||||
DIM='\033[2m'
|
DIM='\033[2m'
|
||||||
RESET='\033[0m'
|
RESET='\033[0m'
|
||||||
|
|
||||||
|
# Collect all repos upfront (sorted for deterministic output order)
|
||||||
|
repo_dirs=()
|
||||||
|
while IFS= read -r gitdir; do
|
||||||
|
repo_dirs+=("$(dirname "$gitdir")")
|
||||||
|
done < <(find "$BASE_DIR" -maxdepth 4 -name .git -type d 2>/dev/null | sort)
|
||||||
|
|
||||||
|
# Temp dir for capturing per-repo output in parallel
|
||||||
|
tmpdir="$(mktemp -d)"
|
||||||
|
trap 'rm -rf "$tmpdir"' EXIT
|
||||||
|
|
||||||
|
# Spawn all git operations in parallel
|
||||||
|
for i in "${!repo_dirs[@]}"; do
|
||||||
|
repo_dir="${repo_dirs[$i]}"
|
||||||
|
repo_name="${repo_dir#"${BASE_DIR}/"}"
|
||||||
|
outfile="$tmpdir/$i"
|
||||||
|
|
||||||
|
(
|
||||||
|
# Skip if no remote configured
|
||||||
|
if ! git -C "$repo_dir" remote get-url origin &>/dev/null; then
|
||||||
|
printf "${DIM} skip %-40s no remote${RESET}\n" "$repo_name" >"$outfile.out"
|
||||||
|
echo "skip" >"$outfile.status"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check for uncommitted changes
|
||||||
|
if ! git -C "$repo_dir" diff --quiet 2>/dev/null || ! git -C "$repo_dir" diff --cached --quiet 2>/dev/null; then
|
||||||
|
has_changes=true
|
||||||
|
else
|
||||||
|
has_changes=false
|
||||||
|
fi
|
||||||
|
|
||||||
|
branch="$(git -C "$repo_dir" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")"
|
||||||
|
|
||||||
|
if $DRYRUN; then
|
||||||
|
if $has_changes; then
|
||||||
|
printf "${YELLOW} dirty %-40s %s (has uncommitted changes)${RESET}\n" "$repo_name" "$branch" >"$outfile.out"
|
||||||
|
echo "dirty" >"$outfile.status"
|
||||||
|
else
|
||||||
|
printf "${GREEN} pull %-40s %s${RESET}\n" "$repo_name" "$branch" >"$outfile.out"
|
||||||
|
echo "pull" >"$outfile.status"
|
||||||
|
fi
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Pull with --ff-only to avoid creating merge commits
|
||||||
|
output="$(git -C "$repo_dir" pull --ff-only 2>&1)" && rc=0 || rc=$?
|
||||||
|
|
||||||
|
if [[ $rc -eq 0 ]]; then
|
||||||
|
if echo "$output" | grep -q "Already up to date"; then
|
||||||
|
printf "${DIM} ok %-40s %s (up to date)${RESET}\n" "$repo_name" "$branch" >"$outfile.out"
|
||||||
|
else
|
||||||
|
printf "${GREEN} pull %-40s %s${RESET}\n" "$repo_name" "$branch" >"$outfile.out"
|
||||||
|
fi
|
||||||
|
echo "pull" >"$outfile.status"
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf "${RED} FAIL %-40s %s${RESET}\n" "$repo_name" "$branch"
|
||||||
|
printf "${DIM} %s${RESET}\n" "$output"
|
||||||
|
} >"$outfile.out"
|
||||||
|
echo "fail" >"$outfile.status"
|
||||||
|
fi
|
||||||
|
) &
|
||||||
|
done
|
||||||
|
|
||||||
|
# Wait for all parallel jobs to finish
|
||||||
|
wait
|
||||||
|
|
||||||
|
# Print results in original sorted order and tally counts
|
||||||
pulled=0
|
pulled=0
|
||||||
skipped=0
|
skipped=0
|
||||||
failed=0
|
failed=0
|
||||||
dirty=0
|
dirty=0
|
||||||
|
|
||||||
# Find all git repos (look for .git dirs, max depth 3 to cover projects/ and octopus/)
|
for i in "${!repo_dirs[@]}"; do
|
||||||
while IFS= read -r gitdir; do
|
outfile="$tmpdir/$i"
|
||||||
repo_dir="$(dirname "$gitdir")"
|
[[ -f "$outfile.out" ]] && cat "$outfile.out"
|
||||||
repo_name="${repo_dir#"${BASE_DIR}/"}"
|
if [[ -f "$outfile.status" ]]; then
|
||||||
|
case "$(cat "$outfile.status")" in
|
||||||
# Skip if no remote configured
|
pull) pulled=$((pulled + 1)) ;;
|
||||||
if ! git -C "$repo_dir" remote get-url origin &>/dev/null; then
|
skip) skipped=$((skipped + 1)) ;;
|
||||||
printf "${DIM} skip %-40s no remote${RESET}\n" "$repo_name"
|
fail) failed=$((failed + 1)) ;;
|
||||||
skipped=$((skipped + 1))
|
dirty) dirty=$((dirty + 1)) ;;
|
||||||
continue
|
esac
|
||||||
fi
|
fi
|
||||||
|
done
|
||||||
# Check for uncommitted changes
|
|
||||||
if ! git -C "$repo_dir" diff --quiet 2>/dev/null || ! git -C "$repo_dir" diff --cached --quiet 2>/dev/null; then
|
|
||||||
has_changes=true
|
|
||||||
else
|
|
||||||
has_changes=false
|
|
||||||
fi
|
|
||||||
|
|
||||||
if $DRYRUN; then
|
|
||||||
branch="$(git -C "$repo_dir" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")"
|
|
||||||
if $has_changes; then
|
|
||||||
printf "${YELLOW} dirty %-40s %s (has uncommitted changes)${RESET}\n" "$repo_name" "$branch"
|
|
||||||
dirty=$((dirty + 1))
|
|
||||||
else
|
|
||||||
printf "${GREEN} pull %-40s %s${RESET}\n" "$repo_name" "$branch"
|
|
||||||
pulled=$((pulled + 1))
|
|
||||||
fi
|
|
||||||
continue
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Pull with --ff-only to avoid creating merge commits
|
|
||||||
branch="$(git -C "$repo_dir" rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")"
|
|
||||||
output="$(git -C "$repo_dir" pull --ff-only 2>&1)" && rc=0 || rc=$?
|
|
||||||
|
|
||||||
if [[ $rc -eq 0 ]]; then
|
|
||||||
if echo "$output" | grep -q "Already up to date"; then
|
|
||||||
printf "${DIM} ok %-40s %s (up to date)${RESET}\n" "$repo_name" "$branch"
|
|
||||||
else
|
|
||||||
printf "${GREEN} pull %-40s %s${RESET}\n" "$repo_name" "$branch"
|
|
||||||
fi
|
|
||||||
pulled=$((pulled + 1))
|
|
||||||
else
|
|
||||||
printf "${RED} FAIL %-40s %s${RESET}\n" "$repo_name" "$branch"
|
|
||||||
printf "${DIM} %s${RESET}\n" "$output"
|
|
||||||
failed=$((failed + 1))
|
|
||||||
fi
|
|
||||||
|
|
||||||
done < <(find "$BASE_DIR" -maxdepth 4 -name .git -type d 2>/dev/null | sort)
|
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
if $DRYRUN; then
|
if $DRYRUN; then
|
||||||
|
|||||||
Reference in New Issue
Block a user