Search in sources :

Example 1 with CloneCommand

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

the class SshDaemonTest method testCloneCommand.

@Test
public void testCloneCommand() throws Exception {
    if (ticgitFolder.exists()) {
        GitBlitSuite.close(ticgitFolder);
        FileUtils.delete(ticgitFolder, FileUtils.RECURSIVE);
    }
    // set clone restriction
    RepositoryModel model = repositories().getRepositoryModel("ticgit.git");
    model.accessRestriction = AccessRestrictionType.CLONE;
    model.authorizationControl = AuthorizationControl.NAMED;
    repositories().updateRepositoryModel(model.name, model, false);
    JschConfigTestSessionFactory sessionFactory = new JschConfigTestSessionFactory(roKeyPair);
    SshSessionFactory.setInstance(sessionFactory);
    CloneCommand clone = Git.cloneRepository();
    clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
    clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
    clone.setDirectory(ticgitFolder);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    Git git = clone.call();
    List<RevCommit> commits = JGitUtils.getRevLog(git.getRepository(), 10);
    GitBlitSuite.close(git);
    assertEquals(10, commits.size());
    // restore anonymous repository access
    model.accessRestriction = AccessRestrictionType.NONE;
    model.authorizationControl = AuthorizationControl.NAMED;
    repositories().updateRepositoryModel(model.name, model, false);
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) RepositoryModel(com.gitblit.models.RepositoryModel) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 2 with CloneCommand

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

the class JGitUtils method cloneRepository.

/**
	 * Clone or Fetch a repository. If the local repository does not exist,
	 * clone is called. If the repository does exist, fetch is called. By
	 * default the clone/fetch retrieves the remote heads, tags, and notes.
	 *
	 * @param repositoriesFolder
	 * @param name
	 * @param fromUrl
	 * @param bare
	 * @param credentialsProvider
	 * @return CloneResult
	 * @throws Exception
	 */
public static CloneResult cloneRepository(File repositoriesFolder, String name, String fromUrl, boolean bare, CredentialsProvider credentialsProvider) throws Exception {
    CloneResult result = new CloneResult();
    if (bare) {
        // bare repository, ensure .git suffix
        if (!name.toLowerCase().endsWith(Constants.DOT_GIT_EXT)) {
            name += Constants.DOT_GIT_EXT;
        }
    } else {
        // normal repository, strip .git suffix
        if (name.toLowerCase().endsWith(Constants.DOT_GIT_EXT)) {
            name = name.substring(0, name.indexOf(Constants.DOT_GIT_EXT));
        }
    }
    result.name = name;
    File folder = new File(repositoriesFolder, name);
    if (folder.exists()) {
        File gitDir = FileKey.resolve(new File(repositoriesFolder, name), FS.DETECTED);
        Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).build();
        result.fetchResult = fetchRepository(credentialsProvider, repository);
        repository.close();
    } else {
        CloneCommand clone = new CloneCommand();
        clone.setBare(bare);
        clone.setCloneAllBranches(true);
        clone.setURI(fromUrl);
        clone.setDirectory(folder);
        if (credentialsProvider != null) {
            clone.setCredentialsProvider(credentialsProvider);
        }
        Repository repository = clone.call().getRepository();
        // Now we have to fetch because CloneCommand doesn't fetch
        // refs/notes nor does it allow manual RefSpec.
        result.createdRepository = true;
        result.fetchResult = fetchRepository(credentialsProvider, repository);
        repository.close();
    }
    return result;
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) Repository(org.eclipse.jgit.lib.Repository) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder)

Example 3 with CloneCommand

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

the class GitDaemonTest method testPushToFrozenRepo.

@Test
public void testPushToFrozenRepo() 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);
    GitBlitSuite.close(clone.call());
    assertTrue(true);
    // freeze repo
    RepositoryModel model = repositories().getRepositoryModel("test/jgit.git");
    model.isFrozen = true;
    repositories().updateRepositoryModel(model.name, model, false);
    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().call();
    for (PushResult result : results) {
        for (RemoteRefUpdate update : result.getRemoteUpdates()) {
            assertEquals(Status.REJECTED_OTHER_REASON, update.getStatus());
        }
    }
    // unfreeze repo
    model.isFrozen = false;
    repositories().updateRepositoryModel(model.name, model, false);
    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) 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 4 with CloneCommand

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

the class GitServletTest method testBogusLoginClone.

@Test
public void testBogusLoginClone() throws Exception {
    // restrict repository access
    RepositoryModel model = repositories().getRepositoryModel("ticgit.git");
    model.accessRestriction = AccessRestrictionType.CLONE;
    repositories().updateRepositoryModel(model.name, model, false);
    // delete any existing working folder
    boolean cloned = false;
    try {
        CloneCommand clone = Git.cloneRepository();
        clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
        clone.setDirectory(ticgit2Folder);
        clone.setBare(false);
        clone.setCloneAllBranches(true);
        clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider("bogus", "bogus"));
        GitBlitSuite.close(clone.call());
        cloned = true;
    } catch (Exception e) {
    // swallow the exception which we expect
    }
    // restore anonymous repository access
    model.accessRestriction = AccessRestrictionType.NONE;
    repositories().updateRepositoryModel(model.name, model, false);
    assertFalse("Bogus login cloned a repository?!", cloned);
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) RepositoryModel(com.gitblit.models.RepositoryModel) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) Test(org.junit.Test)

Example 5 with CloneCommand

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

the class GitServletTest method testUnauthorizedLoginClone.

@Test
public void testUnauthorizedLoginClone() throws Exception {
    // restrict repository access
    RepositoryModel model = repositories().getRepositoryModel("ticgit.git");
    model.accessRestriction = AccessRestrictionType.CLONE;
    model.authorizationControl = AuthorizationControl.NAMED;
    UserModel user = new UserModel("james");
    user.password = "james";
    gitblit().addUser(user);
    repositories().updateRepositoryModel(model.name, model, false);
    FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);
    // delete any existing working folder
    boolean cloned = false;
    try {
        CloneCommand clone = Git.cloneRepository();
        clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
        clone.setDirectory(ticgit2Folder);
        clone.setBare(false);
        clone.setCloneAllBranches(true);
        clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(user.username, user.password));
        GitBlitSuite.close(clone.call());
        cloned = true;
    } catch (Exception e) {
    // swallow the exception which we expect
    }
    assertFalse("Unauthorized login cloned a repository?!", cloned);
    FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);
    // switch to authenticated
    model.authorizationControl = AuthorizationControl.AUTHENTICATED;
    repositories().updateRepositoryModel(model.name, model, false);
    // try clone again
    cloned = false;
    CloneCommand clone = Git.cloneRepository();
    clone.setURI(MessageFormat.format("{0}/ticgit.git", url));
    clone.setDirectory(ticgit2Folder);
    clone.setBare(false);
    clone.setCloneAllBranches(true);
    clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider(user.username, user.password));
    GitBlitSuite.close(clone.call());
    cloned = true;
    assertTrue("Authenticated login could not clone!", cloned);
    FileUtils.delete(ticgit2Folder, FileUtils.RECURSIVE);
    // restore anonymous repository access
    model.accessRestriction = AccessRestrictionType.NONE;
    model.authorizationControl = AuthorizationControl.NAMED;
    repositories().updateRepositoryModel(model.name, model, false);
    delete(user);
}
Also used : UserModel(com.gitblit.models.UserModel) CloneCommand(org.eclipse.jgit.api.CloneCommand) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) RepositoryModel(com.gitblit.models.RepositoryModel) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) Test(org.junit.Test)

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