Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 48f88e1fa3d5193c295da3c15722b77a19ad5c79 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*******************************************************************************
 * Copyright (c) 2013, 2016 Robin Stocker <robin@nibor.org> and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Thomas Wolf <thomas.wolf@paranor.ch> - Bug 493352
 *******************************************************************************/
package org.eclipse.egit.core.internal.gerrit;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.internal.CoreText;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;

/**
 * Gerrit utilities
 */
public class GerritUtil {

	/**
	 * The Gerrit magic push prefix {@value} .
	 */
	public static final String REFS_FOR = "refs/for/"; //$NON-NLS-1$

	/**
	 * The Gerrit magic push prefix {@value}
	 */
	public static final String REFS_PUBLISH = "refs/publish/"; //$NON-NLS-1$

	/**
	 * The Gerrit magic push prefix {@value}
	 */
	public static final String REFS_DRAFTS = "refs/drafts/"; //$NON-NLS-1$

	/**
	 * @param config
	 * @return true if there if "create change ID" is configured
	 */
	public static boolean getCreateChangeId(Config config) {
		return config.getBoolean(ConfigConstants.CONFIG_GERRIT_SECTION,
				ConfigConstants.CONFIG_KEY_CREATECHANGEID, false);
	}

	/**
	 * Set the "create change ID" configuration
	 *
	 * @param config
	 */
	public static void setCreateChangeId(Config config) {
		config.setBoolean(ConfigConstants.CONFIG_GERRIT_SECTION, null,
				ConfigConstants.CONFIG_KEY_CREATECHANGEID, true);
	}

	/**
	 * Find the remote config for the given remote name
	 *
	 * @param config
	 *            the configuration containing the remote config
	 * @param remoteName
	 *            the name of the remote to find
	 * @return the found remoteConfig or {@code null}
	 * @throws URISyntaxException
	 *             if the configuration contains an illegal URI
	 */
	public static RemoteConfig findRemoteConfig(Config config, String remoteName)
			throws URISyntaxException {
		RemoteConfig remoteConfig = null;
		List<RemoteConfig> allRemoteConfigs = RemoteConfig
				.getAllRemoteConfigs(config);
		for (RemoteConfig rc : allRemoteConfigs) {
			if (rc.getName().equals(remoteName)) {
				remoteConfig = rc;
				break;
			}
		}
		return remoteConfig;
	}

	/**
	 * Configure the pushURI for Gerrit
	 *
	 * @param remoteConfig
	 *            the remote configuration to add this to
	 * @param pushURI
	 *            the pushURI to configure
	 */
	public static void configurePushURI(RemoteConfig remoteConfig,
			URIish pushURI) {
		List<URIish> pushURIs = new ArrayList<>(
				remoteConfig.getPushURIs());
		for (URIish urIish : pushURIs) {
			remoteConfig.removePushURI(urIish);
		}
		remoteConfig.addPushURI(pushURI);
	}

	/**
	 * Configure the gerrit push refspec HEAD:refs/for/<gerritBranch>
	 *
	 * @param remoteConfig
	 *            the remote configuration to configure this in
	 * @param gerritBranch
	 *            the branch to push to review for
	 */
	public static void configurePushRefSpec(RemoteConfig remoteConfig,
			String gerritBranch) {
		List<RefSpec> pushRefSpecs = new ArrayList<>(
				remoteConfig.getPushRefSpecs());
		for (RefSpec refSpec : pushRefSpecs) {
			remoteConfig.removePushRefSpec(refSpec);
		}
		remoteConfig.addPushRefSpec(new RefSpec(
				"HEAD:" + GerritUtil.REFS_FOR + gerritBranch)); //$NON-NLS-1$
	}

	/**
	 * Configure fetching review summary notes
	 *
	 * @param remoteConfig
	 *            the remote configuration to configure this in
	 * @return {@code true} if the {@code remoteConfig} was changed,
	 *         {@code false} otherwise.
	 */
	public static boolean configureFetchNotes(RemoteConfig remoteConfig) {
		String notesRef = Constants.R_NOTES + "*"; //$NON-NLS-1$
		List<RefSpec> fetchRefSpecs = remoteConfig.getFetchRefSpecs();
		for (RefSpec refSpec : fetchRefSpecs) {
			if (refSpec.matchSource(notesRef)) {
				return false;
			}
		}
		remoteConfig.addFetchRefSpec(new RefSpec(notesRef + ':' + notesRef));
		return true;
	}


	/**
	 * @param rc
	 *            the remote configuration
	 * @return {@code true} if the remote configuration is configured for
	 *         pushing to Gerrit
	 */
	public static boolean isGerritPush(RemoteConfig rc) {
		for (RefSpec pushSpec : rc.getPushRefSpecs()) {
			String destination = pushSpec.getDestination();
			if (destination == null) {
				continue;
			}
			if (destination.startsWith(GerritUtil.REFS_FOR)
					|| destination.startsWith(GerritUtil.REFS_PUBLISH)
					|| destination.startsWith(GerritUtil.REFS_DRAFTS)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @param rc
	 *            the remote configuration
	 * @return {@code true} if the remote configuration is configured for
	 *         fetching from Gerrit
	 */
	public static boolean isGerritFetch(RemoteConfig rc) {
		for (RefSpec fetchSpec : rc.getFetchRefSpecs()) {
			String source = fetchSpec.getSource();
			String destination = fetchSpec.getDestination();
			if (source == null || destination == null) {
				continue;
			}
			if (source.startsWith(Constants.R_NOTES)
					&& destination.startsWith(Constants.R_NOTES)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * If the repository is not bare and looks like it might be a Gerrit
	 * repository, try to configure it such that EGit's Gerrit support is
	 * enabled.
	 *
	 * @param repository
	 *            to try to configure
	 */
	public static void tryToAutoConfigureForGerrit(
			@NonNull Repository repository) {
		if (repository.isBare()) {
			return;
		}
		StoredConfig config = repository.getConfig();
		boolean isGerrit = false;
		boolean changed = false;
		try {
			for (RemoteConfig remote : RemoteConfig
					.getAllRemoteConfigs(config)) {
				if (isGerritPush(remote)) {
					isGerrit = true;
					if (configureFetchNotes(remote)) {
						changed = true;
						remote.update(config);
					}
				}
			}
		} catch (URISyntaxException ignored) {
			// Ignore it here -- we're just trying to set up Gerrit support.
		}
		if (isGerrit) {
			if (config.getString(ConfigConstants.CONFIG_GERRIT_SECTION, null,
					ConfigConstants.CONFIG_KEY_CREATECHANGEID) != null) {
				// Already configured.
			} else {
				setCreateChangeId(config);
				changed = true;
			}
			if (changed) {
				try {
					config.save();
				} catch (IOException e) {
					Activator.logError(
							MessageFormat.format(
									CoreText.GerritUtil_ConfigSaveError,
									repository.getDirectory()),
							e);
				}
			}
		}
	}

	/**
	 * If the repository is not bare and looks like it might be a Gerrit
	 * repository, try to configure it such that EGit's Gerrit support is
	 * enabled. Does nothing if the {@code repositoryDir} is {@code null} or the
	 * repository cannot be configured.
	 *
	 * @param repositoryDir
	 *            .git Directory of the repository to try to configure
	 */
	public static void tryToAutoConfigureForGerrit(
			@Nullable File repositoryDir) {
		if (repositoryDir != null) {
			try {
				Repository repository = Activator.getDefault()
						.getRepositoryCache().lookupRepository(repositoryDir);
				if (repository != null) {
					tryToAutoConfigureForGerrit(repository);
				}
			} catch (IOException ignored) {
				// Ignore it here -- this is just a best-effort. If the repo
				// cannot be read, other places will report the problem.
			}
		}
	}

}

Back to the top