summaryrefslogtreecommitdiffstats
path: root/uAssets/tools/make-diffpatch.sh
blob: 81e0aabbaff193ea631248a47ff8360049dfeef9 (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
#!/usr/bin/env bash
#
# This script assumes a linux environment

set -e

# To be executed at the root of CDN repo
#
# It's not being hosted at CDN because that
# repo is also used as a website

VERSION=$1
if [[ -z $VERSION ]]; then
    echo "Error: No version provided, aborting"
    exit 1
fi

PATCHES_DIR=$2
if [[ -z $PATCHES_DIR ]]; then
    echo "Error: patches directory is not provided, aborting"
    exit 1
fi

PREVIOUS_VERSION=$(<version)
PREVIOUS_PATCH_FILE="$PATCHES_DIR/$PREVIOUS_VERSION.patch"
: > "$PREVIOUS_PATCH_FILE"

NEXT_PATCH_FILE="$PATCHES_DIR/$VERSION.patch"

# Temporary file to receive the RCS patch data
DIFF=$(mktemp)

FILES=( $(git diff --name-only) )
for FILE in "${FILES[@]}"; do

    # Reference:
    # https://github.com/ameshkov/diffupdates

    if (head "$FILE" | grep -q '^! Version: '); then
        sed -Ei "1,10s;^! Version: .+$;! Version: $VERSION;" "$FILE"
    fi

    # Patches are for filter lists supporting differential updates
    if (head "$FILE" | grep -q '^! Diff-Path: '); then

        # Extract diff name from `! Diff-Path:` field
        DIFF_NAME=$(grep -m 1 -oP '^! Diff-Path: [^#]+#?\K.*' "$FILE")
        # Fall back to `! Diff-Name:` field if no name found
        # Remove once `! Diff-Name:` is no longer needed after transition
        if [[ -z $DIFF_NAME ]]; then
            DIFF_NAME=$(grep -m 1 -oP '^! Diff-Name: \K.+' "$FILE")
        fi
        echo "Info: Diff name for ${FILE} is ${DIFF_NAME}"

        # We need a patch name to generate a valid patch
        if [[ -n $DIFF_NAME ]]; then

            # Compute relative patch path
            PATCH_PATH="$(realpath --relative-to="$(dirname "$FILE")" "$NEXT_PATCH_FILE")"

            # Fill in patch path to next version (do not clobber hash portion)
            sed -Ei "1,10s;^! Diff-Path: [^#]+(#.+)?$;! Diff-Path: $PATCH_PATH\1;" "$FILE"

            # Compute the RCS diff between current version and new version
            git show "HEAD:$FILE" | diff -n - "$FILE" > "$DIFF" || true

            FILE_CHECKSUM="$(sha1sum "$FILE")"
            FILE_CHECKSUM=${FILE_CHECKSUM:0:10}

            DIFF_LINES=$(wc -l < "$DIFF")
            echo "Info: Computed patch for ${FILE} has ${DIFF_LINES} lines"

            # Populate output file with patch information
            echo "Info: Adding patch data of ${FILE} to ${PREVIOUS_PATCH_FILE}"
            echo "diff name:$DIFF_NAME lines:$DIFF_LINES checksum:$FILE_CHECKSUM" >> "$PREVIOUS_PATCH_FILE"
            cat "$DIFF" >> "$PREVIOUS_PATCH_FILE"

        else

            echo "Error: Diff name not found, skipping"

        fi
    fi

    # Stage changed file
    echo "Info: Staging $FILE"
    git add -u "$FILE"

done

# Create a patch only if there was a previous version
if [[ -n $PREVIOUS_VERSION ]]; then
    echo "Info: Staging $PREVIOUS_PATCH_FILE"
    git add "$PREVIOUS_PATCH_FILE"
fi

echo -n "$VERSION" > version
git add version

rm -f "$DIFF"