-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree2m
More file actions
executable file
·137 lines (124 loc) · 3.85 KB
/
Copy pathtree2m
File metadata and controls
executable file
·137 lines (124 loc) · 3.85 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env bash
# Create or reuse a work branch, commit all current changes, push it, and open or reuse a PR.
# Then wait for CI and merge the PR.
#
# Usage:
# ./tree2m [--remote origin] [--base BRANCH] [--title TITLE] [--body BODY] [--draft] [--auto] <branch> <commit-message>
#
# Options must appear before <branch>. The PR title defaults to the commit message. If the branch
# or PR already exists, tree2m reuses it instead of creating a duplicate.
# --auto: use GitHub merge queue / auto-merge instead of immediate merge. Requires merge queue
# or auto-merge to be enabled in the repo's branch protection settings.
set -euo pipefail
usage() {
sed -n '3,8p' "$0" | sed 's/^# \{0,1\}//'
}
remote="origin"
base=""
title=""
body=""
draft=false
auto=false
while [[ $# -gt 0 ]]; do
case "$1" in
--remote)
remote="${2:-}"
[[ -n "$remote" ]] || { echo "--remote requires a value" >&2; exit 1; }
shift 2
;;
--base)
base="${2:-}"
[[ -n "$base" ]] || { echo "--base requires a value" >&2; exit 1; }
shift 2
;;
--title)
title="${2:-}"
[[ -n "$title" ]] || { echo "--title requires a value" >&2; exit 1; }
shift 2
;;
--body)
body="${2:-}"
[[ -n "$body" ]] || { echo "--body requires a value" >&2; exit 1; }
shift 2
;;
--draft)
draft=true
shift
;;
--auto)
auto=true
shift
;;
-h|--help)
usage
exit 0
;;
--)
shift
break
;;
-*)
echo "Unknown option: $1" >&2
usage >&2
exit 1
;;
*)
break
;;
esac
done
branch="${1:-}"
message="${2:-}"
if [[ -z "$branch" || -z "$message" || $# -gt 2 ]]; then
usage >&2
exit 1
fi
command -v rtk >/dev/null 2>&1 || { echo "Missing required command: rtk" >&2; exit 1; }
command -v git >/dev/null 2>&1 || { echo "Missing required command: git" >&2; exit 1; }
command -v gh >/dev/null 2>&1 || { echo "Missing required command: gh" >&2; exit 1; }
if [[ -z "$base" ]]; then
base="$(rtk git symbolic-ref --quiet --short "refs/remotes/${remote}/HEAD" 2>/dev/null | sed "s#^${remote}/##")"
fi
[[ -n "$base" ]] || base="master"
[[ -n "$title" ]] || title="$message"
[[ -n "$body" ]] || body="Created by ./tree2m."
current_branch="$(rtk git branch --show-current)"
if [[ "$current_branch" == "$branch" ]]; then
:
elif rtk git show-ref --verify --quiet "refs/heads/${branch}"; then
rtk git switch --quiet "$branch"
elif rtk git show-ref --verify --quiet "refs/remotes/${remote}/${branch}"; then
rtk git switch --quiet --track -c "$branch" "${remote}/${branch}"
else
rtk git switch --quiet -c "$branch"
fi
rtk git add -A
rtk git commit --quiet -m "$message" 2>/dev/null || true # nothing-to-commit is fine
rtk git push --quiet -u "$remote" "$branch"
existing_pr_url="$(rtk gh pr list --head "$branch" --json url --jq '.[0].url' 2>/dev/null || true)"
if [[ -n "$existing_pr_url" ]]; then
echo "$existing_pr_url"
else
pr_args=(pr create --base "$base" --head "$branch" --title "$title" --body "$body")
if [[ "$draft" == "true" ]]; then
pr_args+=(--draft)
fi
rtk gh "${pr_args[@]}" | grep -E '^https://' || true # only PR URL
fi
pr_ref="$(rtk gh pr list --head "$branch" --json number --jq '.[0].number' 2>/dev/null || true)"
[[ -n "$pr_ref" ]] || pr_ref="$branch"
if [[ "$draft" == "true" ]]; then
rtk gh pr ready "$pr_ref" >/dev/null
fi
if [[ "$auto" == "true" ]]; then
rtk gh pr merge "$pr_ref" --auto --squash --delete-branch >/dev/null
while true; do
state="$(rtk gh pr view "$pr_ref" --json state --jq '.state' 2>/dev/null || echo "MERGED")"
[[ "$state" == "MERGED" ]] && break
[[ "$state" == "CLOSED" ]] && { echo "PR closed without merging" >&2; exit 1; }
sleep 30
done
else
rtk scripts/check-push-workflow.sh --branch "$branch"
rtk gh pr merge "$pr_ref" --squash --delete-branch >/dev/null
fi