1 package git4idea
.update
;
3 import com
.intellij
.openapi
.project
.Project
;
4 import com
.intellij
.openapi
.ui
.DialogWrapper
;
5 import com
.intellij
.openapi
.vcs
.VcsException
;
6 import com
.intellij
.openapi
.vfs
.VirtualFile
;
7 import com
.intellij
.util
.ui
.UIUtil
;
8 import com
.intellij
.vcsUtil
.VcsUtil
;
9 import git4idea
.GitUtil
;
10 import git4idea
.commands
.GitHandler
;
11 import git4idea
.commands
.GitSimpleHandler
;
12 import git4idea
.commands
.StringScanner
;
13 import git4idea
.i18n
.GitBundle
;
14 import git4idea
.ui
.GitUIUtil
;
17 import java
.awt
.event
.ActionEvent
;
18 import java
.awt
.event
.ActionListener
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
21 import java
.util
.concurrent
.atomic
.AtomicBoolean
;
24 * The dialog that displays locally modified files during update process
26 public class GitUpdateLocallyModifiedDialog
extends DialogWrapper
{
30 private JButton myRescanButton
;
32 * The list of files to revert
34 private JList myFilesList
;
38 private JLabel myGitRoot
;
42 private JPanel myRootPanel
;
46 private VirtualFile myRoot
;
48 * The collection with locally modified files
50 private List
<String
> myLocallyModifiedFiles
;
54 * @param project the current project
55 * @param root the vcs root
56 * @param locallyModifiedFiles the collection of locally modified files to use
58 protected GitUpdateLocallyModifiedDialog(final Project project
, final VirtualFile root
, List
<String
> locallyModifiedFiles
) {
61 myLocallyModifiedFiles
= locallyModifiedFiles
;
62 setTitle(GitBundle
.getString("update.locally.modified.title"));
63 myGitRoot
.setText(myRoot
.getPresentableUrl());
64 myFilesList
.setModel(new DefaultListModel());
65 setOKButtonText(GitBundle
.getString("update.locally.modified.revert"));
67 myRescanButton
.addActionListener(new ActionListener() {
68 public void actionPerformed(ActionEvent e
) {
69 myLocallyModifiedFiles
.clear();
71 scanFiles(project
, root
, myLocallyModifiedFiles
);
73 catch (VcsException ex
) {
74 GitUIUtil
.showOperationError(project
, ex
, "Checking for locally modified files");
82 * Refresh list model according to the current content of the collection
84 private void syncListModel() {
85 DefaultListModel listModel
= (DefaultListModel
)myFilesList
.getModel();
86 listModel
.removeAllElements();
87 for(String p
: myLocallyModifiedFiles
) {
88 listModel
.addElement(p
);
96 protected JComponent
createCenterPanel() {
104 protected String
getDimensionServiceKey() {
105 return getClass().getName();
109 * Scan working tree and detect locally modified files
111 * @param project the project to scan
112 * @param root the root to scan
113 * @param files the collection with files
114 * @throws VcsException if there problem with running git or working tree is dirty in unsupported way
116 private static void scanFiles(Project project
, VirtualFile root
, List
<String
> files
) throws VcsException
{
117 String rootPath
= root
.getPath();
118 GitSimpleHandler h
= new GitSimpleHandler(project
, root
, GitHandler
.DIFF
);
119 h
.addParameters("--name-status");
121 h
.setStdoutSuppressed(true);
122 StringScanner s
= new StringScanner(h
.run());
123 while(s
.hasMoreData()) {
128 if(s
.tryConsume("M\t")) {
129 String path
= rootPath
+ "/" + GitUtil
.unescapePath(s
.line());
132 throw new VcsException("Working tree is dirty in unsupported way: "+s
.line());
139 * Show the dialog if needed
141 * @param project the project
142 * @param root the vcs root
143 * @return true if showing is not needed or operation completed successfully
145 public static boolean showIfNeeded(final Project project
, final VirtualFile root
) {
146 final ArrayList
<String
> files
= new ArrayList
<String
>();
148 scanFiles(project
, root
, files
);
149 final AtomicBoolean rc
= new AtomicBoolean(true);
150 if(!files
.isEmpty()) {
151 UIUtil
.invokeAndWaitIfNeeded(new Runnable() {
153 GitUpdateLocallyModifiedDialog d
= new GitUpdateLocallyModifiedDialog(project
, root
, files
);
159 if(!files
.isEmpty()) {
160 revertFiles(project
, root
, files
);
166 catch (final VcsException e
) {
167 UIUtil
.invokeAndWaitIfNeeded(new Runnable() {
169 GitUIUtil
.showOperationError(project
, e
, "Checking for locally modified files");
177 * Revert files from the list
179 * @param project the project
180 * @param root the vcs root
181 * @param files the files to revert
183 private static void revertFiles(Project project
, VirtualFile root
, ArrayList
<String
> files
) throws VcsException
{
184 GitSimpleHandler h
= new GitSimpleHandler(project
, root
, GitHandler
.CHECKOUT
);
187 for(String p
: files
) {
188 h
.addRelativePaths(VcsUtil
.getFilePath(p
));