From eb9744a23c7d4a4216c9ecd35a6a9ce1e8f34811 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 20 Dec 2024 16:46:18 -0800 Subject: [PATCH] improvement: add config files to extract information from repository Add .mirror.conf file which contains '\n' delimited filenames with which to pull the contents from and add to the cloned repository. This is useful for extracting information from repositories for presentation with an external program (such as gitweb). --- mirror-all | 56 +++++++++++++++++++++++++++++-------- test/clone_repos.tests.sh | 2 -- test/extract_files.tests.sh | 56 +++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 14 deletions(-) create mode 100755 test/extract_files.tests.sh diff --git a/mirror-all b/mirror-all index 2cda380..c6c975b 100755 --- a/mirror-all +++ b/mirror-all @@ -15,6 +15,20 @@ TARGET= SERVER= TO_CLONE=() TO_FETCH=() +MIRROR_CONFIG_FILENAME=".mirror.conf" +BRANCH="master" + +clone_repos() { + local name + for repo in "${TO_CLONE[@]}"; do + name="$(basename "$repo")" + "$GIT" clone --mirror "$repo" "$name" > /dev/null 2>&1 + (cd "$name" && "$GIT" update-server-info) + extract_files "$name" + done + + log_info "cloned ${#TO_CLONE[@]} repos" +} detect_git() { log_info "detecting git" @@ -40,16 +54,46 @@ detect_ssh() { fi } +extract_files() { + local config_files + local contents + + readarray -t config_files < <(cd "$1" && get_file_contents "$MIRROR_CONFIG_FILENAME") + + for name in "${config_files[@]}"; do + if [[ -n "$name" ]]; then + contents="$(cd "$1" && get_file_contents "$name")" + (cd "$1" && echo "$contents" > "$name") + log_info "extracted $name from $1" + fi + done +} + fetch_repos() { local name for repo in "${TO_FETCH[@]}"; do name="$(basename "$repo")" (cd "$name" && "$GIT" fetch --prune --prune-tags && git update-server-info) > /dev/null 2>&1 + extract_files "$name" done log_info "fetched ${#TO_FETCH[@]} repos" } +get_file_contents() { + local contents + local content_hash + + content_hash="$("$GIT" ls-tree --object-only "$BRANCH" -- "$1")" + + if [[ -z "$content_hash" ]]; then + echo "" + else + contents="$("$GIT" cat-file blob "$content_hash")" + echo "$contents" + fi +} + find_local_directories() { local repos @@ -97,18 +141,6 @@ find_remote_directories() { done } -clone_repos() { - local name - for repo in "${TO_CLONE[@]}"; do - name="$(basename "$repo")" - "$GIT" clone --mirror "$repo" "$name" > /dev/null 2>&1 - echo "$name" > "$name/description" - (cd "$name" && "$GIT" update-server-info) - done - - log_info "cloned ${#TO_CLONE[@]} repos" -} - log() { if [[ "$LOG_LEVEL" != "error" ]]; then echo -e "$@" diff --git a/test/clone_repos.tests.sh b/test/clone_repos.tests.sh index 248b92f..6d6cb0b 100755 --- a/test/clone_repos.tests.sh +++ b/test/clone_repos.tests.sh @@ -17,8 +17,6 @@ EXPECTED="cloned 1 repos" RESULT=$( .mirror.conf +git add .mirror.conf > /dev/null 2>&1 +git commit -m ... > /dev/null 2>&1 +(cd ../repo3 && git fetch > /dev/null 2>&1) + +cd ../../mirrors +clone_repos > result 2>&1 +EXPECTED=$'extracted description from repo3\n' +EXPECTED+=$'extracted cloneurl from repo3\n' +EXPECTED+=$'cloned 1 repos' +RESULT=$( description +echo "$CLONEURL" > cloneurl +git add description cloneurl > /dev/null 2>&1 +git commit -m ... > /dev/null 2>&1 +(cd ../repo3 && git fetch > /dev/null 2>&1) + +cd ../../mirrors + +TO_FETCH+=("../repos/repo3") +fetch_repos > result 2>&1 +EXPECTED=$'extracted description from repo3\n' +EXPECTED+=$'extracted cloneurl from repo3\n' +EXPECTED+=$'fetched 1 repos' +RESULT=$(