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