summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/dist/s_clang_format
blob: 46b18f4913d99c4e3270df1c519bccbe01913785 (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
#! /bin/bash

set -o pipefail

t=__wt.$$
trap 'rm -rf $t' 0 1 2 3 13 15

download_clang_format() {
    if [ `uname` = "Linux" ]; then
        curl https://s3.amazonaws.com/boxes.10gen.com/build/clang-format-llvm-10.0.0-x86_64-linux-gnu-ubuntu-20.04.tar-1.gz -o dist/clang-format.tar.gz
        tar --strip=1 -C dist/ -xf dist/clang-format.tar.gz clang-format-llvm-10.0.0-x86_64-linux-gnu-ubuntu-20.04/clang-format && rm dist/clang-format.tar.gz
    elif [ `uname` = "Darwin" ]; then
        curl https://s3.amazonaws.com/boxes.10gen.com/build/build/clang-format-llvm-10.0.0-x86_64-apple-darwin.tar.gz -o dist/clang-format.tar.gz
        tar --strip=1 -C dist/ -xf dist/clang-format.tar.gz clang-format-llvm-10.0.0-x86_64-apple-darwin/clang-format && rm dist/clang-format.tar.gz
    else
        echo "$0: unsupported environment $(uname)"
        exit 1
    fi
}

# Find the top-level WiredTiger directory.
dest_dir=$(git rev-parse --show-toplevel)

# If this is a MongoDB source tree, move to the WiredTiger subtree.
git_repo_str=$(git config remote.origin.url)
is_mongo_repo=$(echo "$git_repo_str" | grep "mongodb/mongo")
if [ -n "$is_mongo_repo" ] ; then
       dest_dir="$dest_dir/src/third_party/wiredtiger"
fi
cd "$dest_dir" || exit 1

# Override existing Clang Format versions in the PATH.
export PATH="${PWD}/dist":$PATH

# Download the clang-format binary if it's not in place.
[ ! -x "$(command -v clang-format)" ] && download_clang_format

# Ensure that we have the correct version of clang-format.
desired_version="10.0.0"

# On macOS Catalina, users need to manually approve binaries.
# If we're not allowed to run clang-format, let's exit (should be obvious from the dialog).
current_version=`clang-format --version` || exit 1

echo $current_version | grep "version $desired_version" >/dev/null 2>&1
if test $? -ne 0; then
    download_clang_format
fi

exclude=true
case $# in
0)
    # Get all source files.
    search=`find bench examples ext src test -name '*.[ch]' -o -name '*.cxx' -o -name '*.cpp'`
    ;;
1)
    # If the -F option is not specified then find every source file.
    if [ $1 == "-F" ]; then
        # We are running in fast mode, get all the source files modified according to git.
        search=`git diff --name-only $(git merge-base --fork-point develop) | grep -E '\.(c|h|cpp)$'`
        echo $search
    else
        search="$1"
        exclude=false
    fi
    ;;
*)
    echo "usage: $0 [-F] [file]"
    echo "-F apply clang format to modified source files in the git index"
    echo "Only a file or -F can be provided"
    exit 1
    ;;
esac

if $exclude; then
    for f in `cat dist/s_clang_format.list`; do
        search=`echo "$search" | sed "\#$f#d"`
    done
fi

# Don't format inplace with -i flag.
# We want to be able to detect modifications.
for f in $search; do
    cat "$f" | \
            clang-format --fallback-style=none | \
            python dist/s_comment.py > "$t" || exit 1
    cmp -s "$f" "$t"
    if test $? -ne 0; then
        if test $# -eq 0 ; then
            echo "Modifying $f"
        fi
        cp "$t" "$f"
    fi
done