351 lines
7.7 KiB
Shell
Executable file
351 lines
7.7 KiB
Shell
Executable file
#!/usr/bin/env bash
|
|
|
|
set -Eeuo pipefail
|
|
|
|
prune=false
|
|
force_tags=false
|
|
quiet=false
|
|
|
|
repo=
|
|
remote=
|
|
lock=
|
|
lock_acquired=false
|
|
|
|
log() {
|
|
if "$quiet"; then
|
|
return 0
|
|
fi
|
|
|
|
printf '[%s] %s\n' \
|
|
"$(date '+%Y-%m-%d %H:%M:%S')" \
|
|
"$*" >&2
|
|
}
|
|
|
|
die() {
|
|
printf 'error: %s\n' "$*" >&2
|
|
exit 1
|
|
}
|
|
|
|
usage() {
|
|
printf 'Usage: %s [--prune] [--force-tags] [--quiet] REPOSITORY REMOTE\n' \
|
|
"$(basename "$0")" >&2
|
|
exit 2
|
|
}
|
|
|
|
help() {
|
|
cat <<EOF
|
|
Usage:
|
|
$(basename "$0") [OPTIONS] REPOSITORY REMOTE
|
|
|
|
Synchronize Git branch references and git-annex metadata from a configured
|
|
Git remote into an existing local bare git-annex repository.
|
|
|
|
This command does not transfer annexed file content. It fetches Git objects
|
|
and metadata only; it does not run git-annex get, copy, or move.
|
|
|
|
REPOSITORY must already exist, be a bare Git repository, and be initialized
|
|
with git-annex. REMOTE must be the name of a Git remote configured in that
|
|
repository.
|
|
|
|
The options must appear before REPOSITORY and REMOTE. Use -- to terminate
|
|
option processing.
|
|
|
|
Branch synchronization:
|
|
|
|
1. Remote branches are fetched into:
|
|
|
|
refs/remotes/REMOTE/BRANCH
|
|
|
|
2. Each fetched branch, except git-annex, is then written to:
|
|
|
|
refs/heads/BRANCH
|
|
|
|
Local branch references are updated to exactly match the fetched remote
|
|
branch tips. This includes non-fast-forward rewinds and force-pushes.
|
|
Existing local branch changes are not preserved.
|
|
|
|
The refs/heads/git-annex branch is handled separately by running:
|
|
|
|
git annex merge
|
|
|
|
It is not directly replaced with refs/remotes/REMOTE/git-annex. The exact
|
|
metadata incorporated by git annex merge is determined by git-annex and the
|
|
repository's configured remotes.
|
|
|
|
By default, local branches are retained when they disappear from REMOTE.
|
|
|
|
With --prune, this command deletes every local branch under refs/heads/
|
|
(except refs/heads/git-annex) whose corresponding branch is absent from
|
|
REMOTE. This includes branches not previously created or managed by this
|
|
command.
|
|
|
|
Tag synchronization:
|
|
|
|
Tags are not fetched by default.
|
|
|
|
With --force-tags, tags are force-fetched from REMOTE into refs/tags/*.
|
|
A same-named local tag is replaced when its object ID differs from the
|
|
remote tag. Local-only tags are retained because tag pruning is not used.
|
|
|
|
Tags share one local namespace. If multiple remotes contain the same tag
|
|
name, the most recently synchronized remote can replace the tag.
|
|
|
|
The operation is protected by a repository-side lock directory. A second
|
|
invocation using the same lock path fails. The lock is removed on normal
|
|
exit and on handled INT or TERM signals. A stale lock may require manual
|
|
removal.
|
|
|
|
The operation is not atomic. If a fetch, git-annex merge, branch update, or
|
|
prune operation fails, some references may already have been changed.
|
|
|
|
Options:
|
|
|
|
--prune, -p
|
|
Delete all local branches absent from REMOTE, except the local
|
|
git-annex branch. This can delete unrelated local branches.
|
|
|
|
--force-tags
|
|
Force-update same-named local tags from REMOTE. Local-only tags are
|
|
retained. This option affects tags only; branch updates are forced
|
|
regardless.
|
|
|
|
--quiet, -q
|
|
Suppress informational logging and normal output from Git and
|
|
git-annex. Errors and warnings may still be printed.
|
|
|
|
--help, -h
|
|
Display this help text and exit successfully.
|
|
|
|
Arguments:
|
|
|
|
REPOSITORY
|
|
Path to the existing local bare git-annex repository.
|
|
|
|
REMOTE
|
|
Name of a configured Git remote in REPOSITORY.
|
|
|
|
Example:
|
|
|
|
Synchronize branches and git-annex metadata:
|
|
|
|
$(basename "$0") /srv/repos/data.git origin
|
|
|
|
Exit status:
|
|
|
|
0
|
|
Synchronization completed successfully.
|
|
|
|
1
|
|
Validation failed, the repository was locked, or a Git/git-annex
|
|
operation failed.
|
|
|
|
2
|
|
Invalid command-line syntax or an incorrect number of arguments.
|
|
|
|
EOF
|
|
exit 0
|
|
}
|
|
|
|
cleanup() {
|
|
local status=$?
|
|
|
|
if "$lock_acquired"; then
|
|
rm -rf -- "$lock"
|
|
fi
|
|
|
|
exit "$status"
|
|
}
|
|
|
|
on_signal() {
|
|
local signal_status=$1
|
|
exit "$signal_status"
|
|
}
|
|
|
|
# Parse options.
|
|
while (($# > 0)); do
|
|
case "$1" in
|
|
--prune|-p)
|
|
prune=true
|
|
shift
|
|
;;
|
|
|
|
--force-tags)
|
|
force_tags=true
|
|
shift
|
|
;;
|
|
|
|
--quiet|-q)
|
|
quiet=true
|
|
shift
|
|
;;
|
|
|
|
--help|-h)
|
|
help
|
|
;;
|
|
|
|
--)
|
|
shift
|
|
break
|
|
;;
|
|
|
|
-*)
|
|
usage
|
|
;;
|
|
|
|
*)
|
|
break
|
|
;;
|
|
esac
|
|
done
|
|
|
|
(($# == 2)) || usage
|
|
|
|
repo=$1
|
|
remote=$2
|
|
|
|
umask 077
|
|
|
|
[[ -d "$repo" ]] ||
|
|
die "repository does not exist: $repo"
|
|
|
|
git -C "$repo" rev-parse --git-dir >/dev/null 2>&1 ||
|
|
die "not a Git repository: $repo"
|
|
|
|
bare=$(git -C "$repo" rev-parse --is-bare-repository 2>/dev/null) ||
|
|
die "cannot inspect repository: $repo"
|
|
|
|
[[ "$bare" == true ]] ||
|
|
die "repository is not bare: $repo"
|
|
|
|
annex_uuid=$(git -C "$repo" config --local --get annex.uuid || true)
|
|
|
|
[[ -n "$annex_uuid" ]] ||
|
|
die "repository does not appear to be git-annex initialized: $repo"
|
|
|
|
git -C "$repo" remote get-url "$remote" >/dev/null 2>&1 ||
|
|
die "remote is not configured in repository: $remote"
|
|
|
|
# Acquire a lock using mkdir, which is atomic on normal local filesystems.
|
|
lock="${repo}.mirror.lock"
|
|
|
|
if ! mkdir -- "$lock" 2>/dev/null; then
|
|
die "another mirror operation appears to be running: $lock"
|
|
fi
|
|
|
|
lock_acquired=true
|
|
|
|
trap cleanup EXIT
|
|
trap 'on_signal 130' INT
|
|
trap 'on_signal 143' TERM
|
|
|
|
printf '%s\n' "$$" >"$lock/pid"
|
|
printf '%s\n' "$(date '+%Y-%m-%d %H:%M:%S')" >"$lock/start-time"
|
|
printf '%s\n' "$remote" >"$lock/remote"
|
|
|
|
log "fetching branches from '$remote'"
|
|
|
|
fetch_args=(
|
|
fetch
|
|
--no-tags
|
|
)
|
|
|
|
if "$prune"; then
|
|
fetch_args+=(--prune)
|
|
fi
|
|
|
|
if "$quiet"; then
|
|
fetch_args+=(--quiet)
|
|
fi
|
|
|
|
fetch_args+=(
|
|
"$remote"
|
|
"+refs/heads/*:refs/remotes/$remote/*"
|
|
)
|
|
|
|
git -C "$repo" "${fetch_args[@]}"
|
|
|
|
if "$force_tags"; then
|
|
log "force-updating tags from '$remote'"
|
|
|
|
tag_fetch_args=(
|
|
fetch
|
|
--no-tags
|
|
)
|
|
|
|
if "$quiet"; then
|
|
tag_fetch_args+=(--quiet)
|
|
fi
|
|
|
|
tag_fetch_args+=(
|
|
"$remote"
|
|
"+refs/tags/*:refs/tags/*"
|
|
)
|
|
|
|
# Do not pass --prune here. This preserves local-only tags.
|
|
git -C "$repo" "${tag_fetch_args[@]}"
|
|
fi
|
|
|
|
log "merging git-annex metadata"
|
|
|
|
annex_merge_args=(
|
|
annex
|
|
merge
|
|
)
|
|
|
|
if "$quiet"; then
|
|
annex_merge_args+=(--quiet)
|
|
fi
|
|
|
|
# This updates the local aggregate refs/heads/git-annex branch without
|
|
# replacing it with refs/remotes/$remote/git-annex.
|
|
git -C "$repo" "${annex_merge_args[@]}"
|
|
|
|
log "synchronizing ordinary branches"
|
|
|
|
remote_refs=$(
|
|
git -C "$repo" for-each-ref \
|
|
--format='%(refname) %(objectname)' \
|
|
"refs/remotes/$remote/"
|
|
)
|
|
|
|
while IFS=' ' read -r source_ref object_id; do
|
|
[[ -n "$source_ref" ]] || continue
|
|
|
|
suffix=${source_ref#refs/remotes/$remote/}
|
|
|
|
# The git-annex branch is merged separately.
|
|
[[ "$suffix" == git-annex ]] && continue
|
|
|
|
destination_ref="refs/heads/$suffix"
|
|
|
|
git -C "$repo" update-ref \
|
|
"$destination_ref" \
|
|
"$object_id"
|
|
done <<<"$remote_refs"
|
|
|
|
if "$prune"; then
|
|
log "pruning deleted branches"
|
|
|
|
local_refs=$(
|
|
git -C "$repo" for-each-ref \
|
|
--format='%(refname)' \
|
|
refs/heads/
|
|
)
|
|
|
|
while IFS= read -r destination_ref; do
|
|
[[ -n "$destination_ref" ]] || continue
|
|
|
|
# The local git-annex branch is an aggregate and is never pruned.
|
|
[[ "$destination_ref" == refs/heads/git-annex ]] && continue
|
|
|
|
suffix=${destination_ref#refs/heads/}
|
|
source_ref="refs/remotes/$remote/$suffix"
|
|
|
|
if ! git -C "$repo" show-ref --verify --quiet "$source_ref"; then
|
|
git -C "$repo" update-ref -d "$destination_ref"
|
|
fi
|
|
done <<<"$local_refs"
|
|
fi
|
|
|
|
log "mirror completed"
|
|
|