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