Example usage for org.eclipse.jgit.patch FileHeader getChangeType

List of usage examples for org.eclipse.jgit.patch FileHeader getChangeType

Introduction

In this page you can find the example usage for org.eclipse.jgit.patch FileHeader getChangeType.

Prototype

public ChangeType getChangeType() 

Source Link

Document

Get the change type

Usage

From source file:MySmartApply.java

License:Eclipse Distribution License

/**
 * Executes the {@code ApplyCommand} command with all the options and
 * parameters collected by the setter methods (e.g.
 * {@link #setPatch(InputStream)} of this class. Each instance of this class
 * should only be used for one invocation of the command. Don't call this
 * method twice on an instance./*ww w.  j  av a  2s  .c om*/
 *
 * @return an {@link ApplyResult} object representing the command result
 * @throws GitAPIException
 * @throws PatchFormatException
 * @throws PatchApplyException
 */
public ApplyResult call() throws GitAPIException, PatchFormatException, PatchApplyException {
    ApplyResult r = new ApplyResult();
    try {
        final Patch p = new Patch();
        try {
            p.parse(in);
        } finally {
            in.close();
        }
        if (!p.getErrors().isEmpty())
            throw new PatchFormatException(p.getErrors());
        for (FileHeader fh : p.getFiles()) {

            if (listener != null)
                listener.setFilename(
                        getFile((fh.getChangeType() != ChangeType.ADD) ? fh.getOldPath() : fh.getNewPath(),
                                false).getName());

            if (fh.getChangeType() == ChangeType.ADD || fh.getChangeType() == ChangeType.MODIFY
                    || fh.getChangeType() == ChangeType.COPY) {
                if (fh.getNewPath().contains(".lang.php") || fh.getNewPath().contains(".gitignore")
                        || fh.getNewPath().equals("includes/config.php")
                        || fh.getNewPath().contains("/images")) {
                    if (fh.getNewPath().contains(".lang.php") || fh.getNewPath().contains("/images"))
                        this.failed_files.add(getFile(
                                (fh.getChangeType() != ChangeType.ADD) ? fh.getOldPath() : fh.getNewPath(),
                                false));

                    if (listener != null)
                        listener.skipped();
                    continue;
                }
            }

            if (listener != null)
                listener.started();

            ChangeType type = fh.getChangeType();
            File f = null;
            switch (type) {
            case ADD:
                f = getFile(fh.getNewPath(), true);
                apply(f, fh);
                break;
            case MODIFY:
                f = getFile(fh.getOldPath(), false);
                apply(f, fh);
                break;
            case DELETE:
                f = getFile(fh.getOldPath(), false);
                if (!f.delete())
                    throw new PatchApplyException(MessageFormat.format(JGitText.get().cannotDeleteFile, f));
                break;
            case RENAME:
                f = getFile(fh.getOldPath(), false);
                File dest = getFile(fh.getNewPath(), false);
                if (!f.renameTo(dest))
                    throw new PatchApplyException(
                            MessageFormat.format(JGitText.get().renameFileFailed, f, dest));
                break;
            case COPY:
                f = getFile(fh.getOldPath(), false);
                byte[] bs = IO.readFully(f);
                FileOutputStream fos = new FileOutputStream(getFile(fh.getNewPath(), true));
                try {
                    fos.write(bs);
                } finally {
                    fos.close();
                }
            }

            // ... //

            if (!failed_files.contains(f) && type != ChangeType.DELETE)
                r.addUpdatedFile(f);

            // ... //
        }
    } catch (IOException e) {
        throw new PatchApplyException(MessageFormat.format(JGitText.get().patchApplyException, e.getMessage()),
                e);
    }

    return r;
}

From source file:com.google.gerrit.server.patch.PatchListEntry.java

License:Apache License

private static ChangeType toChangeType(final FileHeader hdr) {
    switch (hdr.getChangeType()) {
    case ADD:/*from  w w  w  .j a va2 s  .c  o m*/
        return Patch.ChangeType.ADDED;
    case MODIFY:
        return Patch.ChangeType.MODIFIED;
    case DELETE:
        return Patch.ChangeType.DELETED;
    case RENAME:
        return Patch.ChangeType.RENAMED;
    case COPY:
        return Patch.ChangeType.COPIED;
    default:
        throw new IllegalArgumentException("Unsupported type " + hdr.getChangeType());
    }
}

From source file:com.google.gitiles.DiffServletTest.java

License:Apache License

@Test
public void diffFileNoParentsText() throws Exception {
    String contents = "foo\ncontents\n";
    RevCommit c = repo.update("master", repo.commit().add("foo", contents));

    FakeHttpServletRequest req = FakeHttpServletRequest.newRequest();
    req.setPathInfo("/test/+diff/" + c.name() + "^!/foo");
    req.setQueryString("format=TEXT");
    FakeHttpServletResponse res = new FakeHttpServletResponse();
    servlet.service(req, res);/*ww  w  .jav  a2  s.  c  o m*/

    Patch p = parsePatch(res.getActualBody());
    FileHeader f = getOnlyElement(p.getFiles());
    assertEquals(ChangeType.ADD, f.getChangeType());
    assertEquals(DiffEntry.DEV_NULL, f.getPath(Side.OLD));
    assertEquals("foo", f.getPath(Side.NEW));

    RawText rt = new RawText(contents.getBytes(UTF_8));
    Edit e = getOnlyElement(getOnlyElement(f.getHunks()).toEditList());
    assertEquals(Type.INSERT, e.getType());
    assertEquals(contents, rt.getString(e.getBeginB(), e.getEndB(), false));
}

From source file:com.google.gitiles.DiffServletTest.java

License:Apache License

@Test
public void diffFileOneParentText() throws Exception {
    String contents1 = "foo\n";
    String contents2 = "foo\ncontents\n";
    RevCommit c1 = repo.update("master", repo.commit().add("foo", contents1));
    RevCommit c2 = repo.update("master", repo.commit().parent(c1).add("foo", contents2));

    FakeHttpServletRequest req = FakeHttpServletRequest.newRequest();
    req.setPathInfo("/test/+diff/" + c2.name() + "^!/foo");
    req.setQueryString("format=TEXT");
    FakeHttpServletResponse res = new FakeHttpServletResponse();
    servlet.service(req, res);//from   www . java  2  s. c om

    Patch p = parsePatch(res.getActualBody());
    FileHeader f = getOnlyElement(p.getFiles());
    assertEquals(ChangeType.MODIFY, f.getChangeType());
    assertEquals("foo", f.getPath(Side.OLD));
    assertEquals("foo", f.getPath(Side.NEW));

    RawText rt2 = new RawText(contents2.getBytes(UTF_8));
    Edit e = getOnlyElement(getOnlyElement(f.getHunks()).toEditList());
    assertEquals(Type.INSERT, e.getType());
    assertEquals("contents\n", rt2.getString(e.getBeginB(), e.getEndB(), false));
}

From source file:org.eclipse.orion.server.git.patch.ApplyCommand.java

License:Open Source License

/**
 * Executes the {@code ApplyCommand} command with all the options and
 * parameters collected by the setter methods (e.g.
 * {@link #setPatch(InputStream)} of this class. Each instance of this class
 * should only be used for one invocation of the command. Don't call this
 * method twice on an instance./*  w  ww.ja v a  2s. c  om*/
 *
 * @return collection of formatting errors, if any
 */
public ApplyResult call() {
    ApplyResult r = new ApplyResult();
    try {
        final Patch p = new Patch();
        p.parse(in);
        if (!p.getErrors().isEmpty())
            return r.setFormatErrors(p.getErrors());
        for (FileHeader fh : p.getFiles()) {
            ChangeType type = fh.getChangeType();
            File f = null;
            switch (type) {
            case ADD:
                f = getFile(fh.getNewPath(), true);
                apply(f, fh, r);
                break;
            case MODIFY:
                f = getFile(fh.getOldPath(), false);
                apply(f, fh, r);
                break;
            case DELETE:
                f = getFile(fh.getOldPath(), false);
                if (!f.delete())
                    r.addApplyError(new ApplyError(fh, ChangeType.DELETE));
                break;
            case RENAME:
                f = getFile(fh.getOldPath(), false);
                File dest = getFile(fh.getNewPath(), false);
                if (!f.renameTo(dest))
                    r.addApplyError(new ApplyError(fh, ChangeType.RENAME));
                break;
            case COPY:
                f = getFile(fh.getOldPath(), false);
                byte[] bs = IO.readFully(f);
                FileWriter fw = new FileWriter(getFile(fh.getNewPath(), true));
                fw.write(new String(bs));
                fw.close();
            }
        }
    } catch (IOException e) {
        r.addApplyError(new ApplyError(e));
    } finally {
        IOUtilities.safeClose(in);
    }
    return r;
}

From source file:org.uberfire.java.nio.fs.jgit.util.commands.DiffBranches.java

License:Apache License

private FileDiff createFileDiff(final DiffEntry elem, final FileHeader header, final Edit edit) {
    try {// w  w w  . j  a  v a2  s. c o m
        final String changeType = header.getChangeType().toString();
        final int startA = edit.getBeginA();
        final int endA = edit.getEndA();
        final int startB = edit.getBeginB();
        final int endB = edit.getEndB();

        String pathA = header.getOldPath();
        String pathB = header.getNewPath();

        final List<String> linesA = getLines(elem.getOldId().toObjectId(), startA, endA);
        final List<String> linesB = getLines(elem.getNewId().toObjectId(), startB, endB);

        return new FileDiff(pathA, pathB, startA, endA, startB, endB, changeType, linesA, linesB);
    } catch (IOException e) {
        throw new GitException("A problem occurred when trying to obtain diffs between files", e);
    }
}