Search in sources :

Example 11 with FileRepository

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

the class AccountsOnInit method insert.

public void insert(ReviewDb db, Account account) throws OrmException, IOException {
    db.accounts().insert(ImmutableSet.of(account));
    File path = getPath();
    if (path != null) {
        try (Repository repo = new FileRepository(path);
            ObjectInserter oi = repo.newObjectInserter()) {
            PersonIdent serverIdent = new GerritPersonIdentProvider(flags.cfg).get();
            AccountsUpdate.createUserBranch(repo, oi, serverIdent, serverIdent, account);
        }
    }
}
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) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdentProvider(com.google.gerrit.server.GerritPersonIdentProvider) File(java.io.File)

Example 12 with FileRepository

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

the class IndexGitHistoryJob method getGitChangeSets.

public void getGitChangeSets() throws IOException, GitAPIException {
    //Repository localRepository = new FileRepository(new File("./repo/server/.git"));
    Repository localRepository = new FileRepository(new File("./repo/thumbor/.git"));
    Git git = new Git(localRepository);
    Iterable<RevCommit> logs = git.log().call();
    List<GitChangeSet> gitChangeSets = new ArrayList<>();
    for (RevCommit rev : logs) {
        String message = rev.getFullMessage();
        String author = rev.getAuthorIdent().getName();
        Date expiry = new Date(Long.valueOf(rev.getCommitTime()) * 1000);
        System.out.println(expiry.toString() + " " + rev.getCommitTime() + " " + rev.getName());
        gitChangeSets.add(new GitChangeSet(message, author, rev.getName(), expiry));
    }
    gitChangeSets = Lists.reverse(gitChangeSets);
    // TODO currently this is ignoring the very first commit changes need to include those
    for (int i = 1; i < gitChangeSets.size(); i++) {
        System.out.println("///////////////////////////////////////////////");
        this.getRevisionChanges(localRepository, git, gitChangeSets.get(i - 1), gitChangeSets.get(i));
    }
}
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) ArrayList(java.util.ArrayList) File(java.io.File) Date(java.util.Date) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 13 with FileRepository

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

the class IndexGitRepoJob method updateGitRepository.

/**
     * Update a git repository and return if it has changed and the differences
     */
public RepositoryChanged updateGitRepository(String repoName, String repoRemoteLocation, String repoUserName, String repoPassword, String repoLocations, String branch, boolean useCredentials) {
    boolean changed = false;
    List<String> changedFiles = new ArrayList<>();
    List<String> deletedFiles = new ArrayList<>();
    Singleton.getLogger().info("Attempting to pull latest from " + repoRemoteLocation + " for " + repoName);
    Repository localRepository = null;
    Git git = null;
    try {
        localRepository = new FileRepository(new File(repoLocations + "/" + repoName + "/.git"));
        Ref head = localRepository.getRef("HEAD");
        git = new Git(localRepository);
        git.reset();
        git.clean();
        PullCommand pullCmd = git.pull();
        if (useCredentials) {
            pullCmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(repoUserName, repoPassword));
        }
        pullCmd.call();
        Ref newHEAD = localRepository.getRef("HEAD");
        if (!head.toString().equals(newHEAD.toString())) {
            changed = true;
            // Get the differences between the the heads which we updated at
            // and use these to just update the differences between them
            ObjectId oldHead = localRepository.resolve(head.getObjectId().getName() + "^{tree}");
            ObjectId newHead = localRepository.resolve(newHEAD.getObjectId().getName() + "^{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();
            for (DiffEntry entry : entries) {
                if ("DELETE".equals(entry.getChangeType().name())) {
                    deletedFiles.add(FilenameUtils.separatorsToUnix(entry.getOldPath()));
                } else {
                    changedFiles.add(FilenameUtils.separatorsToUnix(entry.getNewPath()));
                }
            }
        }
    } catch (IOException | GitAPIException | InvalidPathException ex) {
        changed = false;
        Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + " updateGitRepository for " + repoName + "\n with message: " + ex.getMessage());
    } finally {
        Singleton.getHelpers().closeQuietly(localRepository);
        Singleton.getHelpers().closeQuietly(git);
    }
    return new RepositoryChanged(changed, changedFiles, deletedFiles);
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) PullCommand(org.eclipse.jgit.api.PullCommand) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) ArrayList(java.util.ArrayList) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) InvalidPathException(java.nio.file.InvalidPathException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RepositoryChanged(com.searchcode.app.dto.RepositoryChanged) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Git(org.eclipse.jgit.api.Git) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 14 with FileRepository

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

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 15 with FileRepository

use of org.eclipse.jgit.internal.storage.file.FileRepository in project Corgi by kevinYin.

the class JGitTest method init.

@Before
public void init() throws IOException {
    localPath = "/Users/kevin/tmp/platform-parent";
    remotePath = "http://gits.ibeiliao.net/platform/platform-parent.git";
    localRepo = new FileRepository(localPath + "/.git");
    git = new Git(localRepo);
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Git(org.eclipse.jgit.api.Git) Before(org.junit.Before)

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