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>...] [--]
23 Show what would be pushed without actually updating the destination
26 Force-push the topic HEAD to rewrite the destination branch (use twice
27 to ignore stale remote tracking branches)
30 Do not push the default branch (e.g. master)
33 Do not push the topic HEAD.
36 Do not set an upstream branch (i.e. tracking) for the topic
39 Do not require that submodules be pushed to a remote.
43 .
"$(git --exec-path)/git-sh-setup"
46 grep -E "$@" >/dev
/null
2>/dev
/null
49 # Load the project configuration.
51 gitlab_configured
='' &&
52 config
="${BASH_SOURCE%/*}/config" &&
53 protocol
=$
(git config
-f "$config" --get gitlab.protocol ||
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" &&
64 #-----------------------------------------------------------------------------
75 submodule_config
='-c push.recurseSubmodules=on-demand'
77 # Parse the command line options.
78 while test $# != 0; do
81 if test -n "$force"; then
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
= ;;
95 *) test -z "$remote" || usage
; remote
="$1" ;;
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'
114 remoteref
="refs/remotes/$remote/$topic"
115 if git rev-parse
--verify -q "$remoteref" > /dev
/null
; then
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
129 elif git push
--help | egrep_q
-e '--force-with-lease'; then
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.
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'
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!'
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
=$?
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
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
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.