@Override public Status status(List<String> filter) throws GitException { if (!isInsideWorkTree()) { throw new GitInvalidRepositoryException(NOT_A_GIT_REPOSITORY_ERROR); } String repositoryPath = getRepositoryPath(); // Status can be not actual, if commit is in progress. if (COMMITTING_REPOSITORIES.contains(repositoryPath)) { throw new GitCommitInProgressException(COMMIT_IN_PROGRESS_ERROR); } // Status can be not actual, if checkout is in progress. if (CHECKOUT_REPOSITORIES.contains(repositoryPath)) { throw new GitCheckoutInProgressException(CHECKOUT_IN_PROGRESS_ERROR); } String branchName = getCurrentBranch(); StatusCommand statusCommand = getGit().status(); if (filter != null) { filter.forEach(statusCommand::addPath); } return new JGitStatusImpl(branchName, statusCommand); }
/** * @param branchName current repository branch name * @param statusCommand Jgit status command * @throws GitException when any error occurs */ public JGitStatusImpl(String branchName, StatusCommand statusCommand) throws GitException { this.branchName = branchName; org.eclipse.jgit.api.Status gitStatus; try { gitStatus = statusCommand.call(); } catch (GitAPIException exception) { throw new GitException(exception.getMessage(), exception); } clean = gitStatus.isClean(); added = new ArrayList<>(gitStatus.getAdded()); changed = new ArrayList<>(gitStatus.getChanged()); removed = new ArrayList<>(gitStatus.getRemoved()); missing = new ArrayList<>(gitStatus.getMissing()); modified = new ArrayList<>(gitStatus.getModified()); untracked = new ArrayList<>(gitStatus.getUntracked()); untrackedFolders = new ArrayList<>(gitStatus.getUntrackedFolders()); conflicting = new ArrayList<>(gitStatus.getConflicting()); }
@Test public void shouldPullForcepullNotClean() throws Exception { Git git = mock(Git.class); StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); Repository repository = mock(Repository.class); StoredConfig storedConfig = mock(StoredConfig.class); when(git.status()).thenReturn(statusCommand); when(git.getRepository()).thenReturn(repository); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(false); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); repo.setForcePull(true); boolean shouldPull = repo.shouldPull(git); assertThat("shouldPull was false", shouldPull, is(true)); }
@Test public void shouldPullNotClean() throws Exception { Git git = mock(Git.class); StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); Repository repository = mock(Repository.class); StoredConfig storedConfig = mock(StoredConfig.class); when(git.status()).thenReturn(statusCommand); when(git.getRepository()).thenReturn(repository); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(false); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); boolean shouldPull = repo.shouldPull(git); assertThat("shouldPull was true", shouldPull, is(false)); }
@Test public void shouldPullClean() throws Exception { Git git = mock(Git.class); StatusCommand statusCommand = mock(StatusCommand.class); Status status = mock(Status.class); Repository repository = mock(Repository.class); StoredConfig storedConfig = mock(StoredConfig.class); when(git.status()).thenReturn(statusCommand); when(git.getRepository()).thenReturn(repository); when(repository.getConfig()).thenReturn(storedConfig); when(storedConfig.getString("remote", "origin", "url")).thenReturn("http://example/git"); when(statusCommand.call()).thenReturn(status); when(status.isClean()).thenReturn(true); JGitEnvironmentRepository repo = new JGitEnvironmentRepository(this.environment); boolean shouldPull = repo.shouldPull(git); assertThat("shouldPull was false", shouldPull, is(true)); }
public Status getStatus(String path) throws Exception { fetch(); StatusCommand sc = git.status(); if (path != null) sc.addPath(path); return sc.call(); }
private boolean isClean(Git git) { StatusCommand status = git.status(); try { return status.call().isClean(); } catch (Exception e) { String message = "Could not execute status command on local repository. Cause: (" + e.getClass().getSimpleName() + ") " + e.getMessage(); warn(message, e); return false; } }
@Test public void testInit_gitStatusApiException() throws RevisionGeneratorException, GitAPIException, IOException { String branch = "master"; String hash = UUID.randomUUID().toString().replaceAll("-", "")+UUID.randomUUID().toString().replaceAll("-", "").substring(0,8); String abbreviatedHash = hash.substring(0, 5); AbbreviatedObjectId abbreviatedObjectId = AbbreviatedObjectId.fromString(abbreviatedHash); int commitTime = (int) (System.currentTimeMillis()/1000); RevCommit headCommit = createRevCommit(hash, commitTime); Repository repo = mock(Repository.class); Ref headRef = mock(Ref.class); ObjectId headObjectId = mock(ObjectId.class); LogCommand logCmd = mock(LogCommand.class); StatusCommand statusCmd = mock(StatusCommand.class); GitAPIException exception = new NoHeadException("Dummy Git API Exception"); when(git.getRepository()).thenReturn(repo); when(repo.isBare()).thenReturn(Boolean.FALSE); when(repo.getBranch()).thenReturn(branch); when(repo.getRef(eq("HEAD"))).thenReturn(headRef); when(headRef.getObjectId()).thenReturn(headObjectId); when(headObjectId.abbreviate(eq(5))).thenReturn(abbreviatedObjectId); when(git.log()).thenReturn(logCmd); when(logCmd.setMaxCount(1)).thenReturn(logCmd); when(logCmd.call()).thenReturn(Arrays.asList(headCommit)); when(git.status()).thenReturn(statusCmd); when(statusCmd.call()).thenThrow(exception); exceptions.expect(RevisionGeneratorException.class); exceptions.expectMessage("Issue getting Git Status"); exceptions.expectCause(IsInstanceOf.any(GitAPIException.class)); try { item.init(git, logger); } finally { verify(git).getRepository(); verify(git).log(); verify(repo).isBare(); verify(repo).getRef(eq("HEAD")); verify(repo).getBranch(); verify(headRef, times(2)).getObjectId(); verify(headObjectId).abbreviate(eq(5)); verify(logCmd).setMaxCount(eq(1)); verify(logCmd).call(); verify(git).status(); verify(statusCmd).call(); verifyNoMoreInteractions(git); verifyNoMoreInteractions(repo); verifyNoMoreInteractions(headRef); verifyNoMoreInteractions(headObjectId); verifyNoMoreInteractions(logCmd); verifyNoMoreInteractions(statusCmd); verifyZeroInteractions(logger); } }
@Override protected String doInBackground(GitCommand... commands) { Integer nbChanges = null; for (GitCommand command : commands) { Log.d("doInBackground", "Executing the command <" + command.toString() + ">"); try { if (command instanceof StatusCommand) { // in case we have changes, we want to keep track of it org.eclipse.jgit.api.Status status = ((StatusCommand) command).call(); nbChanges = status.getChanged().size() + status.getMissing().size(); } else if (command instanceof CommitCommand) { // the previous status will eventually be used to avoid a commit if (nbChanges == null || nbChanges > 0) command.call(); }else if (command instanceof PushCommand) { for (final PushResult result : ((PushCommand) command).call()) { // Code imported (modified) from Gerrit PushOp, license Apache v2 for (final RemoteRefUpdate rru : result.getRemoteUpdates()) { switch (rru.getStatus()) { case REJECTED_NONFASTFORWARD: return activity.getString(R.string.git_push_nff_error); case REJECTED_NODELETE: case REJECTED_REMOTE_CHANGED: case NON_EXISTING: case NOT_ATTEMPTED: return activity.getString(R.string.git_push_generic_error) + rru.getStatus().name(); case REJECTED_OTHER_REASON: if ("non-fast-forward".equals(rru.getMessage())) { return activity.getString(R.string.git_push_other_error); } else { return activity.getString(R.string.git_push_generic_error) + rru.getMessage(); } default: break; } } } } else { command.call(); } } catch (Exception e) { e.printStackTrace(); return e.getMessage() + "\nCaused by:\n" + e.getCause(); } } return ""; }