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.

PrePushHook.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C) 2015 Obeo.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.hooks;
  44. import java.io.IOException;
  45. import java.io.PrintStream;
  46. import java.util.Collection;
  47. import org.eclipse.jgit.api.errors.AbortedByHookException;
  48. import org.eclipse.jgit.lib.ObjectId;
  49. import org.eclipse.jgit.lib.Repository;
  50. import org.eclipse.jgit.transport.RemoteRefUpdate;
  51. /**
  52. * The <code>pre-push</code> hook implementation. The pre-push hook runs during
  53. * git push, after the remote refs have been updated but before any objects have
  54. * been transferred.
  55. *
  56. * @since 4.2
  57. */
  58. public class PrePushHook extends GitHook<String> {
  59. /**
  60. * Constant indicating the name of the pre-push hook.
  61. */
  62. public static final String NAME = "pre-push"; //$NON-NLS-1$
  63. private String remoteName;
  64. private String remoteLocation;
  65. private String refs;
  66. /**
  67. * Constructor for PrePushHook
  68. * <p>
  69. * This constructor will use the default error stream.
  70. * </p>
  71. *
  72. * @param repo
  73. * The repository
  74. * @param outputStream
  75. * The output stream the hook must use. {@code null} is allowed,
  76. * in which case the hook will use {@code System.out}.
  77. */
  78. protected PrePushHook(Repository repo, PrintStream outputStream) {
  79. super(repo, outputStream);
  80. }
  81. /**
  82. * Constructor for PrePushHook
  83. *
  84. * @param repo
  85. * The repository
  86. * @param outputStream
  87. * The output stream the hook must use. {@code null} is allowed,
  88. * in which case the hook will use {@code System.out}.
  89. * @param errorStream
  90. * The error stream the hook must use. {@code null} is allowed,
  91. * in which case the hook will use {@code System.err}.
  92. * @since 5.6
  93. */
  94. protected PrePushHook(Repository repo, PrintStream outputStream,
  95. PrintStream errorStream) {
  96. super(repo, outputStream, errorStream);
  97. }
  98. /** {@inheritDoc} */
  99. @Override
  100. protected String getStdinArgs() {
  101. return refs;
  102. }
  103. /** {@inheritDoc} */
  104. @Override
  105. public String call() throws IOException, AbortedByHookException {
  106. if (canRun()) {
  107. doRun();
  108. }
  109. return ""; //$NON-NLS-1$
  110. }
  111. /**
  112. * @return {@code true}
  113. */
  114. private boolean canRun() {
  115. return true;
  116. }
  117. /** {@inheritDoc} */
  118. @Override
  119. public String getHookName() {
  120. return NAME;
  121. }
  122. /**
  123. * {@inheritDoc}
  124. * <p>
  125. * This hook receives two parameters, which is the name and the location of
  126. * the remote repository.
  127. */
  128. @Override
  129. protected String[] getParameters() {
  130. if (remoteName == null) {
  131. remoteName = remoteLocation;
  132. }
  133. return new String[] { remoteName, remoteLocation };
  134. }
  135. /**
  136. * Set remote name
  137. *
  138. * @param name
  139. * remote name
  140. */
  141. public void setRemoteName(String name) {
  142. remoteName = name;
  143. }
  144. /**
  145. * Get remote name
  146. *
  147. * @return remote name or null
  148. * @since 4.11
  149. */
  150. protected String getRemoteName() {
  151. return remoteName;
  152. }
  153. /**
  154. * Set remote location
  155. *
  156. * @param location
  157. * a remote location
  158. */
  159. public void setRemoteLocation(String location) {
  160. remoteLocation = location;
  161. }
  162. /**
  163. * Set Refs
  164. *
  165. * @param toRefs
  166. * a collection of {@code RemoteRefUpdate}s
  167. */
  168. public void setRefs(Collection<RemoteRefUpdate> toRefs) {
  169. StringBuilder b = new StringBuilder();
  170. for (RemoteRefUpdate u : toRefs) {
  171. b.append(u.getSrcRef());
  172. b.append(" "); //$NON-NLS-1$
  173. b.append(u.getNewObjectId().getName());
  174. b.append(" "); //$NON-NLS-1$
  175. b.append(u.getRemoteName());
  176. b.append(" "); //$NON-NLS-1$
  177. ObjectId ooid = u.getExpectedOldObjectId();
  178. b.append((ooid == null) ? ObjectId.zeroId().getName() : ooid
  179. .getName());
  180. b.append("\n"); //$NON-NLS-1$
  181. }
  182. refs = b.toString();
  183. }
  184. }