@@ -13,6 +13,19 @@ ${bold}NAME${normal}
13
13
${bold} SYNOPSIS${normal}
14
14
git create-pr
15
15
16
+ ${bold} Usage: git create-pr [--help]${normal}
17
+
18
+ Create a pull request for the current branch in the terminal.
19
+
20
+ ${bold} Options:${normal}
21
+ --help Display this help message and exit.
22
+
23
+ This script performs the following actions:
24
+ 1. Ensures the current branch is up-to-date with the upstream default branch (target branch).
25
+ 2. Automatically determines the name of the remote (e.g., "origin").
26
+ 3. Automatically determines the remote default branch (target branch) for which to create the pull request.
27
+ 4. Uses the first commit on the branch as the pull request title and description without opening an editor.
28
+
16
29
EOF
17
30
}
18
31
@@ -28,20 +41,49 @@ while [[ $1 = -?* ]]; do
28
41
shift
29
42
done
30
43
31
- # echo "branch..."
32
- branch=$( git rev-parse --abbrev-ref HEAD)
33
- defaultBranch=$( git remote show origin | awk ' /HEAD branch/ {print $NF}' )
34
- # echo "upstream..."
35
- # upstream=$( git rev-parse --abbrev-ref @{upstream} || "" )
36
-
37
- # echo "does it track?"
38
- # if [[ "${upstream}" != "" ]]; then
39
- # echo "branch '$branch' tracks '$upstream'"
40
- # git push
41
- # else
42
- # echo "branch '$branch' has no upstream configured"
43
- git push -u origin $branch
44
- # fi
45
-
46
- hub pull-request -b $defaultBranch
44
+ # Check if hub is installed
45
+ if ! command -v hub > /dev/null 2>&1 ; then
46
+ echo " Error: 'hub' command not found. Please install 'hub' first."
47
+ exit 1
48
+ fi
49
+
50
+ # Get the current branch name
51
+ current_branch=" $( git rev-parse --abbrev-ref HEAD) "
52
+
53
+ remote_name=" origin"
54
+
55
+ # Get the remote URL
56
+ remote_url=" $( git remote get-url " $remote_name " ) "
57
+
58
+ # Regex pattern to extract org/repo from a URL such as git@example.com:org/repo.git
59
+ pattern=' [.:\/]([[:alnum:]_-]+\/[[:alnum:]_-]+)\.git'
60
+
61
+ # Extract org/repo using regex
62
+ if [[ $remote_url =~ $pattern ]]; then
63
+ upstream_repo=" ${BASH_REMATCH[1]} "
64
+ else
65
+ # exit on error
66
+ echo " Error: Could not extract org/repo from remote URL: $remote_url "
67
+ exit 1
68
+ fi
69
+
70
+ # Get the remote default branch (target branch)
71
+ upstream_default_branch=" $( hub api " repos/$upstream_repo " | jq -r ' .default_branch' ) "
72
+
73
+ # Exit with error if upstream_default_branch is empty
74
+ if [ -z " $upstream_default_branch " ]; then
75
+ echo " Error: Could not get the default branch for $upstream_repo "
76
+ exit 1
77
+ fi
78
+
79
+ # Ensure the current branch is up-to-date with the upstream default branch (target branch)
80
+ git fetch " $remote_name " " $upstream_default_branch "
81
+ git rebase " $remote_name /$upstream_default_branch "
82
+
83
+ # Push the current branch to the remote repository
84
+ git push -u " $remote_name " " $current_branch "
85
+
86
+ # Create a pull request using 'hub pull-request' against the remote default branch
87
+ hub pull-request --no-edit -b " $upstream_default_branch "
88
+
47
89
0 commit comments