Matthias Sohn | 375e974 | 2017-09-08 17:38:31 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (C) 2017 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # -------------------------------------------------------- |
| 17 | # Install this hook script as post-receive hook in replicated repositories |
Matthias Sohn | d8182ba | 2019-12-09 14:50:23 +0100 | [diff] [blame] | 18 | # hosted by a gerrit replica which are updated by push replication from the |
| 19 | # corresponding gerrit primary node. |
Matthias Sohn | 375e974 | 2017-09-08 17:38:31 +0200 | [diff] [blame] | 20 | # |
Matthias Sohn | d8182ba | 2019-12-09 14:50:23 +0100 | [diff] [blame] | 21 | # In the gerrit primary node configure the replication plugin to push changes from |
Matthias Sohn | 375e974 | 2017-09-08 17:38:31 +0200 | [diff] [blame] | 22 | # refs/changes/ to refs/tmp/changes/ |
| 23 | # remote.NAME.push = +refs/changes/*:refs/tmp/changes/* |
| 24 | # remote.NAME.push = +refs/heads/*:refs/heads/* |
| 25 | # remote.NAME.push = +refs/tags/*:refs/tags/* |
Sven Selberg | 925b5df | 2019-09-10 12:25:26 +0200 | [diff] [blame] | 26 | # And if it's a Gerrit mirror: |
| 27 | # remote.NAME.push = +refs/meta/*:refs/meta/* |
Matthias Sohn | 375e974 | 2017-09-08 17:38:31 +0200 | [diff] [blame] | 28 | # |
Matthias Sohn | d8182ba | 2019-12-09 14:50:23 +0100 | [diff] [blame] | 29 | # In the replicated repository in the gerrit replica configure |
Matthias Sohn | 375e974 | 2017-09-08 17:38:31 +0200 | [diff] [blame] | 30 | # receive.hideRefs = refs/changes/ |
| 31 | # in order to not advertise the big number of refs in this namespace when |
Matthias Sohn | d8182ba | 2019-12-09 14:50:23 +0100 | [diff] [blame] | 32 | # the gerrit primary's replication plugin is pushing a change |
Matthias Sohn | 375e974 | 2017-09-08 17:38:31 +0200 | [diff] [blame] | 33 | # |
| 34 | # Whenever a ref under refs/tmp/changes/ is arriving this hook will move it |
| 35 | # to refs/changes/. This helps to avoid the large overhead of advertising all |
Matthias Sohn | d8182ba | 2019-12-09 14:50:23 +0100 | [diff] [blame] | 36 | # refs/changes/ refs to the gerrit primary when it replicates changes to the |
| 37 | # replica. |
Matthias Sohn | 375e974 | 2017-09-08 17:38:31 +0200 | [diff] [blame] | 38 | # |
| 39 | # Make this script executable then link to it in the repository you would like |
| 40 | # to use it in. |
| 41 | # cd /path/to/your/repository.git |
| 42 | # ln -sf <shared hooks directory>/post-receive-move-tmp-refs hooks/post-receive |
| 43 | # |
Matthias Sohn | d8182ba | 2019-12-09 14:50:23 +0100 | [diff] [blame] | 44 | # If you want to use this by default for repositories on the Gerrit replica you |
Matthias Sohn | 375e974 | 2017-09-08 17:38:31 +0200 | [diff] [blame] | 45 | # can set up a git template directory $TEMPLATE_DIR/hooks/post-receive and |
| 46 | # configure init.templateDir in the ~/.gitconfig of the user that receives the |
| 47 | # replication on the mirror host. That way when a new repository is created on |
Matthias Sohn | d8182ba | 2019-12-09 14:50:23 +0100 | [diff] [blame] | 48 | # the primary and hence on the mirror (if configured that way) it will |
Matthias Sohn | 375e974 | 2017-09-08 17:38:31 +0200 | [diff] [blame] | 49 | # automatically have the "tmp-refs" commit hook installed. |
| 50 | # See https://git-scm.com/docs/git-init#_template_directory for details. |
| 51 | |
Matthias Sohn | 375e974 | 2017-09-08 17:38:31 +0200 | [diff] [blame] | 52 | # move new changes arriving under refs/tmp/changes/ to refs/changes/ |
| 53 | mv_tmp_refs() |
| 54 | { |
| 55 | oldrev=$1 |
| 56 | newrev=$2 |
| 57 | refname=$3 |
Sven Selberg | 925b5df | 2019-09-10 12:25:26 +0200 | [diff] [blame] | 58 | case "$refname" in refs/tmp/changes/*) |
Matthias Sohn | 375e974 | 2017-09-08 17:38:31 +0200 | [diff] [blame] | 59 | short_refname=${refname##refs/tmp/changes/} |
Sven Selberg | 925b5df | 2019-09-10 12:25:26 +0200 | [diff] [blame] | 60 | $(git update-ref refs/changes/$short_refname $newrev 2>/dev/null) |
Matthias Sohn | 375e974 | 2017-09-08 17:38:31 +0200 | [diff] [blame] | 61 | $(git update-ref -d $refname $newrev 2>/dev/null) |
| 62 | echo "moved \"$refname\" to \"refs/changes/$short_refname\"" |
| 63 | ;; |
| 64 | esac |
| 65 | return 0 |
| 66 | } |
| 67 | |
| 68 | GIT_DIR=$(git rev-parse --git-dir 2>/dev/null) |
| 69 | if [ -z "$GIT_DIR" ]; then |
| 70 | echo >&2 "fatal: post-receive: GIT_DIR not set" |
| 71 | exit 1 |
| 72 | fi |
| 73 | |
| 74 | # read ref updates passed to post-receive hook |
| 75 | while read oldrev newrev refname |
| 76 | do |
| 77 | mv_tmp_refs $oldrev $newrev $refname || continue |
| 78 | done |