Skip to content

Commit 26fa473

Browse files
authored
Create script to create a PR instead of doing direct push to master (#62)
1 parent 8b4b02b commit 26fa473

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

pr-workflow.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/bash
2+
3+
# Usage: ./pr-workflow.sh <new-branch-name>
4+
5+
set -e
6+
7+
if [ $# -lt 1 ]; then
8+
echo "Usage: $0 <new-branch-name>"
9+
exit 1
10+
fi
11+
12+
NEW_BRANCH="$1"
13+
BASE_BRANCH="master"
14+
15+
# Fetch latest changes from origin
16+
git fetch origin
17+
18+
# Check if there are commits ahead of origin/master
19+
AHEAD_COUNT=$(git rev-list --count HEAD..origin/$BASE_BRANCH)
20+
BEHIND_COUNT=$(git rev-list --count origin/$BASE_BRANCH..HEAD)
21+
22+
if [ "$BEHIND_COUNT" -eq 0 ]; then
23+
echo "No new local commits to push. Nothing to PR."
24+
exit 0
25+
fi
26+
27+
if [ "$AHEAD_COUNT" -ne 0 ]; then
28+
echo "Warning: Your local branch is behind origin/$BASE_BRANCH by $AHEAD_COUNT commits."
29+
echo "It's recommended to pull or rebase before creating a PR."
30+
read -p "Continue anyway? (y/N) " CONT
31+
if [[ ! "$CONT" =~ ^[Yy]$ ]]; then
32+
exit 1
33+
fi
34+
fi
35+
36+
# Create and checkout the new branch from current HEAD
37+
git checkout -b "$NEW_BRANCH"
38+
39+
# Push new branch to origin
40+
git push -u origin "$NEW_BRANCH"
41+
42+
# Create a PR to master, auto-fill the title and body
43+
gh pr create --base "$BASE_BRANCH" --fill
44+
45+
# Go back to the base branch
46+
git checkout "$BASE_BRANCH"
47+
48+
# Delete the new branch
49+
git branch -d $NEW_BRANCH
50+
51+
echo "Pull request created! To merge go to GitHub.com"

0 commit comments

Comments
 (0)