Search in sources :

Example 21 with CloneCommand

use of org.eclipse.jgit.api.CloneCommand in project gitblit by gitblit.

the class GitServletTest method testAnonymousPush.

@Test
public void testAnonymousPush() throws Exception {
    GitBlitSuite.close(ticgitFolder);
    if (ticgitFolder.exists()) {
        FileUtils.delete(ticgitFolder, FileUtils.RECURSIVE | FileUtils.RETRY);
    }
    RepositoryModel model = repositories().getRepositoryModel("ticgit.git");
    model.accessRestriction = AccessRestrictionType.NONE;
    repositories().updateRepositoryModel(model.name, model, false);
    CloneCommand clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
    clone.setDirectory(ticgitFolder);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
    GitBlitSuite.close(clone.call());
    assertTrue(true);
    Git git = Git.open(ticgitFolder);
    File file = new File(ticgitFolder, "TODO");
    OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
    BufferedWriter w = new BufferedWriter(os);
    w.write("// hellol中文 " + new Date().toString() + "\n");
    w.close();
    git.add().addFilepattern(file.getName()).call();
    git.commit().setMessage("test commit").call();
    Iterable<PushResult> results = git.push().setPushAll().call();
    GitBlitSuite.close(git);
    for (PushResult result : results) {
        for (RemoteRefUpdate update : result.getRemoteUpdates()) {
            assertEquals(Status.OK, update.getStatus());
        }
    }
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) RepositoryModel(com.gitblit.models.RepositoryModel) PushResult(org.eclipse.jgit.transport.PushResult) File(java.io.File) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 22 with CloneCommand

use of org.eclipse.jgit.api.CloneCommand in project gitblit by gitblit.

the class GitServletTest method testSubfolderPush.

@Test
public void testSubfolderPush() throws Exception {
    GitBlitSuite.close(jgitFolder);
    if (jgitFolder.exists()) {
        FileUtils.delete(jgitFolder, FileUtils.RECURSIVE | FileUtils.RETRY);
    }
    CloneCommand clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/test/jgit.git", url));
    clone.setDirectory(jgitFolder);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password));
    GitBlitSuite.close(clone.call());
    assertTrue(true);
    Git git = Git.open(jgitFolder);
    File file = new File(jgitFolder, "TODO");
    OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file, true), Constants.CHARSET);
    BufferedWriter w = new BufferedWriter(os);
    w.write("// " + new Date().toString() + "\n");
    w.close();
    git.add().addFilepattern(file.getName()).call();
    git.commit().setMessage("test commit").call();
    Iterable<PushResult> results = git.push().setPushAll().setCredentialsProvider(new UsernamePasswordCredentialsProvider(account, password)).call();
    GitBlitSuite.close(git);
    for (PushResult result : results) {
        for (RemoteRefUpdate update : result.getRemoteUpdates()) {
            assertEquals(Status.OK, update.getStatus());
        }
    }
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) PushResult(org.eclipse.jgit.transport.PushResult) File(java.io.File) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 23 with CloneCommand

use of org.eclipse.jgit.api.CloneCommand in project gitblit by gitblit.

the class TicketReferenceTest method configure.

@BeforeClass
public static void configure() throws Exception {
    File repositoryName = new File("TicketReferenceTest.git");
    ;
    GitBlitSuite.close(repositoryName);
    if (repositoryName.exists()) {
        FileUtils.delete(repositoryName, FileUtils.RECURSIVE | FileUtils.RETRY);
    }
    repo = new RepositoryModel("TicketReferenceTest.git", null, null, null);
    if (gitblit().hasRepository(repo.name)) {
        gitblit().deleteRepositoryModel(repo);
    }
    gitblit().updateRepositoryModel(repo.name, repo, true);
    user = new UserModel(account);
    user.displayName = account;
    user.emailAddress = account + "@example.com";
    user.password = password;
    cp = new UsernamePasswordCredentialsProvider(user.username, user.password);
    if (gitblit().getUserModel(user.username) != null) {
        gitblit().deleteUser(user.username);
    }
    repo.authorizationControl = AuthorizationControl.NAMED;
    repo.accessRestriction = AccessRestrictionType.PUSH;
    gitblit().updateRepositoryModel(repo.name, repo, false);
    // grant user push permission
    user.setRepositoryPermission(repo.name, AccessPermission.REWIND);
    gitblit().updateUserModel(user);
    ticketService = gitblit().getTicketService();
    assertTrue(ticketService.deleteAll(repo));
    GitBlitSuite.close(workingCopy);
    if (workingCopy.exists()) {
        FileUtils.delete(workingCopy, FileUtils.RECURSIVE | FileUtils.RETRY);
    }
    CloneCommand clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/{1}", url, repo.name));
    clone.setDirectory(workingCopy);
    clone.setBare(false);
    clone.setBranch("master");
    clone.setCredentialsProvider(cp);
    GitBlitSuite.close(clone.call());
    git = Git.open(workingCopy);
    git.getRepository().getConfig().setString("user", null, "name", user.displayName);
    git.getRepository().getConfig().setString("user", null, "email", user.emailAddress);
    git.getRepository().getConfig().save();
    final RevCommit revCommit1 = makeCommit("initial commit");
    final String initialSha = revCommit1.name();
    Iterable<PushResult> results = git.push().setPushAll().setCredentialsProvider(cp).call();
    GitBlitSuite.close(git);
    for (PushResult result : results) {
        for (RemoteRefUpdate update : result.getRemoteUpdates()) {
            assertEquals(Status.OK, update.getStatus());
            assertEquals(initialSha, update.getNewObjectId().name());
        }
    }
}
Also used : UserModel(com.gitblit.models.UserModel) CloneCommand(org.eclipse.jgit.api.CloneCommand) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) RepositoryModel(com.gitblit.models.RepositoryModel) PushResult(org.eclipse.jgit.transport.PushResult) File(java.io.File) RevCommit(org.eclipse.jgit.revwalk.RevCommit) BeforeClass(org.junit.BeforeClass)

Example 24 with CloneCommand

use of org.eclipse.jgit.api.CloneCommand in project bndtools by bndtools.

the class GitCloneTemplate method generateOutputs.

@Override
public ResourceMap generateOutputs(Map<String, List<Object>> parameters, IProgressMonitor monitor) throws Exception {
    File workingDir = null;
    File gitDir = null;
    // Get existing checkout if available
    synchronized (this) {
        if (checkedOut != null) {
            workingDir = checkedOut.getWorkTree();
            gitDir = new File(workingDir, ".git");
        }
    }
    if (workingDir == null) {
        // Need to do a new checkout
        workingDir = Files.createTempDirectory("checkout").toFile();
        gitDir = new File(workingDir, ".git");
        try {
            CloneCommand cloneCmd = Git.cloneRepository().setURI(params.cloneUrl).setDirectory(workingDir).setNoCheckout(true);
            cloneCmd.setProgressMonitor(new EclipseGitProgressTransformer(monitor));
            Git git = cloneCmd.call();
            CheckoutCommand checkout = git.checkout().setCreateBranch(true).setName("_tmp");
            if (params.branch == null) {
                checkout.setStartPoint(GitCloneTemplateParams.DEFAULT_BRANCH);
            } else {
                String startPoint = null;
                if (params.branch.startsWith(Constants.DEFAULT_REMOTE_NAME + "/")) {
                    startPoint = params.branch;
                } else {
                    // Check for a matching tag
                    for (Ref ref : git.tagList().call()) {
                        if (ref.getName().endsWith("/" + params.branch)) {
                            startPoint = params.branch;
                            break;
                        }
                    }
                    if (startPoint == null) {
                        // Check remote branches
                        for (Ref ref : git.branchList().setListMode(ListMode.REMOTE).call()) {
                            if (ref.getName().endsWith("/" + params.branch)) {
                                startPoint = Constants.DEFAULT_REMOTE_NAME + "/" + params.branch;
                                break;
                            }
                        }
                        if (startPoint == null) {
                            if (SHA1_PATTERN.matcher(params.branch).matches()) {
                                startPoint = params.branch;
                                if (startPoint == null) {
                                    throw new Exception("Unable to find requested ref \"" + params.branch + "\"");
                                }
                            }
                        }
                    }
                }
                checkout.setStartPoint(startPoint);
            }
            checkout.call();
            checkedOut = git.getRepository();
        } catch (JGitInternalException e) {
            Throwable cause = e.getCause();
            if (cause instanceof Exception)
                throw (Exception) cause;
            throw e;
        }
    }
    final File exclude = gitDir;
    FileFilter filter = new FileFilter() {

        @Override
        public boolean accept(File path) {
            return !path.equals(exclude);
        }
    };
    return toResourceMap(workingDir, filter);
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) FileFilter(java.io.FileFilter) File(java.io.File) IOException(java.io.IOException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException)

Example 25 with CloneCommand

use of org.eclipse.jgit.api.CloneCommand in project searchcode-server by boyter.

the class IndexGitRepoJob method cloneGitRepository.

/**
     * Clones the repository from scratch
     */
public RepositoryChanged cloneGitRepository(String repoName, String repoRemoteLocation, String repoUserName, String repoPassword, String repoLocations, String branch, boolean useCredentials) {
    boolean successful = false;
    Singleton.getLogger().info("Attempting to clone " + repoRemoteLocation);
    Git call = null;
    try {
        CloneCommand cloneCommand = Git.cloneRepository();
        cloneCommand.setURI(repoRemoteLocation);
        cloneCommand.setDirectory(new File(repoLocations + "/" + repoName + "/"));
        cloneCommand.setCloneAllBranches(true);
        cloneCommand.setBranch(branch);
        if (useCredentials) {
            cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(repoUserName, repoPassword));
        }
        call = cloneCommand.call();
        successful = true;
    } catch (GitAPIException | InvalidPathException ex) {
        successful = false;
        Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + " cloneGitRepository for " + repoName + "\n with message: " + ex.getMessage());
    } 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)

Aggregations

CloneCommand (org.eclipse.jgit.api.CloneCommand)25 File (java.io.File)18 Git (org.eclipse.jgit.api.Git)18 RepositoryModel (com.gitblit.models.RepositoryModel)16 Test (org.junit.Test)15 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)14 PushResult (org.eclipse.jgit.transport.PushResult)13 RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)13 BufferedWriter (java.io.BufferedWriter)11 FileOutputStream (java.io.FileOutputStream)11 OutputStreamWriter (java.io.OutputStreamWriter)11 Date (java.util.Date)11 UserModel (com.gitblit.models.UserModel)6 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)6 IOException (java.io.IOException)5 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)4 Status (org.eclipse.jgit.transport.RemoteRefUpdate.Status)4 Ref (org.eclipse.jgit.lib.Ref)2 Repository (org.eclipse.jgit.lib.Repository)2