forked from gajdaw-git-recipes/aliases
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-swapid
executable file
·212 lines (189 loc) · 6.57 KB
/
git-swapid
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/sh
#
# Script to swap git/ssh identities.
# It affects two files:
# ~/.gitconfig
# ~/.ssh/config
#
# You need at least two private RSA keys for your identity.
# I assume that your keys are stored in:
#
# /home/john/.ssh/john
# /home/john/.ssh/peter
#
#
# All your identities are stored within .gitconfig.
#
# Add your adentities to your ~/.gitconfig file like in the following listing.
# Every login has to be exactly the same as its key given in [identity "..."].
# If you define identity [identity "foo"] its login has to be "foo".
#
# # extract from ~/.gitconfig
# [identity "john"]
# name = John Doe
# email = [email protected]
# key = /home/john/.ssh/john
# login = john
#
# [identity "peter"]
# name = Peter Nobody
# email = [email protected]
# key = /home/john/.ssh/peter
# login = peter
#
#
# Swapid entry is appended at the end of .ssh/config.
# It will be the last effort entry.
# If you have .ssh/config entries like in:
#
# # extract from ~/config
# Host some-label
# HostName example.com
# User john
# IdentityFile ~/.ssh/john_id_rsa
#
# Host lorem
# HostName ipsum.com
# User sarah
# IdentityFile ~/.ssh/sarah_id_rsa
#
# Host *
# IdentityFile ~/.ssh/another_id_rsa
#
# Then:
# ssh -T some-label --> [email protected] using john_id_rsa
# ssh -T lorem --> [email protected] using sarah_id_rsa
# ssh -T me@ble --> me@ble using another_id_rsa
#
# (C) 2013 gajdaw
#
if [ "$#" -gt 1 ]; then
echo "***** ERROR: Too many arguments!"
exit
fi;
if [ "$#" -eq 1 ]; then
MYID=$1
else
# set default identity if possible
MYID=`git config --global user.login`
if [ ! $MYID ]; then
echo "***** ERROR: Global user.login not set!"
echo "***** ERROR: git swapid called without parameters needs default identity!"
exit
fi;
fi;
if [ ! $MYID ]; then
echo "***** ERROR: The identity cannot be a null string!"
exit
fi;
USER_NAME=`git config --global user.name`
USER_EMAIL=`git config --global user.email`
USER_LOGIN=`git config --global user.login`
MYID_NAME=`git config --global identity.$MYID.name`
MYID_EMAIL=`git config --global identity.$MYID.email`
MYID_KEY=`git config --global identity.$MYID.key`
MYID_LOGIN=`git config --global identity.$MYID.login`
echo "Configuration read from ~/.gitconfig"
echo " user.name = $USER_NAME"
echo " user.email = $USER_EMAIL"
echo " user.login = $USER_LOGIN"
echo " "
echo " identity.$MYID.name = $MYID_NAME"
echo " identity.$MYID.email = $MYID_EMAIL"
echo " identity.$MYID.key = $MYID_KEY"
echo " identity.$MYID.login = $MYID_LOGIN"
echo " "
echo "Configuration read from ~/.ssh/config"
grep -E "^ IdentityFile [^ ;]*$" ~/.ssh/config
echo " "
#if [ "$MYID_NAME $MYID_EMAIL $MYID_KEY $MYID_LOGIN" == " " ]; then
# echo " * Initialazing $MYID identity"
# git config --global identity.$MYID.name "John Doe"
# git config --global identity.$MYID.email [email protected]
# git config --global identity.$MYID.key ~/.ssh/john_id_rsa
# git config --global identity.$MYID.login $MYID
# MYID_NAME=`git config --global identity.$MYID.name`
# MYID_EMAIL=`git config --global identity.$MYID.email`
# MYID_KEY=`git config --global identity.$MYID.key`
# MYID_LOGIN=`git config --global identity.$MYID.login`
#fi;
# We expect all four not null global git config settings for this id
if [ ! "$MYID_NAME" ]; then
echo "***** ERROR: Problem with git global configuration: identity.$MYID.name"
echo " "
echo "You can set it with:"
echo " \$ git config --global identity.$MYID.name \"John Doe\""
exit;
fi
if [ ! $MYID_EMAIL ]; then
echo "***** ERROR: Problem with git global configuration: identity.$MYID.email"
echo " "
echo "You can set it with:"
echo " \$ git config --global identity.$MYID.email [email protected]"
exit;
fi
if [ ! $MYID_KEY ]; then
echo "***** ERROR: Problem with git global configuration: identity.$MYID.key"
echo " "
echo "You can set it with:"
echo " \$ git config --global identity.$MYID.key ~/.ssh/john_id_rsa"
exit;
fi
if [ ! $MYID_LOGIN ]; then
echo "***** ERROR: Problem with git global configuration: identity.$MYID.login"
echo " "
echo "You can set it with:"
echo " \$ git config --global identity.$MYID.login $MYID"
exit;
fi
if [ ! -f $MYID_KEY ]; then
echo "***** ERROR: The private key does not exist!"
echo "***** ERROR: Filename: $MYID_KEY"
echo ""
fi
if [ ! $MYID_LOGIN == $MYID ]; then
git config --global identity.$MYID.login $MYID
MYID_LOGIN=$MYID
echo " * Resetting LOGIN in ~/.gitconfig for $MYID"
fi
# without parameters only print current settings
if [ "$#" -eq 0 ]; then
exit
fi;
# Everything is fine. Swap the identity
# swap global git config
git config --global user.name "$MYID_NAME"
git config --global user.email $MYID_EMAIL
git config --global user.login $MYID_LOGIN
# swap private key in .ssh/config
SSH_CONFIG_LINE=" IdentityFile $MYID_KEY"
ESCAPED="$(echo "$SSH_CONFIG_LINE" | sed -e 's/[\/&]/\\&/g')"
# does the .ssh/config exists?
if [ ! -f ~/.ssh/config ]; then
echo " * Creating ~/.ssh/config"
echo "#swapid" > ~/.ssh/config
echo "Host *" >> ~/.ssh/config
echo "$SSH_CONFIG_LINE" >> ~/.ssh/config
else
# Does ~/.ssh/config contain our IdentityFile?
WASIUSED=`grep -E "^ IdentityFile [^ ;]*$" ~/.ssh/config | wc -l`
if [ $WASIUSED -eq 0 ]; then
# no, there isn't IdentityFile entry matching swapid RE in .ssh/config
echo "***** ERROR: Was .ssh/config used by swapid before?"
echo "***** ERROR: Check .ssh/config - look for #IdentityFile"
# Is there a commented IdentityFile line?
WASIUSED_COMMENTED=`grep -E "^# IdentityFile [^ ;]*$" ~/.ssh/config | wc -l`
if [ $WASIUSED_COMMENTED -eq 0 ]; then
echo "#swapid" >> ~/.ssh/config
echo "Host *" >> ~/.ssh/config
echo "$SSH_CONFIG_LINE" >> ~/.ssh/config
else
# modify commented IdentityFile line with new id
sed -i "s/^# IdentityFile [^ ;]*$/#$ESCAPED/g" ~/.ssh/config
fi;
else
# yes, there is IdentityFile entry matching RE in .ssh/config
echo " * Modifying ~/.ssh/config"
sed -i "s/^ IdentityFile [^ ;]*$/$ESCAPED/g" ~/.ssh/config
fi;
fi;