-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle_header
145 lines (123 loc) · 2.81 KB
/
bundle_header
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#! /bin/sh -ueC
CPR_CMD="cp -rfP"
TAR_CMD="tar -xz --overwrite"
if [ "$(uname -s)" = "Darwin" ]; then
CPR_CMD="cp -rf"
TAR_CMD="tar -xz"
fi
usage() {
cat << 'EOF'
usage: $0 [--work-tree <path>] [--hsh-dir <path>] [--bin <path>]
-C <path> act as if the command was called from <path>
-b, --bin path to where hsh script is checked out, default: './bin'
EOF
}
die() {
echo "$@" >&2
exit 1
}
cleanup() {
if [ -e "$1" ]
then
if [ -e "$(pwd)/.hsh" ]
then
rm -rf "$(pwd)/.hsh"
fi
rm -rf "$1"
fi
}
fixup_worktree() {
mv "$1" "$1.bck"
sed "/[ \t]*\[[ \t]*core[ \t]*\]/,/[ \t]*\[/ { s,[ \t]*worktree[ \t]*=[ \t]*\(.*\),worktree=$2\1,g; }" "$1.bck" > "$1"
rm "$1.bck"
}
run_hook() {
[ -e "$tmp_dir/hooks/$1" ] || return
if [ -e "$tmp_dir/dependencies/$1" ]
then
while IFS= read -r entry
do
d="${entry%.git}"
d="${d%/}"
d="${d##*/}"
run_hook "$d"
done < "$tmp_dir/dependencies/$1"
fi
echo "# running bundle-out hook for $1"
(
export HSH_ROOT="$(pwd)"
export HSH_ACTION="bundle-out"
export HSH_REPOSITORY="$1"
export HSH_BUNDLE_ROOT="$tmp_dir/repos"
sh -ueC $tmp_dir/hooks/$1
)
# avoid runing the same hook twice
rm "$tmp_dir/hooks/$1"
}
while [ $# -ne 0 ]
do
case "$1" in
-h|--help)
usage
exit 0
;;
-C)
shift
[ $# -eq 0 ] && die "missing value for parameter '-C'"
dir="$1"
;;
-b|--bin)
shift
[$tmp_dir/root $# -eq 0 ] && \
die "missing value for parameter --hsh-dir"
bin_dir="$1"
;;
*)
die "unknown option '$1'"
;;
esac
shift
done
if [ -n "${dir-}" ]
then
cd "$dir" || die "Failed to switch to '$dir'"
fi
hsh_dir="$(pwd)/.hsh"
bin_dir="${bin_dir:-$(pwd)/bin}"
tmp_dir="$(pwd)/.hsh_install_$$"
mkdir "$tmp_dir"
trap "cleanup \"$tmp_dir\"" EXIT HUP INT QUIT ABRT TERM
BUNDLE_START=$(awk '/^__HSH_BUNDLE_CONTENT__/ { print NR + 1; exit 0; }' "$0")
tail -n+$BUNDLE_START "$0" | $TAR_CMD -C "$tmp_dir" \
|| die "fail to extract bundle"
# if [ ! -e "$bin_dir" ]
# then
# mkdir "$bin_dir"
# fi
# cp "$tmp_dir/hsh" "$bin_dir/hsh"
if [ ! -e "$hsh_dir" ]
then
mkdir "$hsh_dir"
fi
ls -1 "$tmp_dir/repos" | while IFS= read -r repo
do
fixup_worktree "$tmp_dir/repos/$repo/config" "$(pwd)"
done
$CPR_CMD "$tmp_dir/repos" "$hsh_dir"
find "$tmp_dir/root" | while IFS= read -r entry
do
dest="$(pwd)$(echo "$entry" | sed s,$tmp_dir/root,,)"
if [ -d "$entry" ]
then
[ ! -d $dest ] && mkdir "$dest"
else
cp -fP "$entry" "$dest"
fi
done
while [ $(ls -1 "$tmp_dir/hooks" | wc -l) -ne 0 ]
do
run_hook "$(ls -1 "$tmp_dir/hooks" | head -n 1)"
done
rm -rf "$tmp_dir"
exit $?
__HSH_BUNDLE_CONTENT__