summaryrefslogtreecommitdiff
path: root/utils/manage_static.sh
blob: f6eb1cae334de6dbf7e7f029d3a5ccdc13075ebb (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
#!/usr/bin/env bash
# SPDX-License-Identifier: AGPL-3.0-or-later

BUILD_COMMIT_MESSAGE="[build] /static"

BUILT_PATHS=(
    searx/static/themes/oscar/css
    searx/static/themes/oscar/js
    searx/static/themes/oscar/src/generated/pygments-logicodev.less
    searx/static/themes/oscar/src/generated/pygments-pointhi.less
    searx/static/themes/simple/css
    searx/static/themes/simple/js
    searx/static/themes/simple/src/generated/pygments.less
)

git_log_current_branch() {
    local branch
    branch="$(git branch --show-current)"
    git log "${branch}" --pretty=format:'%h' \
        --not --exclude="${branch}" --branches --remotes
}

is.build.commit() {
    local commit_sha="$1"
    local commit_message
    local commit_files

    # check commit message
    commit_message=$(git show -s --format=%s "${commit_sha}")
    if [ "${commit_message}" != "${BUILD_COMMIT_MESSAGE}" ]; then
        echo "Commit message of ${commit_sha} is '${commit_message}'"
        return 1
    fi

    # check all files of the commit belongs to $BUILT_PATHS
    commit_files=$(git diff-tree --no-commit-id --name-only -r "${commit_sha}")
    for i in ${BUILT_PATHS[*]}; do
        # remove files of ${BUILT_PATHS}
        commit_files=$(echo "${commit_files}" | grep -v "^${i}")
    done

    if [ -n "${commit_files}" ]; then
        echo "Commit $1 contains files that were not build: ${commit_files}"
        return 2
    fi
    return 0
}

static.build.commit.drop() {
    local last_commit_id
    last_commit_id=$(git_log_current_branch | head -1)

    if [ -z "${last_commit_id}" ]; then
        echo "Empty branch"
        return 1
    fi

    if ! is.build.commit "${last_commit_id}"; then
        return $?
    fi
    echo "Drop last commit ${last_commit_id}"
    git reset --hard HEAD~1
}

static.build.commit() {
    local staged_files

    # check for not commited files
    if [ -n "$(git diff --name-only)" ]; then
        echo "Some files are not commited:"
        echo "${NOT_COMMITED_FILES}"
        return 1
    fi

    staged_files=$(git diff --name-only --cached)

    # check for staged files
    if [ -n "${staged_files}" ]; then
        echo "Some files are staged:"
        echo "${staged_files}"
        return 1
    fi

    # drop existing commit
    if static.commit.drop; then
        return $?
    fi

    (
        set -e
        # build the themes
        make themes.all

        # add build files
        for built_path in "${BUILT_PATHS[@]}"; do
            git add -v "${built_path}"
        done

        # check for modified files that are not staged
        if [ -n "$(git diff --name-only)" ]; then
            echo "make themes.all has created files that are not in BUILT_PATHS"
            return 2
        fi
        git commit -m "${BUILD_COMMIT_MESSAGE}"
    )
}

static.git.restore.staged() {
    local STAGED_FILES
    STAGED_FILES=$(git diff --name-only --cached)

    for i in ${BUILT_PATHS[*]}; do
        STAGED_FILES_FOR_I=$(echo "${STAGED_FILES}" | grep "^${i}")
        if [ -n "${STAGED_FILES_FOR_I}" ]; then
            # shellcheck disable=SC2086
            git restore --staged ${STAGED_FILES_FOR_I}
        fi
    done
}

static.git.restore() {
    static.git.restore.staged

    NOT_COMMITED_FILES="$(git diff --name-only)"
    for i in ${BUILT_PATHS[*]}; do
        NOT_COMMITED_FILES_FOR_I=$(echo "${NOT_COMMITED_FILES}" | grep "^${i}")
        if [ -n "${NOT_COMMITED_FILES_FOR_I}" ]; then
            # shellcheck disable=SC2086
            git restore ${NOT_COMMITED_FILES_FOR_I}
        fi
    done
}

main() {
    case $1 in
        static.build.commit.drop)
            # drop last commit if it was made by the "commit" command
            static.build.commit.drop
            ;;
        static.build.commit)
            # call the "static.build.commit.drop" command,
            # then "make themes.all"
            # then commit the built files ($BUILT_PATHS).
            static.build.commit
            ;;
        static.git.restore.staged)
            # after "git add ."
            # remove the built files
            # so only the source are commited
            static.git.restore.staged
            ;;
        static.git.restore)
            # "git restore" of the built files.
            static.git.restore
            ;;
    esac
}

main "$@"