-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommit-msg
More file actions
executable file
·48 lines (39 loc) · 1.54 KB
/
commit-msg
File metadata and controls
executable file
·48 lines (39 loc) · 1.54 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
#!/bin/sh
# https://github.com/thisdougb/git-time-hooks
#
# This hook runs before the partner prepare-commit-msg hook. It
# stamps each commit with the creation time of the current branch.
HOOKNAME=commit-msg
# dot delimited list of branches to ignore
EXCLUDE_BRANCHES=".master.main."
BRANCH=$(git symbolic-ref HEAD --short)
NOW=$(date +%s)
# date options for compatibility on mac and linux
ONEMONTHAGO=$(date -v-1m +%s 2>/dev/null || date -d "1 month ago" +%s)
if [ -n "$BRANCH" ]; then
# a bit of a shell script hack
echo "$EXCLUDE_BRANCHES" | grep ".$BRANCH."
if [ $? = 0 ]; then
echo "[$HOOKNAME] ignoring branch $BRANCH, not adding branch timestamp"
exit 0
fi
# the reflog is created after the first commit is made. so avoid a spurious
# error with this check.
git reflog >/dev/null 2>&1
if [ $? != 0 ]; then
echo "[$HOOKNAME] reflog does not exist yet"
exit 0
fi
# git reflog: list (local) reflog entries in the last month
# grep1: we don't specify the from branch
# grep2: make sure we're checking out our target
# tail: get the last/older occurence
# sed: strip out the timestamp (basic POSIX regex for compatibility)
AGE=$(git reflog show --date=unix --since=$ONEMONTHAGO --format="%gd %gs" \
| grep "checkout: moving from " \
| grep " to $BRANCH" \
| tail -1 \
| sed 's/HEAD@{\([0-9]\+\)}.*/\1/g')
# include the branch name so the post-merge hook is a little safer
echo "\ntime_branch_created $BRANCH $AGE" >> "$1"
fi