Merge topic 'push-with-submodules' into setup
[kiteware-gitsetup.git] / git-gitlab-push
blob908a5f12c0d1ed5b79af0e20abc16d85f7ca0ad7
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 USAGE='[<remote>] [<options>...] [--]
20 OPTIONS
22 --dry-run
23 Show what would be pushed without actually updating the destination
25 -f,--force
26 Force-push the topic HEAD to rewrite the destination branch (use twice
27 to ignore stale remote tracking branches)
29 --no-default
30 Do not push the default branch (e.g. master)
32 --no-topic
33 Do not push the topic HEAD.
35 --no-upstream
36 Do not set an upstream branch (i.e. tracking) for the topic
38 --no-submodules
39 Do not require that submodules be pushed to a remote.
41 OPTIONS_SPEC=
42 SUBDIRECTORY_OK=Yes
43 . "$(git --exec-path)/git-sh-setup"
45 egrep_q() {
46 grep -E "$@" >/dev/null 2>/dev/null
49 # Load the project configuration.
50 gitlab_upstream='' &&
51 gitlab_configured='' &&
52 config="${BASH_SOURCE%/*}/config" &&
53 protocol=$(git config -f "$config" --get gitlab.protocol ||
54 echo "https") &&
55 userpart=$(test "$protocol" = "ssh" && echo "git@" || echo '') &&
56 host=$(git config -f "$config" --get gitlab.host) &&
57 site=$(git config -f "$config" --get gitlab.site ||
58 echo "$protocol://$userpart$host") &&
59 group_path=$(git config -f "$config" --get gitlab.group-path) &&
60 project_path=$(git config -f "$config" --get gitlab.project-path) &&
61 gitlab_upstream="$site/$group_path/$project_path.git" &&
62 gitlab_configured=1
64 #-----------------------------------------------------------------------------
66 remote=''
67 refspecs=''
68 force=''
69 lease=false
70 lease_flag=''
71 no_topic=''
72 no_default=''
73 dry_run=''
74 set_upstream=true
75 submodule_config='-c push.recurseSubmodules=on-demand'
77 # Parse the command line options.
78 while test $# != 0; do
79 case "$1" in
80 -f|--force)
81 if test -n "$force"; then
82 lease=false
83 else
84 lease=true
86 force='+'
88 --no-topic) no_topic=1; set_upstream=false ;;
89 --dry-run) dry_run=--dry-run ;;
90 --no-default) no_default=1 ;;
91 --no-upstream) set_upstream=false ;;
92 --no-submodules) submodule_config= ;;
93 --) shift; break ;;
94 -*) usage ;;
95 *) test -z "$remote" || usage ; remote="$1" ;;
96 esac
97 shift
98 done
99 test $# = 0 || usage
101 # Default remote.
102 test -n "$remote" || remote="gitlab"
104 if test -z "$no_topic"; then
105 # Identify and validate the topic branch name.
106 head="$(git symbolic-ref HEAD)" && topic="${head#refs/heads/}" || topic=''
107 if test -z "$topic" -o "$topic" = "master"; then
108 die 'Please name your topic:
109 git checkout -b descriptive-name'
112 if $lease; then
113 have_ref=false
114 remoteref="refs/remotes/$remote/$topic"
115 if git rev-parse --verify -q "$remoteref" > /dev/null; then
116 have_ref=true
117 else
118 die "It seems that a local ref for the branch is
119 missing; forcing a push is dangerous and may overwrite
120 previous work. Fetch from the $remote remote first or
121 push without '-f' or '--force'."
124 have_lease_flag=false
125 # Note: on Windows 'git push --help' will open a browser, and
126 # the check will fail, so use the flag by default.
127 if git --version | egrep_q -e 'windows'; then
128 have_lease_flag=true
129 elif git push --help | egrep_q -e '--force-with-lease'; then
130 have_lease_flag=true
133 if $have_lease_flag && $have_ref; then
134 # Set the lease flag.
135 lease_flag="--force-with-lease=$topic:$remoteref"
136 # Clear the force string.
137 force=''
141 # The topic branch will be pushed by name.
142 refspecs="${force}HEAD:refs/heads/$topic $refspecs"
145 # Fetch the current remote master branch head.
146 # This helps computation of a minimal pack to push.
147 echo "Fetching $remote master"
148 fetch_out=$(git fetch "$remote" master 2>&1) || die "$fetch_out"
149 gitlab_head=$(git rev-parse FETCH_HEAD) || exit
151 # Fetch the current upstream master branch head.
152 if origin_fetchurl=$(git config --get remote.origin.url) &&
153 test "$origin_fetchurl" = "$gitlab_upstream"; then
154 upstream_remote='origin'
155 else
156 upstream_remote="$gitlab_upstream"
158 echo "Fetching $upstream_remote master"
159 fetch_out=$(git fetch "$upstream_remote" master 2>&1) || die "$fetch_out"
160 upstream_head=$(git rev-parse FETCH_HEAD) || exit
162 # Add a refspec to keep the remote master up to date if possible.
163 if test -z "$no_default" &&
164 base=$(git merge-base "$gitlab_head" "$upstream_head") &&
165 test "$base" = "$gitlab_head"; then
166 refspecs="$upstream_head:refs/heads/master $refspecs"
169 # Exit early if we have nothing to push.
170 if test -z "$refspecs"; then
171 echo 'Nothing to push!'
172 exit 0
175 # Push. Save output and exit code.
176 echo "Pushing to $remote"
177 push_config='-c advice.pushUpdateRejected=false'
178 push_stdout=$(git $push_config $submodule_config push $lease_flag --porcelain $dry_run "$remote" $refspecs); push_exit=$?
179 echo "$push_stdout"
181 if test "$push_exit" -ne 0 && test -z "$force"; then
182 # Advise the user to fetch if needed.
183 if echo "$push_stdout" | egrep_q 'stale info'; then
184 echo "
185 You have pushed to your branch from another machine; you may be overwriting
186 commits unintentionally. Fetch from the $remote remote and check that you are
187 not pushing an outdated branch."
190 # Advise the user to force-push if needed.
191 if echo "$push_stdout" | egrep_q 'non-fast-forward'; then
192 echo '
193 Add "-f" or "--force" to push a rewritten topic.'
195 elif $set_upstream && test -n "$topic"; then
196 git branch -u "$remote/$topic"
199 # Reproduce the push exit code.
200 exit $push_exit