-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathupdate-mine
More file actions
executable file
·149 lines (128 loc) · 3.62 KB
/
update-mine
File metadata and controls
executable file
·149 lines (128 loc) · 3.62 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
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env bash
set -euo pipefail
# Initialize flags
debug=false
all_branches=false
SCRIPT_NAME=$(basename "$0")
# Function to display usage information
usage() {
local exit_code=${1:-0}
cat <<EOF
Usage: $SCRIPT_NAME [--debug] [--all] <reference_branch>
By default, updates branches with open pull requests authored by you by:
1. Checking out the branch.
2. Merging the specified reference branch into it.
3. Pushing the updated branch to the remote.
Options:
--debug Enable debugging output.
--all Update ALL non-protected branches on the remote (not just yours).
Use with caution - this may update branches owned by others.
--help Show this help message and exit.
Arguments:
<reference_branch> The branch to merge into your branches (e.g., 'main').
Examples:
$SCRIPT_NAME main
$SCRIPT_NAME --debug main
$SCRIPT_NAME --all main
$SCRIPT_NAME --debug --all main
EOF
exit "$exit_code"
}
# Parse command-line arguments
reference_branch=""
while [[ $# -gt 0 ]]; do
case "$1" in
--debug)
debug=true
shift
;;
--all)
all_branches=true
shift
;;
--help)
usage
;;
-*)
echo "Unknown option: $1" >&2
usage 1
;;
*)
if [ -z "$reference_branch" ]; then
reference_branch="$1"
else
echo "Error: Multiple reference branches specified: '$reference_branch' and '$1'" >&2
usage 1
fi
shift
;;
esac
done
# Validate that a reference branch was provided
if [ -z "$reference_branch" ]; then
echo "Error: Missing <reference_branch> argument." >&2
echo "Use --help for usage information." >&2
exit 1
fi
# Enable debugging if --debug flag is set
if [ "$debug" = true ]; then
set -x
fi
# Ensure we are inside a Git repository
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
echo "Error: Not inside a Git repository." >&2
exit 1
fi
# Ensure gh CLI is installed
if ! command -v gh >/dev/null 2>&1; then
echo "Error: 'gh' (GitHub CLI) is required but not installed." >&2
echo "Install it from: https://cli.github.com/" >&2
exit 1
fi
# Fetch latest remote branches to avoid using stale references
git remote update >/dev/null 2>&1
# Determine the list of branches to update
if [ "$all_branches" = true ]; then
echo "Fetching all non-protected branches (use with caution)..."
if ! branches=$(gh api repos/:owner/:repo/branches --paginate --jq '.[] | select(.protected == false) | .name' 2>&1); then
echo "Error: Failed to fetch branches from GitHub API." >&2
echo "$branches" >&2
exit 1
fi
else
echo "Fetching branches with open PRs authored by you..."
if ! branches=$(gh pr list --author "@me" --state open --json headRefName -q '.[].headRefName' 2>&1); then
echo "Error: Failed to list PRs. Is 'gh' installed and authenticated?" >&2
echo "$branches" >&2
exit 1
fi
fi
# Iterate through branches and update them
if [ -z "$branches" ]; then
echo "No branches found."
exit 0
fi
while read -r branch; do
# Skip empty lines
[ -z "$branch" ] && continue
echo "Processing branch: $branch"
# Checkout the branch (create tracking branch if it only exists on remote)
if ! git checkout "$branch" 2>/dev/null; then
if ! git switch --track "origin/$branch" 2>/dev/null; then
echo "Error: Failed to checkout branch $branch. Skipping..."
continue
fi
fi
# Merge the reference branch into the current branch
if ! mergewith "$reference_branch"; then
echo "Error: Failed to merge $reference_branch into $branch. Skipping..."
git merge --abort >/dev/null 2>&1
continue
fi
# Push the updated branch
if git push origin "$branch"; then
echo "Successfully updated and pushed branch: $branch"
else
echo "Error: Failed to push branch $branch. Skipping..."
fi
done <<<"$branches"