Search in sources :

Example 1 with RepositoryChanged

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());
}
Also used : RepositoryChanged(com.searchcode.app.dto.RepositoryChanged) SharedService(com.searchcode.app.service.SharedService) IndexGitRepoJob(com.searchcode.app.jobs.repository.IndexGitRepoJob) UniqueRepoQueue(com.searchcode.app.util.UniqueRepoQueue) RepoResult(com.searchcode.app.model.RepoResult)

Example 2 with 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(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);
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) PullCommand(org.eclipse.jgit.api.PullCommand) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) ArrayList(java.util.ArrayList) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) InvalidPathException(java.nio.file.InvalidPathException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RepositoryChanged(com.searchcode.app.dto.RepositoryChanged) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Git(org.eclipse.jgit.api.Git) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 3 with RepositoryChanged

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;
}
Also used : CloneCommand(org.eclipse.jgit.api.CloneCommand) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RepositoryChanged(com.searchcode.app.dto.RepositoryChanged) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) InvalidPathException(java.nio.file.InvalidPathException)

Example 4 with 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;
}
Also used : RepositoryChanged(com.searchcode.app.dto.RepositoryChanged)

Example 5 with 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);
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) PullCommand(org.eclipse.jgit.api.PullCommand) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) ArrayList(java.util.ArrayList) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) InvalidPathException(java.nio.file.InvalidPathException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RepositoryChanged(com.searchcode.app.dto.RepositoryChanged) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Git(org.eclipse.jgit.api.Git) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Aggregations

RepositoryChanged (com.searchcode.app.dto.RepositoryChanged)10 InvalidPathException (java.nio.file.InvalidPathException)4 Git (org.eclipse.jgit.api.Git)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)4 IndexGitRepoJob (com.searchcode.app.jobs.repository.IndexGitRepoJob)2 ArrayList (java.util.ArrayList)2 CloneCommand (org.eclipse.jgit.api.CloneCommand)2 PullCommand (org.eclipse.jgit.api.PullCommand)2 DiffEntry (org.eclipse.jgit.diff.DiffEntry)2 FileRepository (org.eclipse.jgit.internal.storage.file.FileRepository)2 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)2 RunningIndexJob (com.searchcode.app.dto.RunningIndexJob)1 SearchResult (com.searchcode.app.dto.SearchResult)1 RepoResult (com.searchcode.app.model.RepoResult)1 CodeSearcher (com.searchcode.app.service.CodeSearcher)1 SharedService (com.searchcode.app.service.SharedService)1 UniqueRepoQueue (com.searchcode.app.util.UniqueRepoQueue)1 File (java.io.File)1 IOException (java.io.IOException)1