Search in sources :

Example 6 with FileRepository

use of org.eclipse.jgit.internal.storage.file.FileRepository in project searchcode-server by boyter.

the class IndexGitRepoJob method getBlameInfo.

/**
     * Uses the inbuilt git
     * TODO this method appears to leak memory like crazy... need to investigate
     * TODO lots of hairy bits in here need tests to capture issues
     */
public List<CodeOwner> getBlameInfo(int codeLinesSize, String repoName, String repoLocations, String fileName) {
    List<CodeOwner> codeOwners = new ArrayList<>(codeLinesSize);
    try {
        // The / part is required due to centos bug for version 1.1.1
        // This appears to be correct
        String repoLoc = repoLocations + "/" + repoName + "/.git";
        Repository localRepository = new FileRepository(new File(repoLoc));
        BlameCommand blamer = new BlameCommand(localRepository);
        ObjectId commitID = localRepository.resolve("HEAD");
        if (commitID == null) {
            Singleton.getLogger().info("getBlameInfo commitID is null for " + repoLoc + " " + fileName);
            return codeOwners;
        }
        BlameResult blame;
        // Somewhere in here appears to be wrong...
        blamer.setStartCommit(commitID);
        blamer.setFilePath(fileName);
        blame = blamer.call();
        // Hail mary attempt to solve issue on CentOS Attempt to set at all costs
        if (blame == null) {
            // This one appears to solve the issue so don't remove it
            String[] split = fileName.split("/");
            blamer.setStartCommit(commitID);
            if (split.length != 1) {
                blamer.setFilePath(String.join("/", Arrays.asList(split).subList(1, split.length)));
            }
            blame = blamer.call();
        }
        if (blame == null) {
            String[] split = fileName.split("/");
            blamer.setStartCommit(commitID);
            if (split.length != 1) {
                blamer.setFilePath("/" + String.join("/", Arrays.asList(split).subList(1, split.length)));
            }
            blame = blamer.call();
        }
        if (blame == null) {
            Singleton.getLogger().info("getBlameInfo blame is null for " + repoLoc + " " + fileName);
        }
        if (blame != null) {
            // Get all the owners their number of commits and most recent commit
            HashMap<String, CodeOwner> owners = new HashMap<>();
            RevCommit commit;
            PersonIdent authorIdent;
            try {
                for (int i = 0; i < codeLinesSize; i++) {
                    commit = blame.getSourceCommit(i);
                    authorIdent = commit.getAuthorIdent();
                    if (owners.containsKey(authorIdent.getName())) {
                        CodeOwner codeOwner = owners.get(authorIdent.getName());
                        codeOwner.incrementLines();
                        int timestamp = codeOwner.getMostRecentUnixCommitTimestamp();
                        if (commit.getCommitTime() > timestamp) {
                            codeOwner.setMostRecentUnixCommitTimestamp(commit.getCommitTime());
                        }
                        owners.put(authorIdent.getName(), codeOwner);
                    } else {
                        owners.put(authorIdent.getName(), new CodeOwner(authorIdent.getName(), 1, commit.getCommitTime()));
                    }
                }
            } catch (IndexOutOfBoundsException ex) {
                // Ignore this as its not really a problem or is it?
                Singleton.getLogger().info("IndexOutOfBoundsException when trying to get blame for " + repoName + " " + fileName);
            }
            codeOwners = new ArrayList<>(owners.values());
        }
    } catch (IOException ex) {
        Singleton.getLogger().info("IOException getBlameInfo when trying to get blame for " + repoName + " " + fileName + " " + ex.toString());
    } catch (GitAPIException ex) {
        Singleton.getLogger().info("GitAPIException getBlameInfo when trying to get blame for " + repoName + " " + fileName + " " + ex.toString());
    } catch (IllegalArgumentException ex) {
        Singleton.getLogger().info("IllegalArgumentException getBlameInfo when trying to get blame for " + repoName + " " + fileName + " " + ex.toString());
    }
    // Try to clean up
    System.gc();
    return codeOwners;
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) CodeOwner(com.searchcode.app.dto.CodeOwner) BlameResult(org.eclipse.jgit.blame.BlameResult) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BlameCommand(org.eclipse.jgit.api.BlameCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 7 with FileRepository

use of org.eclipse.jgit.internal.storage.file.FileRepository in project searchcode-server by boyter.

the class GitService method fetchFileRevision.

/**
     * Given a repository location, revision and file path will retrieve that files contents. N.B. it returns the whole
     * file so you MAY end up running into serious memory issues, and should be aware of this
     */
public String fetchFileRevision(String repoLocation, String revision, String filePath) throws MissingObjectException, IncorrectObjectTypeException, IOException {
    Repository localRepository = new FileRepository(new File(repoLocation));
    ObjectId id = localRepository.resolve(revision);
    ObjectReader reader = localRepository.newObjectReader();
    try {
        RevWalk walk = new RevWalk(reader);
        RevCommit commit = walk.parseCommit(id);
        RevTree tree = commit.getTree();
        TreeWalk treewalk = TreeWalk.forPath(reader, filePath, tree);
        if (treewalk != null) {
            byte[] data = reader.open(treewalk.getObjectId(0)).getBytes();
            return new String(data, "utf-8");
        } else {
            return "";
        }
    } finally {
        reader.close();
    }
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) File(java.io.File) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

FileRepository (org.eclipse.jgit.internal.storage.file.FileRepository)7 File (java.io.File)5 Repository (org.eclipse.jgit.lib.Repository)5 ArrayList (java.util.ArrayList)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 RevWalk (org.eclipse.jgit.revwalk.RevWalk)3 GerritPersonIdentProvider (com.google.gerrit.server.GerritPersonIdentProvider)2 Git (org.eclipse.jgit.api.Git)2 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)2 ObjectReader (org.eclipse.jgit.lib.ObjectReader)2 PersonIdent (org.eclipse.jgit.lib.PersonIdent)2 RevTree (org.eclipse.jgit.revwalk.RevTree)2 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)1 CodeOwner (com.searchcode.app.dto.CodeOwner)1 RepositoryChanged (com.searchcode.app.dto.RepositoryChanged)1 IOException (java.io.IOException)1 InvalidPathException (java.nio.file.InvalidPathException)1 Date (java.util.Date)1