git-gitlab-push 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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>] [<options>...] [--]
  18. OPTIONS
  19. --dry-run
  20. Show what would be pushed without actually updating the destination
  21. -f,--force
  22. Force-push the topic HEAD to rewrite the destination branch
  23. --no-default
  24. Do not push the default branch (e.g. master)
  25. --no-topic
  26. Do not push the topic HEAD.
  27. '
  28. OPTIONS_SPEC=
  29. SUBDIRECTORY_OK=Yes
  30. . "$(git --exec-path)/git-sh-setup"
  31. egrep-q() {
  32. egrep "$@" >/dev/null 2>/dev/null
  33. }
  34. # Load the project configuration.
  35. gitlab_upstream='' &&
  36. gitlab_configured='' &&
  37. config="${BASH_SOURCE%/*}/config" &&
  38. protocol=$(git config -f "$config" --get gitlab.protocol ||
  39. echo "https") &&
  40. host=$(git config -f "$config" --get gitlab.host) &&
  41. site=$(git config -f "$config" --get gitlab.site ||
  42. echo "$protocol://$host") &&
  43. group_path=$(git config -f "$config" --get gitlab.group-path) &&
  44. project_path=$(git config -f "$config" --get gitlab.project-path) &&
  45. gitlab_upstream="$site/$group_path/$project_path.git" &&
  46. gitlab_pushurl=$(git config --get remote.gitlab.pushurl ||
  47. git config --get remote.gitlab.url) &&
  48. gitlab_configured=1
  49. #-----------------------------------------------------------------------------
  50. remote=''
  51. refspecs=''
  52. force=''
  53. lease=false
  54. lease_flag=''
  55. no_topic=''
  56. no_default=''
  57. dry_run=''
  58. # Parse the command line options.
  59. while test $# != 0; do
  60. case "$1" in
  61. -f|--force) force='+'; lease=true ;;
  62. --no-topic) no_topic=1 ;;
  63. --dry-run) dry_run=--dry-run ;;
  64. --no-default) no_default=1 ;;
  65. --) shift; break ;;
  66. -*) usage ;;
  67. *) test -z "$remote" || usage ; remote="$1" ;;
  68. esac
  69. shift
  70. done
  71. test $# = 0 || usage
  72. # Default remote.
  73. test -n "$remote" || remote="gitlab"
  74. if test -z "$no_topic"; then
  75. # Identify and validate the topic branch name.
  76. head="$(git symbolic-ref HEAD)" && topic="${head#refs/heads/}" || topic=''
  77. if test -z "$topic" -o "$topic" = "master"; then
  78. die 'Please name your topic:
  79. git checkout -b descriptive-name'
  80. fi
  81. if $lease; then
  82. have_ref=false
  83. remoteref="refs/remotes/$remote/$topic"
  84. if git rev-parse --verify -q "$remoteref"; then
  85. have_ref=true
  86. else
  87. die "It seems that a local ref for the branch is
  88. missing; forcing a push is dangerous and may overwrite
  89. previous work. Fetch from the $remote remote first or
  90. push without '-f' or '--force'."
  91. fi
  92. have_lease_flag=false
  93. if git push -h | egrep-q -e '--force-with-lease'; then
  94. have_lease_flag=true
  95. fi
  96. if $have_lease_flag && $have_ref; then
  97. # Set the lease flag.
  98. lease_flag="--force-with-lease=$topic:$remoteref"
  99. # Clear the force string.
  100. force=''
  101. fi
  102. fi
  103. # The topic branch will be pushed by name.
  104. refspecs="${force}HEAD:refs/heads/$topic $refspecs"
  105. fi
  106. # Fetch the current remote master branch head.
  107. # This helps computation of a minimal pack to push.
  108. echo "Fetching $remote master"
  109. fetch_out=$(git fetch "$remote" master 2>&1) || die "$fetch_out"
  110. gitlab_head=$(git rev-parse FETCH_HEAD) || exit
  111. # Fetch the current upstream master branch head.
  112. if origin_fetchurl=$(git config --get remote.origin.url) &&
  113. test "$origin_fetchurl" = "$gitlab_upstream"; then
  114. upstream_remote='origin'
  115. else
  116. upstream_remote="$gitlab_upstream"
  117. fi
  118. echo "Fetching $upstream_remote master"
  119. fetch_out=$(git fetch "$upstream_remote" master 2>&1) || die "$fetch_out"
  120. upstream_head=$(git rev-parse FETCH_HEAD) || exit
  121. # Add a refspec to keep the remote master up to date if possible.
  122. if test -z "$no_default" &&
  123. base=$(git merge-base "$gitlab_head" "$upstream_head") &&
  124. test "$base" = "$gitlab_head"; then
  125. refspecs="$upstream_head:refs/heads/master $refspecs"
  126. fi
  127. # Exit early if we have nothing to push.
  128. if test -z "$refspecs"; then
  129. echo 'Nothing to push!'
  130. exit 0
  131. fi
  132. # Push. Save output and exit code.
  133. echo "Pushing to $remote"
  134. push_config='-c advice.pushUpdateRejected=false'
  135. push_stdout=$(git $push_config push $lease_flag --porcelain $dry_run "$remote" $refspecs); push_exit=$?
  136. echo "$push_stdout"
  137. if test "$push_exit" -ne 0 && test -z "$force"; then
  138. # Advise the user to fetch if needed.
  139. if echo "$push_stdout" | egrep-q 'stale info'; then
  140. echo "
  141. You have pushed to your branch from another machine; you may be overwriting
  142. commits unintentionally. Fetch from the $remote remote and check that you are
  143. not pushing an outdated branch."
  144. fi
  145. # Advise the user to force-push if needed.
  146. if echo "$push_stdout" | egrep-q 'non-fast-forward'; then
  147. echo '
  148. Add "-f" or "--force" to push a rewritten topic.'
  149. fi
  150. fi
  151. # Reproduce the push exit code.
  152. exit $push_exit