| 1 |
#!/usr/bin/env bash |
| 2 |
#============================================================================= |
| 3 |
# Copyright 2010-2012 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 |
USAGE="[<remote>] [--no-topic] [--dry-run] [--]" |
| 19 |
OPTIONS_SPEC= |
| 20 |
SUBDIRECTORY_OK=Yes |
| 21 |
. "$(git --exec-path)/git-sh-setup" |
| 22 |
|
| 23 |
# Load the project configuration. |
| 24 |
config="${BASH_SOURCE%/*}/config" && |
| 25 |
upstream_remote=$(git config -f "$config" --get upstream.remote) && |
| 26 |
host=$(git config -f "$config" --get github.host || echo "github.com") && |
| 27 |
organization_name=$(git config -f "$config" --get github.organization-name) && |
| 28 |
repo_name=$(git config -f "$config" --get github.repo-name) |
| 29 |
github_remote=$(git config --get github.fork.remote) |
| 30 |
#----------------------------------------------------------------------------- |
| 31 |
|
| 32 |
remote="$github_remote" |
| 33 |
refspecs='' |
| 34 |
no_topic='' |
| 35 |
dry_run='' |
| 36 |
force='' |
| 37 |
|
| 38 |
# Parse the command line options. |
| 39 |
while test $# != 0; do |
| 40 |
case "$1" in |
| 41 |
--no-topic) no_topic=1 ;; |
| 42 |
--force) force=--force ;; |
| 43 |
--dry-run) dry_run=--dry-run ;; |
| 44 |
--) shift; break ;; |
| 45 |
-*) usage ;; |
| 46 |
*) test -z "$remote" || usage ; remote="$1" ;; |
| 47 |
esac |
| 48 |
shift |
| 49 |
done |
| 50 |
test $# = 0 || usage |
| 51 |
|
| 52 |
# Default remote. |
| 53 |
test -n "$remote" || remote="origin" |
| 54 |
|
| 55 |
if test -z "$no_topic"; then |
| 56 |
# Identify and validate the topic branch name. |
| 57 |
topic="$(git symbolic-ref HEAD | sed -e 's|^refs/heads/||')" |
| 58 |
if test "$topic" = "master"; then |
| 59 |
die 'Please name your topic: |
| 60 |
git checkout -b descriptive-name' |
| 61 |
fi |
| 62 |
refspecs="HEAD:$topic" |
| 63 |
fi |
| 64 |
|
| 65 |
# Exit early if we have nothing to push. |
| 66 |
if test -z "$refspecs"; then |
| 67 |
echo "Nothing to push!" |
| 68 |
exit 0 |
| 69 |
fi |
| 70 |
|
| 71 |
# Fetch the current upstream master branch head. |
| 72 |
# This helps the computation of a minimal pack to push. |
| 73 |
if test -z "$upstream_remote"; then |
| 74 |
echo "Fetching $upstream_remote master" |
| 75 |
fetch_out=$(git fetch "$upstream_remote" master 2>&1) || die "$fetch_out" |
| 76 |
fi |
| 77 |
|
| 78 |
# Push. Save output and exit code. |
| 79 |
echo "Pushing to $remote" |
| 80 |
push_stdout=$(git push --porcelain $force $dry_run "$remote" $refspecs); push_exit=$? |
| 81 |
echo "$push_stdout" |
| 82 |
|
| 83 |
# Reproduce the push exit code. |
| 84 |
exit $push_exit |