Line data Source code
1 : // Copyright (C) 2019 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.git.receive; 16 : 17 : import com.google.common.annotations.VisibleForTesting; 18 : import com.google.gerrit.entities.Account; 19 : import com.google.gerrit.entities.Project; 20 : import com.google.gerrit.server.CurrentUser; 21 : import com.google.gerrit.server.config.AllUsersName; 22 : import com.google.gerrit.server.config.AllUsersNameProvider; 23 : import com.google.gerrit.server.git.UsersSelfAdvertiseRefsHook; 24 : import com.google.gerrit.server.query.change.InternalChangeQuery; 25 : import com.google.inject.Provider; 26 : import com.google.inject.util.Providers; 27 : import java.util.ArrayList; 28 : import java.util.List; 29 : import org.eclipse.jgit.transport.AdvertiseRefsHook; 30 : import org.eclipse.jgit.transport.AdvertiseRefsHookChain; 31 : 32 : /** 33 : * Helper to ensure that the chain for advertising refs is the same in tests and production code. 34 : */ 35 0 : public class ReceiveCommitsAdvertiseRefsHookChain { 36 : 37 : /** 38 : * Returns a single {@link AdvertiseRefsHook} that encompasses a chain of {@link 39 : * AdvertiseRefsHook} to be used for advertising when processing a Git push. 40 : */ 41 : public static AdvertiseRefsHook create( 42 : AllRefsWatcher allRefsWatcher, 43 : UsersSelfAdvertiseRefsHook usersSelfAdvertiseRefsHook, 44 : AllUsersName allUsersName, 45 : Provider<InternalChangeQuery> queryProvider, 46 : Project.NameKey projectName, 47 : Account.Id user) { 48 97 : return create( 49 : allRefsWatcher, 50 : usersSelfAdvertiseRefsHook, 51 : allUsersName, 52 : queryProvider, 53 : projectName, 54 : user, 55 : false); 56 : } 57 : 58 : /** 59 : * Returns a single {@link AdvertiseRefsHook} that encompasses a chain of {@link 60 : * AdvertiseRefsHook} to be used for advertising when processing a Git push. Omits {@link 61 : * HackPushNegotiateHook} as that does not advertise refs on it's own but adds {@code .have} based 62 : * on history which is not relevant for the tests we have. 63 : */ 64 : @VisibleForTesting 65 : public static AdvertiseRefsHook createForTest( 66 : Provider<InternalChangeQuery> queryProvider, Project.NameKey projectName, CurrentUser user) { 67 1 : return create( 68 : new AllRefsWatcher(), 69 1 : new UsersSelfAdvertiseRefsHook(Providers.of(user)), 70 : new AllUsersName(AllUsersNameProvider.DEFAULT), 71 : queryProvider, 72 : projectName, 73 1 : user.getAccountId(), 74 : true); 75 : } 76 : 77 : private static AdvertiseRefsHook create( 78 : AllRefsWatcher allRefsWatcher, 79 : UsersSelfAdvertiseRefsHook usersSelfAdvertiseRefsHook, 80 : AllUsersName allUsersName, 81 : Provider<InternalChangeQuery> queryProvider, 82 : Project.NameKey projectName, 83 : Account.Id user, 84 : boolean skipHackPushNegotiateHook) { 85 97 : List<AdvertiseRefsHook> advHooks = new ArrayList<>(); 86 97 : advHooks.add(allRefsWatcher); 87 97 : advHooks.add(new ReceiveCommitsAdvertiseRefsHook(queryProvider, projectName, user)); 88 97 : if (!skipHackPushNegotiateHook) { 89 97 : advHooks.add(new HackPushNegotiateHook()); 90 : } 91 97 : if (projectName.equals(allUsersName)) { 92 6 : advHooks.add(usersSelfAdvertiseRefsHook); 93 : } 94 97 : return AdvertiseRefsHookChain.newChain(advHooks); 95 : } 96 : }