]> infiniteadaptability.org Git - mirror-all/commitdiff
improvement: add config files to extract information from repository v1.1.0
authoralex <[email protected]>
Sat, 21 Dec 2024 00:46:18 +0000 (16:46 -0800)
committeralex <[email protected]>
Sat, 21 Dec 2024 00:46:18 +0000 (16:46 -0800)
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
test/clone_repos.tests.sh
test/extract_files.tests.sh [new file with mode: 0755]

index 2cda3803d8958fe67da8be9cad64c1ab320c521f..c6c975be452403cebc3c1964709b1d9c98e4ce26 100755 (executable)
@@ -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 "$@"
index 248b92fdf04b27a98739591a1f783548a4de488d..6d6cb0b97421606ce818e2270a617460abb893d8 100755 (executable)
@@ -17,8 +17,6 @@ EXPECTED="cloned 1 repos"
 RESULT=$(<result)
 assert "$EXPECTED" "$RESULT"
 
-assert "$(<repo3/description)" "repo3"
-
 assert "$(cd repo3 && git rev-parse HEAD)" "$(cd ../repos/repo1 && git rev-parse HEAD)"
 
 cd ..
diff --git a/test/extract_files.tests.sh b/test/extract_files.tests.sh
new file mode 100755 (executable)
index 0000000..3bc44a2
--- /dev/null
@@ -0,0 +1,56 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+# this command will fail if not executed in test/ directory
+source setup.sh
+
+setup_env
+
+TO_CLONE+=("../repos/repo3")
+LOG_LEVEL=info
+
+cd repos/repo1
+echo -e "description\ncloneurl\n" > .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=$(<result)
+assert "$EXPECTED" "$RESULT"
+
+assert "$(<repo3/description)" ""
+assert "$(<repo3/cloneurl)" ""
+
+cd ../repos/repo1
+
+DESCRIPTION="thisisadescription"
+CLONEURL="cloneualsief"
+
+echo "$DESCRIPTION" > 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=$(<result)
+assert "$EXPECTED" "$RESULT"
+
+assert "$(<repo3/description)" "$DESCRIPTION"
+assert "$(<repo3/cloneurl)" "$CLONEURL"
+
+cd ..
+
+test_succeeded