forked from skwp/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 4
/
git
111 lines (96 loc) · 2.88 KB
/
git
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
# -*- mode: sh -*-
# Generic git helper functions.
git_bundleall() {
# b=$(git rev-parse --show-toplevel)
local f=${1:-$MR_REPO}
local d=${2:-/tmp}
f=${f##*/}
echo git bundle create $d/${f##.}.gitrepo.$(date +%+4Y%m%d-%H%M) --all
git bundle create $d/${f##.}.gitrepo.$(date +%+4Y%m%d-%H%M) --all
}
is_branch_present () {
local branch="$1"
git rev-parse "$branch" >/dev/null 2>&1
}
ensure_remote_tracked_locally () {
# Return true (0) if managed to ensure the remote is tracked locally
local remote="$1"
local upstream="$2"
#
if ! is_branch_present "$upstream"; then
info "$upstream is not tracked locally; fetching from $remote ..."
ggf "$remote" || return 1
else
info "Already tracking $upstream"
fi
return 0
}
fetch_and_switch_upstream_to () {
# Return true (0) if the switch was made
local remote="$1"
local upstream="$remote/$remote_branch"
#
ensure_remote_tracked_locally "$remote" "$upstream" || return 1
ggsup "$upstream"
}
configure_upstream () {
# Return true (0) if the upstream was configured
local remote="$1"
#
if [ -z `git config "remote.$remote.url"` ]; then
mr remotes
else
echo "$remote remote already set up"
return 0
fi
if [ -z `git config "remote.$remote.url"` ]; then
error "'mr remotes' failed to configure $remote remote; cannot proceed."
return 1
fi
return 0
}
configure_upstream_and_switch_to () {
# Return true (0) iff the switch was made
local remote="$1"
#
configure_upstream "$remote" || return 1
fetch_and_switch_upstream_to "$remote"
}
is_remote_in_use () {
# Return true (0) iff the given remote is "in use", i.e.
# there is a local branch tracking it.
remote="$1"
git config --list | grep -q "^branch\..*\.remote=$remote\$"
}
remove_remote () {
remote="$1"
if git remote rm $remote; then
info "Removed $remote remote"
return 0
else
error "Failed to remove $remote remote."
return 1
fi
}
clean_unused_remote_matching_URL () {
# Removes any unused remote with a URL matching the given extended regexp.
# Returns true (0) iff a remote was successfully removed, or there
# was no need to remove a remote.
remote="$1"
eregexp="$2"
if is_remote_in_use "$remote"; then
using_branches=$( perl -lne "/^branch\.(.*)\.remote=$remote/ && print \$1" )
warning "$remote is still in use by the following local branches:"
warning "$using_branches"
return 1
fi
remote_url=`git config "remote.$remote.url"`
if [ -z "$remote_url" ]; then
return 0 # wasn't there in the first place
fi
if ! echo "$remote_url" | grep -Eq "$eregexp"; then
info "Not removing $remote remote with URL $remote_url (!~ /$eregexp/)"
return 0
fi
remove_remote $remote
}