1313# gitstack.sh prev # <-- checkout previous branch in stack (e.g. feature-2 -> feature-1)
1414# gitstack.sh next # <-- checkout next branch in stack (e.g. feature-2 -> feature-3)
1515# gitstack.sh push [stack] # <-- force-push all branches in a stack to remote
16+ # gitstack.sh pr [gh-args] # <-- create GitHub PR targeting correct parent branch
1617#
1718# Description:
1819# create -> Creates a new branch named "<base_name>-0".
2526# prev -> Checkout previous branch in stack (e.g. feature-2 -> feature-1).
2627# next -> Checkout next branch in stack (e.g. feature-2 -> feature-3).
2728# push -> Force-push all branches in a stack to remote (uses current stack if none specified).
29+ # pr -> Create GitHub PR targeting correct parent branch in stack.
2830# -----------------------------------------------------------------------------
2931
3032function usage() {
@@ -41,6 +43,7 @@ function usage() {
4143 echo " $0 prev (Checkout previous branch in stack)"
4244 echo " $0 next (Checkout next branch in stack)"
4345 echo " $0 push [stack] (Force-push all branches in a stack to remote)"
46+ echo " $0 pr [gh-args] (Create GitHub PR targeting correct parent branch)"
4447 echo
4548 echo " Commands:"
4649 echo " create Creates a new branch named '<base_name>-0'."
@@ -53,6 +56,7 @@ function usage() {
5356 echo " prev Checkout previous branch in stack (e.g. feature-2 -> feature-1)."
5457 echo " next Checkout next branch in stack (e.g. feature-2 -> feature-3)."
5558 echo " push Force-push all branches in a stack to remote. Uses current stack if none specified."
59+ echo " pr Create GitHub PR targeting correct parent branch. Passes additional args to 'gh pr create'."
5660 exit 1
5761}
5862
@@ -875,6 +879,99 @@ function fp_stack() {
875879 fi
876880}
877881
882+ # Create GitHub PR targeting correct parent branch in stack
883+ # Usage: create_github_pr [additional-gh-args...]
884+ function create_github_pr() {
885+ # Handle help flags
886+ for arg in " $@ " ; do
887+ if [[ " $arg " == " --help" || " $arg " == " -h" ]]; then
888+ echo " git stack pr - Create GitHub PR targeting correct parent branch"
889+ echo
890+ echo " Usage: git stack pr [gh-args...]"
891+ echo
892+ echo " This command automatically:"
893+ echo " • Detects your current stack position (e.g., feature-2)"
894+ echo " • Calculates the correct parent branch (feature-1, or main if feature-0)"
895+ echo " • Creates PR targeting that parent branch"
896+ echo " • Uses --fill to auto-populate title and description from commits"
897+ echo
898+ echo " Examples:"
899+ echo " git stack pr # Basic PR creation"
900+ echo " git stack pr --draft # Create draft PR"
901+ echo " git stack pr --reviewer @user # Add reviewer"
902+ echo
903+ echo " Additional arguments are passed to 'gh pr create'."
904+ exit 0
905+ fi
906+ done
907+
908+ # Check if gh CLI is available
909+ if ! command -v gh & > /dev/null; then
910+ echo " Error: GitHub CLI (gh) is not installed or not in PATH."
911+ echo " Install it from: https://cli.github.com/"
912+ exit 1
913+ fi
914+
915+ # Check if we're on a stack branch
916+ if ! get_stack_info; then
917+ echo " Error: Current branch is not part of a stack (should match '<base>-<number>' pattern)."
918+ echo " Cannot determine parent branch for PR creation."
919+ exit 1
920+ fi
921+
922+ local current_branch
923+ current_branch=$( git rev-parse --abbrev-ref HEAD)
924+ local parent_branch
925+
926+ # Determine parent branch
927+ if [ " $STACK_NUM " -eq 0 ]; then
928+ # For feature-0, target main or master
929+ if git rev-parse --verify main & > /dev/null; then
930+ parent_branch=" main"
931+ elif git rev-parse --verify master & > /dev/null; then
932+ parent_branch=" master"
933+ else
934+ echo " Error: Cannot find main or master branch to target."
935+ exit 1
936+ fi
937+ else
938+ # For feature-N (N > 0), target feature-(N-1)
939+ local parent_num=$(( STACK_NUM - 1 ))
940+ parent_branch=" ${STACK_BASE} -${parent_num} "
941+
942+ # Verify parent branch exists
943+ if ! git rev-parse --verify " $parent_branch " & > /dev/null; then
944+ echo " Error: Parent branch '$parent_branch ' does not exist."
945+ echo " Stack may be incomplete or corrupted."
946+ exit 1
947+ fi
948+ fi
949+
950+ # Show confirmation
951+ echo " Creating GitHub PR:"
952+ echo " From: $current_branch "
953+ echo " To: $parent_branch "
954+ echo
955+
956+ # Confirm with user
957+ read -r -p " Proceed with PR creation? [Y/n] " response
958+ response=${response:- y} # Default to yes
959+
960+ if [[ ! " $response " =~ ^[Yy]$ ]]; then
961+ echo " PR creation cancelled."
962+ exit 0
963+ fi
964+
965+ # Create PR with gh CLI
966+ echo " Running: gh pr create -B $parent_branch --fill $* "
967+ if gh pr create -B " $parent_branch " --fill " $@ " ; then
968+ echo " ✅ GitHub PR created successfully!"
969+ else
970+ echo " ❌ Failed to create GitHub PR"
971+ exit 1
972+ fi
973+ }
974+
878975# Only process arguments if script is run directly (not sourced)
879976if [[ " ${BASH_SOURCE[0]} " == " ${0} " ]]; then
880977 subcommand=" $1 "
@@ -913,6 +1010,9 @@ if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
9131010 push)
9141011 fp_stack " $@ "
9151012 ;;
1013+ pr)
1014+ create_github_pr " $@ "
1015+ ;;
9161016 * )
9171017 usage
9181018 ;;
0 commit comments