setup-upstream 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env bash
  2. #=============================================================================
  3. # Copyright 2010-2015 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 use the
  18. # preferred upstream repository URLs.
  19. # Project configuration instructions:
  20. #
  21. # - Populate adjacent "config" file with:
  22. # upstream.url = Preferred fetch url for upstream remote
  23. # upstream.remote = Preferred name for upstream remote, if not "origin"
  24. die() {
  25. echo 1>&2 "$@" ; exit 1
  26. }
  27. # Make sure we are inside the repository.
  28. cd "${BASH_SOURCE%/*}" &&
  29. # Load the project configuration.
  30. url=$(git config -f config --get upstream.url) &&
  31. remote=$(git config -f config --get upstream.remote ||
  32. echo 'origin') ||
  33. die 'This project is not configured to use a preferred upstream repository.'
  34. # Get current upstream URLs.
  35. fetchurl=$(git config --get remote."$remote".url || echo '') &&
  36. pushurl=$(git config --get remote."$remote".pushurl || echo '') &&
  37. if test "$fetchurl" = "$url"; then
  38. echo 'Remote "'"$remote"'" already uses recommended upstream repository.'
  39. exit 0
  40. fi
  41. upstream_recommend='
  42. We recommended configuring the "'"$remote"'" remote to fetch from upstream at
  43. '"$url"'
  44. '
  45. # Tell user about current configuration.
  46. if test -n "$fetchurl"; then
  47. echo 'Remote "'"$remote"'" is currently configured to fetch from
  48. '"$fetchurl"'
  49. ' &&
  50. if test -n "$pushurl"; then
  51. echo 'and push to
  52. '"$pushurl"
  53. fi &&
  54. echo "$upstream_recommend" &&
  55. if test -n "$pushurl"; then
  56. echo 'and to never push to it directly.
  57. '
  58. fi &&
  59. read -ep 'Reconfigure "'"$remote"'" remote as recommended? [y/N]: ' ans &&
  60. if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
  61. setup=1
  62. else
  63. setup=''
  64. fi
  65. else
  66. echo 'Remote "'"$remote"'" is not yet configured.' &&
  67. echo "$upstream_recommend" &&
  68. read -ep 'Configure "'"$remote"'" remote as recommended? [Y/n]: ' ans &&
  69. if [ "$ans" == "n" ] || [ "$ans" == "N" ]; then
  70. exit 0
  71. else
  72. setup=1
  73. fi
  74. fi &&
  75. # Perform setup if necessary.
  76. if test -n "$setup"; then
  77. if test -z "$fetchurl"; then
  78. git remote add "$remote" "$url"
  79. else
  80. git config remote."$remote".url "$url" &&
  81. if old=$(git config --get remote."$remote".pushurl); then
  82. git config --unset remote."$remote".pushurl ||
  83. echo 'Warning: failed to unset remote.'"$remote"'.pushurl'
  84. fi
  85. fi &&
  86. echo 'Remote "'"$remote"'" is now configured to fetch from
  87. '"$url"'
  88. '
  89. fi