org.eclipse.jgit.diff.RawText Java Examples

The following examples show how to use org.eclipse.jgit.diff.RawText. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GitParser.java    From SZZUnleashed with MIT License 6 votes vote down vote up
/**
 * Map lines between one commit and another.
 *
 * @param foundCommit a blameresult containing information about a commit that have made changes
 *     to a file.
 * @param filePath the file that the commit have made changes to.
 * @return a mapping with the original revision file lines as keys and the values the
 *     corresponding lines in the other commit.
 */
private List<Integer> getLineMappings(BlameResult foundCommit, String filePath)
    throws IOException, GitAPIException {
  foundCommit.computeAll();
  RawText foundContent = foundCommit.getResultContents();

  /*
   * Easiest solution, maybe better with a list and a pair class?
   */
  List<Integer> lineMappings = new LinkedList<>();

  for (int line = 0; line < foundContent.size(); line++) {
    lineMappings.add(foundCommit.getSourceLine(line));
  }
  return lineMappings;
}
 
Example #2
Source File: MergeFormatterPass.java    From onedev with MIT License 6 votes vote down vote up
void formatMerge() throws IOException {
	boolean missingNewlineAtEnd = false;
	for (MergeChunk chunk : res) {
		RawText seq = res.getSequences().get(chunk.getSequenceIndex());
		writeConflictMetadata(chunk);
		// the lines with conflict-metadata are written. Now write the chunk
		for (int i = chunk.getBegin(); i < chunk.getEnd(); i++)
			writeLine(seq, i);
		missingNewlineAtEnd = seq.isMissingNewlineAtEnd();
	}
	// one possible leftover: if the merge result ended with a conflict we
	// have to close the last conflict here
	if (lastConflictingName != null)
		writeConflictEnd();
	if (!missingNewlineAtEnd)
		out.beginln();
}
 
Example #3
Source File: ResolveMerger.java    From onedev with MIT License 6 votes vote down vote up
/**
 * Does the content merge. The three texts base, ours and theirs are
 * specified with {@link CanonicalTreeParser}. If any of the parsers is
 * specified as <code>null</code> then an empty text will be used instead.
 *
 * @param base
 * @param ours
 * @param theirs
 * @param attributes
 *
 * @return the result of the content merge
 * @throws IOException
 */
private MergeResult<RawText> contentMerge(CanonicalTreeParser base,
		CanonicalTreeParser ours, CanonicalTreeParser theirs,
		Attributes attributes)
		throws IOException {
	RawText baseText;
	RawText ourText;
	RawText theirsText;

	try {
		baseText = base == null ? RawText.EMPTY_TEXT : getRawText(
						base.getEntryObjectId(), attributes);
		ourText = ours == null ? RawText.EMPTY_TEXT : getRawText(
						ours.getEntryObjectId(), attributes);
		theirsText = theirs == null ? RawText.EMPTY_TEXT : getRawText(
						theirs.getEntryObjectId(), attributes);
	} catch (BinaryBlobException e) {
		MergeResult<RawText> r = new MergeResult<>(Collections.<RawText>emptyList());
		r.setContainsConflicts(true);
		return r;
	}
	return (mergeAlgorithm.merge(RawTextComparator.DEFAULT, baseText,
			ourText, theirsText));
}
 
Example #4
Source File: ResolveMerger.java    From onedev with MIT License 6 votes vote down vote up
private TemporaryBuffer doMerge(MergeResult<RawText> result)
		throws IOException {
	TemporaryBuffer.LocalFile buf = new TemporaryBuffer.LocalFile(
			db != null ? nonNullRepo().getDirectory() : null, inCoreLimit);
	boolean success = false;
	try {
		new MergeFormatter().formatMerge(buf, result,
				Arrays.asList(commitNames), UTF_8);
		buf.close();
		success = true;
	} finally {
		if (!success) {
			buf.destroy();
		}
	}
	return buf;
}
 
Example #5
Source File: DiffCalculator.java    From diff-check with GNU Lesser General Public License v2.1 5 votes vote down vote up
private List<DiffEntryWrapper> doCalculateIndexedDiff(
        RevCommit oldCommit,
        ObjectReader reader,
        Git git,
        File repoDir) throws Exception {
    Set<String> indexedPathSet = new HashSet<>();
    Status status = git.status().call();
    indexedPathSet.addAll(status.getAdded());
    indexedPathSet.addAll(status.getChanged());
    Map<String, BlobWrapper> indexedFileContentMap = getIndexedFileContentMap(git, indexedPathSet);
    Map<String, BlobWrapper> oldRevFileContentMap = getRevFileContentMap(git, oldCommit, indexedPathSet, reader);
    return indexedPathSet.stream()
            .map(filePath -> {
                BlobWrapper oldBlob = oldRevFileContentMap.get(filePath);
                RawText oldText = oldBlob != null ? new RawText(oldBlob.getContent()) : RawText.EMPTY_TEXT;
                RawText newText = new RawText(indexedFileContentMap.get(filePath).getContent());
                DiffEntry entry = oldBlob == null
                        ? DiffHelper.createAddDiffEntry(filePath, oldCommit)
                        : DiffHelper.createModifyDiffEntry(filePath);
                return DiffEntryWrapper.builder()
                        .gitDir(repoDir)
                        .diffEntry(entry)
                        .editList(calculateEditList(oldText, newText))
                        .build();
            })
            .collect(Collectors.toList());
}
 
Example #6
Source File: JgitUtils.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Generates the differences between the contents of the 2 files.
 *
 * @param baseFile
 *            The base file to examine.
 * @param patchFile
 *            The patch file to examine.
 * @return The iterator containing the differences.
 * @throws IOException
 *             if Exceptions occur while reading the file.
 */
public static Iterator<JgitDifference> getDifferences(File baseFile, File patchFile)
        throws IOException {
    if (diffAlgorithm == null) {
        diffAlgorithm = DiffAlgorithm.getAlgorithm(SupportedAlgorithm.HISTOGRAM);
    }

    final RawText baseFileRaw = new RawText(baseFile);
    final RawText patchFileRaw = new RawText(patchFile);

    return new JgitDifferenceIterator(diffAlgorithm.diff(RawTextComparator.DEFAULT,
            baseFileRaw, patchFileRaw), baseFileRaw, patchFileRaw);
}
 
Example #7
Source File: AutoCRLFComparator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean equals (RawText a, int ai, RawText b, int bi) {
    String line1 = a.getString(ai);
    String line2 = b.getString(bi);
    line1 = trimTrailingEoL(line1);
    line2 = trimTrailingEoL(line2);

    return line1.equals(line2);
}
 
Example #8
Source File: CheckoutRevisionCommand.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void checkoutFile (MergeResult<RawText> merge, String path) throws IOException {
    File file = new File(getRepository().getWorkTree(), path);
    file.getParentFile().mkdirs();
    MergeFormatter format = new MergeFormatter();
    WorkingTreeOptions opt = getRepository().getConfig().get(WorkingTreeOptions.KEY);
    try (OutputStream fos = opt.getAutoCRLF() != CoreConfig.AutoCRLF.FALSE
            ? new AutoCRLFOutputStream(new FileOutputStream(file))
            : new FileOutputStream(file)) {
        format.formatMerge(fos, merge, Arrays.asList(new String[] { "BASE", "OURS", "THEIRS" }), //NOI18N
                Constants.CHARACTER_ENCODING);
    }
}
 
Example #9
Source File: MergeFormatterPass.java    From onedev with MIT License 5 votes vote down vote up
private void writeLine(RawText seq, int i) throws IOException {
	out.beginln();
	seq.writeLine(out, i);
	// still BOL? It was a blank line. But writeLine won't lf, so we do.
	if (out.isBeginln())
		out.write('\n');
}
 
Example #10
Source File: ResolveMerger.java    From onedev with MIT License 5 votes vote down vote up
private RawText getRawText(ObjectId id,
		Attributes attributes)
		throws IOException, BinaryBlobException {
	if (id.equals(ObjectId.zeroId()))
		return new RawText(new byte[] {});

	ObjectLoader loader = LfsFactory.getInstance().applySmudgeFilter(
			getRepository(), reader.open(id, OBJ_BLOB),
			attributes.get(Constants.ATTR_MERGE));
	int threshold = PackConfig.DEFAULT_BIG_FILE_THRESHOLD;
	return RawText.load(loader, threshold);
}
 
Example #11
Source File: DiffingLines.java    From SZZUnleashed with MIT License 5 votes vote down vote up
/**
 * Extract the RawText object from a id.
 *
 * @param id the id on the object, which in this case is a commit.
 * @return either null or a RawText object.
 */
private RawText toRaw(AbbreviatedObjectId id) {
  try {
    ObjectLoader loader = this.repo.open(id.toObjectId());
    return RawText.load(loader, PackConfig.DEFAULT_BIG_FILE_THRESHOLD);
  } catch (Exception e) {
    return null;
  }
}
 
Example #12
Source File: MyersAlgorithm.java    From servicecomb-toolkit with Apache License 2.0 5 votes vote down vote up
@Override
public List<Comparison> compare(String source, String dest) {

  if ((source == null) || (dest == null)) {
    LOGGER.error("Source is {} and dest is {}", source, dest);
    throw new RuntimeException("Source and dest must not be null");
  }

  EditList diffList = new EditList();
  diffList.addAll(MyersDiff.INSTANCE.diff(RawTextComparator.DEFAULT,
      new RawText(source.getBytes()), new RawText(dest.getBytes())));

  List<Comparison> comparisonList = new ArrayList<>();

  diffList.stream().forEachOrdered(edit -> {
    ComparisionType comparisionType;
    switch (edit.getType()) {
      case INSERT:
        comparisionType = ComparisionType.INSERT;
        break;
      case DELETE:
        comparisionType = ComparisionType.DELETE;
        break;
      case REPLACE:
        comparisionType = ComparisionType.REPLACE;
        break;
      default:
        comparisionType = ComparisionType.EQUAL;
        break;
    }
    comparisonList
        .add(new Comparison(comparisionType, edit.getBeginA(), edit.getEndA(), edit.getBeginB(), edit.getEndB()));
  });
  return comparisonList;
}
 
Example #13
Source File: DiffCalculator.java    From diff-check with GNU Lesser General Public License v2.1 5 votes vote down vote up
private List<Edit> calculateEditList(RawText oldText, RawText newText) {
    EditList edits = diffAlgorithm.diff(comparator, oldText, newText);
    List<Edit> editList = new ArrayList<Edit>();
    for (Edit edit : edits) {
        editList.add(edit);
    }
    return editList;
}
 
Example #14
Source File: DiffCalculator.java    From diff-check with GNU Lesser General Public License v2.1 5 votes vote down vote up
private RawText newRawText(DiffEntry entry, DiffEntry.Side side, ObjectReader reader) {
    try {
        return new RawText(DiffHelper.open(entry, side, reader, bigFileThreshold));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #15
Source File: DiffCalculator.java    From diff-check with GNU Lesser General Public License v2.1 5 votes vote down vote up
private List<DiffEntryWrapper> doCalculateCommitDiff(
        RevCommit oldCommit,
        RevCommit newCommit,
        ObjectReader reader,
        Git git,
        File repoDir,
        Set<String> excludedPathSet) throws Exception {

    if (Objects.equals(oldCommit.getId(), newCommit.getId())) {
        return Collections.emptyList();
    }

    if (Objects.equals(oldCommit.getTree().getId(), newCommit.getTree().getId())) {
        return Collections.emptyList();
    }

    RenameDetector detector = new RenameDetector(git.getRepository());
    AbstractTreeIterator oldTree = new CanonicalTreeParser(null, reader, oldCommit.getTree());
    AbstractTreeIterator newTree = new CanonicalTreeParser(null, reader, newCommit.getTree());

    List<DiffEntry> entries = git.diff()
            .setOldTree(oldTree)
            .setNewTree(newTree)
            .call();
    detector.reset();
    detector.addAll(entries);
    entries = detector.compute();

    return entries.stream()
            .filter(entry -> !excludedPathSet.contains(entry.getNewPath()))
            .map(entry -> {
                RawText oldText = newRawText(entry, DiffEntry.Side.OLD, reader);
                RawText newText = newRawText(entry, DiffEntry.Side.NEW, reader);
                return DiffEntryWrapper.builder()
                        .gitDir(repoDir)
                        .diffEntry(entry)
                        .editList(calculateEditList(oldText, newText))
                        .build();
            }).collect(Collectors.toList());
}
 
Example #16
Source File: ResolveMerger.java    From onedev with MIT License 4 votes vote down vote up
/**
 * Updates the index after a content merge has happened. If no conflict has
 * occurred this includes persisting the merged content to the object
 * database. In case of conflicts this method takes care to write the
 * correct stages to the index.
 *
 * @param base
 * @param ours
 * @param theirs
 * @param result
 * @param attributes
 * @throws FileNotFoundException
 * @throws IOException
 */
private void updateIndex(CanonicalTreeParser base,
		CanonicalTreeParser ours, CanonicalTreeParser theirs,
		MergeResult<RawText> result, Attributes attributes)
		throws FileNotFoundException,
		IOException {
	TemporaryBuffer rawMerged = null;
	try {
		rawMerged = doMerge(result);
		File mergedFile = inCore ? null
				: writeMergedFile(rawMerged, attributes);
		if (result.containsConflicts()) {
			// A conflict occurred, the file will contain conflict markers
			// the index will be populated with the three stages and the
			// workdir (if used) contains the halfway merged content.
			add(tw.getRawPath(), base, DirCacheEntry.STAGE_1, EPOCH, 0);
			add(tw.getRawPath(), ours, DirCacheEntry.STAGE_2, EPOCH, 0);
			add(tw.getRawPath(), theirs, DirCacheEntry.STAGE_3, EPOCH, 0);
			mergeResults.put(tw.getPathString(), result);
			return;
		}

		// No conflict occurred, the file will contain fully merged content.
		// The index will be populated with the new merged version.
		DirCacheEntry dce = new DirCacheEntry(tw.getPathString());

		// Set the mode for the new content. Fall back to REGULAR_FILE if
		// we can't merge modes of OURS and THEIRS.
		int newMode = mergeFileModes(tw.getRawMode(0), tw.getRawMode(1),
				tw.getRawMode(2));
		dce.setFileMode(newMode == FileMode.MISSING.getBits()
				? FileMode.REGULAR_FILE : FileMode.fromBits(newMode));
		if (mergedFile != null) {
			dce.setLastModified(
					nonNullRepo().getFS().lastModifiedInstant(mergedFile));
			dce.setLength((int) mergedFile.length());
		}
		dce.setObjectId(insertMergeResult(rawMerged, attributes));
		builder.add(dce);
	} finally {
		if (rawMerged != null) {
			rawMerged.destroy();
		}
	}
}
 
Example #17
Source File: DiffingLines.java    From SZZUnleashed with MIT License 4 votes vote down vote up
/**
 * Get the formatted diff between two commits.
 *
 * @param entry the DiffEntry object containing the real diff.
 * @param edits the edited chunks.
 * @return the Diffing lines with line numbers.
 */
public DiffLines getDiffingLines(DiffEntry entry, EditList edits) throws IOException, GitAPIException {
  /*
   * Access the RawText objects for the old and the new entry.
   */
  RawText old = toRaw(entry.getOldId());
  RawText present = toRaw(entry.getNewId());

  /*
   * If the old file is null, it indicates that a new file has been made.
   */
  if (old == null || present == null) return new DiffLines();

  DiffLines lines = new DiffLines();

  int i = 0;
  /*
   * Loop through all edits.
   */
  while (i < edits.size()) {
      Edit first = edits.get(i);
      int last = last(edits, i);
      Edit second = edits.get(last);

      /*
       * Get the limits for the change in the old file.
       */
      int firstIndex = first.getBeginA() - customContext;
      int firstEnd = second.getEndA() + customContext;

      /*
       * Get the limits for the change in the new file.
       */
      int secondIndex = first.getBeginB() - customContext;
      int secondEnd = second.getEndB() + customContext;

      /*
       * Are they out of boundary?
       */
      firstIndex = 0 > firstIndex ? 0 : firstIndex;
      firstEnd = old.size() < firstEnd ? old.size() : firstEnd;

      secondIndex = 0 > secondIndex ? 0 : secondIndex;
      secondEnd = present.size() < secondEnd ? present.size() : secondEnd;

      /*
       * Loop through both revisions parallel.
       */
      while (firstIndex < firstEnd || secondIndex < secondEnd) {
          String[] info = null;

          if (firstIndex < first.getBeginA() || last + 1 < i) {
              if (this.omitLineText) {
                info = new String[]{Integer.toString(firstIndex), Integer.toString(firstIndex)};
              } else {
                info = new String[]{Integer.toString(firstIndex), old.getString(firstIndex)};
              }
              lines.insertions.add(info);
              
              firstIndex+=1;
              secondIndex+=1;
          } else if (firstIndex < first.getEndA()) {
              if (this.omitLineText) {
                info = new String[]{Integer.toString(firstIndex), Integer.toString(firstIndex)};
              } else {
                info = new String[]{Integer.toString(firstIndex), old.getString(firstIndex)};
              }
              lines.deletions.add(info);
              firstIndex+=1;
          } else if (secondIndex < first.getEndB()) {
              if (this.omitLineText) {
                info = new String[]{Integer.toString(secondIndex), Integer.toString(secondIndex)};
              } else {
                info = new String[]{Integer.toString(secondIndex), present.getString(secondIndex)};
              }
              lines.insertions.add(info);
              secondIndex+=1;
          }

          /*
           * Check if there is a gap between the next diff.
           */
          if (firstIndex >= first.getEndA() &&
              secondIndex >= first.getEndB() &&
              ++i < edits.size()){
              first = edits.get(i);
          }
      }
  }
  return lines;
}
 
Example #18
Source File: Utils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static RawText getRawText (ObjectId id, ObjectDatabase db) throws IOException {
    if (id.equals(ObjectId.zeroId())) {
        return RawText.EMPTY_TEXT;
    }
    return new RawText(db.open(id, Constants.OBJ_BLOB).getCachedBytes());
}
 
Example #19
Source File: MergeFormatterPass.java    From onedev with MIT License 3 votes vote down vote up
/**
 * @param out
 *            the {@link java.io.OutputStream} where to write the textual
 *            presentation
 * @param res
 *            the merge result which should be presented
 * @param seqName
 *            When a conflict is reported each conflicting range will get a
 *            name. This name is following the "&lt;&lt;&lt;&lt;&lt;&lt;&lt;
 *            " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The
 *            names for the sequences are given in this list
 * @param charset
 *            the character set used when writing conflict metadata
 */
MergeFormatterPass(OutputStream out, MergeResult<RawText> res,
		List<String> seqName, Charset charset) {
	this.out = new EolAwareOutputStream(out);
	this.res = res;
	this.seqName = seqName;
	this.charset = charset;
	this.threeWayMerge = (res.getSequences().size() == 3);
}
 
Example #20
Source File: MergeFormatter.java    From onedev with MIT License 2 votes vote down vote up
/**
 * Formats the results of a merge of {@link org.eclipse.jgit.diff.RawText}
 * objects in a Git conformant way. This method also assumes that the
 * {@link org.eclipse.jgit.diff.RawText} objects being merged are line
 * oriented files which use LF as delimiter. This method will also use LF to
 * separate chunks and conflict metadata, therefore it fits only to texts
 * that are LF-separated lines.
 *
 * @param out
 *            the output stream where to write the textual presentation
 * @param res
 *            the merge result which should be presented
 * @param seqName
 *            When a conflict is reported each conflicting range will get a
 *            name. This name is following the "&lt;&lt;&lt;&lt;&lt;&lt;&lt;
 *            " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The
 *            names for the sequences are given in this list
 * @param charsetName
 *            the name of the character set used when writing conflict
 *            metadata
 * @throws java.io.IOException
 * @deprecated Use
 *             {@link #formatMerge(OutputStream, MergeResult, List, Charset)}
 *             instead.
 */
@Deprecated
public void formatMerge(OutputStream out, MergeResult<RawText> res,
		List<String> seqName, String charsetName) throws IOException {
	formatMerge(out, res, seqName, Charset.forName(charsetName));
}
 
Example #21
Source File: MergeFormatter.java    From onedev with MIT License 2 votes vote down vote up
/**
 * Formats the results of a merge of {@link org.eclipse.jgit.diff.RawText}
 * objects in a Git conformant way. This method also assumes that the
 * {@link org.eclipse.jgit.diff.RawText} objects being merged are line
 * oriented files which use LF as delimiter. This method will also use LF to
 * separate chunks and conflict metadata, therefore it fits only to texts
 * that are LF-separated lines.
 *
 * @param out
 *            the output stream where to write the textual presentation
 * @param res
 *            the merge result which should be presented
 * @param seqName
 *            When a conflict is reported each conflicting range will get a
 *            name. This name is following the "&lt;&lt;&lt;&lt;&lt;&lt;&lt;
 *            " or "&gt;&gt;&gt;&gt;&gt;&gt;&gt; " conflict markers. The
 *            names for the sequences are given in this list
 * @param charset
 *            the character set used when writing conflict metadata
 * @throws java.io.IOException
 * @since 5.2
 */
public void formatMerge(OutputStream out, MergeResult<RawText> res,
		List<String> seqName, Charset charset) throws IOException {
	new MergeFormatterPass(out, res, seqName, charset).formatMerge();
}
 
Example #22
Source File: JgitUtils.java    From contribution with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Default constructor.
 *
 * @param edits
 *            The list of base and patch line numbers with differences.
 * @param baseFileRaw
 *            The raw base file.
 * @param patchFileRaw
 *            The raw patch file.
 */
private JgitDifferenceIterator(EditList edits, RawText baseFileRaw, RawText patchFileRaw) {
    this.edits = edits;
    this.baseFileRaw = baseFileRaw;
    this.patchFileRaw = patchFileRaw;
}