Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 38b71bc85e6adc9acff7354d40ef4be407e9cc8d (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*******************************************************************************
 * Copyright (c) 2010, 2014 SAP AG 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:
 *    Stefan Lay (SAP AG) - initial implementation
 *    Benjamin Muskalla (Tasktop Technologies) - extract into operation
 *    Tomasz Zarna (IBM Corporation) - bug 370332
 *    Daniel Megert <daniel_megert@ch.ibm.com> - Allow spaces in path
 *******************************************************************************/
package org.eclipse.egit.core.op;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.eclipse.jgit.lib.Constants.encodeASCII;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.egit.core.Activator;
import org.eclipse.egit.core.EclipseGitProgressTransformer;
import org.eclipse.egit.core.internal.CompareCoreUtils;
import org.eclipse.egit.core.internal.CoreText;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
import org.eclipse.jgit.diff.DiffEntry.Side;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.dircache.DirCacheIterator;
import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.treewalk.FileTreeIterator;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.eclipse.osgi.util.NLS;

/**
 * Creates a patch for a specific commit
 */
public class CreatePatchOperation implements IEGitOperation {

	/**
	 * Diff header format
	 *
	 */
	public enum DiffHeaderFormat {
		/**
		 * No header
		 */
		NONE(CoreText.DiffHeaderFormat_None, false, null),

		/**
		 * Workspace patch
		 */
		WORKSPACE(CoreText.DiffHeaderFormat_Workspace, false, "### Eclipse Workspace Patch 1.0\n"), //$NON-NLS-1$

		/**
		 * Email header
		 */
		EMAIL(CoreText.DiffHeaderFormat_Email, true, "From ${sha1} ${date}\nFrom: ${author}\nDate: ${author date}\nSubject: [PATCH] ${title line}\n${full commit message}\n"), //$NON-NLS-1$

		/**
		 * Header designed to be as compact as possible
		 */
		ONELINE(CoreText.DiffHeaderFormat_Oneline, true, "${sha1} ${title line}\n"); //$NON-NLS-1$

		private final String description;

		private final boolean commitRequired;

		private final String template;

		private DiffHeaderFormat(final String d, final boolean c, final String t) {
			description = d;
			commitRequired = c;
			template = t;
		}

		/**
		 * @return if this format requires a commit.
		 */
		public boolean isCommitRequired() {
			return commitRequired;
		}

		/**
		 * @return the template
		 */
		public String getTemplate() {
			return template;
		}

		/**
		 * @return the description
		 */
		public String getDescription() {
			return description;
		}
	}

	enum DiffHeaderKeyword{
		SHA1, AUTHOR_DATE, AUTHOR, DATE, TITLE_LINE, FULL_COMMIT_MESSAGE
	}

	/**
	 * The default number of lines to use as context
	 */
	public static final int DEFAULT_CONTEXT_LINES = 3;

	private final RevCommit commit;

	private final Repository repository;

	private DiffHeaderFormat headerFormat = DiffHeaderFormat.EMAIL;

	// the encoding for the currently processed file
	private String currentEncoding = null;

	private String patchContent;

	private int contextLines = DEFAULT_CONTEXT_LINES;

	private TreeFilter pathFilter = null;

	/**
	 * Creates the new operation.
	 *
	 * @param repository
	 * @param commit
	 */
	public CreatePatchOperation(Repository repository, RevCommit commit) {
		if (repository == null)
			throw new IllegalArgumentException(
					CoreText.CreatePatchOperation_repoRequired);
		this.repository = repository;
		this.commit = commit;
	}

	@Override
	public void execute(IProgressMonitor monitor) throws CoreException {
		try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
				final DiffFormatter diffFmt = createDiffFormatter(
						outputStream, monitor)) {

			diffFmt.setContext(contextLines);
			final StringBuilder sb = new StringBuilder();
			if (headerFormat != null && headerFormat != DiffHeaderFormat.NONE) {
				writeGitPatchHeader(sb);
			}
			diffFmt.setRepository(repository);
			diffFmt.setPathFilter(pathFilter);

			if (commit != null) {
				List<DiffEntry> diffs = diffFmt.scan(getParentId(), commit.getId());
				for (DiffEntry ent : diffs) {
					String path;
					if (ChangeType.DELETE.equals(ent.getChangeType())) {
						path = ent.getOldPath();
					} else {
						path = ent.getNewPath();
					}
					currentEncoding = CompareCoreUtils
							.getResourceEncoding(repository, path);
					diffFmt.format(ent);
				}
			} else {
				diffFmt.format(new DirCacheIterator(repository.readDirCache()),
						new FileTreeIterator(repository));
			}
			diffFmt.flush();

			appendOutputStream(sb, outputStream);

			if (DiffHeaderFormat.WORKSPACE == headerFormat) {
				updateWorkspacePatchPrefixes(sb, diffFmt);
			}
			patchContent = sb.toString();
		} catch (IOException e) {
			Activator.logError(
					CoreText.CreatePatchOperation_patchFileCouldNotBeWritten,
					e);
		}
	}

	private DiffFormatter createDiffFormatter(
			final ByteArrayOutputStream outputStream,
			IProgressMonitor monitor) {
		DiffFormatter diffFmt = new DiffFormatter(outputStream) {

			private IProject project;

			@Override
			public void format(DiffEntry ent) throws IOException,
					CorruptObjectException, MissingObjectException {
				// for "workspace patches" add project header each time project
				// changes
				if (DiffHeaderFormat.WORKSPACE == headerFormat) {
					IProject p = getProject(ent);
					if (p != null && !p.equals(project)) {
						project = p;
						getOutputStream().write(
								encodeASCII("#P " + project.getName() + "\n")); //$NON-NLS-1$ //$NON-NLS-2$
					}
				}
				super.format(ent);
			}
		};
		diffFmt.setProgressMonitor(new EclipseGitProgressTransformer(monitor));
		return diffFmt;
	}

	private AnyObjectId getParentId() {
		RevCommit[] parents = commit.getParents();
		if (parents.length > 1) {
			throw new IllegalStateException(
					CoreText.CreatePatchOperation_cannotCreatePatchForMergeCommit);
		}

		ObjectId parentId;
		if (parents.length > 0) {
			parentId = parents[0].getId();
		} else {
			parentId = null;
		}
		return parentId;
	}

	private void appendOutputStream(final StringBuilder sb,
			final ByteArrayOutputStream outputStream) {
		try {
			String encoding = currentEncoding != null ? currentEncoding
					: UTF_8.name();
			sb.append(outputStream.toString(encoding));
		} catch (UnsupportedEncodingException e) {
			sb.append(outputStream.toString());
		}
	}

	private IProject getProject(final DiffEntry ent) {
		Side side = ent.getChangeType() == ChangeType.ADD ? Side.NEW : Side.OLD;
		String path = ent.getPath(side);
		return getProject(path);
	}

	private IProject getProject(String path) {
		URI pathUri = repository.getWorkTree().toURI().resolve(URIUtil.toURI(path));
		IFile[] files = ResourcesPlugin.getWorkspace().getRoot()
				.findFilesForLocationURI(pathUri);
		Assert.isLegal(files.length >= 1, NLS.bind(CoreText.CreatePatchOperation_couldNotFindProject, path,	repository));
		return files[0].getProject();
	}

	/**
	 * Retrieves the content of the requested patch
	 *
	 * @return the content of the patch
	 */
	public String getPatchContent() {
		if (patchContent == null)
			throw new IllegalStateException(
					"#execute needs to be called before this method."); //$NON-NLS-1$
		return patchContent;
	}

	private void writeGitPatchHeader(StringBuilder sb) {
		String template = headerFormat.getTemplate();
		String[] segments = template.split("\\$\\{"); //$NON-NLS-1$
		Stack<String> evaluated = new Stack<>();
		evaluated.add(segments[0]);

		for (int i = 1; i < segments.length; i++) {
			String segment = segments[i];
			String value = null;
			int brace = segment.indexOf('}');
			if (brace > 0) {
				String keyword = segment.substring(0, brace);
				keyword = keyword.toUpperCase(Locale.ROOT).replaceAll(" ", "_"); //$NON-NLS-1$ //$NON-NLS-2$
				value = processKeyword(commit, DiffHeaderKeyword.valueOf(keyword));
			}

			String trailingCharacters = segment.substring(brace + 1);
			if (value != null) {
				evaluated.add(value);
				evaluated.add(trailingCharacters);
			} else if (!evaluated.isEmpty())
				evaluated.add(trailingCharacters);
		}
		for (String string : evaluated) {
			sb.append(string);
		}
	}

	private static String processKeyword(RevCommit commit, DiffHeaderKeyword keyword) {
		final SimpleDateFormat dtfmt = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US); //$NON-NLS-1$
		switch (keyword) {
		case SHA1:
			return commit.getId().getName();
		case AUTHOR:
			return commit.getAuthorIdent().getName()
					+ " <" + commit.getAuthorIdent().getEmailAddress() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
		case AUTHOR_DATE:
			dtfmt.setTimeZone(commit.getAuthorIdent().getTimeZone());
			return dtfmt.format(commit.getAuthorIdent().getWhen());
		case DATE:
			return dtfmt.format(Long.valueOf(System.currentTimeMillis()));
		case TITLE_LINE:
			return commit.getShortMessage();
		case FULL_COMMIT_MESSAGE:
			return commit.getFullMessage().substring(
					commit.getShortMessage().length());
		default:
			return null;
		}
	}

	/**
	 * Updates prefixes to workspace paths
	 *
	 * @param sb
	 * @param diffFmt
	 */
	public void updateWorkspacePatchPrefixes(StringBuilder sb, DiffFormatter diffFmt) {
		RawText rt;
		try {
			rt = new RawText(sb.toString().getBytes("UTF-8")); //$NON-NLS-1$
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}

		final String oldPrefix = diffFmt.getOldPrefix();
		final String newPrefix = diffFmt.getNewPrefix();

		StringBuilder newSb = new StringBuilder();
		final Pattern diffPattern = Pattern
				.compile("^diff --git (" + oldPrefix + "(.+)) (" + newPrefix + "(.+))$"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
		final Pattern oldPattern = Pattern
				.compile("^--- (" + oldPrefix + "(.+))$"); //$NON-NLS-1$ //$NON-NLS-2$
		final Pattern newPattern = Pattern
				.compile("^\\+\\+\\+ (" + newPrefix + "(.+))$"); //$NON-NLS-1$ //$NON-NLS-2$

		int i = 0;
		while (i < rt.size()) {
			String line = rt.getString(i);

			Matcher diffMatcher = diffPattern.matcher(line);
			Matcher oldMatcher = oldPattern.matcher(line);
			Matcher newMatcher = newPattern.matcher(line);
			if (diffMatcher.find()) {
				String group = diffMatcher.group(2); // old path
				IProject project = getProject(group);
				IPath newPath = computeWorkspacePath(new Path(group), project);
				line = line.replaceAll(diffMatcher.group(1), newPath.toString());
				group = diffMatcher.group(4); // new path
				newPath = computeWorkspacePath(new Path(group), project);
				line = line.replaceAll(diffMatcher.group(3), newPath.toString());
			} else if (oldMatcher.find()) {
				String group = oldMatcher.group(2);
				IProject project = getProject(group);
				IPath newPath = computeWorkspacePath(new Path(group), project);
				line = line.replaceAll(oldMatcher.group(1), newPath.toString());
			} else if (newMatcher.find()) {
				String group = newMatcher.group(2);
				IProject project = getProject(group);
				IPath newPath = computeWorkspacePath(new Path(group), project);
				line = line.replaceAll(newMatcher.group(1), newPath.toString());
			}
			newSb.append(line);

			i++;
			if (i < rt.size() || !rt.isMissingNewlineAtEnd())
				newSb.append(rt.getLineDelimiter());
		}
		// reset sb to newSb
		sb.setLength(0);
		sb.append(newSb);
	}

	/**
	 * Returns a workspace path
	 *
	 * @param path
	 * @param project
	 * @return path
	 */
	public static IPath computeWorkspacePath(final IPath path, final IProject project) {
		RepositoryMapping rm = RepositoryMapping.getMapping(project);
		if (rm == null) {
			return path;
		}
		String repoRelativePath = rm.getRepoRelativePath(project);
		// the relative path cannot be determined, return unchanged
		if (repoRelativePath == null)
			return path;
		// repository and project at the same level
		if (repoRelativePath.equals("")) //$NON-NLS-1$
			return path;
		return path.removeFirstSegments(path.matchingFirstSegments(new Path(
				repoRelativePath)));
	}


	/**
	 * Change the format of diff header
	 *
	 * @param format header format
	 */
	public void setHeaderFormat(DiffHeaderFormat format) {
		this.headerFormat = format;
	}

	/**
	 * Change the number of lines of context to display.
	 *
	 * @param contextLines line count
	 */
	public void setContextLines(int contextLines) {
		this.contextLines = contextLines;
	}

	/**
	 * Suggests a file name for the patch given the commit.
	 *
	 * @param commit
	 * @return a file name for a patch
	 */
	public static String suggestFileName(RevCommit commit) {
		String name = commit.getShortMessage();

		name = name.trim();
		StringBuilder filteredBuilder = new StringBuilder();
		char[] charArray = name.toCharArray();
		for (char c : charArray) {
			if(Character.isLetter(c) || Character.isDigit(c))
				filteredBuilder.append(c);
			if(Character.isWhitespace(c) || c == '/')
				filteredBuilder.append("-"); //$NON-NLS-1$
		}
		name = filteredBuilder.toString();
		if (name.length() > 52)
			name = name.substring(0, 52);
		while (name.endsWith(".")) //$NON-NLS-1$
			name = name.substring(0, name.length() - 1);
		name = name.concat(".patch"); //$NON-NLS-1$

		return name;
	}

	@Override
	public ISchedulingRule getSchedulingRule() {
		return null;
	}

	/**
	 * Set the filter to produce patch for specified paths only.
	 *
	 * @param pathFilter the filter
	 */
	public void setPathFilter(TreeFilter pathFilter) {
		this.pathFilter = pathFilter;
	}
}

Back to the top