View Javadoc
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  
45  import java.io.IOException;
46  import java.io.PrintStream;
47  import java.util.Collection;
48  
49  import org.eclipse.jgit.api.errors.AbortedByHookException;
50  import org.eclipse.jgit.lib.ObjectId;
51  import org.eclipse.jgit.lib.Repository;
52  import org.eclipse.jgit.transport.RemoteRefUpdate;
53  
54  /**
55   * The <code>pre-push</code> hook implementation. The pre-push hook runs during
56   * git push, after the remote refs have been updated but before any objects have
57   * been transferred.
58   *
59   * @since 4.2
60   */
61  public class PrePushHook extends GitHook<String> {
62  
63  	/**
64  	 * Constant indicating the name of the pre-push hook.
65  	 */
66  	public static final String NAME = "pre-push"; //$NON-NLS-1$
67  
68  	private String remoteName;
69  
70  	private String remoteLocation;
71  
72  	private String refs;
73  
74  	/**
75  	 * Constructor for PrePushHook
76  	 * <p>
77  	 * This constructor will use the default error stream.
78  	 * </p>
79  	 *
80  	 * @param repo
81  	 *            The repository
82  	 * @param outputStream
83  	 *            The output stream the hook must use. {@code null} is allowed,
84  	 *            in which case the hook will use {@code System.out}.
85  	 */
86  	protected PrePushHook(Repository repo, PrintStream outputStream) {
87  		super(repo, outputStream);
88  	}
89  
90  	/**
91  	 * Constructor for PrePushHook
92  	 *
93  	 * @param repo
94  	 *            The repository
95  	 * @param outputStream
96  	 *            The output stream the hook must use. {@code null} is allowed,
97  	 *            in which case the hook will use {@code System.out}.
98  	 * @param errorStream
99  	 *            The error stream the hook must use. {@code null} is allowed,
100 	 *            in which case the hook will use {@code System.err}.
101 	 * @since 5.6
102 	 */
103 	protected PrePushHook(Repository repo, PrintStream outputStream,
104 			PrintStream errorStream) {
105 		super(repo, outputStream, errorStream);
106 	}
107 
108 	/** {@inheritDoc} */
109 	@Override
110 	protected String getStdinArgs() {
111 		return refs;
112 	}
113 
114 	/** {@inheritDoc} */
115 	@Override
116 	public String call() throws IOException, AbortedByHookException {
117 		if (canRun()) {
118 			doRun();
119 		}
120 		return ""; //$NON-NLS-1$
121 	}
122 
123 	/**
124 	 * @return {@code true}
125 	 */
126 	private boolean canRun() {
127 		return true;
128 	}
129 
130 	/** {@inheritDoc} */
131 	@Override
132 	public String getHookName() {
133 		return NAME;
134 	}
135 
136 	/**
137 	 * {@inheritDoc}
138 	 * <p>
139 	 * This hook receives two parameters, which is the name and the location of
140 	 * the remote repository.
141 	 */
142 	@Override
143 	protected String[] getParameters() {
144 		if (remoteName == null) {
145 			remoteName = remoteLocation;
146 		}
147 		return new String[] { remoteName, remoteLocation };
148 	}
149 
150 	/**
151 	 * Set remote name
152 	 *
153 	 * @param name
154 	 *            remote name
155 	 */
156 	public void setRemoteName(String name) {
157 		remoteName = name;
158 	}
159 
160 	/**
161 	 * Get remote name
162 	 *
163 	 * @return remote name or null
164 	 * @since 4.11
165 	 */
166 	protected String getRemoteName() {
167 		return remoteName;
168 	}
169 
170 	/**
171 	 * Set remote location
172 	 *
173 	 * @param location
174 	 *            a remote location
175 	 */
176 	public void setRemoteLocation(String location) {
177 		remoteLocation = location;
178 	}
179 
180 	/**
181 	 * Set Refs
182 	 *
183 	 * @param toRefs
184 	 *            a collection of {@code RemoteRefUpdate}s
185 	 */
186 	public void setRefs(Collection<RemoteRefUpdate> toRefs) {
187 		StringBuilder b = new StringBuilder();
188 		for (RemoteRefUpdate u : toRefs) {
189 			b.append(u.getSrcRef());
190 			b.append(" "); //$NON-NLS-1$
191 			b.append(u.getNewObjectId().getName());
192 			b.append(" "); //$NON-NLS-1$
193 			b.append(u.getRemoteName());
194 			b.append(" "); //$NON-NLS-1$
195 			ObjectId ooid = u.getExpectedOldObjectId();
196 			b.append((ooid == null) ? ObjectId.zeroId().getName() : ooid
197 					.getName());
198 			b.append("\n"); //$NON-NLS-1$
199 		}
200 		refs = b.toString();
201 	}
202 }