You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PostReceiveHookChain.java 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (C) 2011, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.transport;
  11. import java.util.Collection;
  12. import java.util.List;
  13. /**
  14. * {@link org.eclipse.jgit.transport.PostReceiveHook} that delegates to a list
  15. * of other hooks.
  16. * <p>
  17. * Hooks are run in the order passed to the constructor.
  18. */
  19. public class PostReceiveHookChain implements PostReceiveHook {
  20. private final PostReceiveHook[] hooks;
  21. private final int count;
  22. /**
  23. * Create a new hook chaining the given hooks together.
  24. *
  25. * @param hooks
  26. * hooks to execute, in order.
  27. * @return a new hook chain of the given hooks.
  28. */
  29. public static PostReceiveHook newChain(
  30. List<? extends PostReceiveHook> hooks) {
  31. PostReceiveHook[] newHooks = new PostReceiveHook[hooks.size()];
  32. int i = 0;
  33. for (PostReceiveHook hook : hooks)
  34. if (hook != PostReceiveHook.NULL)
  35. newHooks[i++] = hook;
  36. switch (i) {
  37. case 0:
  38. return PostReceiveHook.NULL;
  39. case 1:
  40. return newHooks[0];
  41. default:
  42. return new PostReceiveHookChain(newHooks, i);
  43. }
  44. }
  45. /** {@inheritDoc} */
  46. @Override
  47. public void onPostReceive(ReceivePack rp,
  48. Collection<ReceiveCommand> commands) {
  49. for (int i = 0; i < count; i++)
  50. hooks[i].onPostReceive(rp, commands);
  51. }
  52. private PostReceiveHookChain(PostReceiveHook[] hooks, int count) {
  53. this.hooks = hooks;
  54. this.count = count;
  55. }
  56. }