/** * Checks if the commit button should be enabled. * * @param forceEnable <code>true</code> to make the button enable without any additional checks. */ private void toggleCommitButton(boolean forceEnable) { boolean enable = false; if (forceEnable) { enable = true; } else { try { RepositoryState repositoryState = gitAccess.getRepository().getRepositoryState(); if (repositoryState == RepositoryState.MERGING_RESOLVED && gitAccess.getStagedFiles().isEmpty() && gitAccess.getUnstagedFiles().isEmpty()) { enable = true; commitMessage.setText(translator.getTranslation(Tags.CONCLUDE_MERGE_MESSAGE)); } else if (!gitAccess.getStagedFiles().isEmpty()) { enable = true; } else if (repositoryState == RepositoryState.MERGING && translator.getTranslation(Tags.CONCLUDE_MERGE_MESSAGE).equals(commitMessage.getText())) { commitMessage.setText(""); } } catch (NoRepositorySelected e) { // Remains disabled } } commitButton.setEnabled(enable); }
@BeforeMethod public void setup() { jGitConnection = spy( new JGitConnection( repository, credentialsLoader, sshKeyProvider, eventService, userResolver)); RepositoryState repositoryState = mock(RepositoryState.class); GitUser gitUser = mock(GitUser.class); when(repositoryState.canAmend()).thenReturn(true); when(repositoryState.canCommit()).thenReturn(true); when(repository.getRepositoryState()).thenReturn(repositoryState); when(gitUser.getName()).thenReturn("username"); when(gitUser.getEmail()).thenReturn("email"); when(userResolver.getUser()).thenReturn(gitUser); when(repository.getDirectory()).thenReturn(directory); when(directory.getPath()).thenReturn("path"); }
private boolean canMerge(final Repository repository) { String message = null; Exception ex = null; try { Ref head = repository.getRef(Constants.HEAD); if (head == null || !head.isSymbolic()) message = UIText.MergeAction_HeadIsNoBranch; else if (!repository.getRepositoryState().equals( RepositoryState.SAFE)) message = NLS.bind(UIText.MergeAction_WrongRepositoryState, repository.getRepositoryState()); else if (!head.getLeaf().getName().startsWith("refs/heads/features")) { //$NON-NLS-1$ message = "Current branch is not a feature branch."; } } catch (IOException e) { message = e.getMessage(); ex = e; } if (message != null) org.eclipse.egit.ui.Activator.handleError(message, ex, true); return (message == null); }
/** * @return the state of the current repository or <code>null</code>, if there's no repository selected. */ private RepositoryState getRepositoryState() { RepositoryState repositoryState = null; Repository repo = getCurrentRepository(); if (repo != null) { repositoryState = repo.getRepositoryState(); } return repositoryState; }
/** * Check if the repository is in merging state. * * @return <code>true</code> if the repository is in merging state. */ private boolean isRepoInMergingState() { boolean toReturn = false; Repository repo = getCurrentRepository(); if (repo != null) { RepositoryState repositoryState = repo.getRepositoryState(); toReturn = repositoryState == RepositoryState.MERGING_RESOLVED || repositoryState == RepositoryState.MERGING; } return toReturn; }
/** * Pushes all the commits from the local repository to the remote repository * * @param username * - Git username * @param password * - Git password * * @throws GitAPIException * @throws TransportException * @throws InvalidRemoteException */ public PushResponse push(final String username, final String password) throws GitAPIException { AuthenticationInterceptor.install(); PushResponse response = new PushResponse(); RepositoryState repositoryState = git.getRepository().getRepositoryState(); if (repositoryState == RepositoryState.MERGING) { response.setStatus(org.eclipse.jgit.transport.RemoteRefUpdate.Status.REJECTED_OTHER_REASON); response.setMessage(translator.getTranslation(Tags.PUSH_WITH_CONFLICTS)); return response; } if (getPullsBehind() > 0) { response.setStatus(org.eclipse.jgit.transport.RemoteRefUpdate.Status.REJECTED_OTHER_REASON); response.setMessage(translator.getTranslation(Tags.BRANCH_BEHIND)); return response; } String sshPassphrase = OptionsManager.getInstance().getSshPassphrase(); Iterable<PushResult> call = git.push() .setCredentialsProvider(new SSHUserCredentialsProvider(username, password, sshPassphrase)).call(); Iterator<PushResult> results = call.iterator(); logger.debug("Push Ended"); while (results.hasNext()) { PushResult result = results.next(); for (RemoteRefUpdate info : result.getRemoteUpdates()) { response.setStatus(info.getStatus()); return response; } } response.setStatus(org.eclipse.jgit.transport.RemoteRefUpdate.Status.REJECTED_OTHER_REASON); response.setMessage(translator.getTranslation(Tags.PUSH_FAILED_UNKNOWN)); return response; }
@Test public void testRestartMerge() throws Exception { pushOneFileToRemote(); gitAccess.setRepository(SECOND_LOCAL_TEST_REPOSITORY); OptionsManager.getInstance().saveSelectedRepository(SECOND_LOCAL_TEST_REPOSITORY); File file = new File(SECOND_LOCAL_TEST_REPOSITORY + "/test.txt"); file.createNewFile(); PrintWriter out = new PrintWriter(LOCAL_TEST_REPOSITPRY + "/test.txt"); out.println("teeeeeest"); out.close(); gitAccess.add(new FileStatus(GitChangeType.ADD, "test.txt")); gitAccess.commit("conflict"); gitAccess.pull("", ""); for (FileStatus fileStatus : gitAccess.getUnstagedFiles()) { if(fileStatus.getChangeType() == GitChangeType.CONFLICT){ gitAccess.add(fileStatus); } } RepositoryState actual = gitAccess.getRepository().getRepositoryState(); RepositoryState expected = RepositoryState.MERGING_RESOLVED; assertEquals(expected, actual); gitAccess.restartMerge(); actual = gitAccess.getRepository().getRepositoryState(); expected = RepositoryState.MERGING; assertEquals(expected, actual); }
@Override public boolean hasStagedFiles() { if ( git.getRepository().getRepositoryState() == RepositoryState.SAFE ) { return !getStagedFiles().isEmpty(); } else { return git.getRepository().getRepositoryState().canCommit(); } }
@Override protected void run () throws GitException { Repository repository = getRepository(); ObjectId originalCommit = getOriginalCommit(); ObjectId head = getHead(); List<RebaseTodoLine> steps; try { switch (operation) { case BEGIN: // initialize sequencer and cherry-pick steps if there are // more commits to cherry-pick steps = prepareCommand(head); // apply the selected steps applySteps(steps, false); break; case ABORT: // delete the sequencer and reset to the original head if (repository.getRepositoryState() == RepositoryState.CHERRY_PICKING || repository.getRepositoryState() == RepositoryState.CHERRY_PICKING_RESOLVED) { if (originalCommit == null) { // maybe the sequencer is not created in that case simply reset to HEAD originalCommit = head; } } Utils.deleteRecursively(getSequencerFolder()); if (originalCommit != null) { ResetCommand reset = new ResetCommand(repository, getClassFactory(), originalCommit.name(), GitClient.ResetType.HARD, new DelegatingGitProgressMonitor(monitor), listener); reset.execute(); } result = createCustomResult(GitCherryPickResult.CherryPickStatus.ABORTED); break; case QUIT: // used to reset the sequencer only Utils.deleteRecursively(getSequencerFolder()); switch (repository.getRepositoryState()) { case CHERRY_PICKING: // unresolved conflicts result = createResult(CherryPickResult.CONFLICT); break; case CHERRY_PICKING_RESOLVED: result = createCustomResult(GitCherryPickResult.CherryPickStatus.UNCOMMITTED); break; default: result = createCustomResult(GitCherryPickResult.CherryPickStatus.OK); break; } break; case CONTINUE: switch (repository.getRepositoryState()) { case CHERRY_PICKING: // unresolved conflicts, cannot continue result = createResult(CherryPickResult.CONFLICT); break; case CHERRY_PICKING_RESOLVED: // cannot continue without manual commit result = createCustomResult(GitCherryPickResult.CherryPickStatus.UNCOMMITTED); break; default: // read steps from sequencer and apply them // if sequencer is empty this will be a noop steps = readTodoFile(repository); applySteps(steps, true); break; } break; default: throw new IllegalStateException("Unexpected operation " + operation.name()); } } catch (GitAPIException | IOException ex) { throw new GitException(ex); } }
public void testRepositoryState () { for (RepositoryState state : RepositoryState.values()) { assertNotNull(GitRepositoryState.getStateFor(state)); } }
/** * Returns the current state of the repository this client is associated with. * The state indicates what commands may be run on the repository and if the repository * requires any additional commands to get into the normal state. * @param monitor progress monitor * @return current repository state * @throws GitException an unexpected error occurs */ public GitRepositoryState getRepositoryState (ProgressMonitor monitor) throws GitException { Repository repository = gitRepository.getRepository(); RepositoryState state = repository.getRepositoryState(); return GitRepositoryState.getStateFor(state); }
/** * Check if the merging has been resolved. * * @param repositoryState the repository state. * * @return <code>true</code> if the merging has been resolved. */ private boolean isMergingResolved() { RepositoryState repositoryState = getRepositoryState(); return repositoryState != null && repositoryState == RepositoryState.MERGING_RESOLVED; }