use of com.intellij.openapi.diff.impl.patch.FilePatch in project intellij-community by JetBrains.
the class TextFilePatchInProgress method getDiffRequestProducers.
@NotNull
@Override
public DiffRequestProducer getDiffRequestProducers(final Project project, final PatchReader patchReader) {
final PatchChange change = getChange();
final FilePatch patch = getPatch();
final String path = patch.getBeforeName() == null ? patch.getAfterName() : patch.getBeforeName();
final Getter<CharSequence> baseContentGetter = new Getter<CharSequence>() {
@Override
public CharSequence get() {
return patchReader.getBaseRevision(project, path);
}
};
return new DiffRequestProducer() {
@NotNull
@Override
public DiffRequest process(@NotNull UserDataHolder context, @NotNull ProgressIndicator indicator) throws DiffRequestProducerException, ProcessCanceledException {
if (myCurrentBase != null && myCurrentBase.getFileType() == UnknownFileType.INSTANCE) {
return new UnknownFileTypeDiffRequest(myCurrentBase, getName());
}
if (isConflictingChange()) {
final VirtualFile file = getCurrentBase();
Getter<ApplyPatchForBaseRevisionTexts> getter = new Getter<ApplyPatchForBaseRevisionTexts>() {
@Override
public ApplyPatchForBaseRevisionTexts get() {
return ApplyPatchForBaseRevisionTexts.create(project, file, VcsUtil.getFilePath(file), getPatch(), baseContentGetter);
}
};
String afterTitle = getPatch().getAfterVersionId();
if (afterTitle == null)
afterTitle = "Patched Version";
return PatchDiffRequestFactory.createConflictDiffRequest(project, file, getPatch(), afterTitle, getter, getName(), context, indicator);
} else {
return PatchDiffRequestFactory.createDiffRequest(project, change, getName(), context, indicator);
}
}
@NotNull
@Override
public String getName() {
final File ioCurrentBase = getIoCurrentBase();
return ioCurrentBase == null ? getCurrentPath() : ioCurrentBase.getPath();
}
};
}
use of com.intellij.openapi.diff.impl.patch.FilePatch in project intellij-community by JetBrains.
the class BinaryPatchWriter method writeBinaries.
public static void writeBinaries(@Nullable String basePath, @NotNull List<BinaryFilePatch> patches, @NotNull Writer writer) throws IOException {
//use it for git headers&binary content, otherwise git won't parse&apply it properly
String lineSeparator = "\n";
for (FilePatch patch : patches) {
BinaryFilePatch filePatch = (BinaryFilePatch) patch;
writer.write(String.format(GIT_DIFF_HEADER, filePatch.getBeforeName(), filePatch.getAfterName()));
writer.write(lineSeparator);
File afterFile = new File(basePath, filePatch.getAfterName());
if (filePatch.isDeletedFile()) {
writer.write(getFileModeHeader(FileStatus.DELETED, REGULAR_FILE_MODE));
writer.write(lineSeparator);
} else if (filePatch.isNewFile()) {
writer.write(getFileModeHeader(FileStatus.ADDED, !SystemInfo.isWindows && afterFile.canExecute() ? EXECUTABLE_FILE_MODE : REGULAR_FILE_MODE));
writer.write(lineSeparator);
}
byte[] afterContent = filePatch.getAfterContent();
writer.write(getIndexHeader(filePatch.isNewFile() ? NOT_COMMITTED_HASH : getSha1ForContent(filePatch.getBeforeContent()), filePatch.isDeletedFile() ? NOT_COMMITTED_HASH : getSha1ForContent(afterContent)));
writer.write(lineSeparator);
writer.write(GIT_BINARY_HEADER);
writer.write(lineSeparator);
writer.write(String.format(LITERAL_HEADER, afterContent == null ? 0 : afterContent.length));
writer.write(lineSeparator);
try {
BinaryEncoder.encode(afterFile.exists() ? new FileInputStream(afterFile) : new ByteArrayInputStream(ArrayUtil.EMPTY_BYTE_ARRAY), writer);
} catch (BinaryEncoder.BinaryPatchException e) {
LOG.error("Can't write patch for binary file: " + afterFile.getPath(), e);
}
writer.write(lineSeparator);
}
}
use of com.intellij.openapi.diff.impl.patch.FilePatch in project intellij-community by JetBrains.
the class ImportToShelfExecutor method apply.
@Override
public void apply(@NotNull List<FilePatch> remaining, @NotNull final MultiMap<VirtualFile, TextFilePatchInProgress> patchGroupsToApply, @Nullable LocalChangeList localList, @Nullable final String fileName, @Nullable ThrowableComputable<Map<String, Map<String, CharSequence>>, PatchSyntaxException> additionalInfo) {
if (fileName == null) {
LOG.error("Patch file name shouldn't be null");
return;
}
final VcsCatchingRunnable vcsCatchingRunnable = new VcsCatchingRunnable() {
@Override
public void runImpl() throws VcsException {
final VirtualFile baseDir = myProject.getBaseDir();
final File ioBase = new File(baseDir.getPath());
final List<FilePatch> allPatches = new ArrayList<>();
for (VirtualFile virtualFile : patchGroupsToApply.keySet()) {
final File ioCurrentBase = new File(virtualFile.getPath());
allPatches.addAll(ContainerUtil.map(patchGroupsToApply.get(virtualFile), new Function<TextFilePatchInProgress, TextFilePatch>() {
public TextFilePatch fun(TextFilePatchInProgress patchInProgress) {
final TextFilePatch was = patchInProgress.getPatch();
was.setBeforeName(PathUtil.toSystemIndependentName(FileUtil.getRelativePath(ioBase, new File(ioCurrentBase, was.getBeforeName()))));
was.setAfterName(PathUtil.toSystemIndependentName(FileUtil.getRelativePath(ioBase, new File(ioCurrentBase, was.getAfterName()))));
return was;
}
}));
}
if (!allPatches.isEmpty()) {
PatchEP[] patchTransitExtensions = null;
if (additionalInfo != null) {
try {
final Map<String, PatchEP> extensions = new HashMap<>();
for (Map.Entry<String, Map<String, CharSequence>> entry : additionalInfo.compute().entrySet()) {
final String filePath = entry.getKey();
Map<String, CharSequence> extToValue = entry.getValue();
for (Map.Entry<String, CharSequence> innerEntry : extToValue.entrySet()) {
TransitExtension patchEP = (TransitExtension) extensions.get(innerEntry.getKey());
if (patchEP == null) {
patchEP = new TransitExtension(innerEntry.getKey());
extensions.put(innerEntry.getKey(), patchEP);
}
patchEP.put(filePath, innerEntry.getValue());
}
}
Collection<PatchEP> values = extensions.values();
patchTransitExtensions = values.toArray(new PatchEP[values.size()]);
} catch (PatchSyntaxException e) {
VcsBalloonProblemNotifier.showOverChangesView(myProject, "Can not import additional patch info: " + e.getMessage(), MessageType.ERROR);
}
}
try {
final ShelvedChangeList shelvedChangeList = ShelveChangesManager.getInstance(myProject).importFilePatches(fileName, allPatches, patchTransitExtensions);
ShelvedChangesViewManager.getInstance(myProject).activateView(shelvedChangeList);
} catch (IOException e) {
throw new VcsException(e);
}
}
}
};
ProgressManager.getInstance().runProcessWithProgressSynchronously(vcsCatchingRunnable, "Import Patch to Shelf", true, myProject);
if (!vcsCatchingRunnable.get().isEmpty()) {
AbstractVcsHelper.getInstance(myProject).showErrors(vcsCatchingRunnable.get(), IMPORT_TO_SHELF);
}
}
use of com.intellij.openapi.diff.impl.patch.FilePatch in project intellij-community by JetBrains.
the class MatchPatchPaths method workWithNotExisting.
private void workWithNotExisting(@NotNull PatchBaseDirectoryDetector directoryDetector, @NotNull List<FilePatch> newOrWithoutMatches, @NotNull MultiMap<VirtualFile, AbstractFilePatchInProgress> result) {
for (FilePatch patch : newOrWithoutMatches) {
String afterName = patch.getAfterName();
final String[] strings = afterName != null ? afterName.replace('\\', '/').split("/") : ArrayUtil.EMPTY_STRING_ARRAY;
Pair<VirtualFile, Integer> best = null;
for (int i = strings.length - 2; i >= 0; --i) {
final String name = strings[i];
final Collection<VirtualFile> files = findFilesFromIndex(directoryDetector, name);
if (!files.isEmpty()) {
// check all candidates
for (VirtualFile file : files) {
Pair<VirtualFile, Integer> pair = compareNamesImpl(strings, file, i);
if (pair != null && pair.getSecond() < i) {
if (best == null || pair.getSecond() < best.getSecond() || isGoodAndProjectBased(best, pair)) {
best = pair;
}
}
}
}
}
if (best != null) {
final AbstractFilePatchInProgress patchInProgress = createPatchInProgress(patch, best.getFirst());
if (patchInProgress == null)
break;
processStipUp(patchInProgress, best.getSecond());
result.putValue(best.getFirst(), patchInProgress);
} else {
final AbstractFilePatchInProgress patchInProgress = createPatchInProgress(patch, myBaseDir);
if (patchInProgress == null)
break;
result.putValue(myBaseDir, patchInProgress);
}
}
}
use of com.intellij.openapi.diff.impl.patch.FilePatch in project intellij-community by JetBrains.
the class MatchPatchPaths method findCandidates.
private void findCandidates(@NotNull List<? extends FilePatch> list, @NotNull final PatchBaseDirectoryDetector directoryDetector, @NotNull List<PatchAndVariants> candidates, @NotNull List<FilePatch> newOrWithoutMatches) {
for (final FilePatch patch : list) {
final String fileName = patch.getBeforeFileName();
if (patch.isNewFile() || (patch.getBeforeName() == null)) {
newOrWithoutMatches.add(patch);
continue;
}
final Collection<VirtualFile> files = new ArrayList<>(findFilesFromIndex(directoryDetector, fileName));
// for directories outside the project scope but under version control
if (patch.getBeforeName() != null && patch.getBeforeName().startsWith("..")) {
final VirtualFile relativeFile = VfsUtil.findRelativeFile(myBaseDir, patch.getBeforeName().replace('\\', '/').split("/"));
if (relativeFile != null) {
files.add(relativeFile);
}
}
if (files.isEmpty()) {
newOrWithoutMatches.add(patch);
} else {
//files order is not defined, so get the best variant depends on it, too
final List<AbstractFilePatchInProgress> variants = ObjectsConvertor.convert(files, new Convertor<VirtualFile, AbstractFilePatchInProgress>() {
@Override
public AbstractFilePatchInProgress convert(VirtualFile o) {
return processMatch(patch, o);
}
}, ObjectsConvertor.NOT_NULL);
if (variants.isEmpty()) {
// just to be sure
newOrWithoutMatches.add(patch);
} else {
candidates.add(new PatchAndVariants(variants));
}
}
}
}
Aggregations