Files
sync-git/sync.sh
2026-04-19 19:21:35 +02:00

78 lines
1.9 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# Geht durch alle Git-Repos unter ~/projects/ und führt
# pull, add, commit, push aus. Am Ende: Zusammenfassung.
set -u
PROJECTS_DIR="$HOME/projects"
ok=()
no_changes=()
pull_failed=()
push_failed=()
no_upstream=()
not_a_repo=()
for dir in "$PROJECTS_DIR"/*/; do
[ -d "$dir" ] || continue
name="$(basename "$dir")"
if [ ! -d "$dir/.git" ]; then
not_a_repo+=("$name")
continue
fi
echo "=== $name ==="
cd "$dir" || continue
if ! git pull --ff-only; then
echo ">> Pull-Konflikt oder Fehler in '$name' übersprungen"
pull_failed+=("$name")
continue
fi
git add -A
if git diff --cached --quiet; then
committed=false
else
git commit -m "update"
committed=true
fi
if ! git rev-parse --abbrev-ref --symbolic-full-name '@{u}' >/dev/null 2>&1; then
echo ">> Kein Upstream gesetzt für '$name'"
no_upstream+=("$name")
continue
fi
if git push; then
if [ "$committed" = true ]; then
ok+=("$name")
else
no_changes+=("$name")
fi
else
echo ">> Push fehlgeschlagen für '$name'"
push_failed+=("$name")
fi
done
echo
echo "=========================================="
echo " Zusammenfassung"
echo "=========================================="
print_group() {
local label="$1"; shift
if [ "$#" -gt 0 ]; then
echo "$label ($#):"
printf ' - %s\n' "$@"
fi
}
print_group "Erfolgreich gepusht" "${ok[@]:+${ok[@]}}"
print_group "Keine Änderungen" "${no_changes[@]:+${no_changes[@]}}"
print_group "Pull-Konflikt/Fehler" "${pull_failed[@]:+${pull_failed[@]}}"
print_group "Push fehlgeschlagen" "${push_failed[@]:+${push_failed[@]}}"
print_group "Kein Upstream" "${no_upstream[@]:+${no_upstream[@]}}"
print_group "Kein Git-Repo" "${not_a_repo[@]:+${not_a_repo[@]}}"