-
Notifications
You must be signed in to change notification settings - Fork 0
/
request
executable file
·191 lines (178 loc) · 6.1 KB
/
request
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
#!/bin/bash
#
# request
# =======
#
# Small script to ease the development with the nested *-source
# directories inside the apes super repositories (treelm, musubi,
# seeder and ateles).
#
# The idea is to work undisturbed on the respective sources, and to
# not have to mind to the git submodule layout at all.
#
# Pre-Requisite: have github cli installed (https://cli.github.com/)
#
# Work as usual on the code in the source subdirectory and have a
# branch for your changes.
# When it is ready to be put into a pull request, run request.
# It takes care of the superrepository and creates the corresponding
# pull requests.
#
# If you make changes that need to be pushed, you can run request on
# your branch again.
#
# request always uses the current branch you are working on in the
# source subdirectory and the pull request is filled in by the commit
# message. There are no arguments it takes for its work.
#
# Please note that this is just a rough script, without bells and
# whistles, it is designed specifically for the one usage scenario,
# and probably easily confused.
#
# If you do provide arguments, those will be passed on to the git
# push command that is used in invocations of updating the PRs.
# It usually shouldn't be needed, but it allows you to force change
# the commits on the branch.
#
# *************************************************************************** #
#
# Copyright (c) 2024 Harald Klimach <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHORS “AS IS” AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL UNIVERSITY OF SIEGEN OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# **************************************************************************** #
if ! gh --version &>/dev/null; then
echo ""
echo "Need to install github cli (https://cli.github.com/) to create"
echo "Pull Requests!"
echo ""
exit 1
fi
owd=$(pwd)
subdir=$(git rev-parse --show-toplevel 2>/dev/null)
subremote=$(git remote get-url origin 2>/dev/null)
subrepogit=$(basename $subremote 2>/dev/null)
subrepo=${subrepogit%.git}
sub=$(basename $subdir 2>/dev/null)
superdir=$subdir/..
remote=$(cd $superdir && git remote get-url origin 2>/dev/null)
repogit=$(basename $remote 2>/dev/null)
repo=${repogit%.git}
match_repo () {
if [ "$1" = "$2" ]; then
return 0
else
echo "Inside $3, but not a $2 repository!"
echo "request only works inside the respective parent repository!"
return 1
fi
}
case "$sub" in
"atl")
if match_repo $repo ateles $sub; then
echo "Working in Ateles"
else
exit 1
fi
;;
"mus")
if match_repo $repo musubi $sub; then
echo "Working in Musubi"
else
exit 1
fi
;;
"sdr")
if match_repo $repo seeder $sub; then
echo "Working in Seeder"
else
exit 1
fi
;;
"tem")
if match_repo $repo treelm $sub; then
echo "Working in Treelm"
else
exit 1
fi
;;
*)
echo "Not in a valid directory ($owd) for request!"
echo ""
echo "Need to be in a source repository of an Apes repository"
echo "(ateles/atl, musubi/mus, seeder/sdr or treelm/tem)."
exit 1
;;
esac
echo "works"
subbranch=$(git branch --show-current)
submsg=$(git show --pretty=format:%s -s HEAD)
echo $subbranch in $subdir
echo "@: $submsg"
echo ""
superbranch=$(cd $superdir && git branch --show-current)
if [ "$subbranch" = "main" ]; then
echo "Not attempting to work on main branch!"
echo "Please create a branch for your work."
exit 1
fi
if output=$(git status --porcelain) && [ -z "$output" ]; then
cd $superdir
if git checkout -b $subbranch 2>/dev/null; then
git submodule set-branch -b $subbranch $sub
else
git checkout $subbranch
fi
git diff --quiet || git commit -a -m "$submsg"
# Create the pull request in the submodule if it does not exist yet.
cd $subdir
PRnum=$(gh pr status --json number,headRefName --jq '.currentBranch.number')
if [ -z "$PRnum" ]; then
gh pr create -d -a @me
else
git push $@
fi
subPR=$(gh pr status --json number,headRefName --jq '.currentBranch.number')
subPRtitle=$(gh pr status --json title --jq '.currentBranch.title')
# Create the pull request in the supermodule if it does not exist yet.
echo ""
echo "Moving to super repository"
echo ""
cd $superdir
PRnum=$(gh pr status --json number,headRefName --jq '.currentBranch.number')
if [ -z "$PRnum" ]; then
gh pr create -d -t "$subPRtitle" -b "See [PR in $subrepo](https://github.com/apes-suite/$subrepo/pull/$subPR)."
else
git push $@
fi
else
echo ""
echo "$sub directory is not clean"
echo "Please make sure that all your changes are committed!"
echo ""
git status
echo ""
echo "If you don't want the outstanding changes to be part"
echo "of your request, stash them, do the request and pop"
echo "them from thes stash again."
fi
cd $owd