List of usage examples for org.eclipse.jgit.lib RepositoryState canCommit
public abstract boolean canCommit();
From source file:org.eclipse.egit.ui.internal.actions.CommitAction.java
License:Open Source License
@Override public void execute(IAction act) { // let's see if there is any dirty editor around and // ask the user if they want to save or abort if (!PlatformUI.getWorkbench().saveAllEditors(true)) { return;//from w w w . j a va2s . co m } resetState(); try { buildIndexHeadDiffList(); } catch (IOException e) { handle(new TeamException(UIText.CommitAction_errorComputingDiffs, e), UIText.CommitAction_errorDuringCommit, UIText.CommitAction_errorComputingDiffs); return; } catch (CoreException e) { handle(new TeamException(UIText.CommitAction_errorComputingDiffs, e), UIText.CommitAction_errorDuringCommit, UIText.CommitAction_errorComputingDiffs); return; } Repository[] repos = getRepositoriesFor(getProjectsForSelectedResources()); Repository repository = null; amendAllowed = repos.length == 1; for (Repository repo : repos) { repository = repo; RepositoryState state = repo.getRepositoryState(); // currently we don't support committing a merge commit if (state == RepositoryState.MERGING_RESOLVED || !state.canCommit()) { MessageDialog.openError(getTargetPart().getSite().getShell(), UIText.CommitAction_cannotCommit, NLS.bind(UIText.CommitAction_repositoryState, state.getDescription())); return; } } loadPreviousCommit(); if (files.isEmpty()) { if (amendAllowed && previousCommit != null) { boolean result = MessageDialog.openQuestion(getTargetPart().getSite().getShell(), UIText.CommitAction_noFilesToCommit, UIText.CommitAction_amendCommit); if (!result) return; amending = true; } else { MessageDialog.openWarning(getTargetPart().getSite().getShell(), UIText.CommitAction_noFilesToCommit, UIText.CommitAction_amendNotPossible); return; } } String author = null; String committer = null; if (repository != null) { final RepositoryConfig config = repository.getConfig(); author = config.getAuthorName(); final String authorEmail = config.getAuthorEmail(); author = author + " <" + authorEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$ committer = config.getCommitterName(); final String committerEmail = config.getCommitterEmail(); committer = committer + " <" + committerEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$ } CommitDialog commitDialog = new CommitDialog(getTargetPart().getSite().getShell()); commitDialog.setAmending(amending); commitDialog.setAmendAllowed(amendAllowed); commitDialog.setFileList(files); commitDialog.setPreselectedFiles(getSelectedFiles()); commitDialog.setAuthor(author); commitDialog.setCommitter(committer); if (previousCommit != null) { commitDialog.setPreviousCommitMessage(previousCommit.getMessage()); PersonIdent previousAuthor = previousCommit.getAuthor(); commitDialog .setPreviousAuthor(previousAuthor.getName() + " <" + previousAuthor.getEmailAddress() + ">"); //$NON-NLS-1$ //$NON-NLS-2$ } if (commitDialog.open() != IDialogConstants.OK_ID) return; final CommitOperation commitOperation = new CommitOperation(commitDialog.getSelectedFiles(), notIndexed, notTracked, commitDialog.getAuthor(), commitDialog.getCommitter(), commitDialog.getCommitMessage()); if (commitDialog.isAmending()) { commitOperation.setAmending(true); commitOperation.setPreviousCommit(previousCommit); commitOperation.setRepos(repos); } String jobname = UIText.CommitAction_CommittingChanges; Job job = new Job(jobname) { @Override protected IStatus run(IProgressMonitor monitor) { try { commitOperation.execute(monitor); for (IProject proj : getProjectsForSelectedResources()) { RepositoryMapping.getMapping(proj).fireRepositoryChanged(); } } catch (CoreException e) { return Activator.createErrorStatus(UIText.CommitAction_CommittingFailed, e); } finally { GitLightweightDecorator.refresh(); } return Status.OK_STATUS; } }; job.setUser(true); job.schedule(); }
From source file:org.eclipse.egit.ui.internal.actions.CommitActionHandler.java
License:Open Source License
public Object execute(final ExecutionEvent event) throws ExecutionException { // let's see if there is any dirty editor around and // ask the user if they want to save or abort if (!PlatformUI.getWorkbench().saveAllEditors(true)) { return null; }//from www . j a v a 2 s. c o m resetState(); final IProject[] projects = getProjectsInRepositoryOfSelectedResources(event); try { PlatformUI.getWorkbench().getProgressService().busyCursorWhile(new IRunnableWithProgress() { public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { try { buildIndexHeadDiffList(projects, monitor); } catch (IOException e) { throw new InvocationTargetException(e); } } }); } catch (InvocationTargetException e) { Activator.handleError(UIText.CommitAction_errorComputingDiffs, e.getCause(), true); return null; } catch (InterruptedException e) { return null; } Repository[] repos = getRepositoriesFor(getProjectsForSelectedResources(event)); Repository repository = null; Repository mergeRepository = null; amendAllowed = repos.length == 1; boolean isMergedResolved = false; for (Repository repo : repos) { repository = repo; RepositoryState state = repo.getRepositoryState(); if (!state.canCommit()) { MessageDialog.openError(getShell(event), UIText.CommitAction_cannotCommit, NLS.bind(UIText.CommitAction_repositoryState, state.getDescription())); return null; } else if (state.equals(RepositoryState.MERGING_RESOLVED)) { isMergedResolved = true; mergeRepository = repo; } } loadPreviousCommit(event); if (files.isEmpty()) { if (amendAllowed && previousCommit != null) { boolean result = MessageDialog.openQuestion(getShell(event), UIText.CommitAction_noFilesToCommit, UIText.CommitAction_amendCommit); if (!result) return null; amending = true; } else { MessageDialog.openWarning(getShell(event), UIText.CommitAction_noFilesToCommit, UIText.CommitAction_amendNotPossible); return null; } } String author = null; String committer = null; if (repository != null) { final UserConfig config = repository.getConfig().get(UserConfig.KEY); author = config.getAuthorName(); final String authorEmail = config.getAuthorEmail(); author = author + " <" + authorEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$ committer = config.getCommitterName(); final String committerEmail = config.getCommitterEmail(); committer = committer + " <" + committerEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$ } CommitDialog commitDialog = new CommitDialog(getShell(event)); commitDialog.setAmending(amending); commitDialog.setAmendAllowed(amendAllowed); commitDialog.setFileList(files, indexDiffs); commitDialog.setPreselectedFiles(getSelectedFiles(event)); commitDialog.setAuthor(author); commitDialog.setCommitter(committer); commitDialog.setAllowToChangeSelection(!isMergedResolved); if (previousCommit != null) { commitDialog.setPreviousCommitMessage(previousCommit.getFullMessage()); PersonIdent previousAuthor = previousCommit.getAuthorIdent(); commitDialog .setPreviousAuthor(previousAuthor.getName() + " <" + previousAuthor.getEmailAddress() + ">"); //$NON-NLS-1$ //$NON-NLS-2$ } if (isMergedResolved) { commitDialog.setCommitMessage(getMergeResolveMessage(mergeRepository, event)); } if (commitDialog.open() != IDialogConstants.OK_ID) return null; final CommitOperation commitOperation = new CommitOperation(commitDialog.getSelectedFiles(), notIndexed, notTracked, commitDialog.getAuthor(), commitDialog.getCommitter(), commitDialog.getCommitMessage()); if (commitDialog.isAmending()) { commitOperation.setAmending(true); commitOperation.setPreviousCommit(previousCommit); commitOperation.setRepos(repos); } commitOperation.setComputeChangeId(commitDialog.getCreateChangeId()); commitOperation.setCommitAll(isMergedResolved); if (isMergedResolved) commitOperation.setRepos(repos); String jobname = UIText.CommitAction_CommittingChanges; Job job = new Job(jobname) { @Override protected IStatus run(IProgressMonitor monitor) { try { commitOperation.execute(monitor); for (IProject proj : getProjectsForSelectedResources(event)) { RepositoryMapping.getMapping(proj).fireRepositoryChanged(); } } catch (CoreException e) { return Activator.createErrorStatus(UIText.CommitAction_CommittingFailed, e); } catch (ExecutionException e) { return Activator.createErrorStatus(UIText.CommitAction_CommittingFailed, e); } finally { GitLightweightDecorator.refresh(); } return Status.OK_STATUS; } @Override public boolean belongsTo(Object family) { if (family.equals(JobFamilies.COMMIT)) return true; return super.belongsTo(family); } }; job.setUser(true); job.schedule(); return null; }
From source file:org.eclipse.egit.ui.internal.commit.CommitHelper.java
License:Open Source License
private void calculateCommitInfo() { Repository mergeRepository = null;//from w w w . ja v a2s. co m isMergedResolved = false; isCherryPickResolved = false; RepositoryState state = repository.getRepositoryState(); canCommit = state.canCommit(); if (!canCommit) { cannotCommitMessage = NLS.bind(UIText.CommitAction_repositoryState, state.getDescription()); return; } if (state.equals(RepositoryState.MERGING_RESOLVED)) { isMergedResolved = true; mergeRepository = repository; } else if (state.equals(RepositoryState.CHERRY_PICKING_RESOLVED)) { isCherryPickResolved = true; mergeRepository = repository; } previousCommit = getHeadCommit(repository); final UserConfig config = repository.getConfig().get(UserConfig.KEY); author = config.getAuthorName(); final String authorEmail = config.getAuthorEmail(); author = author + " <" + authorEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$ committer = config.getCommitterName(); final String committerEmail = config.getCommitterEmail(); committer = committer + " <" + committerEmail + ">"; //$NON-NLS-1$ //$NON-NLS-2$ if (isMergedResolved || isCherryPickResolved) { commitMessage = getMergeResolveMessage(mergeRepository); } if (isCherryPickResolved) { author = getCherryPickOriginalAuthor(mergeRepository); } }
From source file:org.flowerplatform.web.git.operation.CommitOperation.java
License:Open Source License
public CommitPageDto getPageDto() { try {//from w w w . jav a 2 s . co m repository = getRepository(selection); if (repository == null) { channel.appendOrSendCommand( new DisplaySimpleMessageClientCommand(CommonPlugin.getInstance().getMessage("error"), GitPlugin.getInstance().getMessage("git.commit.errorDifferentRepositories"), DisplaySimpleMessageClientCommand.ICON_ERROR)); return null; } RepositoryState state = repository.getRepositoryState(); if (!state.canCommit()) { channel.appendOrSendCommand( new DisplaySimpleMessageClientCommand(CommonPlugin.getInstance().getMessage("error"), GitPlugin.getInstance().getMessage("git.commit.repositoryState", new Object[] { state.getDescription() }), DisplaySimpleMessageClientCommand.ICON_ERROR)); return null; } boolean isMergedResolved = false; boolean isCherryPickResolved = false; if (state.equals(RepositoryState.MERGING_RESOLVED)) { isMergedResolved = true; } else if (state.equals(RepositoryState.CHERRY_PICKING_RESOLVED)) { isCherryPickResolved = true; } User user = (User) CommunicationPlugin.tlCurrentPrincipal.get().getUser(); String committer = user.getName() + " <" + user.getEmail() + ">"; String author = user.getName() + " <" + user.getEmail() + ">"; if (isCherryPickResolved) { author = getCherryPickOriginalAuthor(repository); } if (author == null) { author = user.getName() + " <" + user.getEmail() + ">"; } String message = null; if (isMergedResolved || isCherryPickResolved) { message = getMergeResolveMessage(repository); } org.eclipse.jgit.api.Status repoStatus = new Git(repository).status().call(); Set<String> files = new HashSet<String>(); includeList(repoStatus.getAdded(), files); includeList(repoStatus.getChanged(), files); includeList(repoStatus.getRemoved(), files); includeList(repoStatus.getMissing(), files); includeList(repoStatus.getModified(), files); includeList(repoStatus.getUntracked(), files); List<CommitResourceDto> commitResources = new ArrayList<CommitResourceDto>(); for (String path : files) { CommitResourceDto commitDto = new CommitResourceDto(); commitDto.setLabel(path); commitDto.setPath(path); commitDto.setImage("images/file.gif"); if (repoStatus.getUntracked().contains(path)) { commitDto.setState(CommitResourceDto.UNTRACKED); } commitResources.add(commitDto); } CommitPageDto dto = new CommitPageDto(); dto.setCommitResources(commitResources); dto.setAuthor(author); dto.setCommitter(committer); dto.setMessage(message); dto.setRepository(repository.getDirectory().getAbsolutePath()); return dto; } catch (Exception e) { logger.debug(GitPlugin.getInstance().getMessage("git.commit.error"), e); channel.appendOrSendCommand( new DisplaySimpleMessageClientCommand(CommonPlugin.getInstance().getMessage("error"), GitPlugin.getInstance().getMessage("git.commit.error"), e.getMessage(), DisplaySimpleMessageClientCommand.ICON_ERROR)); return null; } }