forked from fwolf/copyright-year-updater.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-commit.sample.sh
executable file
·45 lines (34 loc) · 1.42 KB
/
pre-commit.sample.sh
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
#!/usr/bin/env bash
#====================================================================
# Git pre commit hook to update copyright year
#
# Copy this file as '.git/hooks/pre-commit', chmod +x.
#
# @see https://stackoverflow.com/questions/22858850
#====================================================================
export COPYRIGHT_YEAR_UPDATER='copyright-year-updater.sh'
# Command mktemp is a little different in MacOS X, need '-t' param
git diff-index --cached --name-only HEAD | xargs -I % sh -c '
git ls-files --stage % | while read MODE OBJECT STAGE FILE_PATH; do
case ${MODE} in
10*)
# Copy file to temporary
STAGED_FILE=$(mktemp -t t)
git show ${OBJECT} > "${STAGED_FILE}"
# Do change copyright year
FORMATTED_FILE=$(mktemp -t t)
cp "${STAGED_FILE}" "${FORMATTED_FILE}"
${COPYRIGHT_YEAR_UPDATER} "${FORMATTED_FILE}"
# Write new file blob to object database
FORMATTED_HASH=`git hash-object -w "${FORMATTED_FILE}"`
# Register new written file to working tree index
git update-index --cacheinfo ${MODE} ${FORMATTED_HASH} "${FILE_PATH}"
# Patch file in workspace, make it seems changed too
diff "${STAGED_FILE}" "${FORMATTED_FILE}" | patch "${FILE_PATH}"
rm "${STAGED_FILE}"
rm "${FORMATTED_FILE}"
;;
esac
done
'
exit 0