summaryrefslogtreecommitdiff
path: root/apply_patches.sh
blob: 81ef25cbcddd5431d72fd37337a7f7facd1d5676 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/bin/bash
set -euo pipefail

cd "$(realpath $(dirname $0))"

WORKDIR="$PWD"

MIRROR_URL="https://static.mrcyjanek.net/download_mirror/git/mirror.git"
SUPPORTED_REPOS=("monero" "wownero" "zano")
ROOT_COMMIT=$(git rev-parse HEAD)

# Function to try getting repo from bundle
try_bundle() {
    local repo="$1"
    local target_commit="$2"
    local original_url=$(git config -f .gitmodules submodule.$repo.url)
    
    # Convert URL to bundle name using same logic as mirror.sh
    local bundle_name=$(echo -n -e "$original_url" | sed 's|https://||' | sed 's|\.git$||' | tr '/' '-' | tr '.' '-' | tr '[:upper:]' '[:lower:]')-${target_commit:0:8}.bundle
    
    echo "Attempting to clone from bundle: $bundle_name"
    mkdir -p $WORKDIR/tools/mirror/mirrors/bundles
    if [ -f "$WORKDIR/tools/mirror/mirrors/bundles/$bundle_name" ]; then
        echo "Bundle already exists, using cached version"
        if git clone "$WORKDIR/tools/mirror/mirrors/bundles/$bundle_name" "$repo"; then
            return 0
        fi
    else
        if curl -f -s -o "$WORKDIR/tools/mirror/mirrors/bundles/$bundle_name" "https://static.mrcyjanek.net/download_mirror/git/$bundle_name"; then
            if git clone "$WORKDIR/tools/mirror/mirrors/bundles/$bundle_name" "$repo"; then
                return 0
            fi
        fi
    fi
    return 1
}

# Function to update a single submodule
update_submodule() {
    local path="$1"
    local parent_path="$2"
    local full_path="${parent_path:+$parent_path/}$path"
    
    echo "Updating submodule: $full_path"
    
    # Get original URL from .gitmodules
    original_url=$(git config -f .gitmodules submodule.$path.url)
    
    # Get target commit - first try from the index (after patches), then fallback to config
    target_commit=$(git ls-tree HEAD "$path" | awk '{print $3}' || \
                   git config -f .git/modules/$full_path/config core.commitish 2>/dev/null || \
                   git submodule status "$path" | awk '{print $1}' | sed 's/^[+-]//')
    
    if [[ -z "$original_url" ]]; then
        echo "Warning: No URL found for submodule $path in .gitmodules, skipping"
        return 0
    fi
    
    echo "Original URL: $original_url"
    echo "Target commit: $target_commit"
    
    # Store the original directory
    local original_dir="$PWD"
    
    # Try original URL first
    if ! git submodule update --init --force "$path"; then
        echo "Failed with original URL, trying bundle for $path..."
        rm -rf "$path"
        # Try bundle
        if ! try_bundle "$path" "$target_commit"; then
            echo "Bundle attempt failed for $path"
            # Try original URL one last time
            if ! git clone "$original_url" "$path"; then
                echo "Both bundle and original source failed for $path"
                return 1
            fi
        fi
        
        # Checkout the correct commit
        cd "$path"
        if ! git checkout "$target_commit"; then
            echo "Failed to checkout target commit $target_commit"
            cd "$original_dir"
            return 1
        fi
        
        # Update any nested submodules
        if [[ -f ".gitmodules" ]]; then
            echo "Updating nested submodules in $full_path"
            while IFS= read -r submodule; do
                local subpath
                subpath=$(echo "$submodule" | awk '{print $2}')
                update_submodule "$subpath" "$full_path"
            done < <(git config -f .gitmodules --get-regexp '^submodule\..*\.path$')
        fi
        cd "$original_dir"
    fi
}

# Function to check if repatching is needed
needs_repatching() {
    local repo="$1"
    local current_root_commit="$2"
    
    if [[ ! -f "$repo/.patch-applied" ]]; then
        return 0 # True, needs patching
    fi
    
    # Get stored commit hash from .patch-applied
    local stored_commit
    stored_commit=$(cat "$repo/.patch-applied")
    
    if [[ "$stored_commit" != "$current_root_commit" ]]; then
        echo "Root repository changed ($stored_commit -> $current_root_commit)"
        echo "Removing releases/$repo directory..."
        rm -rf "releases/$repo"
        return 0 # True, needs patching
    fi
    
    return 1 # False, no need to patch
}

# Function to process a single repository
process_repo() {
    local repo="$1"
    echo "Processing $repo..."
    
    # Get target commit before attempting any clones
    local target_commit
    target_commit=$(git config -f .git/modules/$repo/config core.commitish || git submodule status "$repo" | awk '{print $1}' | sed 's/^[+-]//')
    
    # Check if repatching is needed
    if ! needs_repatching "$repo" "$ROOT_COMMIT"; then
        echo "No repatching needed for $repo (already at current root commit)"
        return 0
    fi
    BASE_PATH="$PWD"
    # If directory doesn't exist or needs update, try cloning
    if [[ ! -d "$repo" ]]; then
        echo "Fetching $repo..."
        # Try original URL first
        if ! git submodule update --init --recursive --force "$repo"; then
            echo "Failed with original URL, trying bundle..."
            rm -rf "$repo"
            # Try bundle
            if ! try_bundle "$repo" "$target_commit"; then
                echo "Bundle attempt failed for $repo"
                # Try original URL one last time with direct clone
                original_url=$(git config -f .gitmodules submodule.$repo.url)
                if ! git clone --recursive "$original_url" "$repo"; then
                    echo "Both bundle and original source failed for $repo"
                    return 1
                fi
            fi
            
            # Checkout the correct commit
            cd "$repo"
            if ! git checkout "$target_commit"; then
                echo "Failed to checkout target commit $target_commit"
                cd - >/dev/null
                return 1
            fi
            
            # Update any nested submodules
            if [[ -f ".gitmodules" ]]; then
                echo "Updating nested submodules in $repo"
                while IFS= read -r submodule; do
                    local subpath
                    subpath=$(echo "$submodule" | awk '{print $2}')
                    update_submodule "$subpath" ""  # Pass empty string as initial parent path
                done < <(git config -f .gitmodules --get-regexp '^submodule\..*\.path$')
            fi
            cd - >/dev/null
        fi
    fi
    cd "$BASE_PATH"
    if [[ ! -d "$repo" ]]; then
        echo "Failed to fetch $repo"
        return 1
    fi

    # Verify repository is properly initialized
    pushd "$repo"
    if ! git rev-parse --git-dir > /dev/null 2>&1; then
        echo "Error: $repo is not a valid git repository"
        popd
        return 1
    fi

    # Verify we have a clean working directory
    if ! git diff --quiet HEAD; then
        echo "Error: $repo has uncommitted changes"
        popd
        return 1
    fi

    # Apply patches
    echo "Applying patches..."
    if ! git am -3 --whitespace=fix --reject ../patches/$repo/*.patch; then
        echo "Error: Failed to apply patches to $repo"
        git am --abort
        popd
        return 1
    fi

    # Create .patch-applied marker with root commit hash
    echo "$ROOT_COMMIT" > .patch-applied
    git add .patch-applied
    git commit -m "add .patch-applied with root commit: ${ROOT_COMMIT}"

    # Check if submodules changed after patching
    if [[ -f ".gitmodules" ]]; then
        echo "Checking for submodule changes after patching..."
        while IFS= read -r submodule; do
            local subpath
            subpath=$(echo "$submodule" | awk '{print $2}')
            update_submodule "$subpath" ""
        done < <(git config -f .gitmodules --get-regexp '^submodule\..*\.path$')
    fi

    popd
    echo "Finished processing $repo"
}

# Function to fetch wownero-seed submodule
fetch_wownero_seed() {
    local repo="wownero_libwallet2_api_c/wownero-seed"
    echo "Fetching $repo..."
    
    # Create directory structure
    mkdir -p "$(dirname "$repo")"
    
    # Skip if already exists
    if [[ -d "$repo" ]]; then
        echo "$repo already exists, skipping"
        return 0
    fi
    
    # Get target commit
    local target_commit
    target_commit=$(git config -f .git/modules/$repo/config core.commitish || git submodule status "$repo" | awk '{print $1}' | sed 's/^[+-]//')
    
    # Try original URL first
    if ! git submodule update --init --force "$repo"; then
        echo "Failed with original URL, trying bundle..."
        rm -rf "$repo"
        # Try bundle
        if ! try_bundle "$repo" "$target_commit"; then
            echo "Bundle attempt failed for $repo"
            # Try original URL one last time with direct clone
            original_url=$(git config -f .gitmodules submodule.$repo.url)
            if ! git clone --recursive "$original_url" "$repo"; then
                echo "Both bundle and original source failed for $repo"
                return 1
            fi
        fi
        
        # Checkout the correct commit
        cd "$repo"
        git checkout "$target_commit"
        cd - >/dev/null
    fi
}

# Main script
fetch_wownero_seed  # Add this line before the main repository processing

if [[ $# -eq 0 ]]; then
    # No arguments, process all repositories
    echo "No repository specified, processing all repositories..."
    for repo in "${SUPPORTED_REPOS[@]}"; do
        echo "=== Processing $repo ==="
        if ! process_repo "$repo"; then
            echo "Warning: Failed to process $repo"
        fi
        echo "=== Finished $repo ==="
        echo
    done
else
    # Process specific repository
    repo="$1"
    if [[ ! " ${SUPPORTED_REPOS[@]} " =~ " ${repo} " ]]; then
        echo "Usage: $0 [monero/wownero/zano]"
        echo "Invalid target given, only monero, wownero and zano are supported targets"
        exit 1
    fi
    process_repo "$repo"
fi

echo "All done!"