File: pre-commit

package info (click to toggle)
rocsparse 6.4.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 234,069; f90: 9,307; sh: 2,262; python: 1,939; makefile: 1,585; ansic: 440; xml: 26
file content (67 lines) | stat: -rwxr-xr-x 2,128 bytes parent folder | download | duplicates (3)
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
#!/bin/bash
#
# This pre-commit hook checks if any versions of clang-format
# are installed, and if so, uses the installed version to format
# the staged changes.

export PATH=/usr/bin:/bin

set -x

format=/opt/rocm/llvm/bin/clang-format

# Redirect stdout to stderr.
exec >&2

# Do everything from top - level
cd $(git rev-parse --show-toplevel)

if git rev-parse --verify HEAD >/dev/null 2>&1; then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

if [[ "$1" == "--reformat" ]]; then
    files=$(git ls-files --exclude-standard)
else
    files=$(git diff-index --cached --name-only $against)
fi

[[ -z "$files" ]] && exit

# Change the copyright date at the top of any text files
for file in $files; do
    if [[ -e $file ]]; then
        /usr/bin/perl -pi -e 'INIT { exit 1 if !-f $ARGV[0] || -B $ARGV[0]; $year = (localtime)[5] + 1900 }
            s/^([*\/#[:space:]]*)Copyright\s+(?:\(C\)\s*)?(\d+)(?:\s*-\s*\d+)?/qq($1Copyright (C) $2@{[$year != $2 ? "-$year" : ""]})/ie
            if $. < 10' "$file" && git add -u "$file"
    fi
done

# do the formatting
for file in $files; do
    if [[ -e $file ]] && echo $file | grep -Eq '\.c$|\.h$|\.hpp$|\.cpp$|\.cl$|\.in$|\.txt$|\.yaml$|\.sh$|\.py$|\.pl$|\.cmake$|\.md$|\.rst$|\.groovy$'; then
        sed -i -e 's/[[:space:]]*$//' "$file" # Remove whitespace at end of lines
        sed -i -e '$a\' "$file" # Add missing newline to end of file
        # Convert UTF8 non-ASCII to ASCII
        temp=$(mktemp)
        [[ -w $temp ]] || exit
        iconv -s -f utf-8 -t ascii//TRANSLIT "$file" > "$temp" || exit
        chmod --reference="$file" "$temp" || exit
        mv -f "$temp" "$file" || exit
        git add -u "$file"
    fi
done

# if clang-format exists, run it on C/C++ files
if [[ -x $format ]]; then
    for file in $files; do
       if [[ -e $file ]] && echo $file | grep -Eq '\.c$|\.h$|\.hpp$|\.cpp$|\.cl$|\.h\.in$|\.hpp\.in$|\.cpp\.in$'; then
            echo "$format $file"
            "$format" -i -style=file "$file"
            git add -u "$file"
        fi
    done
fi