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
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() {
36 * Checks the repository to see if there are uncommitted changes, and
37 * prompts the user to clean them up.
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
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
,
55 UIText
.AbstractRebaseCommandHandler_cleanupDialog_title
,
60 * Checks the repository to see if there are uncommitted changes, and
61 * prompts the user to clean them up.
66 * the parent shell for opening the dialog
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
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
{
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
);
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.
94 * {@link Repository} we're working on
96 * with uncommitted changes
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(
108 UIText
.AbstractRebaseCommandHandler_cleanupDialog_text
, repo
,
110 cleanupUncomittedChangesDialog
.open();
111 return cleanupUncomittedChangesDialog
.shouldContinue();