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