| 1 |
#!/usr/bin/env bash |
| 2 |
#============================================================================= |
| 3 |
# Copyright 2018 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 |
# https://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 |
|
| 18 |
# Run this script to set up the local Git repository to push to |
| 19 |
# a personal fork for this project in GitHub. |
| 20 |
|
| 21 |
# Project configuration instructions: |
| 22 |
# |
| 23 |
# - Populate adjacent "config" file with: |
| 24 |
# github.protocol = Top GitHub protocol, if not 'https' |
| 25 |
# github.host = Top GitHub fully qualified host name, if not github.com |
| 26 |
# github.site = Top GitHub URL, if not "<protocol>://<host>" |
| 27 |
# github.organization-name = Name of organization containing project in GitHub |
| 28 |
# github.repo-name = Name of repository within GitHub organization |
| 29 |
# github.url = GitHub push URL with "$username" placeholder, |
| 30 |
# if not "<site>/$username/<repo-name>.git" |
| 31 |
# github.pushurl = GitHub push URL with "$username" placeholder, |
| 32 |
# if not "git@<host>:$username/<repo-name>.git" |
| 33 |
# github.remote = GitHub remote name, if not "origin" |
| 34 |
# upstream.url = Preferred fetch url for upstream remote |
| 35 |
# upstream.remote = Preferred name for upstream remote |
| 36 |
|
| 37 |
die() { |
| 38 |
echo 1>&2 "$@" ; exit 1 |
| 39 |
} |
| 40 |
|
| 41 |
# Make sure we are inside the repository. |
| 42 |
cd "${BASH_SOURCE%/*}" && |
| 43 |
|
| 44 |
# Load the project configuration. |
| 45 |
protocol=$(git config -f config --get github.protocol || |
| 46 |
echo "https") && |
| 47 |
host=$(git config -f config --get github.host || echo "github.com") && |
| 48 |
site=$(git config -f config --get github.site || |
| 49 |
echo "$protocol://$host") && |
| 50 |
organization_name=$(git config -f config --get github.organization-name) && |
| 51 |
repo_name=$(git config -f config --get github.repo-name) && |
| 52 |
pushurl_=$(git config -f config --get github.pushurl || |
| 53 |
echo "git@$host:\$username/$repo_name.git") && |
| 54 |
remote=$(git config -f config --get github.remote || |
| 55 |
echo "origin") && |
| 56 |
fetchurl_=$(git config -f config --get github.url || |
| 57 |
echo "$site/\$username/$repo_name.git") || |
| 58 |
die 'This project is not configured to use GitHub.' |
| 59 |
|
| 60 |
read -ep "Remote name for your forked repository? ['"$remote"']: " ans && |
| 61 |
if [ -n "$ans" ] && [ ! "$ans" == "$remote" ]; then |
| 62 |
remote=$ans |
| 63 |
fi |
| 64 |
git config github.fork.remote $remote |
| 65 |
|
| 66 |
# Get standard upstream url |
| 67 |
upstream_url=$(git config -f config --get upstream.url) |
| 68 |
upstream_remote=$(git config -f config --get upstream.remote) |
| 69 |
remote_url=$(git config --get remote."$remote".url) |
| 70 |
# Check if the remote was cloned with the upstream URL and recommend removal |
| 71 |
# if so. |
| 72 |
if test "$upstream_url" = "$remote_url"; then |
| 73 |
echo 'Remote "'"$remote"'" uses the repository recommended to use as the "'"$upstream_remote"'" repository.' |
| 74 |
read -ep 'Remove "'"$remote"'" remote and reconfigure as recommended? [y/N]: ' ans && |
| 75 |
if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then |
| 76 |
git remote remove "$remote" |
| 77 |
setup=1 |
| 78 |
else |
| 79 |
setup='' |
| 80 |
fi |
| 81 |
fi |
| 82 |
|
| 83 |
# Get current github push URL. |
| 84 |
pushurl=$(git config --get remote."$remote".pushurl || |
| 85 |
git config --get remote."$remote".url || echo '') && |
| 86 |
|
| 87 |
# Tell user about current configuration. |
| 88 |
if test -n "$pushurl"; then |
| 89 |
echo 'Remote "'"$remote"'" is currently configured to push to |
| 90 |
|
| 91 |
'"$pushurl"' |
| 92 |
' && |
| 93 |
read -ep 'Reconfigure GitHub? [y/N]: ' ans && |
| 94 |
if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then |
| 95 |
setup=1 |
| 96 |
else |
| 97 |
setup='' |
| 98 |
fi |
| 99 |
else |
| 100 |
echo 'Remote "'"$remote"'" is not configured. |
| 101 |
' && |
| 102 |
read -ep 'Configure GitHub to contribute to '"$repo_name"'? [Y/n]: ' ans && |
| 103 |
if [ "$ans" == "n" ] || [ "$ans" == "N" ]; then |
| 104 |
exit 0 |
| 105 |
else |
| 106 |
setup=1 |
| 107 |
fi |
| 108 |
fi && |
| 109 |
|
| 110 |
setup_instructions='Add your SSH public keys at |
| 111 |
|
| 112 |
'"$site"'/settings/keys |
| 113 |
|
| 114 |
Then visit the main repository at: |
| 115 |
|
| 116 |
'"$site/$organization_name/$repo_name"' |
| 117 |
|
| 118 |
and use the Fork button in the upper right. |
| 119 |
' |
| 120 |
|
| 121 |
# Perform setup if necessary. |
| 122 |
if test -n "$setup"; then |
| 123 |
echo 'Sign-in to GitHub to get/set your username at |
| 124 |
|
| 125 |
'"$site/login"' |
| 126 |
|
| 127 |
'"$setup_instructions" && |
| 128 |
read -ep "GitHub username? [$USER]: " gu && |
| 129 |
if test -z "$gu"; then |
| 130 |
gu="$USER" |
| 131 |
fi && |
| 132 |
fetchurl="${fetchurl_/\$username/$gu}" && |
| 133 |
if test -z "$pushurl"; then |
| 134 |
git remote add "$remote" "$fetchurl" |
| 135 |
else |
| 136 |
git config remote."$remote".url "$fetchurl" |
| 137 |
fi && |
| 138 |
pushurl="${pushurl_/\$username/$gu}" && |
| 139 |
git config remote."$remote".pushurl "$pushurl" && |
| 140 |
echo 'Remote "'"$remote"'" is now configured to push to |
| 141 |
|
| 142 |
'"$pushurl"' |
| 143 |
' |
| 144 |
fi && |
| 145 |
|
| 146 |
# Optionally test GitHub access. |
| 147 |
if test -n "$pushurl"; then |
| 148 |
read -ep 'Test access to GitHub (SSH)? [y/N]: ' ans && |
| 149 |
if [ "$ans" == "y" ] || [ "$ans" == "Y" ]; then |
| 150 |
echo -n 'Testing GitHub access by SSH...' |
| 151 |
if git ls-remote --heads "$pushurl" >/dev/null; then |
| 152 |
echo 'passed.' |
| 153 |
else |
| 154 |
echo 'failed.' && |
| 155 |
die 'Could not access your GitHub fork of this project. |
| 156 |
'"$setup_instructions" |
| 157 |
fi |
| 158 |
fi |
| 159 |
fi |