You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gitnonymous 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env bash
  2. # See the README for instructions setting up.
  3. # ./make-anonymous-key KEYNAME
  4. # figure out which directory we are stored in
  5. # https://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in
  6. _GITNONYMOUS_SRC="${BASH_SOURCE[0]}"
  7. # resolve $_GITNONYMOUS_SRC until the file is no longer a symlink
  8. while [ -h "$_GITNONYMOUS_SRC" ]; do
  9. dir="$( cd -P "$( dirname "$_GITNONYMOUS_SRC" )" && pwd )"
  10. _GITNONYMOUS_SRC="$(readlink "$_GITNONYMOUS_SRC")"
  11. # if $_GITNONYMOUS_SRC was a relative symlink, we need to resolve it
  12. # relative to the path where the symlink file was located
  13. [[ $_GITNONYMOUS_SRC != /* ]] && _GITNONYMOUS_SRC="$DIR/$_GITNONYMOUS_SRC"
  14. done
  15. dir="$( cd -P "$( dirname "$_GITNONYMOUS_SRC" )" && pwd )"
  16. unset _GITNONYMOUS_SRC
  17. gitnonymous_usage () {
  18. if [ "$1" != "" ]
  19. then
  20. gitnonymous_error "Don't call this script directly. 'Source' it instead."
  21. fi
  22. echo "Usage:"
  23. echo ". ${BASH_SOURCE[0]} KEYNAME"
  24. }
  25. # function to throw error messages
  26. gitnonymous_error () {
  27. >&2 echo $@
  28. }
  29. # when we are missing a config file call this
  30. gitnonymous_error_missing () {
  31. gitnonymous_error $@
  32. gitnonymous_error "To create it run: ${dir}/gitnonymous-setup ${keyname}"
  33. }
  34. # Make sure we were called with 'source' instead of directly
  35. if [[ "${BASH_SOURCE[0]}" != "$0" ]]
  36. then
  37. # check for the keyname parameter
  38. if [ "$1" = "" ]
  39. then
  40. gitnonymous_usage
  41. return 1
  42. else
  43. # set the key and the config file
  44. keyname="${1}"
  45. rootdir="${HOME}/.gitnonymous-${keyname}"
  46. configfile="${rootdir}/config"
  47. sshdir="${rootdir}/ssh"
  48. oldenvfile="${rootdir}/previous-environment"
  49. if [ "$_GITNONYMOUS" != "" ]
  50. # toggle everything off again
  51. then
  52. echo "Unsetting gitnonymous environment ${keyname}"
  53. # kill our special ssh agent
  54. ssh-agent -k
  55. # restore the command prompt
  56. export PS1="${_GITNONYMOUS_OLDPS1}"
  57. unset GIT_COMMITTER_EMAIL
  58. unset GIT_COMMITTER_NAME
  59. unset GIT_AUTHOR_EMAIL
  60. unset GIT_AUTHOR_NAME
  61. unset GIT_SSH
  62. unset GIT_PROXY_COMMAND
  63. unset ALL_PROXY
  64. # restore any GIT_ environment variables set before
  65. . "${oldenvfile}"
  66. # unset our environment variables we set
  67. unset _GITNONYMOUS_OLDPS1
  68. unset _GITNONYMOUS
  69. echo "Done unsetting ${keyname}."
  70. else
  71. # add our custom local SSH key (make the key with ./make-anonymous-key)
  72. if [ -f ${sshdir}/id_rsa ]
  73. then
  74. # start a new SSH agent just for this session
  75. eval $(ssh-agent -a "${sshdir}/.ssh-agent")
  76. # add the key
  77. ssh-add ${sshdir}/id_rsa
  78. else
  79. gitnonymous_error_missing "SSH config in ${sshdir} not found!"
  80. return 3
  81. fi
  82. # load the user's anonymous GIT settings and environment
  83. if [ -f "${configfile}" ]
  84. then
  85. # save the old GIT values
  86. export | grep -E "GIT_|ALL_PROXY" > ${oldenvfile}
  87. # source the config file
  88. . "${configfile}"
  89. else
  90. gitnonymous_error_missing "GIT config in ${configfile} not found!"
  91. return 4
  92. fi
  93. # trap exit of this shell and kill ssh-agent
  94. trap 'ssh-agent -k' EXIT
  95. # tell GIT to use our customised anonymous SSH wrapper
  96. export GIT_SSH="${rootdir}/git-ssh-wrap"
  97. # tell GIT to use the tor proxy for all other traffic
  98. export GIT_PROXY_COMMAND="${dir}/git-proxy-command"
  99. export ALL_PROXY="socks5://127.0.0.1:9050"
  100. # make it obvious we're in a special environment
  101. _GITNONYMOUS_OLDPS1="${PS1}"
  102. export PS1="${keyname}⚔ ${PS1}"
  103. export _GITNONYMOUS="${keyname}"
  104. fi
  105. fi
  106. else
  107. gitnonymous_usage true
  108. exit 2
  109. fi