xaizek / uncov (License: AGPLv3+) (since 2018-12-07)
Uncov(er) is a tool that collects and processes code coverage reports.
Commit 5b2cf7154c455c5c3ec6df8ddcb3d5b2fb7b591b

Add git pre- and post-commit hooks to repository
Author: xaizek
Author date (UTC): 2017-01-08 22:28
Committer name: xaizek
Committer date (UTC): 2017-01-08 22:28
Parent(s): c187171ca6ddcc21f029d2da47bbd6b4348c0b45
Signing key: 99DC5E4DB05F6BE2
Tree: 50c7906e6c4942f7cd0ed98236640129a29b2ac2
File Lines added Lines deleted
scripts/git-hook-post-commit 46 0
scripts/git-hook-pre-commit 112 0
File scripts/git-hook-post-commit added (mode: 100755) (index 0000000..9af7dff)
1 #!/bin/bash
2 #
3 # Hook script to collect coverage information for uncov.
4 #
5 # Define SKIP_POST_HOOK to skip coverage collection, e.g., on amend of README:
6 #
7 # SKIP_POST_HOOK=1 git commit --amend
8
9 if [ -n "$SKIP_POST_HOOK" ]; then
10 exit 0
11 fi
12
13 function pop_stashed()
14 {
15 # pop stashed changes if working directory wasn't clean
16 if [ -z "$DIRTY" ]; then
17 return
18 fi
19
20 git reset --hard HEAD
21 local skipped="$(git ls-files -t | grep ^S | cut -d' ' -f2-)"
22 git stash pop --quiet --index
23
24 while read file; do
25 git update-index --skip-worktree "$file"
26 done <<< "$skipped"
27 }
28
29 # determine working tree status
30 if ! git diff --quiet || \
31 [ "$(git status --short | tail -1 | cut -f1 -d' ')" = '??' ]; then
32 DIRTY=1
33 fi
34
35 # stash not staged for commit changes
36 if [ -n "$DIRTY" ]; then
37 if ! git stash save --include-untracked --keep-index; then
38 exit 1
39 fi
40 trap pop_stashed EXIT
41 fi
42
43 nice ionice make --jobs 6 coverage
44 RESULT="$?"
45
46 # stashed changes (if any) are automatically restored by EXIT trap handler
File scripts/git-hook-pre-commit added (mode: 100755) (index 0000000..1515b3c)
1 #!/bin/bash
2 #
3 # Hook script to verify what is about to be committed.
4 # Called by "git commit" with no arguments. The hook should
5 # exit with non-zero status after issuing an appropriate message if
6 # it wants to stop the commit.
7
8 # This hook:
9 # * checks for whitespace errors
10 # * checks for bad characters
11 # * runs tests on changes that are about to be committed
12
13 if git rev-parse --verify HEAD >/dev/null 2>&1; then
14 against=HEAD
15 else
16 # Initial commit: diff against an empty tree object
17 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
18 fi
19
20 # If you want to allow non-ascii filenames set this variable to true.
21 allownonascii=$(git config hooks.allownonascii)
22
23 # Cross platform projects tend to avoid non-ascii filenames; prevent
24 # them from being added to the repository. We exploit the fact that the
25 # printable range starts at the space character and ends with tilde.
26 if [ "$allownonascii" != "true" ] &&
27 # Note that the use of brackets around a tr range is ok here, (it's
28 # even required, for portability to Solaris 10's /usr/bin/tr), since
29 # the square bracket bytes happen to fall in the designated range.
30 test "$(git diff --cached --name-only --diff-filter=A -z $against |
31 LC_ALL=C tr -d '[ -~]\0')"
32 then
33 echo "Error: Attempt to add a non-ascii file name."
34 echo
35 echo "This can cause problems if you want to work"
36 echo "with people on other platforms."
37 echo
38 echo "To be portable it is advisable to rename the file ..."
39 echo
40 echo "If you know what you are doing you can disable this"
41 echo "check using:"
42 echo
43 echo " git config hooks.allownonascii true"
44 echo
45 exit 1
46 fi
47
48 function pop_stashed()
49 {
50 # pop stashed changes if working directory wasn't clean
51 if [ -z "$DIRTY" ]; then
52 return
53 fi
54
55 git reset --hard HEAD
56 local skipped="$(git ls-files -t | grep ^S | cut -d' ' -f2-)"
57 if ! git stash pop --quiet --index; then
58 return 1
59 fi
60
61 while read file; do
62 git update-index --skip-worktree "$file"
63 done <<< "$skipped"
64 }
65
66 function check_odd_whitespace()
67 {
68 # check for odd whitespace
69 git diff-index --check --color $1 $against --
70 local check_exit_code="$?"
71 if [ "$check_exit_code" -ne "0" ]; then
72 echo -e "\e[1;31m""Your changes introduce whitespace errors" "\e[7;0m"
73 if [[ -z "$ALLOW_WHITESPACE_ERRORS" ]]; then
74 exit $check_exit_code
75 fi
76 fi
77 }
78
79 # check for odd whitespace before build
80 check_odd_whitespace --cached
81
82 # determine working tree status
83 if ! git diff --quiet || \
84 [ "$(git status --short | tail -1 | cut -f1 -d' ')" = '??' ]; then
85 DIRTY=1
86 fi
87
88 # stash not staged for commit changes
89 if [ -n "$DIRTY" ]; then
90 if ! git stash save --include-untracked --keep-index; then
91 return 1
92 fi
93 trap pop_stashed EXIT
94 fi
95
96 if ! git diff --quiet HEAD; then
97 # run build using staged changes only
98 # use release build as with its optimization level compiler is able to issue
99 # more warnings
100 nice ionice make --jobs 6 check release
101 RESULT="$?"
102 else
103 RESULT="0"
104 fi
105
106 # check for odd whitespace after build
107 check_odd_whitespace
108
109 # stashed changes (if any) are automatically restored by EXIT trap handler
110
111 # return result of build
112 exit $RESULT
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/uncov

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/uncov

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master