@@ -15,19 +15,26 @@ get_remote_branch () {
15
15
echo " ${branch[0]} "
16
16
}
17
17
18
+ branch_exists () {
19
+ git rev-parse -q --verify " 1ドル " > /dev/null
20
+ }
21
+
18
22
opt_discover=0
19
23
opt_branch=0
20
24
opt_update=0
21
- opt_keep=0
22
- while getopts dbuk ch; do
25
+ opt_cleanup=0
26
+ to_rev=HEAD
27
+ while getopts dbukt: ch; do
23
28
case $ch in
29
+ (t) to_rev=$OPTARG
30
+ ;;
24
31
(d) opt_discover=1
25
32
;;
26
33
(b) opt_branch=1
27
34
;;
28
35
(u) opt_update=1
29
36
;;
30
- (k) opt_keep =1
37
+ (k) opt_cleanup =1
31
38
;;
32
39
esac
33
40
done
@@ -42,12 +49,9 @@ base=1ドル
42
49
remote=${2:- origin}
43
50
target=$( git remote get-url $remote | awk -F/ ' {printf "%s/%s", $(NF-1), $NF}' )
44
51
45
- echo " base: $base "
46
- echo " target: $target "
47
-
48
52
old_head=$( git rev-parse --abbrev-ref HEAD)
49
53
50
- revs=($( git log --reverse --grep ' ^x-branch:' --format=%H ${base} ..HEAD ) )
54
+ revs=($( git log --reverse --grep ' ^x-branch:' --format=%H ${base} ..${to_rev} ) )
51
55
branches=()
52
56
for rev in " ${revs[@]} " ; do
53
57
remote_branch=$( get_remote_branch $rev ) || exit 1
@@ -64,25 +68,33 @@ for (( i=0; i<${#revs[*]}; i++ )); do
64
68
rev=${revs[i]}
65
69
remote_branch=${branches[i]}
66
70
67
- echo " BRANCH $remote_branch @${rev: 0: 7} "
68
- git branch -q -D $remote_branch > /dev/null 2>&1 || :
69
- git checkout -q -b $remote_branch $rev
71
+ if branch_exists $remote_branch ; then
72
+ echo " BRANCH $remote_branch @${rev: 0: 7} -- exists"
73
+ git checkout -q $remote_branch
74
+ git reset -q --hard $rev
75
+ else
76
+ echo " BRANCH $remote_branch @${rev: 0: 7} -- create"
77
+ git branch -q -D $remote_branch > /dev/null 2>&1 || :
78
+ git checkout -q -b $remote_branch $rev
79
+ fi
70
80
71
81
(( $opt_branch == 1 )) && continue
72
82
73
83
echo " PUSH ${rev: 0: 7} -> $remote_branch "
74
84
git push -u -f origin $remote_branch
75
85
76
- if (( $opt_update == 0 )) ; then
77
- if ! gh pr view > /dev/null 2>&1 ; then
78
- echo " PR $remote_branch @${rev: 0: 7} "
79
- gh pr create \
80
- -t " $( git show --quiet --format=%s) " \
81
- -b " $( git show --quiet --format=%b | grep -v ' ^x-branch:' ) "
82
- fi
86
+ (( $opt_update == 1 )) && continue
87
+
88
+ if gh pr view > /dev/null 2>&1 ; then
89
+ echo " PR $remote_branch @${rev: 0: 7} -- exists"
90
+ else
91
+ echo " PR $remote_branch @${rev: 0: 7} -- creating"
92
+ gh pr create \
93
+ -t " $( git show --quiet --format=%s) " \
94
+ -b " $( git show --quiet --format=%b | grep -v ' ^x-branch:' ) "
83
95
fi
84
96
85
- if (( $opt_keep == 0 )) ; then
97
+ if (( $opt_cleanup == 1 )) ; then
86
98
echo " DELETE $remote_branch "
87
99
git checkout -q --detach
88
100
git branch -D $remote_branch > /dev/null 2>&1
0 commit comments