View Javadoc
1   /*
2    * Copyright (C) 2015 Obeo. 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.hooks;
11  
12  import java.io.IOException;
13  import java.io.PrintStream;
14  import java.util.Collection;
15  
16  import org.eclipse.jgit.api.errors.AbortedByHookException;
17  import org.eclipse.jgit.lib.ObjectId;
18  import org.eclipse.jgit.lib.Repository;
19  import org.eclipse.jgit.transport.RemoteRefUpdate;
20  
21  /**
22   * The <code>pre-push</code> hook implementation. The pre-push hook runs during
23   * git push, after the remote refs have been updated but before any objects have
24   * been transferred.
25   *
26   * @since 4.2
27   */
28  public class PrePushHook extends GitHook<String> {
29  
30  	/**
31  	 * Constant indicating the name of the pre-push hook.
32  	 */
33  	public static final String NAME = "pre-push"; //$NON-NLS-1$
34  
35  	private String remoteName;
36  
37  	private String remoteLocation;
38  
39  	private String refs;
40  
41  	/**
42  	 * Constructor for PrePushHook
43  	 * <p>
44  	 * This constructor will use the default error stream.
45  	 * </p>
46  	 *
47  	 * @param repo
48  	 *            The repository
49  	 * @param outputStream
50  	 *            The output stream the hook must use. {@code null} is allowed,
51  	 *            in which case the hook will use {@code System.out}.
52  	 */
53  	protected PrePushHook(Repository repo, PrintStream outputStream) {
54  		super(repo, outputStream);
55  	}
56  
57  	/**
58  	 * Constructor for PrePushHook
59  	 *
60  	 * @param repo
61  	 *            The repository
62  	 * @param outputStream
63  	 *            The output stream the hook must use. {@code null} is allowed,
64  	 *            in which case the hook will use {@code System.out}.
65  	 * @param errorStream
66  	 *            The error stream the hook must use. {@code null} is allowed,
67  	 *            in which case the hook will use {@code System.err}.
68  	 * @since 5.6
69  	 */
70  	protected PrePushHook(Repository repo, PrintStream outputStream,
71  			PrintStream errorStream) {
72  		super(repo, outputStream, errorStream);
73  	}
74  
75  	/** {@inheritDoc} */
76  	@Override
77  	protected String getStdinArgs() {
78  		return refs;
79  	}
80  
81  	/** {@inheritDoc} */
82  	@Override
83  	public String call() throws IOException, AbortedByHookException {
84  		if (canRun()) {
85  			doRun();
86  		}
87  		return ""; //$NON-NLS-1$
88  	}
89  
90  	/**
91  	 * @return {@code true}
92  	 */
93  	private boolean canRun() {
94  		return true;
95  	}
96  
97  	/** {@inheritDoc} */
98  	@Override
99  	public String getHookName() {
100 		return NAME;
101 	}
102 
103 	/**
104 	 * {@inheritDoc}
105 	 * <p>
106 	 * This hook receives two parameters, which is the name and the location of
107 	 * the remote repository.
108 	 */
109 	@Override
110 	protected String[] getParameters() {
111 		if (remoteName == null) {
112 			remoteName = remoteLocation;
113 		}
114 		return new String[] { remoteName, remoteLocation };
115 	}
116 
117 	/**
118 	 * Set remote name
119 	 *
120 	 * @param name
121 	 *            remote name
122 	 */
123 	public void setRemoteName(String name) {
124 		remoteName = name;
125 	}
126 
127 	/**
128 	 * Get remote name
129 	 *
130 	 * @return remote name or null
131 	 * @since 4.11
132 	 */
133 	protected String getRemoteName() {
134 		return remoteName;
135 	}
136 
137 	/**
138 	 * Set remote location
139 	 *
140 	 * @param location
141 	 *            a remote location
142 	 */
143 	public void setRemoteLocation(String location) {
144 		remoteLocation = location;
145 	}
146 
147 	/**
148 	 * Set Refs
149 	 *
150 	 * @param toRefs
151 	 *            a collection of {@code RemoteRefUpdate}s
152 	 */
153 	public void setRefs(Collection<RemoteRefUpdate> toRefs) {
154 		StringBuilder b = new StringBuilder();
155 		for (RemoteRefUpdate u : toRefs) {
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 			b.append("\n"); //$NON-NLS-1$
166 		}
167 		refs = b.toString();
168 	}
169 }