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);
}
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;
}
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());
}
}
}
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);
}
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);
}
Aggregations