summaryrefslogtreecommitdiff
path: root/tools/mirror/submodule-scanner.sh
blob: 11b97f17a955be22be10fa753e82050998414c09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/bash
set -euo pipefail

cd "$(dirname "$0")"

OUTPUT_DIR="$PWD"
OUTPUT_FILE="${OUTPUT_DIR}/submodule-list.txt"
TEMP_FILE="${OUTPUT_DIR}/temp-list.txt"

mkdir -p "${OUTPUT_DIR}"

> "${TEMP_FILE}"

TEMP_DIR=$(mktemp -d)
trap 'rm -rf "${TEMP_DIR}"' EXIT

process_current_submodules() {
    local repo_path="$1"
    local is_main_repo="$2"
    local parent_path="${3:-}"
    local original_dir="$PWD"
    cd "${repo_path}"
    
    echo "DEBUG: Processing submodules in: $(pwd)"
    
    if [[ -f ".gitmodules" ]]; then
        echo "DEBUG: Found .gitmodules file"
        git config --file .gitmodules --get-regexp '^submodule\.[^\.]+\.url' | while read -r key url; do
            name=${key#submodule.}
            name=${name%.url}
            
            path=$(git config --file "${PWD}/.gitmodules" --get "submodule.${name}.path")
            
            if [[ -z "$path" ]]; then
                echo "Warning: Empty path for submodule ${name}, skipping..."
                continue
            fi
            
            full_path="${parent_path:+$parent_path/}${path}"
            
            url=$(echo "$url" | tr -d '"' | tr -d "'")
            
            echo "DEBUG: Found submodule: name=${name}, path=${full_path}, url=${url}"
            
            if [[ -d "${path}" ]]; then
                hash=$(git submodule status "${path}" 2>/dev/null | awk '{print $1}' | tr -d '+' | tr -d '-' || echo "")
                if [[ "${hash}" == "b7a695cf4b41645a5f5c5a5c1f0d565b283a3585" ]]; then
                    echo "Skipping known problematic commit: ${commit}"
                    return 0
                fi
                if [[ -n "${hash}" ]]; then
                    echo "${url} ${hash}" >> "${TEMP_FILE}"
                    
                    echo "DEBUG: Initializing submodule: ${path}"
                    if git submodule update --force --init "${path}" 2>/dev/null; then
                        if [[ -f "${path}/.gitmodules" ]]; then
                            echo "DEBUG: Processing nested submodules in ${full_path}"
                            process_current_submodules "${PWD}/${path}" "false" "${full_path}"
                        fi
                    else
                        echo "Warning: Could not update submodule ${full_path} at commit ${hash}, skipping..."
                        continue
                    fi
                else
                    echo "Warning: Could not get hash for submodule ${full_path}, skipping..."
                    exit 1
                fi
            fi
        done
    fi
    cd "${original_dir}"
}

process_main_repo() {
    local repo_path="$1"
    cd "${repo_path}"

    echo "Getting commit history for main repository..."
    local COMMITS=$(git log --format="%H" --reverse)

    for commit in $COMMITS; do
        echo "Processing commit: ${commit}"
        
        git checkout --force "${commit}"
        git reset --hard
        
        process_current_submodules "${repo_path}" "true"
    done
}

echo "Cloning repository to temporary directory..."
git clone --no-checkout https://github.com/MrCyjaneK/monero_c --branch develop "${TEMP_DIR}/main"

process_main_repo "${TEMP_DIR}/main"

cat "${OUTPUT_FILE}" >> "${TEMP_FILE}"
sort -u "${TEMP_FILE}" > "${OUTPUT_FILE}"

rm "${TEMP_FILE}"

echo "Submodule information has been written to ${OUTPUT_FILE}"