blob: fa0684f5df3530a7abc4f0081b11ba6864c33462 [file] [log] [blame]
Matthias Sohn375e9742017-09-08 17:38:31 +02001#!/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 Sohnd8182ba2019-12-09 14:50:23 +010018# hosted by a gerrit replica which are updated by push replication from the
19# corresponding gerrit primary node.
Matthias Sohn375e9742017-09-08 17:38:31 +020020#
Matthias Sohnd8182ba2019-12-09 14:50:23 +010021# In the gerrit primary node configure the replication plugin to push changes from
Matthias Sohn375e9742017-09-08 17:38:31 +020022# 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 Selberg925b5df2019-09-10 12:25:26 +020026# And if it's a Gerrit mirror:
27# remote.NAME.push = +refs/meta/*:refs/meta/*
Matthias Sohn375e9742017-09-08 17:38:31 +020028#
Matthias Sohnd8182ba2019-12-09 14:50:23 +010029# In the replicated repository in the gerrit replica configure
Matthias Sohn375e9742017-09-08 17:38:31 +020030# receive.hideRefs = refs/changes/
31# in order to not advertise the big number of refs in this namespace when
Matthias Sohnd8182ba2019-12-09 14:50:23 +010032# the gerrit primary's replication plugin is pushing a change
Matthias Sohn375e9742017-09-08 17:38:31 +020033#
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 Sohnd8182ba2019-12-09 14:50:23 +010036# refs/changes/ refs to the gerrit primary when it replicates changes to the
37# replica.
Matthias Sohn375e9742017-09-08 17:38:31 +020038#
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 Sohnd8182ba2019-12-09 14:50:23 +010044# If you want to use this by default for repositories on the Gerrit replica you
Matthias Sohn375e9742017-09-08 17:38:31 +020045# 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 Sohnd8182ba2019-12-09 14:50:23 +010048# the primary and hence on the mirror (if configured that way) it will
Matthias Sohn375e9742017-09-08 17:38:31 +020049# automatically have the "tmp-refs" commit hook installed.
50# See https://git-scm.com/docs/git-init#_template_directory for details.
51
Matthias Sohn375e9742017-09-08 17:38:31 +020052# move new changes arriving under refs/tmp/changes/ to refs/changes/
53mv_tmp_refs()
54{
55 oldrev=$1
56 newrev=$2
57 refname=$3
Sven Selberg925b5df2019-09-10 12:25:26 +020058 case "$refname" in refs/tmp/changes/*)
Matthias Sohn375e9742017-09-08 17:38:31 +020059 short_refname=${refname##refs/tmp/changes/}
Sven Selberg925b5df2019-09-10 12:25:26 +020060 $(git update-ref refs/changes/$short_refname $newrev 2>/dev/null)
Matthias Sohn375e9742017-09-08 17:38:31 +020061 $(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
68GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
69if [ -z "$GIT_DIR" ]; then
70 echo >&2 "fatal: post-receive: GIT_DIR not set"
71 exit 1
72fi
73
74# read ref updates passed to post-receive hook
75while read oldrev newrev refname
76do
77 mv_tmp_refs $oldrev $newrev $refname || continue
78done