use of com.searchcode.app.dto.RepositoryChanged in project searchcode-server by boyter.
the class IndexBaseRepoJobTest method testExecuteHasMethodInQueueNewRepository.
public void testExecuteHasMethodInQueueNewRepository() throws JobExecutionException {
SharedService sharedServiceMock = mock(SharedService.class);
when(sharedServiceMock.getPauseBackgroundJobs()).thenReturn(false);
when(sharedServiceMock.getBackgroundJobsEnabled()).thenReturn(true);
IndexGitRepoJob indexGitRepoJob = new IndexGitRepoJob(sharedServiceMock);
IndexGitRepoJob spy = spy(indexGitRepoJob);
spy.haveRepoResult = false;
UniqueRepoQueue uniqueRepoQueue = new UniqueRepoQueue();
uniqueRepoQueue.add(new RepoResult(1, "name", "scm", "url", "username", "password", "source", "branch", "{}"));
when(spy.getNextQueuedRepo()).thenReturn(uniqueRepoQueue);
when(spy.isEnabled()).thenReturn(true);
when(spy.getNewRepository(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyBoolean())).thenReturn(new RepositoryChanged(false, null, null));
when(mockCodeIndexer.shouldPauseAdding()).thenReturn(false);
spy.codeIndexer = mockCodeIndexer;
spy.execute(this.mockContext);
assertThat(spy.haveRepoResult).isTrue();
assertThat(spy.LOWMEMORY).isTrue();
verify(spy).getNextQueuedRepo();
verify(spy, times(2)).getNewRepository(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyBoolean());
}
use of com.searchcode.app.dto.RepositoryChanged 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 com.searchcode.app.dto.RepositoryChanged 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 com.searchcode.app.dto.RepositoryChanged in project searchcode-server by boyter.
the class IndexSvnRepoJob method checkoutSvnRepository.
public RepositoryChanged checkoutSvnRepository(RepoResult repoResult, String repoLocations, boolean useCredentials) {
boolean successful = false;
this.logger.info("50552307::attempting to checkout " + repoResult.getUrl());
ProcessBuilder processBuilder;
// http://stackoverflow.com/questions/34687/subversion-ignoring-password-and-username-options#38386
if (useCredentials == false) {
processBuilder = new ProcessBuilder(this.SVN_BINARY_PATH, "checkout", "--no-auth-cache", "--non-interactive", repoResult.getUrl(), repoResult.getDirectoryName());
} else {
processBuilder = new ProcessBuilder(this.SVN_BINARY_PATH, "checkout", "--no-auth-cache", "--non-interactive", "--username", repoResult.getUsername(), "--password", repoResult.getPassword(), repoResult.getUrl(), repoResult.getDirectoryName());
}
processBuilder.directory(new File(repoLocations));
Process process = null;
BufferedReader bufferedReader = null;
try {
File file = new File(repoLocations);
if (!file.exists()) {
boolean success = file.mkdir();
if (!success) {
throw new IOException("Was unable to create directory " + repoLocations);
}
}
process = processBuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is, Values.CHARSET_UTF8);
bufferedReader = new BufferedReader(isr);
String line;
while ((line = bufferedReader.readLine()) != null) {
this.logger.info("b9143c4f::" + line);
}
successful = true;
} catch (IOException ex) {
this.logger.severe(String.format("b20daaf6::error in class %s exception %s for repository %s", ex.getClass(), ex.getMessage(), repoResult.getName()));
} finally {
Singleton.getHelpers().closeQuietly(process);
Singleton.getHelpers().closeQuietly(bufferedReader);
}
RepositoryChanged repositoryChanged = new RepositoryChanged(successful);
repositoryChanged.setClone(true);
return repositoryChanged;
}
use of com.searchcode.app.dto.RepositoryChanged 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(String repoName, String repoRemoteLocation, String repoUserName, String repoPassword, String repoLocations, String branch, boolean useCredentials) {
boolean changed = false;
List<String> changedFiles = new ArrayList<>();
List<String> deletedFiles = new ArrayList<>();
Singleton.getLogger().info("Attempting to pull latest from " + repoRemoteLocation + " for " + repoName);
Repository localRepository = null;
Git git = null;
try {
localRepository = new FileRepository(new File(repoLocations + "/" + repoName + "/.git"));
Ref head = localRepository.getRef("HEAD");
git = new Git(localRepository);
git.reset();
git.clean();
PullCommand pullCmd = git.pull();
if (useCredentials) {
pullCmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(repoUserName, repoPassword));
}
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;
Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + " updateGitRepository for " + repoName + "\n with message: " + ex.getMessage());
} finally {
Singleton.getHelpers().closeQuietly(localRepository);
Singleton.getHelpers().closeQuietly(git);
}
return new RepositoryChanged(changed, changedFiles, deletedFiles);
}
Aggregations