setup-gerrit 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #!/usr/bin/env bash
  2. #=============================================================================
  3. # Copyright 2010-2012 Kitware, Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #=============================================================================
  17. # Run this script to set up the local Git repository to push to
  18. # a Gerrit Code Review instance for this project.
  19. # Project configuration instructions:
  20. #
  21. # - Run a Gerrit Code Review server
  22. #
  23. # - Populate adjacent "config" file with:
  24. # gerrit.site = Top Gerrit URL (not project-specific)
  25. # gerrit.project = Name of project in Gerrit
  26. # gerrit.pushurl = Review site push URL with "$username" placeholder
  27. # gerrit.remote = Gerrit remote name, if not "gerrit"
  28. # gerrit.url = Gerrit project URL, if not "$site/p/$project"
  29. # optionally with "$username" placeholder
  30. die() {
  31. echo 1>&2 "$@" ; exit 1
  32. }
  33. # Make sure we are inside the repository.
  34. cd "${BASH_SOURCE%/*}" &&
  35. # Load the project configuration.
  36. site=$(git config -f config --get gerrit.site) &&
  37. project=$(git config -f config --get gerrit.project) &&
  38. remote=$(git config -f config --get gerrit.remote ||
  39. echo "gerrit") &&
  40. fetchurl_=$(git config -f config --get gerrit.url ||
  41. echo "$site/p/$project") &&
  42. pushurl_=$(git config -f config --get gerrit.pushurl ||
  43. git config -f config --get gerrit.url) ||
  44. die 'This project is not configured to use Gerrit.'
  45. # Get current gerrit push URL.
  46. pushurl=$(git config --get remote."$remote".pushurl ||
  47. git config --get remote."$remote".url || echo '') &&
  48. # Tell user about current configuration.
  49. if test -n "$pushurl"; then
  50. echo 'Remote "'"$remote"'" is currently configured to push to
  51. '"$pushurl"'
  52. ' &&
  53. read -ep 'Reconfigure Gerrit? [y/N]: ' ans &&
  54. if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
  55. setup=1
  56. else
  57. setup=''
  58. fi
  59. else
  60. echo 'Remote "'"$remote"'" is not yet configured.
  61. '"$project"' changes must be pushed to our Gerrit Code Review site:
  62. '"$site/p/$project"'
  63. Register a Gerrit account and select a username (used below).
  64. You will need an OpenID:
  65. http://openid.net/get-an-openid/
  66. ' &&
  67. read -ep 'Configure Gerrit? [Y/n]: ' ans &&
  68. if [ "$ans" == "n" ] || [ "$ans" == "N" ]; then
  69. exit 0
  70. else
  71. setup=1
  72. fi
  73. fi &&
  74. # Perform setup if necessary.
  75. if test -n "$setup"; then
  76. echo 'Sign-in to Gerrit to get/set your username at
  77. '"$site"'/#/settings
  78. Add your SSH public keys at
  79. '"$site"'/#/settings/ssh-keys
  80. ' &&
  81. read -ep "Gerrit username? [$USER]: " gu &&
  82. if test -z "$gu"; then
  83. gu="$USER"
  84. fi &&
  85. fetchurl="${fetchurl_/\$username/$gu}" &&
  86. if test -z "$pushurl"; then
  87. git remote add "$remote" "$fetchurl"
  88. else
  89. git config remote."$remote".url "$fetchurl"
  90. fi &&
  91. pushurl="${pushurl_/\$username/$gu}" &&
  92. if test "$pushurl" != "$fetchurl"; then
  93. git config remote."$remote".pushurl "$pushurl"
  94. fi &&
  95. echo 'Remote "'"$remote"'" is now configured to push to
  96. '"$pushurl"'
  97. '
  98. fi &&
  99. # Optionally test Gerrit access.
  100. if test -n "$pushurl"; then
  101. read -ep 'Test access to Gerrit (SSH)? [y/N]: ' ans &&
  102. if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
  103. echo -n 'Testing Gerrit access by SSH...'
  104. if git ls-remote --heads "$pushurl" >/dev/null; then
  105. echo 'passed.'
  106. else
  107. echo 'failed.' &&
  108. die 'Could not access Gerrit. Add your SSH public keys at
  109. '"$site"'/#/settings/ssh-keys
  110. '
  111. fi
  112. fi
  113. fi &&
  114. # Set up GerritId hook.
  115. hook=$(git config --get hooks.GerritId || echo '') &&
  116. if test -z "$hook"; then
  117. echo '
  118. Enabling GerritId hook to add a "Change-Id" footer to commit
  119. messages for interaction with Gerrit. Run
  120. git config hooks.GerritId false
  121. to disable this feature (but you will be on your own).' &&
  122. git config hooks.GerritId true
  123. else
  124. echo 'GerritId hook already configured to "'"$hook"'".'
  125. fi