Search in sources :

Example 1 with GitService

use of com.searchcode.app.service.GitService 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)

Aggregations

CodeIndexDocument (com.searchcode.app.dto.CodeIndexDocument)1 GitService (com.searchcode.app.service.GitService)1 SimpleDateFormat (java.text.SimpleDateFormat)1 DiffEntry (org.eclipse.jgit.diff.DiffEntry)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 ObjectReader (org.eclipse.jgit.lib.ObjectReader)1 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)1