-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathentpain-git-hook-post-rewrite.sh
executable file
·70 lines (57 loc) · 1.93 KB
/
entpain-git-hook-post-rewrite.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
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
#!/usr/bin/env bash
#
# this is a git post-rewrite hook to cache auto generated files per branch
# and quickly switch between them without having to regenerate the files.
#
# it's complementary to the post-checkout hook and should be placed in `.git/hooks/post-rewrite` (note: without the `.sh`!).
# I personally just symlink it in there from wherever I placed the original ;)
#
set -e
DEBUG=false
ROOTDIR=$(pwd)
ENTPAINDIR=${ROOTDIR}/tmp/entpain
ENTPAINPACKDIRS=${ENTPAINPACKDIRS:-ent/,quickfix/dtcc/,}
ENTPAINPACKDIRS=$(echo "$ENTPAINPACKDIRS" | tr "," "\n")
function dbg() {
echo "$*" | tee -a ${ROOTDIR}/tmp/entpain.log 1>&2
}
function entpainunpack() {
BRANCH=$1
BRANCHSLUG=$(echo "$1" | sed 's/\//__/g')
# can only unpack if there's an archive
if [[ -f ${ENTPAINDIR}/${BRANCHSLUG}.tar.gz ]]; then
dbg "entpain:unpack $BRANCH"
# for each dir to unpack delete any git ignored files
for ENTPAINPACKDIR in $ENTPAINPACKDIRS; do
if [[ -d $ENTPAINPACKDIR ]]; then
git clean -q -f -d -X $ENTPAINPACKDIR
fi
done
# unpack from our archive
tar xzf ${ENTPAINDIR}/${BRANCHSLUG}.tar.gz
fi
}
if [[ "$1" == "rebase" && "$2" == "" ]]; then
HEAD=
while read LINE
do
if [[ "$LINE" != "" ]]; then
COMMITS=($LINE)
HEAD=${COMMITS[1]}
fi
done < /dev/stdin
if [[ "$HEAD" == "" ]]; then
exit 0
fi
BRANCH=$(git name-rev --exclude 'remotes/*' --exclude='tags/*' --name-only $HEAD || echo "")
# master is special, there's often other branches with the same ref but we want to prioritize master ...
MASTERSHA=$(git rev-parse master)
if [[ "$VHEAD" == "$MASTERSHA" ]]; then
BRANCH=master
fi
dbg "entpain:post-rewrite $HEAD = $BRANCH"
# if we know which branch we're switch towards than we can try to unpack its archive
if [[ "$BRANCH" != "" && ! "$BRANCH" =~ remotes\/.+ && ! "$BRANCH" =~ ~[0-9]+$ && "$BRANCH" != "undefined" ]]; then
entpainunpack "$BRANCH"
fi
fi