-
Notifications
You must be signed in to change notification settings - Fork 16
/
gitnonymous
executable file
·140 lines (127 loc) · 4.31 KB
/
gitnonymous
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
#!/usr/bin/env bash
# See the README for instructions setting up.
# ./make-anonymous-key KEYNAME
# figure out which directory we are stored in
# https://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
_GITNONYMOUS_SRC="${BASH_SOURCE[0]}"
# resolve $_GITNONYMOUS_SRC until the file is no longer a symlink
while [ -h "$_GITNONYMOUS_SRC" ]; do
dir="$( cd -P "$( dirname "$_GITNONYMOUS_SRC" )" && pwd )"
_GITNONYMOUS_SRC="$(readlink "$_GITNONYMOUS_SRC")"
# if $_GITNONYMOUS_SRC was a relative symlink, we need to resolve it
# relative to the path where the symlink file was located
[[ $_GITNONYMOUS_SRC != /* ]] && _GITNONYMOUS_SRC="$DIR/$_GITNONYMOUS_SRC"
done
dir="$( cd -P "$( dirname "$_GITNONYMOUS_SRC" )" && pwd )"
unset _GITNONYMOUS_SRC
gitnonymous_usage () {
if [ "$1" != "" ]
then
gitnonymous_error "Don't call this script directly. 'Source' it instead."
fi
echo "Usage:"
echo ". ${BASH_SOURCE[0]} KEYNAME"
}
# function to throw error messages
gitnonymous_error () {
>&2 echo $@
}
# when we are missing a config file call this
gitnonymous_error_missing () {
gitnonymous_error $@
gitnonymous_error "To create it run: ${dir}/gitnonymous-setup ${keyname}"
}
# Make sure we were called with 'source' instead of directly
if [[ "${BASH_SOURCE[0]}" != "$0" ]]
then
# check for the keyname parameter
if [ "$1" = "" ]
then
gitnonymous_usage
return 1
else
# set the key and the config file
keyname="${1}"
rootdir="${HOME}/.gitnonymous-${keyname}"
configfile="${rootdir}/config"
sshdir="${rootdir}/ssh"
oldenvfile="${rootdir}/previous-environment"
sshagentsocket="${sshdir}/.ssh-agent-$$"
if [ "$_GITNONYMOUS" != "" ]
# toggle everything off again
then
echo "Unsetting gitnonymous environment ${keyname}"
# kill our special ssh agent
ssh-agent -k
# restore the command prompt
export PS1="${_GITNONYMOUS_OLDPS1}"
unset GIT_COMMITTER_EMAIL
unset GIT_COMMITTER_NAME
unset GIT_AUTHOR_EMAIL
unset GIT_AUTHOR_NAME
unset GIT_SSH
unset GIT_PROXY_COMMAND
unset ALL_PROXY
unset TZ
# restore any GIT_ environment variables set before
. "${oldenvfile}"
# unset our environment variables we set
unset _GITNONYMOUS_OLDPS1
unset _GITNONYMOUS
unalias "gitnonymous-exit"
echo "Done unsetting ${keyname}."
else
# add our custom local SSH key (make the key with ./make-anonymous-key)
if [ -f ${sshdir}/id_rsa -o -f ${sshdir}/identity ]
then
# if a stale socket already exists then throw an error
if [ -f "${sshagentsocket}" ]
then
gitnonymous_error "Stale SSH agent socket '${sshagentsocket}' exists."
gitnonymous_error "You can probably delete it safely and try again."
return 2
fi
# start a new SSH agent just for this session
eval $(ssh-agent -a "${sshagentsocket}")
# add the key
if [ -f ${sshdir}/id_rsa ]
then
ssh-add ${sshdir}/id_rsa
else
ssh-add ${sshdir}/identity
fi
else
gitnonymous_error_missing "SSH config in ${sshdir} not found!"
return 3
fi
# load the user's anonymous GIT settings and environment
if [ -f "${configfile}" ]
then
# save the old GIT values
export | grep -E "GIT_|ALL_PROXY|TZ" > ${oldenvfile}
# source the config file
. "${configfile}"
else
gitnonymous_error_missing "GIT config in ${configfile} not found!"
return 4
fi
# trap exit of this shell and kill ssh-agent
trap 'ssh-agent -k' EXIT
# tell GIT to use our customised anonymous SSH wrapper
export GIT_SSH="${rootdir}/git-ssh-wrap"
# tell GIT to use the tor proxy for all other traffic
export GIT_PROXY_COMMAND="${dir}/git-proxy-command"
export ALL_PROXY="socks5:https://127.0.0.1:9050"
# make it obvious we're in a special environment
_GITNONYMOUS_OLDPS1="${PS1}"
export PS1="${keyname}⚔ ${PS1}"
export _GITNONYMOUS="${keyname}"
echo "Run 'gitnonymous-exit' to exit this identity."
# alias exit command
alias "gitnonymous-exit"=". ${BASH_SOURCE[0]} ${keyname}"
fi
fi
else
gitnonymous_usage true
exit 2
fi