diff options
| author | Jan Sydänviita <jan@sydanviita.fi> | 2026-08-21 14:35:38 +0300 |
|---|---|---|
| committer | Jan Sydänviita <jan@sydanviita.fi> | 2026-08-21 14:35:38 +0300 |
| commit | 6f1ab23e5e4210e8167011e0fea1e1504fdd566e (patch) | |
| tree | 2f5b0e57a6258fef590bae23db830ecaba5d85a1 | |
| parent | e338ac032c925506caaec79e2f1725a38e4c7b3b (diff) | |
Add mirror-from
| -rw-r--r-- | roles/jails/22_forge/files/git-mirrors.sh | 30 | ||||
| -rw-r--r-- | roles/jails/22_forge/files/git-shell-commands/help | 1 | ||||
| -rw-r--r-- | roles/jails/22_forge/files/git-shell-commands/mirror-from | 44 | ||||
| -rw-r--r-- | roles/jails/22_forge/files/git-shell-commands/mirror-to | 5 | ||||
| -rw-r--r-- | roles/jails/22_forge/tasks/main.yml | 1 |
5 files changed, 73 insertions, 8 deletions
diff --git a/roles/jails/22_forge/files/git-mirrors.sh b/roles/jails/22_forge/files/git-mirrors.sh index 6de665a..df78129 100644 --- a/roles/jails/22_forge/files/git-mirrors.sh +++ b/roles/jails/22_forge/files/git-mirrors.sh @@ -1,5 +1,6 @@ #!/bin/sh -# Run hourly via cron. Mirrors all repos that have a 'mirror' remote configured. +# Run hourly via cron. +# Handles mirror-to (push) and mirror-from (fetch) remotes for all repos. REPOS_DIR="/var/db/repos" LOG="/var/log/git-mirrors.log" @@ -9,17 +10,30 @@ export GIT_CONFIG_GLOBAL="$CONFIG_DIR/.gitconfig" echo "$(date) Starting mirror run" >> "$LOG" -for repo in "$REPOS_DIR"/*.git; do +for repo in "$REPOS_DIR"/*.git "$REPOS_DIR"/.*.git; do [ -d "$repo" ] || continue name="$(basename "$repo" .git)" - remote=$(git -C "$repo" remote get-url mirror-to 2>/dev/null) || continue + # mirror-to: push all refs to remote + if git -C "$repo" remote get-url mirror-to >/dev/null 2>&1; then + remote=$(git -C "$repo" remote get-url mirror-to) + echo "$(date) [push] $name -> $remote" >> "$LOG" + if git -C "$repo" push --mirror mirror-to >> "$LOG" 2>&1; then + echo "$(date) [push] OK $name" >> "$LOG" + else + echo "$(date) [push] FAILED $name" >> "$LOG" + fi + fi - echo "$(date) Mirroring $name -> $remote" >> "$LOG" - if git -C "$repo" push --mirror mirror-to >> "$LOG" 2>&1; then - echo "$(date) OK $name" >> "$LOG" - else - echo "$(date) FAILED $name" >> "$LOG" + # mirror-from: fetch all refs from remote + if git -C "$repo" remote get-url mirror-from >/dev/null 2>&1; then + remote=$(git -C "$repo" remote get-url mirror-from) + echo "$(date) [fetch] $name <- $remote" >> "$LOG" + if git -C "$repo" fetch --prune mirror-from '+refs/*:refs/*' >> "$LOG" 2>&1; then + echo "$(date) [fetch] OK $name" >> "$LOG" + else + echo "$(date) [fetch] FAILED $name" >> "$LOG" + fi fi done diff --git a/roles/jails/22_forge/files/git-shell-commands/help b/roles/jails/22_forge/files/git-shell-commands/help index e8f2631..acd24a9 100644 --- a/roles/jails/22_forge/files/git-shell-commands/help +++ b/roles/jails/22_forge/files/git-shell-commands/help @@ -8,5 +8,6 @@ Available commands: rename [OLD_NAME] [NEW_NAME] rename a repository delete [REPO_NAME] delete a repository mirror-to [REPO_NAME] [GIT_URL] set up hourly mirror to a remote git URL + mirror-from [REPO_NAME] [GIT_URL] set up hourly fetch from a remote git URL EOF diff --git a/roles/jails/22_forge/files/git-shell-commands/mirror-from b/roles/jails/22_forge/files/git-shell-commands/mirror-from new file mode 100644 index 0000000..2666cac --- /dev/null +++ b/roles/jails/22_forge/files/git-shell-commands/mirror-from @@ -0,0 +1,44 @@ +#!/bin/sh +# mirror-from [REPO_NAME] [GIT_URL] +# Configures a repo to pull updates from a remote git URL hourly. +# Pass empty URL to remove the mirror source. +# Example: mirror-from myrepo https://github.com/user/myrepo.git +if [ -z "$1" ]; then + echo "Usage: mirror-from [REPO_NAME] [GIT_URL]" + echo " mirror-from [REPO_NAME] - remove mirror source" + exit 1 +fi +if [ ! -d "$1.git" ]; then + echo "Repository $1 does not exist" + exit 1 +fi + +REPO="$1" +REMOTE="$2" +REPO_PATH="$HOME/$REPO.git" + +cd "$REPO_PATH" || exit 1 + +if [ -z "$REMOTE" ]; then + if git remote get-url mirror-from >/dev/null 2>&1; then + git remote remove mirror-from + echo "Removed mirror source for $REPO" + else + echo "No mirror source configured for $REPO" + fi + exit 0 +fi + +if git remote get-url mirror-to >/dev/null 2>&1; then + echo "Error: $REPO already has a mirror-to configured. Remove it first with: mirror-to $REPO" + exit 1 +fi + +if git remote get-url mirror-from >/dev/null 2>&1; then + git remote set-url mirror-from "$REMOTE" +else + git remote add mirror-from "$REMOTE" +fi + +echo "Configured $REPO to mirror from $REMOTE" +echo "Mirrors run hourly. Logs: /var/log/git-mirrors.log" diff --git a/roles/jails/22_forge/files/git-shell-commands/mirror-to b/roles/jails/22_forge/files/git-shell-commands/mirror-to index d161283..6a0c20e 100644 --- a/roles/jails/22_forge/files/git-shell-commands/mirror-to +++ b/roles/jails/22_forge/files/git-shell-commands/mirror-to @@ -29,6 +29,11 @@ if [ -z "$REMOTE" ]; then exit 0 fi +if git remote get-url mirror-from >/dev/null 2>&1; then + echo "Error: $REPO already has a mirror-from configured. Remove it first with: mirror-from $REPO" + exit 1 +fi + if git remote get-url mirror-to >/dev/null 2>&1; then git remote set-url mirror-to "$REMOTE" else diff --git a/roles/jails/22_forge/tasks/main.yml b/roles/jails/22_forge/tasks/main.yml index 03108df..e837d72 100644 --- a/roles/jails/22_forge/tasks/main.yml +++ b/roles/jails/22_forge/tasks/main.yml @@ -27,6 +27,7 @@ - rename - describe - mirror-to + - mirror-from # --- Remove legacy git user --- |
