diff options
-rwxr-xr-x | git-pull-request.sh | 71 |
1 files changed, 69 insertions, 2 deletions
diff --git a/git-pull-request.sh b/git-pull-request.sh index bb25c51..7ba565f 100755 --- a/git-pull-request.sh +++ b/git-pull-request.sh @@ -145,6 +145,16 @@ EOF rm -f "${GIT_DIR}/PULL_REQUEST_META" } +commit_is_merged () +{ + local WHAT WHERE EMPTY + WHAT="$1" + WHERE="$2" + EMPTY=$(rgit rev-list "$WHERE" | sane_grep -F "$WHAT") + test "x$EMPTY" = "x" && return 1 + return 0 +} + ############################################################################# ## Command implementations ################################################ ############################################################################# @@ -244,7 +254,7 @@ cmd_create () { METADATA[COMMIT]="${_COMMIT}" done <<<$(rgit show-ref --verify $(rgit symbolic-ref HEAD)) while read _UNAMBIG _LAND; do - METADATA[LANDINTO]=$(echo "${_LAND}" | sed -e's!^[^/]+/!!') + METADATA[LANDINTO]=$(echo "${_LAND}" | sed -e's@^[^/]*/@@') METADATA[ONTOP]=$(rgit merge-base "${METADATA[COMMIT]}" "${_UNAMBIG}") done <<<$(rgit for-each-ref --format="%(upstream) %(upstream:short)" \ $(rgit symbolic-ref HEAD)) @@ -293,6 +303,63 @@ EOF echo "Pull request ${PR} created successfully" } +cmd_merge () { + local PR + PR="$1" + shift + if test "x$PR" = "x"; then + echo >&2 "Please specify a pull-request name to create." + exit 1 + fi + local COMMIT BRANCH + while read _COMMIT _BRANCH; do + COMMIT="${_COMMIT}" + BRANCH="${_BRANCH}" + done <<<$(rgit show-ref "$PR"__meta) + if test "x$COMMIT" = "x"; then + echo >&2 "Specified pull request '${PR}' does not exist." + exit 1 + fi + load_meta_from "${COMMIT}" + display_meta_summary "$PR" + + require_clean_work_tree + + # 1. Find the merge source + local MERGESRC + MERGESRC=$(rgit for-each-ref --format="%(refname)" | \ + sane_grep -v -F "/remotes/" | sane_grep -F "/$PR"__anchor) + test "x$MERGESRC" = "x" && MERGESRC="${METADATA[COMMIT]}" + # 2. Verify that it's present in the repository + if ! rgit cat-file -t "$MERGESRC" 2>/dev/null >/dev/null; then + echo >&2 "Unable to locate merge source ${MERGESRC}. Cannot continue" + exit 1 + fi + # 3. Establish if the current branch is suitable. + # ONTOP must exist in this branch + # COMMIT must not exist in this branch + if ! commit_is_merged "${METADATA[ONTOP]}" "HEAD"; then + echo >&2 "Unable to proceed, the pull-request is on top of something" + echo >&2 "which is not in this branch." + echo >&2 "${METADATA[ONTOP]}" + exit 1 + fi + if commit_is_merged "${METADATA[COMMIT]}" "HEAD"; then + echo >&2 "Unable to proceed. Pull-request already merged into here?" + exit 1 + fi + # 4. Warn if the current branch is not the target + local CURNAME + CURNAME=$(rgit for-each-ref --format="%(refname:short)" \ + $(rgit symbolic-ref HEAD)) + if test "x$CURNAME" != "x${METADATA[LANDINTO]}"; then + say "WARNING: Branch wants to land into '${METADATA[LANDINTO]}', not '${CURNAME}'" + say " Merge will proceed but pull-request will not auto-close" + fi + # 5. Do the merge + rgit merge --no-ff --no-commit "${MERGESRC}" +} + ############################################################################# ## Command invocation ##################################################### ############################################################################# @@ -304,4 +371,4 @@ else usage fi -exit 0
\ No newline at end of file +exit 0 |