Debian Codegraph @

Utilities/GitSetup/setup-upstream

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