-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-lava-common
86 lines (67 loc) · 1.49 KB
/
git-lava-common
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
#!/bin/bash
# shell output
warn() { echo "$@" >&2; }
die() { warn "$@"; exit 1; }
escape() {
echo "$1" | sed 's/\([\.\$\*]\)/\\\1/g'
}
# set logic
has() {
local item=$1; shift
echo " $@ " | grep -q " $(escape $item) "
}
# git functionality
git_local_branches() { git branch --no-color | sed 's/^[* ] //'; }
git_remote_branches() { git branch -r --no-color | sed 's/^[* ] //'; }
git_local_branch_exists() {
has $1 $(git_local_branches)
}
git_remote_branch_exists() {
has $1 $(git_remote_branches)
}
git_branch_exists() {
has $1 $(git_all_branches)
}
current_branch() {
git for-each-ref --format='%(refname:short)' `git symbolic-ref HEAD`
}
local_merge_target() {
if [ -z $2 ]; then
# get
git config branch.$1.localmerge
else
# set
git config branch.$1.localmerge $2
fi
}
branch_origin() {
git config branch.$1.remote
}
# Assertions
require_local_branch() {
if ! git_local_branch_exists $1; then
die "fatal: Local branch '$1' does not exist and is required."
fi
}
require_remote_branch() {
if ! has $1 $(git_remote_branches); then
die "Remote branch '$1' does not exist and is required."
fi
}
require_branch() {
if ! has $1 $(git_all_branches); then
die "Branch '$1' does not exist and is required."
fi
}
require_branch_absent() {
if has $1 $(git_all_branches); then
die "Branch '$1' already exists. Pick another name."
fi
}
#misc
run_hook() {
local DOT_GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
if [ -x "$DOT_GIT_DIR/hooks/$1" ]; then
. "$DOT_GIT_DIR/hooks/$1"
fi
}