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