Search in sources :

Example 6 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project searchcode-server by boyter.

the class IndexGitHistoryJob method getRevisionChanges.

public void getRevisionChanges(Repository localRepository, Git git, GitChangeSet oldRevison, GitChangeSet newRevision) throws IOException, GitAPIException {
    ObjectId oldHead = localRepository.resolve(oldRevison.getRevision() + "^{tree}");
    ObjectId newHead = localRepository.resolve(newRevision.getRevision() + "^{tree}");
    ObjectReader reader = localRepository.newObjectReader();
    CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
    oldTreeIter.reset(reader, oldHead);
    CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
    newTreeIter.reset(reader, newHead);
    List<DiffEntry> entries = git.diff().setNewTree(newTreeIter).setOldTree(oldTreeIter).call();
    GitService gs = new GitService();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    for (DiffEntry entry : entries) {
        if ("DELETE".equals(entry.getChangeType().name())) {
            System.out.println("DEL " + entry.getOldPath());
            String contents = gs.fetchFileRevision(localRepository.getWorkTree().toString() + "/.git", oldRevison.getRevision(), entry.getOldPath());
            CodeIndexDocument cd = new CodeIndexDocument(entry.getNewPath(), "thumbor", entry.getOldPath(), entry.getOldPath(), entry.getOldPath(), "md5hash", "Java", contents.split("\\r?\\n").length, contents, "", oldRevison.getAuthor());
            cd.setRevision(oldRevison.getRevision());
            cd.setYearMonthDay(sdf.format(oldRevison.getExpiry()));
            cd.setYearMonth(cd.getYearMonthDay().substring(0, 6));
            cd.setYear(cd.getYearMonthDay().substring(0, 4));
            cd.setMessage(oldRevison.getMessage());
            cd.setDeleted("TRUE");
            Singleton.getCodeIndexer().indexTimeDocument(cd);
        } else {
            System.out.println("ADD " + entry.getNewPath());
            String contents = gs.fetchFileRevision(localRepository.getWorkTree().toString() + "/.git", newRevision.getRevision(), entry.getNewPath());
            CodeIndexDocument cd = new CodeIndexDocument(entry.getNewPath(), "thumbor", entry.getNewPath(), entry.getNewPath(), entry.getNewPath(), "md5hash", "Java", contents.split("\\r?\\n").length, contents, "", newRevision.getAuthor());
            cd.setRevision(newRevision.getRevision());
            cd.setYearMonthDay(sdf.format(oldRevison.getExpiry()));
            cd.setYearMonth(cd.getYearMonthDay().substring(0, 6));
            cd.setYear(cd.getYearMonthDay().substring(0, 4));
            cd.setMessage(newRevision.getMessage());
            cd.setDeleted("FALSE");
            Singleton.getCodeIndexer().indexTimeDocument(cd);
        }
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) CodeIndexDocument(com.searchcode.app.dto.CodeIndexDocument) GitService(com.searchcode.app.service.GitService) ObjectReader(org.eclipse.jgit.lib.ObjectReader) SimpleDateFormat(java.text.SimpleDateFormat) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 7 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project fabric8 by jboss-fuse.

the class GitPatchManagementServiceImpl method loadPatch.

@Override
public Patch loadPatch(PatchDetailsRequest request) throws PatchException {
    File descriptor = new File(patchesDir, request.getPatchId() + ".patch");
    try {
        Patch patch = loadPatch(descriptor, true);
        if (patch == null) {
            return null;
        }
        Git repo = gitPatchRepository.findOrCreateMainGitRepository();
        List<DiffEntry> diff = null;
        if (request.isFiles() || request.isDiff()) {
            // fetch the information from git
            ObjectId commitId = repo.getRepository().resolve(patch.getManagedPatch().getCommitId());
            RevCommit commit = new RevWalk(repo.getRepository()).parseCommit(commitId);
            diff = gitPatchRepository.diff(repo, commit.getParent(0), commit);
        }
        if (request.isBundles()) {
        // it's already in PatchData
        }
        if (request.isFiles() && diff != null) {
            for (DiffEntry de : diff) {
                DiffEntry.ChangeType ct = de.getChangeType();
                String newPath = de.getNewPath();
                String oldPath = de.getOldPath();
                switch(ct) {
                    case ADD:
                        patch.getManagedPatch().getFilesAdded().add(newPath);
                        break;
                    case MODIFY:
                        patch.getManagedPatch().getFilesModified().add(newPath);
                        break;
                    case DELETE:
                        patch.getManagedPatch().getFilesRemoved().add(oldPath);
                        break;
                }
            }
        }
        if (request.isDiff() && diff != null) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            DiffFormatter formatter = new DiffFormatter(baos);
            formatter.setContext(4);
            formatter.setRepository(repo.getRepository());
            for (DiffEntry de : diff) {
                formatter.format(de);
            }
            formatter.flush();
            patch.getManagedPatch().setUnifiedDiff(new String(baos.toByteArray(), "UTF-8"));
        }
        return patch;
    } catch (IOException | GitAPIException e) {
        throw new PatchException(e.getMessage(), e);
    }
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Git(org.eclipse.jgit.api.Git) PatchException(io.fabric8.patch.management.PatchException) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) ManagedPatch(io.fabric8.patch.management.ManagedPatch) Patch(io.fabric8.patch.management.Patch) DiffEntry(org.eclipse.jgit.diff.DiffEntry) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 8 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project incubator-gobblin by apache.

the class GitConfigMonitor method processGitConfigChanges.

/**
 * Fetch the list of changes since the last refresh of the repository and apply the changes to the {@link FlowCatalog}
 * @throws GitAPIException
 * @throws IOException
 */
@VisibleForTesting
void processGitConfigChanges() throws GitAPIException, IOException {
    // if not active or if the flow catalog is not up yet then can't process config changes
    if (!isActive || !this.flowCatalog.isRunning()) {
        log.info("GitConfigMonitor: skip poll since the JobCatalog is not yet running.");
        return;
    }
    List<DiffEntry> changes = this.gitRepo.getChanges();
    for (DiffEntry change : changes) {
        switch(change.getChangeType()) {
            case ADD:
            case MODIFY:
                addSpec(change);
                break;
            case DELETE:
                removeSpec(change);
                break;
            case RENAME:
                removeSpec(change);
                addSpec(change);
                break;
            default:
                throw new RuntimeException("Unsupported change type " + change.getChangeType());
        }
    }
    // Done processing changes, so checkpoint
    this.gitRepo.moveCheckpointAndHashesForward();
}
Also used : DiffEntry(org.eclipse.jgit.diff.DiffEntry) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 9 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project MGit by maks.

the class CommitDiffTask method doInBackground.

@Override
protected Boolean doInBackground(Void... params) {
    boolean result = getCommitDiff();
    if (!result) {
        return false;
    }
    mDiffStrs = new ArrayList<String>(mDiffEntries.size());
    for (DiffEntry diffEntry : mDiffEntries) {
        try {
            String diffStr = parseDiffEntry(diffEntry);
            mDiffStrs.add(diffStr);
        } catch (StopTaskException e) {
            return false;
        }
    }
    return true;
}
Also used : StopTaskException(me.sheimi.sgit.exception.StopTaskException) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 10 with DiffEntry

use of org.eclipse.jgit.diff.DiffEntry in project gitiles by GerritCodeReview.

the class LogSoyData method toEntrySoyData.

private Map<String, Object> toEntrySoyData(Paginator paginator, RevCommit c, DateFormatter df) throws IOException {
    if (csd == null) {
        csd = new CommitSoyData();
    }
    Map<String, Object> entry = csd.setRevWalk(paginator.getWalk()).toSoyData(req, c, fields, df);
    DiffEntry rename = paginator.getRename(c);
    if (rename != null) {
        entry.put("rename", toRenameSoyData(rename));
    }
    return ImmutableMap.of("variant", variant, "entry", entry);
}
Also used : DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Aggregations

DiffEntry (org.eclipse.jgit.diff.DiffEntry)66 RevCommit (org.eclipse.jgit.revwalk.RevCommit)24 RevWalk (org.eclipse.jgit.revwalk.RevWalk)23 DiffFormatter (org.eclipse.jgit.diff.DiffFormatter)22 ObjectReader (org.eclipse.jgit.lib.ObjectReader)21 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)20 IOException (java.io.IOException)19 Git (org.eclipse.jgit.api.Git)18 ObjectId (org.eclipse.jgit.lib.ObjectId)15 ArrayList (java.util.ArrayList)14 File (java.io.File)13 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)12 RevTree (org.eclipse.jgit.revwalk.RevTree)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Path (java.nio.file.Path)8 Test (org.junit.Test)8 FileTreeIterator (org.eclipse.jgit.treewalk.FileTreeIterator)7 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)7 Ref (org.eclipse.jgit.lib.Ref)6 Repository (org.eclipse.jgit.lib.Repository)6