diff options
author | heqnx <root@heqnx.com> | 2025-05-27 22:22:05 +0300 |
---|---|---|
committer | heqnx <root@heqnx.com> | 2025-05-27 22:22:05 +0300 |
commit | 3e6a96781e7acddf475b751f88efbc210fa66be6 (patch) | |
tree | 9a382af286e3ba39c0803108b2ba74f8c17fd3d5 | |
parent | f1a0d2eb829cc0cb9beaf08371605d68734f2a35 (diff) | |
download | cgit-3e6a96781e7acddf475b751f88efbc210fa66be6.tar.gz cgit-3e6a96781e7acddf475b751f88efbc210fa66be6.zip |
-rw-r--r-- | cgit-backup.sh | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/cgit-backup.sh b/cgit-backup.sh index 73eab0c..066788a 100644 --- a/cgit-backup.sh +++ b/cgit-backup.sh @@ -21,12 +21,14 @@ # variables: # GIT_STRATEGY: clone +#!/bin/bash cgit="https://cgit.heqnx.com" mkdir -p repos -repos=$(curl -sSL "${cgit}" | grep -o "<a href='/[^']*/'>" | sed -E "s/^<a href='([^']*)'>/\1/" | sort -u) +repos=$(curl -sSL "${cgit}" || { printf "%s\n" "[err] failed to fetch repository list"; exit 1; }) +repos=$(printf "%s\n" "$repos" | grep -o "<a href='/[^']*/'>" | sed -E "s/^<a href='([^']*)'>/\1/" | sort -u) while IFS= read -r repo_path; do repo_name=$(basename "${repo_path}") @@ -40,17 +42,33 @@ while IFS= read -r repo_path; do printf "%s\n" "[inf] restoring .git from .git.bak for ${repo_name}" mv "${repo_dir}/.git.bak" "${repo_dir}/.git" fi + printf "%s\n" "[inf] fetching updates for ${repo_name}" + default_branch=$(git -C "${repo_dir}" remote show origin | grep "HEAD branch" | awk '{print $NF}' || true) + if [ -z "$default_branch" ] || [ "$default_branch" = "(unknown)" ]; then + printf "%s\n" "[wrn] could not determine default branch for ${repo_name}, trying 'main' or 'master'" + default_branch=$(git -C "${repo_dir}" ls-remote --heads origin | grep -E 'refs/heads/(main|master)$' | head -1 | awk -F'/' '{print $NF}' || true) + default_branch=${default_branch:-main} + fi + printf "%s\n" "[inf] using default branch: ${default_branch}" git -C "${repo_dir}" fetch origin - git -C "${repo_dir}" reset --hard origin/HEAD + git -C "${repo_dir}" reset --hard "origin/${default_branch}" else printf "%s\n" "[inf] cloning ${repo_name}" git -C repos clone "${repo_url}" + default_branch=$(git -C "${repo_dir}" remote show origin | grep "HEAD branch" | awk '{print $NF}' || true) + if [ -z "$default_branch" ] || [ "$default_branch" = "(unknown)" ]; then + printf "%s\n" "[wrn] could not determine default branch for ${repo_name}, trying 'main' or 'master'" + default_branch=$(git -C "${repo_dir}" ls-remote --heads origin | grep -E 'refs/heads/(main|master)$' | head -1 | awk -F'/' '{print $NF}' || true) + default_branch=${default_branch:-main} + fi + printf "%s\n" "[inf] using default branch: ${default_branch}" + git -C "${repo_dir}" checkout "${default_branch}" fi if test -d "${repo_dir}/.git"; then printf "%s\n" "[inf] moving .git to .git.bak for ${repo_name}" mv "${repo_dir}/.git" "${repo_dir}/.git.bak" fi - done <<< "${repos}" + |