Search in sources :

Example 26 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider 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(RepoResult repoResult, String repoLocations, boolean useCredentials) {
    boolean changed = false;
    List<String> changedFiles = new ArrayList<>();
    List<String> deletedFiles = new ArrayList<>();
    this.logger.info(String.format("6cffea0f::attempting to pull latest from %s for %s", repoLocations, repoResult.getName()));
    Repository localRepository = null;
    Git git = null;
    try {
        localRepository = new FileRepository(new File(repoLocations + "/" + repoResult.getDirectoryName() + "/.git"));
        Ref head = localRepository.getRef("HEAD");
        git = new Git(localRepository);
        git.reset();
        git.clean();
        PullCommand pullCmd = git.pull();
        if (useCredentials) {
            pullCmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(repoResult.getUsername(), repoResult.getPassword()));
        }
        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;
        String error = String.format("c6646806::error in class %s exception %s repository %s", ex.getClass(), ex.getMessage(), repoResult.getName());
        this.logger.severe(error);
        repoResult.getData().indexError = error;
        Singleton.getRepo().saveRepo(repoResult);
    } 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 27 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project searchcode-server by boyter.

the class IndexGitRepoJob method cloneGitRepository.

/**
 * Clones the repository from scratch
 */
public RepositoryChanged cloneGitRepository(RepoResult repoResult, String repoLocations, boolean useCredentials) {
    boolean successful;
    this.logger.info(String.format("664f20c7::attempting to clone %s", repoResult.getUrl()));
    Git call = null;
    try {
        CloneCommand cloneCommand = Git.cloneRepository();
        cloneCommand.setURI(repoResult.getUrl());
        cloneCommand.setDirectory(new File(repoLocations + "/" + repoResult.getDirectoryName() + "/"));
        cloneCommand.setCloneAllBranches(true);
        cloneCommand.setBranch(repoResult.getBranch());
        if (useCredentials) {
            cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(repoResult.getUsername(), repoResult.getPassword()));
        }
        call = cloneCommand.call();
        successful = true;
    } catch (GitAPIException | InvalidPathException ex) {
        successful = false;
        String error = String.format("6e56fa26::error in class %s exception %s repository %s", ex.getClass(), ex.getMessage(), repoResult.getName());
        this.logger.severe(error);
        repoResult.getData().indexError = error;
        Singleton.getRepo().saveRepo(repoResult);
    } finally {
        Singleton.getHelpers().closeQuietly(call);
    }
    RepositoryChanged repositoryChanged = new RepositoryChanged(successful);
    repositoryChanged.setClone(true);
    return repositoryChanged;
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RepositoryChanged(com.searchcode.app.dto.RepositoryChanged) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) InvalidPathException(java.nio.file.InvalidPathException)

Example 28 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project gerrit by GerritCodeReview.

the class AbstractGitOverHttpServlet method beforeEach.

@Before
public void beforeEach() throws Exception {
    jettyServer = server.getHttpdInjector().getInstance(JettyServer.class);
    CredentialsProvider.setDefault(new UsernamePasswordCredentialsProvider(admin.username(), admin.httpPassword()));
    selectProtocol(AbstractPushForReview.Protocol.HTTP);
// Don't clear audit events here, since we can't guarantee all test setup has run yet.
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) JettyServer(com.google.gerrit.pgm.http.jetty.JettyServer) Before(org.junit.Before)

Example 29 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project gerrit by GerritCodeReview.

the class HttpForcePushIT method cloneProjectOverHttp.

@Before
public void cloneProjectOverHttp() throws Exception {
    CredentialsProvider.setDefault(new UsernamePasswordCredentialsProvider(admin.username(), admin.httpPassword()));
    testRepo = GitUtil.cloneProject(project, admin.getHttpUrl(server) + "/" + project.get());
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Before(org.junit.Before)

Example 30 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project gerrit by GerritCodeReview.

the class HttpSubmitOnPushIT method cloneProjectOverHttp.

@Before
public void cloneProjectOverHttp() throws Exception {
    CredentialsProvider.setDefault(new UsernamePasswordCredentialsProvider(admin.username(), admin.httpPassword()));
    testRepo = GitUtil.cloneProject(project, admin.getHttpUrl(server) + "/a/" + project.get());
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Before(org.junit.Before)

Aggregations

UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)76 Git (org.eclipse.jgit.api.Git)47 File (java.io.File)34 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)23 IOException (java.io.IOException)19 Test (org.junit.Test)18 CloneCommand (org.eclipse.jgit.api.CloneCommand)17 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)15 RepositoryModel (com.gitblit.models.RepositoryModel)12 PushResult (org.eclipse.jgit.transport.PushResult)12 RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)9 FileOutputStream (java.io.FileOutputStream)8 PushCommand (org.eclipse.jgit.api.PushCommand)8 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 BufferedWriter (java.io.BufferedWriter)7 OutputStreamWriter (java.io.OutputStreamWriter)7 Date (java.util.Date)7 Ref (org.eclipse.jgit.lib.Ref)7 Repository (org.eclipse.jgit.lib.Repository)7 URIish (org.eclipse.jgit.transport.URIish)7