Files
MovePatchesFromToMahle/sync-push.sh
2026-02-21 16:44:57 +01:00

122 lines
5.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# sync-push.sh — Export new commits as a git bundle to the shared folder.
#
# Usage: ./sync-push.sh [<share-dir>]
# Default share dir: $SYNC_SHARE or ~/fifiletrans
#
# On first run: creates a full bundle of all branches.
# On subsequent runs: creates an incremental bundle (only new commits).
# Checkpoint per branch is stored in git config: sync.exported.<branch>
set -euo pipefail
# ── Config ────────────────────────────────────────────────────────────────────
SHARE_DIR="${1:-${SYNC_SHARE:-$HOME/fifiletrans}}"
# ── Sanity checks ─────────────────────────────────────────────────────────────
if ! git rev-parse --git-dir &>/dev/null; then
echo "ERROR: Not inside a git repository." >&2
exit 1
fi
REPO_ROOT=$(git rev-parse --show-toplevel)
REPO_NAME=$(basename "$REPO_ROOT")
HOST=$(hostname -s)
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
BUNDLE_FILE="${REPO_NAME}-from-${HOST}-${TIMESTAMP}.bundle"
BUNDLE_PATH="${SHARE_DIR}/${BUNDLE_FILE}"
mkdir -p "$SHARE_DIR"
# ── Collect branches ───────────────────────────────────────────────────────────
BRANCHES=()
while IFS= read -r branch; do
BRANCHES+=("$branch")
done < <(git branch --format='%(refname:short)')
if [ ${#BRANCHES[@]} -eq 0 ]; then
echo "ERROR: No local branches found." >&2
exit 1
fi
# ── Check if there is anything new to export ───────────────────────────────────
has_new=false
for branch in "${BRANCHES[@]}"; do
checkpoint=$(git config "sync.exported.${branch}" 2>/dev/null || true)
branch_tip=$(git rev-parse "$branch")
if [ -z "$checkpoint" ] || ! git rev-parse --verify "$checkpoint" &>/dev/null || [ "$branch_tip" != "$checkpoint" ]; then
has_new=true
break
fi
done
if ! $has_new; then
echo "Nothing new to export — all branches match the last export checkpoint."
exit 0
fi
# ── Build rev-list args ────────────────────────────────────────────────────────
# Include all branches; exclude commits already exported (per branch checkpoint)
EXCLUSIONS=()
for branch in "${BRANCHES[@]}"; do
checkpoint=$(git config "sync.exported.${branch}" 2>/dev/null || true)
if [ -n "$checkpoint" ] && git rev-parse --verify "$checkpoint" &>/dev/null; then
EXCLUSIONS+=("^${checkpoint}")
fi
done
# ── Report ─────────────────────────────────────────────────────────────────────
echo "Repository : $REPO_NAME"
echo "Host : $HOST"
echo "Branches : ${BRANCHES[*]}"
if [ ${#EXCLUSIONS[@]} -eq 0 ]; then
echo "Mode : full (first export)"
NEW_COUNT=$(git log --oneline --all | wc -l)
else
echo "Mode : incremental"
NEW_COUNT=$(git log --oneline "${BRANCHES[@]}" "${EXCLUSIONS[@]}" | wc -l)
fi
echo "New commits: $NEW_COUNT"
# ── Show what will be exported ─────────────────────────────────────────────────
for branch in "${BRANCHES[@]}"; do
checkpoint=$(git config "sync.exported.${branch}" 2>/dev/null || true)
if [ -z "$checkpoint" ] || ! git rev-parse --verify "$checkpoint" &>/dev/null; then
n=$(git log --oneline "$branch" | wc -l)
else
n=$(git log --oneline "${checkpoint}..${branch}" | wc -l)
fi
if [ "$n" -gt 0 ]; then
echo " $branch: $n new commit(s)"
git log --oneline "${checkpoint:-}${checkpoint:+..}${branch}" | sed 's/^/ /'
fi
done
echo ""
# ── Create bundle ──────────────────────────────────────────────────────────────
git bundle create "$BUNDLE_PATH" "${BRANCHES[@]}" "${EXCLUSIONS[@]}"
# ── Update checkpoints ─────────────────────────────────────────────────────────
for branch in "${BRANCHES[@]}"; do
tip=$(git rev-parse "$branch")
git config "sync.exported.${branch}" "$tip"
done
# ── Done ───────────────────────────────────────────────────────────────────────
SIZE=$(du -sh "$BUNDLE_PATH" | cut -f1)
echo "Bundle created : $BUNDLE_PATH ($SIZE)"
# ── Optional Gitea auto-push ───────────────────────────────────────────────────
if [ "$(git config sync.gitea.autopush 2>/dev/null)" = "true" ]; then
GITEA_REMOTE=$(git config sync.gitea.remote 2>/dev/null || echo "gitea")
echo ""
echo "Auto-pushing to Gitea remote '$GITEA_REMOTE'..."
git push "$GITEA_REMOTE" --all --tags
echo "Gitea push done."
fi
echo ""
echo "Next step on the other machine:"
echo " ./sync-pull.sh $SHARE_DIR"