Merge topic 'push-with-submodules' into setup
[kiteware-gitsetup.git] / setup-gitlab
blob418a4eeac505cbd7bef8304fce1488205e78a026
1 #!/usr/bin/env bash
2 #=============================================================================
3 # Copyright 2010-2015 Kitware, Inc.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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 #=============================================================================
18 # Run this script to set up the local Git repository to push to
19 # a personal fork for this project in GitLab.
21 # Project configuration instructions:
23 # - Run a GitLab server
25 # - Populate adjacent "config" file with:
26 # gitlab.protocol = Top GitLab protocol, if not 'https'
27 # gitlab.host = Top GitLab fully qualified host name
28 # gitlab.site = Top GitLab URL, if not "<protocol>://<host>"
29 # gitlab.group-name = Name of group containing project in GitLab
30 # gitlab.group-path = Path of group containing project in GitLab
31 # gitlab.project-name = Name of project within GitLab group
32 # gitlab.project-path = Path of project within GitLab group
33 # gitlab.url = GitLab push URL with "$username" placeholder,
34 # if not "<site>/$username/<project-path>.git"
35 # gitlab.pushurl = GitLab push URL with "$username" placeholder,
36 # if not "git@<host>:$username/<project-path>.git"
37 # gitlab.remote = GitLab remote name, if not "gitlab"
39 die() {
40 echo 1>&2 "$@" ; exit 1
43 # Make sure we are inside the repository.
44 cd "${BASH_SOURCE%/*}" &&
46 # Load the project configuration.
47 protocol=$(git config -f config --get gitlab.protocol ||
48 echo "https") &&
49 userpart=$(test "$protocol" = "ssh" && echo "git@" || echo '') &&
50 host=$(git config -f config --get gitlab.host) &&
51 site=$(git config -f config --get gitlab.site ||
52 echo "$protocol://$userpart$host") &&
53 group_path=$(git config -f config --get gitlab.group-path) &&
54 group_name=$(git config -f config --get gitlab.group-name) &&
55 project_name=$(git config -f config --get gitlab.project-name) &&
56 project_path=$(git config -f config --get gitlab.project-path) &&
57 pushurl_=$(git config -f config --get gitlab.pushurl ||
58 echo "git@$host:\$username/$project_path.git") &&
59 remote=$(git config -f config --get gitlab.remote ||
60 echo "gitlab") &&
61 fetchurl_=$(git config -f config --get gitlab.url ||
62 echo "$site/\$username/$project_path.git") ||
63 die 'This project is not configured to use GitLab.'
65 # Get current gitlab push URL.
66 pushurl=$(git remote get-url --push "$remote" ||
67 echo '') &&
69 # Tell user about current configuration.
70 if test -n "$pushurl"; then
71 echo 'Remote "'"$remote"'" is currently configured to push to
73 '"$pushurl"'
74 ' &&
75 read -ep 'Reconfigure GitLab? [y/N]: ' ans &&
76 if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
77 setup=1
78 else
79 setup=''
81 else
82 echo 'Remote "'"$remote"'" is not yet configured.
83 ' &&
84 read -ep 'Configure GitLab to contribute to '"$project_name"'? [Y/n]: ' ans &&
85 if [ "$ans" == "n" ] || [ "$ans" == "N" ]; then
86 exit 0
87 else
88 setup=1
90 fi &&
92 setup_instructions='Add your SSH public keys at
94 '"$site"'/profile/keys
96 Then visit the main repository at:
98 '"$site/$group_path/$project_path"'
100 and use the Fork button in the upper right.
103 # Perform setup if necessary.
104 if test -n "$setup"; then
105 echo 'Sign-in to GitLab to get/set your username at
107 '"$site/profile/account"'
109 '"$setup_instructions" &&
110 read -ep "GitLab username? [$USER]: " gu &&
111 if test -z "$gu"; then
112 gu="$USER"
113 fi &&
114 fetchurl="${fetchurl_/\$username/$gu}" &&
115 if test -z "$pushurl"; then
116 git remote add "$remote" "$fetchurl"
117 else
118 git config remote."$remote".url "$fetchurl"
119 fi &&
120 pushurl="${pushurl_/\$username/$gu}" &&
121 git config remote."$remote".pushurl "$pushurl" &&
122 echo 'Remote "'"$remote"'" is now configured to push to
124 '"$pushurl"'
126 fi &&
128 # Optionally test GitLab access.
129 if test -n "$pushurl"; then
130 read -ep 'Test access to GitLab (SSH)? [y/N]: ' ans &&
131 if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then
132 echo -n 'Testing GitLab access by SSH...'
133 if git ls-remote --heads "$pushurl" >/dev/null; then
134 echo 'passed.'
135 else
136 echo 'failed.' &&
137 die 'Could not access your GitLab fork of this project.
138 '"$setup_instructions"