git-gerrit-push 2.2 KB

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