Merge topic 'push-with-submodules' into setup
[kiteware-gitsetup.git] / git-gerrit-push
blobb46f753eb22d0e17ae514f43ae454c2848b033e5
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>] [--no-topic] [--dry-run] [--]"
19 OPTIONS_SPEC=
20 SUBDIRECTORY_OK=Yes
21 . "$(git --exec-path)/git-sh-setup"
23 #-----------------------------------------------------------------------------
25 remote=''
26 refspecs=''
27 no_topic=''
28 dry_run=''
30 # Parse the command line options.
31 while test $# != 0; do
32 case "$1" in
33 --no-topic) no_topic=1 ;;
34 --dry-run) dry_run=--dry-run ;;
35 --) shift; break ;;
36 -*) usage ;;
37 *) test -z "$remote" || usage ; remote="$1" ;;
38 esac
39 shift
40 done
41 test $# = 0 || usage
43 # Default remote.
44 test -n "$remote" || remote="gerrit"
46 if test -z "$no_topic"; then
47 # Identify and validate the topic branch name.
48 head="$(git symbolic-ref HEAD)" && topic="${head#refs/heads/}" || topic=''
49 if test -z "$topic" -o "$topic" = "master"; then
50 die 'Please name your topic:
51 git checkout -b descriptive-name'
53 # The topic branch will be pushed by name.
54 refspecs="HEAD:refs/for/master/$topic $refspecs"
57 # Fetch the current upstream master branch head.
58 # This helps computation of a minimal pack to push.
59 echo "Fetching $remote master"
60 fetch_out=$(git fetch "$remote" master 2>&1) || die "$fetch_out"
62 # Exit early if we have nothing to push.
63 if test -z "$refspecs"; then
64 echo 'Nothing to push!'
65 exit 0
68 # Push. Save output and exit code.
69 echo "Pushing to $remote"
70 push_stdout=$(git push --porcelain $dry_run "$remote" $refspecs); push_exit=$?
71 echo "$push_stdout"
73 # Reproduce the push exit code.
74 exit $push_exit