summaryrefslogtreecommitdiff
path: root/pbs-plumb-shlib
blob: 5f2014c210eaaf27878d5bbda325951204178a5b (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
#!/bin/bash

. libremessages

# "Primitive" git functions ##########################################

# Usage: in_git
# Returns 0 if somewhere in a git repository, nonzero if not.
in_git() {
	gitroot &>/dev/null
}

# Usage: gitroot
# Prints the root directory of the git checkout
gitroot() {
	git rev-parse --show-toplevel
}

# Usage: gitbranch
# Prints the current git branch
gitbranch() {
	git rev-parse --abbrev-ref HEAD
}

# Usage: have_file $file
# Returns 0 if `$(gitroot)/$file` exists.
have_file() {
	local file=$1
	if in_git && [[ -f "$(gitroot)/$file" ]]; then
		return 0;
	fi
	return 1
}

# Deal with being in a PBS package repository ########################

in_pbs-package() {
	have_file .pbs-package
}

ensure_in_pbs-package() {
	if ! in_pbs-package; then
		error "Not in a PBS package directory"
		exit 1
	fi
}

pbs-package-root() {
	ensure_in_pbs-package
	gitroot
}

cdto_pbs-package-root() {
	ensure_in_pbs-package
	cd "$(gitroot)"
}

# Deal with being in a PBS repository ################################

in_pbs() (
	if in_pbs-package; then
		cd "$(pbs-package-root)/.."
	fi
	have_file .pbs-root
)

ensure_in_pbs() {
	if ! in_pbs; then
		error "Not in a PBS directory"
		exit 1
	fi
}

pbs-root() {
	ensure_in_pbs
	(
		if in_pbs-package; then
			cd "$(pbs-package-root)/.."
		fi
		gitroot
	)
}

cdto_pbs-root() {
	ensure_in_pbs
	if in_pbs-package; then
		cd "$(pbs-package-root)/.."
	fi
	cd "$(gitroot)"
}