setup-stage 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 topic stage for pushing changes.
  18. # Project configuration instructions:
  19. #
  20. # - Run a Topic Stage repository next to the main project repository.
  21. #
  22. # - Populate adjacent "config" file with:
  23. # stage.url = Topic Stage repository URL
  24. # stage.pushurl = Topic Stage push URL if not "$url"
  25. egrep-q() {
  26. egrep "$@" >/dev/null 2>/dev/null
  27. }
  28. die() {
  29. echo 1>&2 "$@" ; exit 1
  30. }
  31. # Make sure we are inside the repository.
  32. cd "${BASH_SOURCE%/*}" &&
  33. # Load the project configuration.
  34. fetchurl_=$(git config -f config --get stage.url) &&
  35. pushurl_=$(git config -f config --get stage.pushurl || echo "$fetchurl_") &&
  36. remote=$(git config -f config --get stage.remote || echo 'stage') ||
  37. die 'This project is not configured to use a topic stage.'
  38. # Get current stage push URL.
  39. pushurl=$(git config --get remote."$remote".pushurl ||
  40. git config --get remote."$remote".url || echo '') &&
  41. # Tell user about current configuration.
  42. if test -n "$pushurl"; then
  43. echo 'Remote "'"$remote"'" is currently configured to push to
  44. '"$pushurl"'
  45. ' &&
  46. read -ep 'Reconfigure Topic Stage? [y/N]: ' ans &&
  47. if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
  48. setup=1
  49. else
  50. setup=''
  51. fi
  52. else
  53. setup=1
  54. fi
  55. # Perform setup if necessary.
  56. if test -n "$setup"; then
  57. echo 'Setting up the topic stage...' &&
  58. fetchurl="${fetchurl_}" &&
  59. if test -z "$pushurl"; then
  60. git remote add "$remote" "$fetchurl"
  61. else
  62. git config remote."$remote".url "$fetchurl"
  63. fi &&
  64. pushurl="${pushurl_}" &&
  65. if test "$pushurl" != "$fetchurl"; then
  66. git config remote."$remote".pushurl "$pushurl"
  67. fi &&
  68. echo 'Remote "'"$remote"'" is now configured to push to
  69. '"$pushurl"'
  70. '
  71. fi || die 'Could not configure the topic stage remote.'