-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpre-commit
More file actions
50 lines (43 loc) · 1.55 KB
/
Copy pathpre-commit
File metadata and controls
50 lines (43 loc) · 1.55 KB
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
#!/bin/sh
# This hook does not know which files are about to be committed.
# This hook script will revert any locally updated JSON files that only differs
# in the timestamp element.
# Then it will update the timestamp element on any remaining updated JSON files.
TZ= datestring=`date +%Y%m%d%H%M`
git diff --staged --name-only 2>/dev/null | while read f
do
# Checking staged files
case "$f" in
*.json) ;;
*) continue ;;
esac
if git diff --staged --ignore-cr-at-eol -I'"timestamp" *: *"' --exit-code --quiet "$f" 2> /dev/null
then
# File has not changed other than the timestamp, will roll back.
# This change to "timestamp" has probably come from a generator.
git checkout HEAD "$f" 2> /dev/null
continue
fi
# Update the timestamp string
( rm "$f" ; sed -e "/\"timestamp\" *: *\"/s/:.*\$/: \"$datestring\",/" > "$f" ) < "$f"
git add "$f" 2> /dev/null
done
git diff --name-only 2>/dev/null | while read f
do
# Checking unstaged files
case "$f" in
*.json) ;;
*) continue ;;
esac
if git diff --ignore-cr-at-eol -I'"timestamp" *: *"' --exit-code --quiet "$f" 2> /dev/null
then
# File has not changed other than the timestamp, will roll back."
# This change to "timestamp" has probably come from a generator.
git checkout HEAD "$f" 2> /dev/null
continue
fi
# Update the timestamp string
( rm "$f" ; sed -e "/\"timestamp\" *: *\"/s/:.*\$/: \"$datestring\",/" > "$f" ) < "$f"
git add "$f" 2> /dev/null
done
exit 0