setup-gerrit 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. die() {
  30. echo 1>&2 "$@" ; exit 1
  31. }
  32. # Make sure we are inside the repository.
  33. cd "${BASH_SOURCE%/*}" &&
  34. # Load the project configuration.
  35. site=$(git config -f config --get gerrit.site) &&
  36. project=$(git config -f config --get gerrit.project) &&
  37. pushurl_=$(git config -f config --get gerrit.pushurl) &&
  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. die 'This project is not configured to use Gerrit.'
  43. # Get current gerrit push URL.
  44. pushurl=$(git config --get remote."$remote".pushurl ||
  45. git config --get remote."$remote".url || echo '') &&
  46. # Tell user about current configuration.
  47. if test -n "$pushurl"; then
  48. echo 'Remote "'"$remote"'" is currently configured to push to
  49. '"$pushurl"'
  50. ' &&
  51. read -ep 'Reconfigure Gerrit? [y/N]: ' ans &&
  52. if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
  53. setup=1
  54. else
  55. setup=''
  56. fi
  57. else
  58. echo 'Remote "'"$remote"'" is not yet configured.
  59. '"$project"' changes must be pushed to our Gerrit Code Review site:
  60. '"$fetchurl"'
  61. Register a Gerrit account and select a username (used below).
  62. You will need an OpenID:
  63. http://openid.net/get-an-openid/
  64. ' &&
  65. read -ep 'Configure Gerrit? [Y/n]: ' ans &&
  66. if [ "$ans" == "n" ] || [ "$ans" == "N" ]; then
  67. exit 0
  68. else
  69. setup=1
  70. fi
  71. fi &&
  72. # Perform setup if necessary.
  73. if test -n "$setup"; then
  74. echo 'Sign-in to Gerrit to get/set your username at
  75. '"$site"'/#/settings
  76. Add your SSH public keys at
  77. '"$site"'/#/settings/ssh-keys
  78. ' &&
  79. read -ep "Gerrit username? [$USER]: " gu &&
  80. if test -z "$gu"; then
  81. gu="$USER"
  82. fi &&
  83. if test -z "$pushurl"; then
  84. git remote add "$remote" "$fetchurl"
  85. else
  86. git config remote."$remote".url "$fetchurl"
  87. fi &&
  88. pushurl="${pushurl_/\$username/$gu}" &&
  89. git config remote."$remote".pushurl "$pushurl" &&
  90. echo 'Remote "'"$remote"'" is now configured to push to
  91. '"$pushurl"'
  92. '
  93. fi &&
  94. # Optionally test Gerrit access.
  95. if test -n "$pushurl"; then
  96. read -ep 'Test access to Gerrit (SSH)? [y/N]: ' ans &&
  97. if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
  98. echo -n 'Testing Gerrit access by SSH...'
  99. if git ls-remote --heads "$pushurl" >/dev/null; then
  100. echo 'passed.'
  101. else
  102. echo 'failed.' &&
  103. die 'Could not access Gerrit. Add your SSH public keys at
  104. '"$site"'/#/settings/ssh-keys
  105. '
  106. fi
  107. fi
  108. fi &&
  109. # Set up GerritId hook.
  110. hook=$(git config --get hooks.GerritId || echo '') &&
  111. if test -z "$hook"; then
  112. echo '
  113. Enabling GerritId hook to add a "Change-Id" footer to commit
  114. messages for interaction with Gerrit. Run
  115. git config hooks.GerritId false
  116. to disable this feature (but you will be on your own).' &&
  117. git config hooks.GerritId true
  118. else
  119. echo 'GerritId hook already configured to "'"$hook"'".'
  120. fi