StagingView wrongly sorted by state initially
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / UIRepositoryUtils.java
bloba813c4952323cf844bfc536c0b090b90bce7b52a
1 /*******************************************************************************
2 * Copyright (c) 2014 Maik Schreiber
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
11 * Contributors:
12 * Maik Schreiber - initial implementation
13 *******************************************************************************/
14 package org.eclipse.egit.ui.internal;
16 import java.text.MessageFormat;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.List;
21 import org.eclipse.egit.ui.Activator;
22 import org.eclipse.egit.ui.internal.branch.CleanupUncomittedChangesDialog;
23 import org.eclipse.jgit.api.Git;
24 import org.eclipse.jgit.api.Status;
25 import org.eclipse.jgit.api.errors.GitAPIException;
26 import org.eclipse.jgit.lib.Repository;
27 import org.eclipse.swt.widgets.Shell;
29 /** Utility class for handling repositories in the UI. */
30 public final class UIRepositoryUtils {
31 private UIRepositoryUtils() {
32 // nothing to do
35 /**
36 * Checks the repository to see if there are uncommitted changes, and
37 * prompts the user to clean them up.
39 * @param repo
40 * the repository
41 * @param shell
42 * the parent shell for opening the dialog
43 * @return true if the git status was clean or it was dirty and the user
44 * cleaned up the uncommitted changes and the previous action may
45 * continue
46 * @throws GitAPIException
47 * if there was an error checking the repository
49 public static boolean handleUncommittedFiles(Repository repo, Shell shell)
50 throws GitAPIException {
51 String repoName = Activator.getDefault().getRepositoryUtil()
52 .getRepositoryName(repo);
53 return handleUncommittedFiles(repo, shell,
54 MessageFormat.format(
55 UIText.AbstractRebaseCommandHandler_cleanupDialog_title,
56 repoName));
59 /**
60 * Checks the repository to see if there are uncommitted changes, and
61 * prompts the user to clean them up.
63 * @param repo
64 * the repository
65 * @param shell
66 * the parent shell for opening the dialog
67 * @param dialogTitle
68 * the dialog title
69 * @return true if the git status was clean or it was dirty and the user
70 * cleaned up the uncommitted changes and the previous action may
71 * continue
72 * @throws GitAPIException
73 * if there was an error checking the repository
75 public static boolean handleUncommittedFiles(Repository repo, Shell shell,
76 String dialogTitle) throws GitAPIException {
77 Status status = null;
78 try (Git git = new Git(repo)) {
79 status = git.status().call();
81 if (status != null && status.hasUncommittedChanges()) {
82 List<String> files = new ArrayList<>(status.getModified());
83 return showCleanupDialog(repo, files, dialogTitle, shell);
85 return true;
88 /**
89 * Displays a dialog giving the user the opportunity to commit, stash, or
90 * reset uncommitted changes before doing a repository operation, or to
91 * abandon the operation.
93 * @param repo
94 * {@link Repository} we're working on
95 * @param files
96 * with uncommitted changes
97 * @param title
98 * for the dialog
99 * @param shell
100 * to parent the dialog off
101 * @return whether to continue the operation
103 public static boolean showCleanupDialog(Repository repo, List<String> files,
104 String title, Shell shell) {
105 Collections.sort(files);
106 CleanupUncomittedChangesDialog cleanupUncomittedChangesDialog = new CleanupUncomittedChangesDialog(
107 shell, title,
108 UIText.AbstractRebaseCommandHandler_cleanupDialog_text, repo,
109 files);
110 cleanupUncomittedChangesDialog.open();
111 return cleanupUncomittedChangesDialog.shouldContinue();