-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpre-commit
executable file
·101 lines (89 loc) · 2.97 KB
/
pre-commit
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
#!/bin/bash
# called with no arguments by 'git commit'
#
# if necessary, stops the commit by exiting with a non-zero exit status
# redirect all output to stderr
exec 1>&2
# bail if a command fails
set -e
# figure out the commit to diff against
against=HEAD
# diff against an empty tree object if this is the initial commit
if ! git rev-parse --verify HEAD &>/dev/null; then
against="$(git hash-object -t tree /dev/null)"
fi
# check for whitespace errors
git diff-index --check --cached $against --
# prevent non-ascii filenames from being added
echo "Checking for non-ASCII filenames..."
if git diff --cached --name-only --diff-filter=A -z $against \
| LC_ALL=C tr -d '[ -~]\0' \
| wc -c \
| grep -qvx 0
then
# try to show files with non-ascii filenames
git diff --cached --name-only --diff-filter=A -z $against \
| tr '\0' '\n' \
| grep '[ -~]'
echo "Non-ASCII filenames detected. Rename them before committing."
exit 1
fi
## update the version
#verfile=bin/myscript
#srcfiles=(
# src/*
#)
#updatever() {
# local oldver="$(sed -E -n "/version\s*=/ {s/.*version\s*=[\"']?//; s/[\"']$//;p;q}" "$verfile")"
# #local oldver="$( "./$verfile" --version )"
# local newver="${oldver%.*}.$(( ${oldver##*.}+1 ))"
# echo "Updating version from $oldver to $newver..."
# sed -E -i "/version\s*=/ s/=.*/='$newver'/" "$verfile"
#}
## check if we're committing a change to a source file
#if git diff --name-only --cached \
# | grep -q -xFf <(printf "%s\n" "$verfile" "${srcfiles[@]}")
#then
# # check if $verfile has no unstaged changes
# if ! git status --porcelain | grep -q "^.M $verfile"; then
# # update version and add $verfile to commit
# updatever
# echo "Adding $verfile to commit..."
# git add "$verfile"
# # update the version and check if it changed
# elif updatever; git diff "$verfile" | egrep -q '^[+-].*version\s*='; then
# # prompt user to add the updated version to the commit
# echo "Add the version change with \`git add -p $verfile\` before committing."
# exit 1
# fi
#fi
# update docs
make doc
git add doc/* README.asciidoc
## run tests
#make -kB check
## unstage a file if the only lines in it that changed start with a pattern
#file=PKGBUILD
#lname='$pkgver'
#pattern='\s*pkgver\s*='
#if ! git diff --cached "$file" \
# | egrep -v -e "^[+-]$pattern" -e '^[+-]{3}' \
# | grep -q '^[+-]'
#then
# echo "Unstaging $file because only $lname changed..."
# git restore --staged "$file"
#fi
## aur: update .SRCINFO, based on:
## https://github.com/eli-schwartz/aurpublish/blob/master/pre-commit.hook
#git diff --cached --name-status | grep "^[AM].*PKGBUILD" | while read M file; do
# dir="$(dirname "$file")"
# echo "Updating .SRCINFO metadata..."
# ( cd "$dir" && makepkg --printsrcinfo > .SRCINFO )
# git add "$dir/.SRCINFO"
#done
## jekyll: update last_modified_at date in frontmatter
#lastmodified="$(date -u "+%Y-%m-%d %T %Z")"
#git diff --cached --name-status | grep "^[AM]" | while read M file; do
# sed -E -i "/^---/,/^---/ s/^(last_modified_at:).*/\1 $lastmodified/" "$file"
# git add "$file"
#done