Example usage for org.eclipse.jgit.diff EditList EditList

List of usage examples for org.eclipse.jgit.diff EditList EditList

Introduction

In this page you can find the example usage for org.eclipse.jgit.diff EditList EditList.

Prototype

public EditList() 

Source Link

Document

Create a new, empty edit list.

Usage

From source file:MyDiffFormatter.java

License:Eclipse Distribution License

public FormatResult getFormatResult(DiffEntry ent) throws IOException {
    final FormatResult res = new FormatResult();
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    final EditList editList;
    final FileHeader.PatchType type;

    formatHeader(buf, ent);//from   w w  w.  j a  v  a 2s  .  c o m

    if (ent.getOldMode() == GITLINK || ent.getNewMode() == GITLINK) {
        formatOldNewPaths(buf, ent);
        writeGitLinkDiffText(buf, ent);
        editList = new EditList();
        type = PatchType.UNIFIED;

    } else if (ent.getOldId() == null || ent.getNewId() == null) {
        // Content not changed (e.g. only mode, pure rename)
        editList = new EditList();
        type = PatchType.UNIFIED;

    } else {
        assertHaveRepository();

        byte[] aRaw = open(OLD, ent);
        byte[] bRaw = open(NEW, ent);

        if (aRaw == BINARY || bRaw == BINARY //
                || RawText.isBinary(aRaw) || RawText.isBinary(bRaw)) {
            formatOldNewPaths(buf, ent);
            buf.write(encodeASCII("Binary files differ\n")); //$NON-NLS-1$
            editList = new EditList();
            type = PatchType.BINARY;

        } else {
            res.a = new RawText(aRaw);
            res.b = new RawText(bRaw);
            editList = diff(res.a, res.b);
            type = PatchType.UNIFIED;

            switch (ent.getChangeType()) {
            case RENAME:
            case COPY:
                if (!editList.isEmpty())
                    formatOldNewPaths(buf, ent);
                break;

            default:
                formatOldNewPaths(buf, ent);
                break;
            }
        }
    }

    res.header = new FileHeader(buf.toByteArray(), editList, type);
    return res;
}

From source file:com.diffplug.gradle.spotless.DiffMessageFormatter.java

License:Apache License

/**
 * Returns a git-style diff between the two unix strings.
 *
 * Output has no trailing newlines./*from  w w w . ja  va2  s  .  c  o m*/
 *
 * Boolean args determine whether whitespace or line endings will be visible.
 */
private static String diffWhitespaceLineEndings(String dirty, String clean, boolean whitespace,
        boolean lineEndings) throws IOException {
    dirty = visibleWhitespaceLineEndings(dirty, whitespace, lineEndings);
    clean = visibleWhitespaceLineEndings(clean, whitespace, lineEndings);

    RawText a = new RawText(dirty.getBytes(StandardCharsets.UTF_8));
    RawText b = new RawText(clean.getBytes(StandardCharsets.UTF_8));
    EditList edits = new EditList();
    edits.addAll(MyersDiff.INSTANCE.diff(RawTextComparator.DEFAULT, a, b));

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (DiffFormatter formatter = new DiffFormatter(out)) {
        formatter.format(edits, a, b);
    }
    String formatted = out.toString(StandardCharsets.UTF_8.name());

    // we don't need the diff to show this, since we display newlines ourselves
    formatted = formatted.replace("\\ No newline at end of file\n", "");
    return NEWLINE_MATCHER.trimTrailingFrom(formatted);
}

From source file:com.wira.ews.test.textDiff.TestJGit.java

License:Apache License

@SuppressWarnings("resource")
@Test//from  w  w w .ja  v  a 2  s. c o  m
public void tesGetDiff() throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    try {
        //         RawText rt1 = new RawText(new File("/home/wladek/Downloads/t1.txt"));
        //         RawText rt2 = new RawText(new File("/home/wladek/Downloads/t2.txt"));

        RawText rt1 = new RawText("Testing diffs".getBytes());
        RawText rt2 = new RawText("Testing diffs2".getBytes());
        EditList diffList = new EditList();
        diffList.addAll(new HistogramDiff().diff(RawTextComparator.DEFAULT, rt2, rt1));
        new DiffFormatter(out).format(diffList, rt2, rt1);
    } catch (IOException e) {
        e.printStackTrace();
    }

    String output = out.toString();
    //      
    //      File fileTo = new File("/home/wladek/Downloads/testjGIT.txt");
    //
    //      if (!fileTo.exists()) {
    //         fileTo.createNewFile();
    //      }
    //
    //      FileWriter fw = new FileWriter(fileTo.getAbsoluteFile());
    //
    //      BufferedWriter bw = new BufferedWriter(fw);
    //      
    //      
    //      bw.write(output);
    //      
    //      bw.flush();

    if (output.isEmpty()) {
        logger.info("Empty === ");
    } else {
        logger.info(" NOT EMPTY ");
    }

    System.out.println(" DIFFERENCE " + output);
}

From source file:org.apache.nifi.persistence.TemplateSerializerTest.java

License:Apache License

@Test
public void validateDiffWithChangingComponentIdAndAdditionalElements() throws Exception {

    // Create initial template (TemplateDTO)
    FlowSnippetDTO snippet = new FlowSnippetDTO();
    Set<ProcessorDTO> procs = new HashSet<>();
    for (int i = 4; i > 0; i--) {
        ProcessorDTO procDTO = new ProcessorDTO();
        procDTO.setType("Processor" + i + ".class");
        procDTO.setId(ComponentIdGenerator.generateId().toString());
        procs.add(procDTO);//from   w w  w. java  2  s. c  o m
    }
    snippet.setProcessors(procs);
    TemplateDTO origTemplate = new TemplateDTO();
    origTemplate.setDescription("MyTemplate");
    origTemplate.setId("MyTemplate");
    origTemplate.setSnippet(snippet);
    byte[] serTemplate = TemplateSerializer.serialize(origTemplate);

    // Deserialize Template into TemplateDTO
    ByteArrayInputStream in = new ByteArrayInputStream(serTemplate);
    JAXBContext context = JAXBContext.newInstance(TemplateDTO.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    XMLStreamReader xsr = XmlUtils.createSafeReader(in);
    JAXBElement<TemplateDTO> templateElement = unmarshaller.unmarshal(xsr, TemplateDTO.class);
    TemplateDTO deserTemplate = templateElement.getValue();

    // Modify deserialized template
    FlowSnippetDTO deserSnippet = deserTemplate.getSnippet();
    Set<ProcessorDTO> deserProcs = deserSnippet.getProcessors();
    int c = 0;
    for (ProcessorDTO processorDTO : deserProcs) {
        if (c % 2 == 0) {
            processorDTO.setName("Hello-" + c);
        }
        c++;
    }

    // add new Processor
    ProcessorDTO procDTO = new ProcessorDTO();
    procDTO.setType("ProcessorNew" + ".class");
    procDTO.setId(ComponentIdGenerator.generateId().toString());
    deserProcs.add(procDTO);

    // Serialize modified template
    byte[] serTemplate2 = TemplateSerializer.serialize(deserTemplate);

    RawText rt1 = new RawText(serTemplate);
    RawText rt2 = new RawText(serTemplate2);
    EditList diffList = new EditList();
    diffList.addAll(new HistogramDiff().diff(RawTextComparator.DEFAULT, rt1, rt2));

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try (DiffFormatter diff = new DiffFormatter(out)) {
        diff.format(diffList, rt1, rt2);

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(new ByteArrayInputStream(out.toByteArray()), StandardCharsets.UTF_8));

        List<String> changes = reader.lines().peek(System.out::println)
                .filter(line -> line.startsWith("+") || line.startsWith("-")).collect(Collectors.toList());

        assertEquals("+            <name>Hello-0</name>", changes.get(0));
        assertEquals("+            <name>Hello-2</name>", changes.get(1));
        assertEquals("+        <processors>", changes.get(2));
        assertEquals("+            <type>ProcessorNew.class</type>", changes.get(4));
        assertEquals("+        </processors>", changes.get(5));
    }
}

From source file:org.apache.oozie.command.coord.CoordUpdateXCommand.java

License:Apache License

/**
 * Get the differences in git format./*w w w.  j a v a 2  s.c  om*/
 *
 * @param string1 the string1
 * @param string2 the string2
 * @return the diff
 * @throws IOException Signals that an I/O exception has occurred.
 */
private String getDiffinGitFormat(String string1, String string2) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    RawText rt1 = new RawText(string1.getBytes());
    RawText rt2 = new RawText(string2.getBytes());
    EditList diffList = new EditList();
    diffList.addAll(new HistogramDiff().diff(RawTextComparator.DEFAULT, rt1, rt2));
    new DiffFormatter(out).format(diffList, rt1, rt2);
    return out.toString();
}

From source file:playRepository.FileDiff.java

License:Apache License

/**
 * Remove every line not related to the given lines which "interest".
 *
 * Do nothing if {@link #editList} is null, because a file has been added
 * or deleted./*from  ww  w  .j av  a  2s  . c om*/
 *
 * @param lineA an added line which interests. Ignore if it is <code>null</code>
 * @param lineB an removed line which interests. Ignored if it is <code>null</code>
 */
public void updateRange(Integer lineA, Integer lineB) {
    if (editList == null) {
        return;
    }

    EditList newEditList = new EditList();

    for (Edit edit : editList) {
        if (lineA != null) {
            if ((lineA >= edit.getBeginA() - context) && (lineA <= edit.getEndA() + context)) {
                newEditList.add(edit);
            }
        }

        if (lineB != null) {
            if ((lineB >= edit.getBeginB() - context) && (lineB <= edit.getEndB() + context)) {
                newEditList.add(edit);
            }
        }
    }

    editList = newEditList;
}