be more stylistically standard
[LibreOffice.git] / logerrit
blob8432acde79f5cb339ffe00f9b52aefce28b290db
1 #!/usr/bin/env bash
3 #GERRITHOST=gerrit.libreoffice.org
4 GERRITHOST=logerrit
5 GERRITURL="ssh://$GERRITHOST/core"
7 get_SHA_for_change() {
8 SHA=$(ssh "${GERRITHOST?}" gerrit query --all-approvals change:"$1" | grep ref | tail -1 | cut -d: -f2 | sed 's/^ *//')
11 submit() {
12 BRANCH=$1
13 TYPE=${2:-''}
14 if test -z "$BRANCH"
15 then
16 BRANCH=$(git symbolic-ref HEAD 2> /dev/null)
17 BRANCH="${BRANCH##refs/heads/}"
18 if test -z "$BRANCH"
19 then
20 echo "no branch specified, and could not guess the current branch"
21 exit 1
23 echo "no branch specified, guessing current branch $BRANCH"
26 if [ "$BRANCH" = "master" ]; then
27 WEEKOLDDATE=$(date --date="7 days ago" +%s 2> /dev/null)
28 if [ "$WEEKOLDDATE" = "" ]; then
29 WEEKOLDDATE=$(date -v-7d +%s) # BSD equivalent
31 PARENTDATE=$(git show -s --format=%ct HEAD~1)
32 if [[ $PARENTDATE -lt $WEEKOLDDATE ]]; then
33 echo "Your branch is older than a week, do './g pull -r' and retry"
34 exit 1
37 git push "$GERRITURL" "HEAD:refs/for/$BRANCH$TYPE"
40 logerrit() {
41 echo "Host logerrit gerrit.libreoffice.org"
42 if test -n "${2-}" && test -f "$HOME/.ssh/id_$2"; then
43 echo " IdentityFile ~/.ssh/id_$2"
45 echo " User $1"
46 echo " Port 29418"
47 echo " HostName gerrit.libreoffice.org"
50 case "$1" in
51 help|--help|"")
52 echo "Usage: ./logerrit subcommand [options]"
53 echo "simple and basic tool to interact with LibreOffice gerrit"
54 echo "see https://wiki.documentfoundation.org/Development/gerrit for details."
55 echo
56 echo "subcommands:"
57 echo " setup walking you though your gerrit setup"
58 echo " test test your gerrit setup"
59 echo
60 echo " --- for submitters:"
61 echo " submit [BRANCH] submit your change for review"
62 echo " submit [BRANCH]%private submit your change as private"
63 echo " submit [BRANCH]%wip submit your change as work-in-progress"
64 echo " nextchange [BRANCH] reset branch to the remote to start with the next change"
65 echo " testfeature [BRANCH] [CHANGEID]"
66 echo " trigger a test of a feature branch on gerrit"
67 echo
68 echo "Note: private changes are only visibly to yourself and those that you explicitly add as reviewers."
69 echo "For full documentation, see https://gerrit.libreoffice.org/Documentation/intro-user.html#private-changes"
70 echo
71 echo " --- for reviewers:"
72 echo " checkout CHANGEID checkout the changes for review"
73 echo " pull CHANGEID pull (and merge) the changes on current branch"
74 echo " cherry-pick CHANGEID cherry-pick the change on current branch"
75 echo " patch CHANGEID show the change as a patch"
76 echo " query ... query for changes for review on project core"
77 echo " <any other gerrit command>"
78 echo
79 echo "advanced users should consider using git review instead:"
80 echo "https://wiki.documentfoundation.org/Development/GitReview"
81 exit
83 setup)
84 script_canonical_file=$(readlink -f "$0")
85 script_canonical_dir=$(dirname "$script_canonical_file")
86 if ! cd "$script_canonical_dir"; then
87 echo "Can't cd to $script_canonical_dir"
88 exit 1
90 ssh_home="$HOME/.ssh";
91 ssh_key=
92 created_ssh=
93 if ! test -d "$ssh_home"; then
94 echo "It appears that you have no ssh setup, running ssh-keygen to create that:"
95 mkdir -m0700 "$ssh_home"
96 created_ssh=TRUE
97 echo
98 echo "Hit enter to generate an ssh key - you will need to enter a pass-phrase"
99 echo
100 read -r
101 all_algo="$(ssh -Q key)"
102 if grep -q -x ssh-ed25519 <<< "$all_algo"
103 then
104 algo="ed25519"
105 elif grep -q -x ssh-rsa <<< "$all_algo"
106 then
107 algo="rsa"
108 else
109 echo "Could not find 'ssh-ed25519' or 'ssh-rsa' in the output from 'ssh -Q key'"
110 exit 1
112 ssh-keygen -t "$algo" # Generate the key pair using the selected algorithm
114 if test -d "$ssh_home"; then
115 # order algos based on the PubkeyAcceptedKeyTypes option from OpenSSH 8.1
116 for ssh_key_type in ecdsa ed25519 rsa; do
117 pk="$ssh_home/id_${ssh_key_type}.pub"
118 ssh_key=""
119 if test -f "$pk" && ssh_key="$(< "$pk")" && test -n "$ssh_key"; then
120 break
122 done
124 echo "Please go to https://gerrit.libreoffice.org/ and click the \"Sign in\" link"
125 echo "at the top right of the page. You'll be sent to our Single Sign-On portal"
126 echo "for authentication (create an account if needs be), and automatically"
127 echo "redirected back to gerrit afterwards."
128 echo
129 echo "Visit https://gerrit.libreoffice.org/settings/#SSHKeys and paste the public"
130 if test -z "$ssh_key"; then
131 echo "part of your SSH key in the 'New SSH key' form."
132 else
133 echo "key below in the 'New SSH key' form."
134 echo
135 printf '%s\n' "$ssh_key"
136 echo
138 echo
139 echo "Note that you need to register additional email addresses, if you want to"
140 echo "commit from them. Each additional email address must be confirmed by"
141 echo "following the verification link sent to it."
142 echo
143 read -r -p 'Which user name did you choose? ' GERRITUSER
144 if test -z "$created_ssh"; then
145 echo
146 echo "Please now add the following to your ~/.ssh/config, creating the file if needed:"
147 echo
148 logerrit "$GERRITUSER" ${ssh_key:+"$ssh_key_type"}
149 echo
150 else
151 echo "Automatically creating your ssh config"
152 logerrit "$GERRITUSER" ${ssh_key:+"$ssh_key_type"} >"$ssh_home/config"
154 # setup the remote properly ...
155 git config remote.origin.pushurl ssh://logerrit/core
156 echo "To see if your setup was successful, run './logerrit test' then."
157 # a good place to make sure the hooks are set up
158 ./g -z
160 test)
161 if test -n "$(ssh "$GERRITHOST" 2>&1|grep "Welcome to Gerrit Code Review")"
162 then
163 echo "Your gerrit setup was successful!"
164 else
165 echo "There seems to be trouble. Please have the output of:"
166 echo "ssh -vvvv $GERRITHOST"
167 echo "at hand when looking for help."
170 submit)
171 submit "$2"
173 nextchange)
174 if test -n "$(git status -s -uno)"
175 then
176 echo "You have uncommitted changes. Please commit or stash these:"
177 git status
178 exit 1
180 CHANGEID=$(git log --format=format:%b -1 HEAD|grep Change-Id|cut -d: -f2|tr -d \ )
181 if test -z "$CHANGEID"
182 then
183 CHANGEID="NOCHANGEID"
185 BACKUPBRANCH=backup/$CHANGEID-$(date +%F-%H%M%S)
186 git branch "$BACKUPBRANCH"
187 echo "current state backed up as $BACKUPBRANCH"
188 BRANCH=$2
189 if test -z "$BRANCH"
190 then
191 BRANCH=$(git symbolic-ref HEAD 2> /dev/null)
192 BRANCH="${BRANCH##refs/heads/}"
193 if test -z "$BRANCH"
194 then
195 echo "no branch specified, and could not guess the current branch"
196 exit 1
198 echo "no branch specified, guessing current branch $BRANCH"
200 git reset --hard "remotes/origin/$BRANCH"
202 checkout)
203 get_SHA_for_change "$2"
204 git fetch "$GERRITURL" "$SHA" && git checkout FETCH_HEAD
206 review)
207 echo "'./logerrit review' has been removed as obsolete."
208 echo "Please use either:"
209 echo " - git-review: https://wiki.documentfoundation.org/Development/GitReview"
210 echo " - or the web-UI directly: https://gerrit.libreoffice.org/"
211 echo "Both provide a better experience."
212 exit 1;
214 pull)
215 get_SHA_for_change "$2"
216 git pull "$GERRITURL" "$SHA"
218 cherry-pick)
219 get_SHA_for_change "$2"
220 git fetch "$GERRITURL" "$SHA" && git cherry-pick FETCH_HEAD
222 patch)
223 get_SHA_for_change "$2"
224 git fetch "$GERRITURL" "$SHA" && git format-patch -1 --stdout FETCH_HEAD
226 query)
227 shift
228 ssh "${GERRITHOST?}" gerrit query project:core "${@@Q}"
230 testfeature)
231 CHANGEID=${3#I}
232 if test -n "$3" -a \( ${#3} -ne 41 -o -n "${CHANGEID//[0-9a-f]/}" \)
233 then
234 echo "${3} is not a valid Gerrit change id"
235 exit 1
237 CHANGEID=$3
239 BRANCH=$2
240 if test -z "$BRANCH"
241 then
242 BRANCH=$(git symbolic-ref HEAD 2> /dev/null)
243 BRANCH="${BRANCH##refs/heads/}"
244 if test -z "$BRANCH"
245 then
246 echo "no branch specified, and could not guess the current branch"
247 exit 1
249 echo "no branch specified, guessing current branch $BRANCH"
251 BRANCH="${BRANCH##feature/}"
252 WORKDIR=$(mktemp -d)
253 if test -z "$WORKDIR"
254 then
255 echo "could not create work directory."
256 exit 1
258 echo "workdir at $WORKDIR"
259 git clone -s "$(dirname "$0")" "$WORKDIR/core"
261 pushd "$WORKDIR/core" || { echo "Changing directory failed."; exit 1; }
262 echo "noop commit: trigger test build for branch feature/$BRANCH" > ../commitmsg
263 echo >> ../commitmsg
264 echo "branch is at:" >> ../commitmsg
265 echo >> ../commitmsg
266 git log -1|sed -e "s/Change-Id:/XXXXXX:/" >> ../commitmsg
267 if test -n "$CHANGEID"
268 then
269 echo >> ../commitmsg
270 echo "Change-Id: $CHANGEID" >> ../commitmsg
272 git fetch https://git.libreoffice.org/core "feature/$BRANCH" && \
273 git checkout -b featuretst FETCH_HEAD && \
274 cp -a .git-hooks/* .git/hooks && \
275 git commit --allow-empty -F ../commitmsg && \
276 git push "$GERRITURL" "HEAD:refs/for/feature/$BRANCH"
277 popd || { echo "Changing directory failed."; exit 1; }
279 rm -rf "$WORKDIR/core"
280 rm -f "$WORKDIR/commitmsg"
281 rmdir "$WORKDIR"
284 ssh "${GERRITHOST?}" gerrit "${@@Q}"
286 esac
288 # vim: set noet sw=4 ts=4: