forked from skwp/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgit-remotes
More file actions
127 lines (113 loc) · 2.97 KB
/
git-remotes
File metadata and controls
127 lines (113 loc) · 2.97 KB
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
# -*- mode: sh -*-
# Utilities for ensuring that a git repository has the right remotes
# set.
#
# Sample usage from within an .mrconfig file:
#
# [repo]
# checkout = git clone ...
# remotes = git_add_remotes "
# remote1 git@github.com:$MY_GITHUB_USERNAME/$MR_NAME.git
# mydomain ssh://foo@REMOTE.com/path/to/git/repo
# ...
# "
#
# or even
#
# [repo]
# checkout = git clone ...
# fixups = git_add_remotes "..."
git_add_new_remote () {
remote="$1"
url="$2"
if git remote add "$remote" "$url"; then
echo "+ Added remote $remote -> $url"
return 0
else
error "! Failed to add remote $remote"
return 1
fi
}
git_set_remote () {
remote="$1"
url="$2"
existing_url="$3"
if git remote set-url "$remote" "$url"; then
info "+ Repointed remote $remote -> $url (was $existing_url)"
return 0
else
error "! Failed to repoint remote $remote"
return 1
fi
}
# git_add_remote REMOTE-NAME URL
git_add_remote () {
remote="$1"
url="$2"
existing_url=$( git config "remote.$remote.url" ) || true
if [ -n "$existing_url" ]; then
if [ "$url" = "$existing_url" ]; then
#info ". Remote $remote already points to $url"
return
fi
if [ -n "$MR_OVERWRITE_REMOTES" ]; then
git_set_remote "$remote" "$url" "$existing_url"
return
else
warning "! Remote $remote already points to $existing_url not $url"
warning "! Use MR_OVERWRITE_REMOTES=y to rewrite URL."
return
fi
fi
git_add_new_remote "$remote" "$url"
}
# git_add_remotes REMOTES
#
# REMOTES is a multi-line string where each line is
#
# REMOTE URL [EXCEPTION]
#
# Invokes git_add_remote for each line,
# substituting 'REMOTE' for REMOTE in URL,
# except for lines where localhost nickname equals EXCEPTION
git_add_remotes () {
cd "$MR_REPO"
echo "$*" | while read remote url; do
if [ -z "$remote$url" ]; then
# presumably got a blank line
continue
fi
url=$( echo "$url" | sed "s/REMOTE/$remote/g" )
if [ -z "$exception" ]; then
exception="$remote"
fi
git_add_remote "$remote" "$url"
done
}
# git_rm_remote REMOTE-NAME
git_rm_remote () {
remote="$1"
if git config remote."$remote".url >/dev/null; then
if git remote remove "$remote"; then
echo "- Removed remote $remote"
else
echo >&2 "WARNING: Failed to remove remote $remote"
fi
else
echo ". $remote remote was already removed"
fi
}
# git_rm_remotes REMOTES
#
# REMOTES is a multi-line string with one remote per line.
# Invokes git_rm_remote for each line.
git_rm_remotes () {
cd "$MR_REPO"
echo "$*" | while read remote; do
if [ -z "$remote$url" ]; then
# presumably got a blank line
continue
fi
git_rm_remote "$remote"
done
}