logerrit 8.34 KB
Newer Older
1
#!/bin/bash
2 3 4 5 6 7

#GERRITHOST=gerrit.libreoffice.org
GERRITHOST=logerrit
GERRITURL=ssh://$GERRITHOST/core

get_SHA_for_change() {
8
    SHA=$(ssh ${GERRITHOST?} gerrit query --all-approvals change:$1|grep ref|tail -1|cut -d: -f2)
9 10
}

11
submit() {
12 13 14 15
        TYPE=$1
        BRANCH=$2
        if test -z "$BRANCH"
        then
16
            BRANCH=$(git symbolic-ref HEAD 2> /dev/null)
17 18 19 20 21 22 23 24 25
            BRANCH="${BRANCH##refs/heads/}"
            if test -z "$BRANCH"
            then
                echo "no branch specified, and could not guess the current branch"
                exit 1
            fi
            echo "no branch specified, guessing current branch $BRANCH"
        fi
        git push $GERRITURL HEAD:refs/$TYPE/$BRANCH
26 27
}

28
logerrit() {
29
        echo "Host logerrit gerrit.libreoffice.org"
30 31 32 33 34 35
        echo "    IdentityFile ~/.ssh/id_rsa"
        echo "    User $1"
        echo "    Port 29418"
        echo "    HostName gerrit.libreoffice.org"
}

36
case "$1" in
37 38 39
    help|--help|"")
        echo "Usage: ./logerrit subcommand [options]"
        echo "simple and basic tool to interact with LibreOffice gerrit"
40 41
        echo "see https://wiki.documentfoundation.org/Development/gerrit for details."
        echo
42 43 44 45 46 47 48 49
        echo "subcommands:"
        echo "             setup                   walking you though your gerrit setup"
        echo "             test                    test your gerrit setup"
        echo
        echo " --- for submitters:"
        echo "             submit [BRANCH]         submit your change for review"
        echo "             submit-draft [BRANCH]   submit your change as draft"
        echo "             nextchange [BRANCH]     reset branch to the remote to start with the next change"
50
        echo "             testfeature [BRANCH]    trigger a test of a feature branch on gerrit"
51 52 53 54 55 56 57 58 59 60 61 62 63 64
        echo "Note: drafts are only visibly to yourself and those that you explicitly add as reviewers."
        echo
        echo " --- for reviewers:"
        echo "             checkout CHANGEID       checkout the changes for review"
        echo "             pull CHANGEID           pull (and merge) the changes on current branch"
        echo "             cherry-pick CHANGEID    cherry-pick the change on current branch"
        echo "             patch CHANGEID          show the change as a patch"
        echo "             query ....              query for changes for review on project core"
        echo "             <any other gerrit command>"
        echo
        echo "advanced users should consider using git review instead:"
        echo "http://wiki.documentfoundation.org/Development/GitReview"
        exit
    ;;
65
    setup)
66 67 68 69 70 71
        script_canonical_file=$(readlink -f "$0")
        script_canonical_dir=$(dirname "$script_canonical_file")
        if ! cd "$script_canonical_dir"; then
            echo "Can't cd to $script_canonical_dir"
            exit 1
        fi
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	ssh_home="$HOME/.ssh";
	ssh_key=
	created_ssh=
	if ! test -d $ssh_home; then
	    echo "It appears that you have no ssh setup, running ssh-keygen to create that:"
	    mkdir $ssh_home
	    chmod 0700 $ssh_home
	    created_ssh=TRUE
	    echo
	    echo "Hit enter to generate an ssh key - you will need to enter a pass-phrase"
	    echo
	    read
	    ssh-keygen -t rsa -f "$ssh_home/id_rsa"
	fi
	if test -d $ssh_home; then
	    if test -f "$ssh_home/id_rsa.pub"; then
88
		ssh_key=$(cat $ssh_home/id_rsa.pub);
89
	    elif test -f "$ssh_home/id_dsa.pub"; then
90
		ssh_key=$(cat $ssh_home/id_dsa.pub);
91 92
	    fi
	fi
93
        echo "Please go to https://gerrit.libreoffice.org/ and:"
94
        echo "- press the 'register' button in the top right corner"
95
        echo "- after login set yourself a username (its recommended to use your IRC-nick)"
96 97 98 99 100 101 102 103
	if test "z$ssh_key" = "z"; then
            echo "- add your public ssh-key into the ssh keys settings."
	else
	    echo "- paste the key below into the 'Add SSH Public Key' box."
	    echo
	    echo "$ssh_key"
	    echo
	fi
104 105 106
        echo
        echo "Note that you need to register additional email addresses, if you want to"
        echo "commit from them. Additional emails must be confirmed with repling to the"
Miklos Vajna's avatar
Miklos Vajna committed
107
        echo "invitation mail it sends you."
108
        echo
109
        read -p 'Which user name did you choose? ' GERRITUSER
110 111 112 113 114 115 116 117 118 119 120 121
	if test "z$created_ssh" = "z"; then
            echo
            echo "Please now add the following to your ~/.ssh/config, creating the file if needed:"
            echo
	    logerrit $GERRITUSER
            echo
        else
	    echo "Automatically creating your ssh config"
	    (logerrit $GERRITUSER) > "$ssh_home/config"
	fi
	# setup the remote properly ...
	git config remote.origin.pushurl ssh://logerrit/core
122
        echo "To see if your setup was successful, run './logerrit test' then."
123 124
        # a good place to make sure the hooks are set up
        ./g -z
125
    ;;
126
    test)
127
        if test -n "$(ssh $GERRITHOST 2>&1|grep "Welcome to Gerrit Code Review")"
128 129 130
        then
            echo "Your gerrit setup was successful!"
        else
131 132
            echo "There seems to be trouble. Please have the output of:"
            echo "ssh -vvvv "$GERRITHOST
133 134 135 136 137 138 139 140 141 142
            echo "at hand when looking for help."
        fi
    ;;
    submit)
        submit 'for' $2
    ;;
    submit-draft)
        submit drafts $2
    ;;
    nextchange)
143
        if test -n "$(git status -s -uno)"
144 145 146 147 148
        then
            echo "You have uncommitted changes. Please commit or stash these:"
            git status
            exit 1
        fi
149
        CHANGEID=$(git log --format=format:%b -1 HEAD|grep Change-Id|cut -d: -f2|tr -d \ )
150 151 152 153
        if test -z "$CHANGEID"
        then
            CHANGEID="NOCHANGEID"
        fi
154
        BACKUPBRANCH=backup/$CHANGEID-$(date +%F-%H%M%S)
155 156 157 158 159
        git branch $BACKUPBRANCH
        echo "current state backed up as $BACKUPBRANCH"
        BRANCH=$2
        if test -z "$BRANCH"
        then
160
            BRANCH=$(git symbolic-ref HEAD 2> /dev/null)
161 162 163 164 165 166 167 168 169 170 171 172 173 174
            BRANCH="${BRANCH##refs/heads/}"
            if test -z "$BRANCH"
            then
                echo "no branch specified, and could not guess the current branch"
                exit 1
            fi
            echo "no branch specified, guessing current branch $BRANCH"
        fi
        git reset --hard remotes/origin/$BRANCH
    ;;
    checkout)
        get_SHA_for_change $2
        git fetch $GERRITURL $SHA && git checkout FETCH_HEAD
    ;;
175 176 177 178 179 180 181 182
    review)
        echo "'./logerrit review' has be removed as obsolete."
        echo "Please use either:"
        echo " - git-review:              https://wiki.documentfoundation.org/Development/GitReview"
        echo " - or the web-UI directly:  https://gerrit.libreoffice.org/"
        echo "Both provide a better experience."
        exit 1;
    ;;
183 184 185 186 187 188 189 190 191 192 193 194 195 196
    pull)
        get_SHA_for_change $2
        git pull $GERRITURL $SHA
    ;;
    cherry-pick)
        get_SHA_for_change $2
        git fetch $GERRITURL $SHA && git cherry-pick FETCH_HEAD
    ;;
    patch)
        get_SHA_for_change $2
        git fetch $GERRITURL $SHA && git format-patch -1 --stdout FETCH_HEAD
    ;;
    query)
        shift
197
        ssh ${GERRITHOST?} gerrit query project:core "$@"
198
    ;;
199 200 201 202
    testfeature)
        BRANCH=$2
        if test -z "$BRANCH"
        then
203
            BRANCH=$(git symbolic-ref HEAD 2> /dev/null)
204 205 206 207 208 209 210 211 212
            BRANCH="${BRANCH##refs/heads/}"
            if test -z "$BRANCH"
            then
                echo "no branch specified, and could not guess the current branch"
                exit 1
            fi
            echo "no branch specified, guessing current branch $BRANCH"
        fi
        BRANCH="${BRANCH##feature/}"
213
        WORKDIR=$(mktemp -d)
214 215 216 217 218 219
        if test -z "$WORKDIR"
        then
            echo "could no create work directory."
            exit 1
        fi
        echo workdir at $WORKDIR
220
        git clone -s "$(dirname $0)" $WORKDIR/core
221 222 223 224 225 226 227 228 229 230 231 232 233
        pushd $WORKDIR/core
        echo "noop commit: trigger test build for branch feature/$BRANCH" > ../commitmsg
        echo >> ../commitmsg
        echo "branch is at:" >> ../commitmsg
        git log -1|sed -e "s/Change-Id:/XXXXXX:/" >> ../commitmsg
        git fetch git://gerrit.libreoffice.org/core.git feature/$BRANCH && \
            git checkout -b featuretst FETCH_HEAD && \
            cp -a .git-hooks/* .git/hooks
            git commit --allow-empty -F ../commitmsg && \
            git push $GERRITURL HEAD:refs/for/feature/$BRANCH
        popd
        rm -rf $WORKDIR/core
    ;;
234
    *)
235
        ssh ${GERRITHOST?} gerrit "$@"
236
    ;;
237
esac