summaryrefslogtreecommitdiff
path: root/scripts/utils
blob: a689c12ba92c8eea5e8175193465c26eb20b062e (plain)
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
# vim: ft=sh

# Returns current job's id
jobid() {
	basename "$0" | sed 's/\.run$//'
}

# Return current template's id
# If this is not template then this returns job's id
templateid() {
	basename "$(readlink -f "$0")" | sed 's/\.run$//'
}

TWORKSPACE="~/workspace/$(templateid)"
[ "$(jobid)" != "$(templateid)" ] || TWORKSPACE="~/workspace/notemplate"

## Simple echo wrappers #########################################################
echo_stage() {
	echo -e "\033[1;34m========== $@ ==========\033[0m ($(date))" >&2
}
echo_info() {
	echo -e "\033[1;32m---------- $@ ----------\033[0m" >&2
}
echo_debug() {
	echo -e "\033[1;30m--- $@ ---\033[0m" >&2
}
echo_fail() {
	echo -e "\033[1;31m---------- $@ ----------\033[0m" >&2
	return 1
}

## Cleanup handler ##############################################################
_LAMINAR_LOCKS=""
_handle_exit() {
	# Release all locks
	for LOCK in $_LAMINAR_LOCKS; do
		laminarc release "$(jobid)-$LOCK"
	done
}
trap _handle_exit EXIT

# Verify that we are running in process that was configured with trap (not in
# subprocess)
_check_trap() {
	[ -n "$(trap -p EXIT)" ] || \
		echo_fail "Unable to use this function as trap handling is not available in subprocess"
}

#################################################################################
# Fetch bare git repository
# First argument has to be a source URL
# Second argument is path to directory to which will be repository cloned to.
git_fetch_bare() {
	if [ -d "$2" ]; then
		git --git-dir="$2" --bare remote update --prune
	else
		git clone --mirror "$1" "$2"
	fi
}

# Common function for git_fetch_{w,g}
# Additional fourth argument should be base path to git mirror
_git_fetch() {
	local BRANCH="$3"
	[ -n "$BRANCH" ] || BRANCH=master
	git_fetch_bare "$1" "$4/$2"
	git --git-dir="$4/$2" --bare worktree add --detach "$2" "$BRANCH"
}

# Fetch git repository with mirror
#  _w: mirror is in WORKSPACE
#  _t: mirror is in TWORKSPACE
#  _g: mirror is in global workspace
# First agument has to be a source URL
# Second argument is name of directory to which will be directory cloned in.
# Second argument is optional and should be branch name (master is used if not
# provided).
git_fetch_w() {
	_git_fetch "$1" "$2" "$3" "$WORKSPACE/git-mirror"
}
git_fetch_t() {
	_git_fetch "$1" "$2" "$3" "$TWORKSPACE/git-mirror"
}
git_fetch_g() {
	_git_fetch "$1" "$2" "$3" "~/workspace/git-mirror"
}

# Clones one depth given repository with given branch/tag/hash
# First agument has to be a source URL
# Second argument is name of directory to which will be directory cloned in.
# Second argument is optional and should be branch name (master is used if not
# provided).
git_clone() {
	local BRANCH="$3"
	[ -n "$BRANCH" ] || BRANCH=master
	(
	mkdir "$2"
	cd "$2"
	git init
	git remote add origin "$1"
	git fetch --depth 1 origin "$BRANCH"
	git checkout FETCH_HEAD
	)
}

# Lock special lock for this job
# Extension appended to lock name has to be provided as first argument
laminar_self_lock() {
	_check_trap
	_LAMINAR_LOCKS="$_LAMINAR_LOCKS $1"
	laminarc lock "$(jobid)-$1"
	echo_debug "Locking $1"
}

# Unlock special lock for this job
# Extension appended to lock name has to be provided as first argument
laminar_self_release() {
	_check_trap
	laminarc release "$(jobid)-$1"
	_LAMINAR_LOCKS="$(echo "$_LAMINAR_LOCKS" | sed "s/ $1//")"
	echo_debug "Releasing $1"
}