diff mbox series

[2/3] request-pull: add -o/--output option

Message ID 20211003122943.338199-3-bagasdotme@gmail.com (mailing list archive)
State New, archived
Headers show
Series request-pull: write to file option | expand

Commit Message

Bagas Sanjaya Oct. 3, 2021, 12:29 p.m. UTC
Add -o option (and long dash counterpart --option), which allow users to
write pull request message to the specified file.

Signed-off-by: Bagas Sanjaya <bagasdotme@gmail.com>
---
 git-request-pull.sh     | 28 +++++++++++++++++++++++-----
 t/t5150-request-pull.sh | 13 +++++++++++++
 2 files changed, 36 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/git-request-pull.sh b/git-request-pull.sh
index 88a7a9cf0d..f4d6f846d0 100755
--- a/git-request-pull.sh
+++ b/git-request-pull.sh
@@ -7,10 +7,12 @@ 
 SUBDIRECTORY_OK='Yes'
 OPTIONS_KEEPDASHDASH=
 OPTIONS_STUCKLONG=
-OPTIONS_SPEC='git request-pull [options] start url [end]
+OPTIONS_SPEC="\
+git request-pull [options] start url [end]
 --
-p    show patch text as well
-'
+p            show patch text as well
+o,output=    output pull request to the specified file
+"
 
 . git-sh-setup
 
@@ -18,11 +20,16 @@  GIT_PAGER=
 export GIT_PAGER
 
 patch=
-while	case "$#" in 0) break ;; esac
+out=
+while test $# != 0
 do
 	case "$1" in
 	-p)
 		patch=-p ;;
+	-o|--output)
+		case "$2" in '') usage ;; esac
+		out="$2"
+		shift ;;
 	--)
 		shift; break ;;
 	-*)
@@ -180,6 +187,17 @@  $dash_line" $headrev &&
 
 message=$(display_message) || status=1
 
-echo "$message"
+if test $status -ne 0
+then
+	echo "There's an error when outputting message, exiting"
+	exit $status
+fi
+
+if test -n "$out"
+then
+	echo "$message" > "$out"
+else
+	echo "$message"
+fi
 
 exit $status
diff --git a/t/t5150-request-pull.sh b/t/t5150-request-pull.sh
index cb67bac1c4..b87e9c9869 100755
--- a/t/t5150-request-pull.sh
+++ b/t/t5150-request-pull.sh
@@ -168,6 +168,19 @@  test_expect_success 'pull request after push' '
 
 '
 
+test_expect_success 'pull request output with -o' '
+
+	rm -fr downstream.git &&
+	git init --bare downstream.git &&
+	(
+		cd local &&
+		git checkout initial &&
+		git merge --ff-only main &&
+		git push origin main:for-upstream &&
+		git request-pull -o ../request initial origin main:for-upstream
+	)
+'
+
 test_expect_success 'request asks HEAD to be pulled' '
 
 	rm -fr downstream.git &&