-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-stack.sh
More file actions
217 lines (181 loc) · 5.83 KB
/
git-stack.sh
File metadata and controls
217 lines (181 loc) · 5.83 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash
set -e
MAX_BRANCH_LENGTH=60
# -----------------------------------------------------------------------
# Function to create a new stack
# -----------------------------------------------------------------------
function usage_create() {
echo "Usage: $0 create -m <commit message>"
}
function create() {
local OPTARG
local msg
while getopts ":m:" opt; do
case $opt in
m)
msg=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage_create
return 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage_create
return 1
;;
esac
done
if [ -z "$msg" ]; then
usage_create
return 1
fi
# replace all non-alphanumeric characters with an underscore
# convert to lowercase
# remove leading and trailing underscores
local branch=$(echo $msg \
| sed 's/[^a-zA-Z0-9]/_/g' \
| tr -dc '[:alnum:]_' \
| tr '[:upper:]' '[:lower:]' \
| sed 's/^_*//' \
| sed 's/_$//' \
)
# prefix with the branch prefix and append the date
branch="${GIT_BRANCH_PREFIX}$(date +%Y%m%d)_${branch}"
# replace multiple underscores with a single underscore
branch=$(echo $branch | tr -s '_')
# if branch is longer than maximum length in characters, truncate it with a 8 character MD5 hash
if [ ${#branch} -gt $MAX_BRANCH_LENGTH ]; then
branch="${branch:0:$((MAX_BRANCH_LENGTH - 9))}_$(echo $msg | md5sum | cut -c1-8)"
fi
echo "Creating git branch $branch"
git checkout -q -b $branch
echo "Adding all files and committing changes"
git add . && git commit -q -m "$msg"
}
# -----------------------------------------------------------------------
# Function to submit a stack
# -----------------------------------------------------------------------
function usage_submit() {
echo "Usage: $0 submit"
}
function submit() {
local OPTARG
while getopts ":" opt; do
case $opt in
\?)
echo "Invalid option: -$OPTARG" >&2
usage_submit
return 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
usage_submit
return 1
;;
esac
done
echo "Pushing branch to remote"
git push -q -u origin $(git branch --show-current)
echo "Creating pull request"
gh pr create --fill -w
}
# -----------------------------------------------------------------------
# Check the prerequisites to ensure the script will run properly
# -----------------------------------------------------------------------
function check_prerequisites() {
# check for environment variable name GIT_BRANCH_PREFIX
if [ -z "$GIT_BRANCH_PREFIX" ]; then
echo "GIT_BRANCH_PREFIX is not set. Set it to your branches prefix (e.g. yourname/)"
return 1
fi
if ! [ -x "$(command -v git)" ]; then
echo "git is not installed"
return 1
fi
if ! [ -x "$(command -v gh)" ]; then
echo "GihHub CLI is not installed. See https://github.com/cli/cli#installation"
return 1
fi
if ! gh auth status &>/dev/null; then
echo "GitHub CLI is not authenticated. Please run 'gh auth login' to authenticate."
return 1
fi
return 0
}
if ! check_prerequisites; then
exit 1
fi
# -----------------------------------------------------------------------
# Function to sync and close branches with closed PRs
# -----------------------------------------------------------------------
function usage_sync() {
echo "Usage: $0 sync"
}
function sync() {
local OPTARG
while getopts ":" opt; do
case $opt in
\?)
echo "Invalid option: -$OPTARG" >&2
usage_sync
return 1
;;
esac
done
echo "Fetching latest changes from remote"
git fetch -q --all --prune
main_branch=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
echo "Checking for closed PRs and removing corresponding local branches"
for branch in $(git for-each-ref --format='%(refname:short)' refs/heads/); do
# Skip master branch
if [[ "$branch" == "$main_branch" ]]; then
continue
fi
# Check if the branch has a closed PR
pr_info=$(gh pr view "$branch" --json state,url -q '{state: .state, url: .url}' 2>/dev/null || echo '{"state":"LOCAL"}')
pr_state=$(echo $pr_info | jq -r '.state')
pr_url=$(echo $pr_info | jq -r '.url // ""')
if [[ "$pr_state" == "CLOSED" || "$pr_state" == "MERGED" ]]; then
echo " $branch [$pr_state] $pr_url"
if [[ "$branch" == "$(git rev-parse --abbrev-ref HEAD)" ]]; then
echo " -> Closed PR: switching to $main_branch."
git checkout -q $main_branch
fi
echo " -> Closed PR: removing local branch"
git branch -q -D "$branch"
fi
done
echo "Pulling latest changes from remote"
git pull -q --prune
echo "Sync complete"
}
# -----------------------------------------------------------------------
# Parse the command and delegate to the appropriate function
# -----------------------------------------------------------------------
function usage() {
echo "Usage: $0 <command> [options]"
echo "Commands:"
echo " create"
echo " submit"
echo " sync"
}
case "$1" in
create)
shift
create "$@"
;;
submit)
shift
submit "$@"
;;
sync)
shift
sync "$@"
;;
*)
usage
exit 1
;;
esac