Skip to content

Commit 05b207e

Browse files
committed
sync-upstream: allows providing the local branch via cli
1 parent ff33018 commit 05b207e

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

contrib/sync-upstream.sh

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
set -eou pipefail
44

55
help() {
6-
echo "$0 range [end]"
7-
echo " merges every merge commit present in upstream and missing locally."
6+
echo "$0 [-b <branch>] range [end]"
7+
echo " merges every merge commit present in upstream and missing in <branch> (default: master)."
88
echo " If the optional [end] commit is provided, only merges up to [end]."
9+
echo " If the optional [-b branch] provided, then ."
910
echo
10-
echo "$0 select <commit> ... <commit>"
11-
echo " merges every selected merge commit"
11+
echo "$0 [-b <branch>] select <commit> ... <commit>"
12+
echo " merges every selected merge commit into <branch> (default: master)"
1213
echo
1314
echo "This tool creates a branch and a script that can be executed to create the"
1415
echo "PR automatically. The script requires the github-cli tool (aka gh)."
@@ -17,12 +18,9 @@ help() {
1718
exit 1
1819
}
1920

20-
if [ "$#" -lt 1 ]; then
21-
help
22-
fi
23-
2421
REMOTE=upstream
2522
REMOTE_BRANCH="$REMOTE/master"
23+
LOCAL_BRANCH="master"
2624
# Makes sure you have a remote "upstream" that is up-to-date
2725
setup() {
2826
ret=0
@@ -41,7 +39,7 @@ setup() {
4139
}
4240

4341
range() {
44-
RANGESTART_COMMIT=$(git merge-base "$REMOTE_BRANCH" master)
42+
RANGESTART_COMMIT=$(git merge-base "$REMOTE_BRANCH" "$LOCAL_BRANCH")
4543
RANGEEND_COMMIT=$(git rev-parse "$REMOTE_BRANCH")
4644
if [ "$#" = 1 ]; then
4745
RANGEEND_COMMIT=$1
@@ -57,18 +55,37 @@ range() {
5755
esac
5856
}
5957

58+
# Process -b <branch> argument
59+
while getopts "b:" opt; do
60+
case $opt in
61+
b)
62+
LOCAL_BRANCH=$OPTARG
63+
;;
64+
\?)
65+
echo "Invalid option: -$OPTARG" >&2
66+
;;
67+
esac
68+
done
69+
70+
# Shift off the processed options
71+
shift $((OPTIND -1))
72+
73+
if [ "$#" -lt 1 ]; then
74+
help
75+
fi
76+
6077
case $1 in
6178
range)
6279
shift
6380
setup
6481
range "$@"
65-
REPRODUCE_COMMAND="$0 range $RANGEEND_COMMIT"
82+
REPRODUCE_COMMAND="$0 range -b $LOCAL_BRANCH $RANGEEND_COMMIT"
6683
;;
6784
select)
6885
shift
6986
setup
7087
COMMITS=$*
71-
REPRODUCE_COMMAND="$0 select $@"
88+
REPRODUCE_COMMAND="$0 select -b $LOCAL_BRANCH $@"
7289
;;
7390
help)
7491
help
@@ -96,7 +113,7 @@ echo "-----------------------------------"
96113
echo "$BODY"
97114
echo "-----------------------------------"
98115
# Create branch from PR commit and create PR
99-
git checkout master
116+
git checkout "$LOCAL_BRANCH"
100117
git pull --autostash
101118
git checkout -b temp-merge-"$PRNUM"
102119

@@ -115,7 +132,7 @@ cat <<EOT > "$FNAME"
115132
#!/bin/sh
116133
gh pr create -t '$TITLE' -b '$BODY' --web
117134
# Remove temporary branch
118-
git checkout master
135+
git checkout "$LOCAL_BRANCH"
119136
git branch -D temp-merge-"$PRNUM"
120137
EOT
121138
chmod +x "$FNAME"

0 commit comments

Comments
 (0)