Last active
April 10, 2025 11:04
-
-
Save takahirom/85a3ce3e533b86a58e77e12e9db8c1f2 to your computer and use it in GitHub Desktop.
my stacked pr workflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Check if gh CLI is installed | |
if ! command -v gh &> /dev/null; then | |
echo "gh CLI is not installed. Please install it from https://cli.github.com/." | |
exit 1 | |
fi | |
# Get the current branch name | |
current_branch=$(git rev-parse --abbrev-ref HEAD) | |
if [ -z "$current_branch" ]; then | |
echo "Could not determine the current branch. Please run this script within a Git repository." | |
exit 1 | |
fi | |
# Try to get the upstream (tracking) branch as the base branch | |
base_branch=$(git rev-parse --abbrev-ref --symbolic-full-name "@{u}" 2>/dev/null || true) | |
if [ -n "$base_branch" ]; then | |
echo "Found upstream branch: $base_branch" | |
fi | |
# Count commits between base_branch and current_branch (if base_branch exists) | |
if [ -n "$base_branch" ]; then | |
commit_count=$(git rev-list --count "${base_branch}..${current_branch}") | |
else | |
commit_count=0 | |
fi | |
# If no commits are found, try using the previous branch | |
if [ "$commit_count" -eq 0 ]; then | |
previous_branch=$(git rev-parse --abbrev-ref @{-1} 2>/dev/null || true) | |
# Check if previous_branch is literally "@{-1}" which indicates an unresolved value. | |
if [ "$previous_branch" = "@{-1}" ]; then | |
previous_branch="" | |
fi | |
if [ -n "$previous_branch" ] && [ "$previous_branch" != "$current_branch" ]; then | |
alt_commit_count=$(git rev-list --count "${previous_branch}..${current_branch}") | |
if [ "$alt_commit_count" -gt 0 ]; then | |
echo "No commits found between ${base_branch:-'upstream'} and ${current_branch}." | |
echo "Falling back to previous branch: $previous_branch" | |
base_branch="$previous_branch" | |
commit_count=$alt_commit_count | |
fi | |
fi | |
fi | |
# If still no base branch or no commits found, prompt the user for the base branch | |
if [ -z "$base_branch" ] || [ "$commit_count" -eq 0 ]; then | |
echo "No commits found between the detected base branch and ${current_branch}." | |
read -rp "Enter the base branch for the PR: " base_branch | |
commit_count=$(git rev-list --count "${base_branch}..${current_branch}") | |
if [ "$commit_count" -eq 0 ]; then | |
echo "No commits found between ${base_branch} and ${current_branch}. Exiting." | |
exit 1 | |
fi | |
fi | |
echo "Base branch: $base_branch" | |
echo "Number of commits from ${base_branch} to ${current_branch}: $commit_count" | |
# Generate the PR title based on the commit count | |
if [ "$commit_count" -eq 1 ]; then | |
echo "The diff contains one commit. Auto-generating PR title from the commit message." | |
pr_title=$(git log "${base_branch}..${current_branch}" -1 --pretty=%s) | |
else | |
echo "Warning: The diff contains ${commit_count} commits." | |
read -rp "Enter the PR title: " pr_title | |
fi | |
# Create a draft PR using the gh command (without a PR body) | |
echo "Creating a draft PR for branch '${current_branch}' against base branch '${base_branch}'..." | |
gh pr create --draft --base "$base_branch" --head "$current_branch" --title "$pr_title" | |
echo "Draft PR has been created." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
branch Add xxx | |
git commit -m "Add xxx" | |
git push origin HEAD | |
create_draft_pr |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function branch | |
if test (count $argv) -eq 0 | |
git branch --sort=-committerdate | |
else | |
set name (echo $argv | \ | |
tr '[:upper:]' '[:lower:]' | \ | |
sed -E 's/[^a-z0-9]+/-/g; s/^-//; s/-$//') | |
set current_date (date +%Y-%m-%d) | |
git checkout -b "tm/$name/$current_date" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment