Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

PrePushHook.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. *
  69. * @param repo
  70. * The repository
  71. * @param outputStream
  72. * The output stream the hook must use. {@code null} is allowed,
  73. * in which case the hook will use {@code System.out}.
  74. */
  75. protected PrePushHook(Repository repo, PrintStream outputStream) {
  76. super(repo, outputStream);
  77. }
  78. /** {@inheritDoc} */
  79. @Override
  80. protected String getStdinArgs() {
  81. return refs;
  82. }
  83. /** {@inheritDoc} */
  84. @Override
  85. public String call() throws IOException, AbortedByHookException {
  86. if (canRun()) {
  87. doRun();
  88. }
  89. return ""; //$NON-NLS-1$
  90. }
  91. /**
  92. * @return {@code true}
  93. */
  94. private boolean canRun() {
  95. return true;
  96. }
  97. /** {@inheritDoc} */
  98. @Override
  99. public String getHookName() {
  100. return NAME;
  101. }
  102. /**
  103. * {@inheritDoc}
  104. * <p>
  105. * This hook receives two parameters, which is the name and the location of
  106. * the remote repository.
  107. */
  108. @Override
  109. protected String[] getParameters() {
  110. if (remoteName == null) {
  111. remoteName = remoteLocation;
  112. }
  113. return new String[] { remoteName, remoteLocation };
  114. }
  115. /**
  116. * Set remote name
  117. *
  118. * @param name
  119. * remote name
  120. */
  121. public void setRemoteName(String name) {
  122. remoteName = name;
  123. }
  124. /**
  125. * Get remote name
  126. *
  127. * @return remote name or null
  128. * @since 4.11
  129. */
  130. protected String getRemoteName() {
  131. return remoteName;
  132. }
  133. /**
  134. * Set remote location
  135. *
  136. * @param location
  137. * a remote location
  138. */
  139. public void setRemoteLocation(String location) {
  140. remoteLocation = location;
  141. }
  142. /**
  143. * Set Refs
  144. *
  145. * @param toRefs
  146. * a collection of {@code RemoteRefUpdate}s
  147. */
  148. public void setRefs(Collection<RemoteRefUpdate> toRefs) {
  149. StringBuilder b = new StringBuilder();
  150. boolean first = true;
  151. for (RemoteRefUpdate u : toRefs) {
  152. if (!first)
  153. b.append("\n"); //$NON-NLS-1$
  154. else
  155. first = false;
  156. b.append(u.getSrcRef());
  157. b.append(" "); //$NON-NLS-1$
  158. b.append(u.getNewObjectId().getName());
  159. b.append(" "); //$NON-NLS-1$
  160. b.append(u.getRemoteName());
  161. b.append(" "); //$NON-NLS-1$
  162. ObjectId ooid = u.getExpectedOldObjectId();
  163. b.append((ooid == null) ? ObjectId.zeroId().getName() : ooid
  164. .getName());
  165. }
  166. refs = b.toString();
  167. }
  168. }