summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <daniel.silverstone@codethink.co.uk>2012-05-24 09:48:00 +0100
committerDaniel Silverstone <daniel.silverstone@codethink.co.uk>2012-05-24 09:48:00 +0100
commit47bd40679c04433ab06f700b3903043bcba83e28 (patch)
treed7651aedc4859f50b0abdc87a186e8bcfc8daae8
parentdfb2b45f05dc8c4d35f6e785f8dd39ff22835975 (diff)
downloadgit-pull-request-47bd40679c04433ab06f700b3903043bcba83e28.tar.bz2
Creation of pull requests
-rwxr-xr-xgit-pull-request.sh106
1 files changed, 103 insertions, 3 deletions
diff --git a/git-pull-request.sh b/git-pull-request.sh
index f84e8f1..bb25c51 100755
--- a/git-pull-request.sh
+++ b/git-pull-request.sh
@@ -72,7 +72,7 @@ find_requests ()
local SUB
SUB="$1"
test "x$SUB" = "x" && SUB="heads"
- rgit show-ref | sane_grep "refs/pull-requests/${SUB}/.*__meta\$" | \
+ rgit show-ref | sane_grep " refs/pull-requests/${SUB}/.*__meta\$" | \
sed -e 's!.* refs/pull-requests/'"${SUB}"'/!!' | sed -e 's/__meta$//'
}
@@ -133,6 +133,18 @@ summarise_meta_at ()
display_meta_summary "${PR}"
}
+inject_metadata_file ()
+{
+ cat > "${GIT_DIR}/PULL_REQUEST_META" <<EOF
+STATE=${METADATA[STATE]}
+COMMIT=${METADATA[COMMIT]}
+LANDINTO=${METADATA[LANDINTO]}
+ONTOP=${METADATA[ONTOP]}
+EOF
+ rgit hash-object -t blob -w "${GIT_DIR}/PULL_REQUEST_META"
+ rm -f "${GIT_DIR}/PULL_REQUEST_META"
+}
+
#############################################################################
## Command implementations ################################################
#############################################################################
@@ -164,7 +176,7 @@ cmd_list () {
display_meta_summary "${META}"
SHOWN=1
fi
- done <<<$(rgit show-ref "${META}"__meta)
+ done <<<$(rgit show-ref "${META}"__meta | sane_grep -v -F "/remotes/")
done
if test "x$SHOWN" != "x1"; then
@@ -193,6 +205,94 @@ cmd_precis () {
rgit log --pretty="%B" "${METADATA[ROOT]}"
}
+cmd_create () {
+ local PR
+ PR="$1"
+ 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
+ load_meta_from "${COMMIT}"
+ echo >&2 "A pull request with that name already exists."
+ display_meta_summary "$PR"
+ exit 1
+ fi
+
+ require_clean_work_tree
+
+ # 0. Ensure we're a tracking something
+ BRANCH=$(rgit for-each-ref --format="%(refname:short)" \
+ $(rgit symbolic-ref HEAD))
+ local TRACKING
+ TRACKING=$(rgit config "branch.${BRANCH}.remote" 2>/dev/null)
+ if test "x$TRACKING" = "x"; then
+ echo >&2 "This branch is not a tracking branch. Cannot continue"
+ exit 1
+ fi
+
+ # 1. Determine metadata
+ unset -v METADATA
+ declare -g -A METADATA
+ METADATA[STATE]="new"
+ while read _COMMIT _BRANCH; do
+ 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[ONTOP]=$(rgit merge-base "${METADATA[COMMIT]}" "${_UNAMBIG}")
+ done <<<$(rgit for-each-ref --format="%(upstream) %(upstream:short)" \
+ $(rgit symbolic-ref HEAD))
+
+ # 2. Produce precis template and edit it
+ cat > "${GIT_DIR}/PULL_REQUEST_EDITMSG" <<EOF
+
+# This message will become the precis for your pull request.
+# If you do not want to create the pull request, you should quit the editor
+# without saving the file.
+#
+# Any line starting with a hash mark will be removed before the pull request
+# is committed to the repository.
+#
+# The first line of your message will be used as the title and the remainder
+# will be the body of the precis. The entire message will be used as the
+# commit message for the pull request.
+#
+# This pull request is called: ${PR}
+# It will be of ${METADATA[COMMIT]}
+# Against ${METADATA[LANDINTO]} at ${METADATA[ONTOP]}
+#
+EOF
+ local PRESUM POSTSUM
+ PRESUM=$(rgit hash-object -t blob "${GIT_DIR}/PULL_REQUEST_EDITMSG")
+ git_editor "${GIT_DIR}/PULL_REQUEST_EDITMSG"
+ POSTSUM=$(rgit hash-object -t blob "${GIT_DIR}/PULL_REQUEST_EDITMSG")
+ # 2.5 (if unchanged, clean up and quit)
+ if test "x$PRESUM" = "x$POSTSUM"; then
+ echo >&2 "Message unchanged, aborting creation of pull-request"
+ rm -f "${GIT_DIR}/PULL_REQUEST_EDITMSG"
+ exit 1
+ fi
+ # 3. Create metadata file, tree, commit and ref.
+ local METASUM TREESUM COMMITSUM
+ METASUM=$(inject_metadata_file)
+ TREESUM=$(echo -e "100644 blob ${METASUM}\tpull-request.meta" | \
+ rgit mktree)
+ COMMITSUM=$(sed -e'/^#/d' < "${GIT_DIR}/PULL_REQUEST_EDITMSG" | \
+ rgit commit-tree "${TREESUM}")
+ rgit update-ref -m "Creation of pull-request ${PR}" \
+ "refs/pull-requests/heads/${PR}__meta" "${COMMITSUM}"
+ rgit update-ref -m "Copying anchor for pull-request ${PR}" \
+ "refs/pull-requests/heads/${PR}__anchor" "${METADATA[COMMIT]}"
+ rm -f "${GIT_DIR}/PULL_REQUEST_EDITMSG"
+ echo "Pull request ${PR} created successfully"
+}
+
#############################################################################
## Command invocation #####################################################
#############################################################################
@@ -204,4 +304,4 @@ else
usage
fi
-exit 0
+exit 0 \ No newline at end of file