Search in sources :

Example 21 with FileRepository

use of org.eclipse.jgit.internal.storage.file.FileRepository in project Aspose.Cells-for-Java by aspose-cells.

the class GitHelper method updateRepository.

public static void updateRepository(String localPath, String remotePath) throws Exception {
    Repository localRepo;
    try {
        localRepo = new FileRepository(localPath + "/.git");
        Git git = new Git(localRepo);
        {
            AsposeConstants.println("Cloning Repository [" + remotePath + "]....");
        }
        // First try to clone the repository
        try {
            Git.cloneRepository().setURI(remotePath).setDirectory(new File(localPath)).call();
        } catch (Exception ex) {
            // If clone fails, try to pull the changes
            try {
                git.pull().call();
            } catch (Exception exPull) {
                // Pull also failed. Throw this exception to caller
                {
                    AsposeConstants.println("Pull also failed.");
                }
                // throw it
                throw exPull;
            }
        }
    } catch (Exception ex) {
        throw new Exception("Could not download Repository from Github. Error: " + ex.getMessage());
    }
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) File(java.io.File)

Example 22 with FileRepository

use of org.eclipse.jgit.internal.storage.file.FileRepository in project Aspose.Cells-for-Java by aspose-cells.

the class GitHelper method syncRepository.

public static void syncRepository(String localPath, String remotePath) throws Exception {
    Repository localRepo;
    try {
        localRepo = new FileRepository(localPath + "/.git");
        Git git = new Git(localRepo);
        AsposeConstants.println("Syncronizing Repository [" + remotePath + "]....");
        // Pull the changes
        try {
            git.pull().call();
        } catch (Exception exPull) {
            // If pull failed. Throw this exception to caller
            {
                AsposeConstants.println("Pull failed.");
            }
            // throw it
            throw exPull;
        }
    } catch (Exception ex) {
        throw new Exception("Could not update Repository from Github. Error: " + ex.getMessage());
    }
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git)

Example 23 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 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) {
            this.logger.severe(String.format("caca9ca8::getblameinfo commitId is null for repository %s filename %s", 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) {
            this.logger.info(String.format("273e0a9e::getblameinfo is null for repository %s filename %s", 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) {
                // blame.getSourceCommit(i) will throw this exception and there is no way I can see to correctly
                // identify when it will occur. Its not something that is severe so we log it under info
                this.logger.info(String.format("4cf371a5::error in class %s exception %s for repository %s index out of bounds, this can safely be ignored but is something to fix in the future", ex.getClass(), ex.getMessage(), repoName));
            }
            codeOwners = new ArrayList<>(owners.values());
        }
    } catch (IOException ex) {
        this.logger.severe(String.format("85cd8d0c::error in class %s exception %s for repository %s", ex.getClass(), ex.getMessage(), repoName));
    } catch (GitAPIException ex) {
        this.logger.severe(String.format("91029067::error in class %s exception %s for repository %s", ex.getClass(), ex.getMessage(), repoName));
    } catch (IllegalArgumentException ex) {
        this.logger.severe(String.format("8b6da512::error in class %s exception %s for repository %s", ex.getClass(), ex.getMessage(), repoName));
    }
    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 24 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)

Example 25 with FileRepository

use of org.eclipse.jgit.internal.storage.file.FileRepository in project gerrit by GerritCodeReview.

the class VersionedMetaDataOnInit method save.

protected void save(PersonIdent ident, String msg) throws IOException, ConfigInvalidException {
    File path = getPath();
    if (path == null) {
        throw new IOException(project + " does not exist.");
    }
    try (Repository repo = new FileRepository(path);
        ObjectInserter i = repo.newObjectInserter();
        ObjectReader r = repo.newObjectReader();
        RevWalk rw = new RevWalk(r)) {
        inserter = i;
        reader = r;
        RevTree srcTree = revision != null ? rw.parseTree(revision) : null;
        newTree = readTree(srcTree);
        CommitBuilder commit = new CommitBuilder();
        commit.setAuthor(ident);
        commit.setCommitter(ident);
        commit.setMessage(msg);
        onSave(commit);
        ObjectId res = newTree.writeTree(inserter);
        if (res.equals(srcTree)) {
            return;
        }
        commit.setTreeId(res);
        if (revision != null) {
            commit.addParentId(revision);
        }
        ObjectId newRevision = inserter.insert(commit);
        updateRef(repo, ident, newRevision, "commit: " + msg);
        revision = rw.parseCommit(newRevision);
    } finally {
        inserter = null;
        reader = null;
    }
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Repository(org.eclipse.jgit.lib.Repository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) ObjectReader(org.eclipse.jgit.lib.ObjectReader) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) File(java.io.File) RevTree(org.eclipse.jgit.revwalk.RevTree)

Aggregations

FileRepository (org.eclipse.jgit.internal.storage.file.FileRepository)34 Repository (org.eclipse.jgit.lib.Repository)29 Git (org.eclipse.jgit.api.Git)18 File (java.io.File)16 TestRepository (org.eclipse.jgit.junit.TestRepository)6 IOException (java.io.IOException)5 Test (org.junit.Test)5 Path (java.nio.file.Path)4 ArrayList (java.util.ArrayList)4 Before (org.junit.Before)4 GerritPersonIdentProvider (com.google.gerrit.server.GerritPersonIdentProvider)3 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)3 PersonIdent (org.eclipse.jgit.lib.PersonIdent)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 FileBasedAllProjectsConfigProvider (com.google.gerrit.server.config.FileBasedAllProjectsConfigProvider)2 SitePaths (com.google.gerrit.server.config.SitePaths)2 RepositoryChanged (com.searchcode.app.dto.RepositoryChanged)2 ObjectReader (org.eclipse.jgit.lib.ObjectReader)2