summaryrefslogtreecommitdiff
path: root/dev-tools/reformat-all.sh
blob: befc589a4b1002db522e9dd82a976c9838e6cb49 (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
#!/bin/sh
# reformat-all.sh - Reformat all git files in the checked out
#                   git branch using uncrustify.
#
# Copyright (C) 2016-2018 - David Sommerseth <davids@openvpn.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

tstamp="$(date +%Y%m%d-%H%M%S)"
files="$(pwd)/reformat-all_files-$tstamp.lst"
log="$(pwd)/reformat-all_log-$tstamp.txt"

srcroot="$(git rev-parse --show-toplevel)"
cfg="$srcroot/dev-tools/uncrustify.conf"
specialfiles="$srcroot/dev-tools/special-files.lst"

export gitfiles=0
export procfiles=0

# Go to the root of the source tree
cd "$srcroot"

{
    echo -n "** Starting $0: "
    date

    # Find all C source/header files
    git ls-files | grep -E ".*\.[ch](\.in$|$)" > "${files}.git"

    # Manage files which needs special treatment
    awk -F\# '{gsub("\n| ", "", $1); print $1}' "$specialfiles" > "${files}.sp"
    while read srcfile
    do
        res=$(grep "$srcfile" "${files}.sp" 2>/dev/null)
        if [ $? -ne 0 ]; then
            # If grep didn't find the file among special files,
            # process it normally
            echo "$srcfile" >> "$files"
        else
            mode=$(echo "$res" | cut -d:  -f1)
            case "$mode" in
                E)
                    echo "** INFO **  Excluding '$srcfile'"
                    ;;
                P)
                    echo "** INFO **  Pre-patching '$srcfile'"
                    patchfile="${srcroot}"/dev-tools/reformat-patches/before_$(echo "$srcfile" | tr "/" "_").patch
                    if [ -r "$patchfile" ]; then
                        git apply "$patchfile"
                        if [ $? -ne 0 ]; then
                            echo "** ERROR **  Failed to apply pre-patch file: $patchfile"
                            exit 2
                        fi
                    else
                        echo "** WARN ** Pre-patch file for $srcfile is missing: $patchfile"
                    fi
                    echo "$srcfile" >> "${files}.postpatch"
                    echo "$srcfile" >> "$files"
                    ;;
                *)
                    echo "** WARN ** Unknown mode '$mode' for file '$srcfile'"
                    ;;
            esac
        fi
    done < "${files}.git"
    rm -f "${files}.git" "${files}.sp"

    # Kick off uncrustify
    echo
    echo "** INFO ** Running: uncrustify -c $cfg --no-backup -l C -p debug.uncr -F $files"
    uncrustify -c "$cfg" --no-backup -l C -p debug.uncr -F "$files" 2>&1
    res=$?
    echo "** INFO ** Uncrustify completed (exit code $res)"
} | tee "${log}-1"  # Log needs to be closed here, to be processed in next block

{
    # Check the results
    gitfiles=$(wc -l "$files" | cut -d\  -f1)
    procfiles=$(grep "Parsing: " "${log}-1" | wc -l)
    echo
    echo "C source/header files checked into git: $gitfiles"
    echo "Files processed by uncrustify:          $procfiles"
    echo

    # Post-Patch files modified after we uncrustify have adjusted them
    if [ -r "${files}.postpatch" ]; then
        while read srcfile;
        do
            patchfile="${srcroot}"/dev-tools/reformat-patches/after_$(echo "$srcfile" | tr "/" "_").patch
            if [ -r "$patchfile" ]; then
                echo "** INFO **  Post-patching '$srcfile'"
                git apply "$patchfile"
                if [ $? -ne 0 ]; then
                    echo "** WARN ** Failed to apply $patchfile"
                fi
            else
                echo "** WARN ** Post-patch file for $srcfile is missing: $patchfile"
            fi
        done < "${files}.postpatch"
        rm -f "${files}.postpatch"
    fi
} | tee "${log}-2" # Log needs to be closed here, to be processed in next block

cat "${log}-1" "${log}-2" > "$log"

{
    ec=1
    echo
    if [ "$gitfiles" -eq "$procfiles" ]; then
        echo "Reformatting completed successfully"
        ec=0
    else
        last=$(tail -n1 "${log}-1")
        echo "** ERROR ** Reformating failed to process all files."
        echo "            uncrustify exit code: $res"
        echo "            Last log line: $last"
        echo
    fi
    rm -f "${log}-1" "${log}-2"
} | tee -a "$log"
rm -f "${files}"

exit $ec