new NullProgressMonitor());
else
gitMonitor = new EclipseGitProgressTransformer(monitor);
final StringBuilder sb = new StringBuilder();
final DiffFormatter diffFmt = new DiffFormatter(
new ByteArrayOutputStream() {
@Override
public synchronized void write(byte[] b, int off, int len) {
super.write(b, off, len);
try {
if (currentEncoding == null)
sb.append(toString("UTF-8")); //$NON-NLS-1$
else
sb.append(toString(currentEncoding));
} catch (UnsupportedEncodingException e) {
sb.append(toString());
}
reset();
}
}) {
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.equals(project)) {
project = p;
getOutputStream().write(
encodeASCII("#P " + project.getName() + "\n")); //$NON-NLS-1$ //$NON-NLS-2$
}
}
super.format(ent);
}
};
diffFmt.setProgressMonitor(gitMonitor);
diffFmt.setContext(contextLines);
if (headerFormat != null && headerFormat != DiffHeaderFormat.NONE)
writeGitPatchHeader(sb);
diffFmt.setRepository(repository);
diffFmt.setPathFilter(pathFilter);
try {
if (commit != null) {
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;
List<DiffEntry> diffs = diffFmt.scan(parentId, 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));
} catch (IOException e) {
Activator.logError(CoreText.CreatePatchOperation_patchFileCouldNotBeWritten, e);
}