Skip to content

Commit 67d0711

Browse files
authored
Merge pull request #3 from dagjomar/feat/stack-pr-0
feat/stack pr 0
2 parents 56d4d6b + a2d9d9d commit 67d0711

2 files changed

Lines changed: 125 additions & 10 deletions

File tree

TODO.txt

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
- Detect and warn about potential stack issues
1414

1515
## Stack Status & Overview
16-
- Add `git stack status` command showing:
17-
- Current active stack
18-
- All branches in the stack with latest commits
16+
- Add `git stack status` command showing: (COMPLETED)
17+
- Current active stack (COMPLETED)
18+
- All branches in the stack with latest commits (COMPLETED)
1919
- Remote sync status for each branch
20-
- Conflict detection between stack branches
21-
- Stack depth and health metrics
20+
- Conflict detection between stack branches (COMPLETED)
21+
- Stack depth and health metrics (COMPLETED)
2222

2323
## Advanced Stack Operations
2424
- `git stack rebase` - Rebase entire stack onto main
@@ -28,12 +28,14 @@
2828
- Undo/redo support for stack operations
2929

3030
## Documentation Improvements
31-
- Rewrite README with clear use-cases
32-
- Add visual examples of stack workflows
33-
- Document common patterns and best practices
31+
- ✅ Rewrite README with a clear use-case (COMPLETED)
32+
- ✅ Add a visual example of stack workflow (COMPLETED)
33+
- Document common patterns and best practices (COMPLETED)
34+
- ✅ Include a real-world example (COMPLETED)
3435
- Add troubleshooting section
35-
- Include real-world examples
3636
- Add architecture documentation
37+
- Create better usage examples and use cases explaining how to best use it in a normal workflow
38+
- Create command to manage rebasing
3739

3840
## Configuration & Customization
3941
- Customizable branch naming patterns
@@ -43,7 +45,20 @@
4345
- Custom stack naming conventions
4446

4547
## CI/CD Integration
46-
- GitHub/GitLab PR creation for stack branches
48+
- **Smart PR/MR Creation for Stack Branches** (PRIORITY)
49+
- `git stack pr` - Auto-create GitHub PR targeting correct parent branch
50+
- Auto-detect current stack position (e.g. feature-2)
51+
- Calculate parent branch (feature-1, or main if feature-0)
52+
- Detect `gh` CLI availability
53+
- Run: `gh pr create -B <parent-branch> [additional-args]`
54+
- Show confirmation of target branch before creating
55+
- `git stack mr` - Auto-create GitLab MR targeting correct parent branch
56+
- Same logic but for `glab` CLI
57+
- Run: `glab mr create -b <parent-branch> [additional-args]`
58+
- Enhanced PR/MR descriptions
59+
- Auto-populate with stack context
60+
- Show branch hierarchy in description
61+
- Include stack health status
4762
- Auto-update PR descriptions with stack info
4863
- Stack visualization in PR comments
4964
- CI pipeline integration

bin/gitstack.sh

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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".
@@ -25,6 +26,7 @@
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

3032
function 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)
879976
if [[ "${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

Comments
 (0)