setup-gitlab 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 push to
  18. # a personal fork for this project in GitLab.
  19. # Project configuration instructions:
  20. #
  21. # - Run a GitLab server
  22. #
  23. # - Populate adjacent "config" file with:
  24. # gitlab.protocol = Top GitLab protocol, if not 'https'
  25. # gitlab.host = Top GitLab fully qualified host name
  26. # gitlab.site = Top GitLab URL, if not "<protocol>://<host>"
  27. # gitlab.group-name = Name of group containing project in GitLab
  28. # gitlab.group-path = Path of group containing project in GitLab
  29. # gitlab.project-name = Name of project within GitLab group
  30. # gitlab.project-path = Path of project within GitLab group
  31. # gitlab.url = GitLab push URL with "$username" placeholder,
  32. # if not "<site>/$username/<project-path>.git"
  33. # gitlab.pushurl = GitLab push URL with "$username" placeholder,
  34. # if not "git@<host>:$username/<project-path>.git"
  35. # gitlab.remote = GitLab remote name, if not "gitlab"
  36. die() {
  37. echo 1>&2 "$@" ; exit 1
  38. }
  39. # Make sure we are inside the repository.
  40. cd "${BASH_SOURCE%/*}" &&
  41. # Load the project configuration.
  42. protocol=$(git config -f config --get gitlab.protocol ||
  43. echo "https") &&
  44. host=$(git config -f config --get gitlab.host) &&
  45. site=$(git config -f config --get gitlab.site ||
  46. echo "$protocol://$host") &&
  47. group_path=$(git config -f config --get gitlab.group-path) &&
  48. group_name=$(git config -f config --get gitlab.group-name) &&
  49. project_name=$(git config -f config --get gitlab.project-name) &&
  50. project_path=$(git config -f config --get gitlab.project-path) &&
  51. pushurl_=$(git config -f config --get gitlab.pushurl ||
  52. echo "git@$host:\$username/$project_path.git") &&
  53. remote=$(git config -f config --get gitlab.remote ||
  54. echo "gitlab") &&
  55. fetchurl_=$(git config -f config --get gitlab.url ||
  56. echo "$site/\$username/$project_path.git") ||
  57. die 'This project is not configured to use GitLab.'
  58. # Get current gitlab push URL.
  59. pushurl=$(git config --get remote."$remote".pushurl ||
  60. git config --get remote."$remote".url || echo '') &&
  61. # Tell user about current configuration.
  62. if test -n "$pushurl"; then
  63. echo 'Remote "'"$remote"'" is currently configured to push to
  64. '"$pushurl"'
  65. ' &&
  66. read -ep 'Reconfigure GitLab? [y/N]: ' ans &&
  67. if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
  68. setup=1
  69. else
  70. setup=''
  71. fi
  72. else
  73. echo 'Remote "'"$remote"'" is not yet configured.
  74. ' &&
  75. read -ep 'Configure GitLab to contribute to '"$project_name"'? [Y/n]: ' ans &&
  76. if [ "$ans" == "n" ] || [ "$ans" == "N" ]; then
  77. exit 0
  78. else
  79. setup=1
  80. fi
  81. fi &&
  82. setup_instructions='Add your SSH public keys at
  83. '"$site"'/profile/keys
  84. Then visit the main repository at:
  85. '"$site/$group_path/$project_path"'
  86. and use the Fork button in the upper right.
  87. '
  88. # Perform setup if necessary.
  89. if test -n "$setup"; then
  90. echo 'Sign-in to GitLab to get/set your username at
  91. '"$site/profile/account"'
  92. '"$setup_instructions" &&
  93. read -ep "GitLab username? [$USER]: " gu &&
  94. if test -z "$gu"; then
  95. gu="$USER"
  96. fi &&
  97. fetchurl="${fetchurl_/\$username/$gu}" &&
  98. if test -z "$pushurl"; then
  99. git remote add "$remote" "$fetchurl"
  100. else
  101. git config remote."$remote".url "$fetchurl"
  102. fi &&
  103. pushurl="${pushurl_/\$username/$gu}" &&
  104. git config remote."$remote".pushurl "$pushurl" &&
  105. echo 'Remote "'"$remote"'" is now configured to push to
  106. '"$pushurl"'
  107. '
  108. fi &&
  109. # Optionally test GitLab access.
  110. if test -n "$pushurl"; then
  111. read -ep 'Test access to GitLab (SSH)? [y/N]: ' ans &&
  112. if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
  113. echo -n 'Testing GitLab access by SSH...'
  114. if git ls-remote --heads "$pushurl" >/dev/null; then
  115. echo 'passed.'
  116. else
  117. echo 'failed.' &&
  118. die 'Could not access your GitLab fork of this project.
  119. '"$setup_instructions"
  120. fi
  121. fi
  122. fi