IDEA-68731 Batch process hg revert and hg resolve. Get rid of HgFile.
[idea/community.git] / plugins / hg4idea / src / org / zmlx / hg4idea / command / HgResolveCommand.java
1 // Copyright 2008-2010 Victor Iacoban
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software distributed under
10 // the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11 // either express or implied. See the License for the specific language governing permissions and
12 // limitations under the License.
13 package org.zmlx.hg4idea.command;
14
15 import com.intellij.openapi.project.Project;
16 import com.intellij.openapi.vcs.FilePath;
17 import com.intellij.openapi.vfs.VirtualFile;
18 import com.intellij.util.Consumer;
19 import com.intellij.vcsUtil.VcsFileUtil;
20 import org.apache.commons.lang.StringUtils;
21 import org.jetbrains.annotations.Nullable;
22 import org.zmlx.hg4idea.HgFile;
23 import org.zmlx.hg4idea.execution.HgCommandExecutor;
24 import org.zmlx.hg4idea.execution.HgCommandResult;
25 import org.zmlx.hg4idea.execution.HgCommandResultHandler;
26
27 import java.io.File;
28 import java.util.*;
29
30 public class HgResolveCommand {
31
32   private static final int ITEM_COUNT = 3;
33
34   private final Project myProject;
35
36   public HgResolveCommand(Project project) {
37     myProject = project;
38   }
39
40
41   public Map<HgFile, HgResolveStatusEnum> getListSynchronously(VirtualFile repo) {
42     if (repo == null) {
43       return Collections.emptyMap();
44     }
45     final HgCommandExecutor executor = new HgCommandExecutor(myProject);
46     executor.setSilent(true);
47     final HgCommandResult result = executor.executeInCurrentThread(repo, "resolve", Arrays.asList("--list"));
48     if (result == null) {
49       return Collections.emptyMap();
50     }
51     return handleResult(repo, result);
52   }
53
54   public void list(final VirtualFile repo, final Consumer<Map<HgFile, HgResolveStatusEnum>> resultHandler) {
55     if (repo == null) {
56       resultHandler.consume(Collections.<HgFile, HgResolveStatusEnum>emptyMap());
57     }
58     final HgCommandExecutor executor = new HgCommandExecutor(myProject);
59     executor.setSilent(true);
60     executor.execute(repo, "resolve", Arrays.asList("--list"), new HgCommandResultHandler() {
61       @Override
62       public void process(@Nullable HgCommandResult result) {
63         if (result == null) {
64           resultHandler.consume(Collections.<HgFile, HgResolveStatusEnum>emptyMap());
65         }
66
67         final Map<HgFile, HgResolveStatusEnum> resolveStatus = handleResult(repo, result);
68         resultHandler.consume(resolveStatus);
69       }
70     });
71   }
72
73   private static Map<HgFile, HgResolveStatusEnum> handleResult(VirtualFile repo, HgCommandResult result) {
74     final Map<HgFile, HgResolveStatusEnum> resolveStatus = new HashMap<HgFile, HgResolveStatusEnum>();
75     for (String line : result.getOutputLines()) {
76       if (StringUtils.isBlank(line) || line.length() < ITEM_COUNT) {
77         continue;
78       }
79       final HgResolveStatusEnum status = HgResolveStatusEnum.valueOf(line.charAt(0));
80       if (status != null) {
81         File ioFile = new File(repo.getPath(), line.substring(2));
82         resolveStatus.put(new HgFile(repo, ioFile), status);
83       }
84     }
85     return resolveStatus;
86   }
87
88   public void markResolved(VirtualFile repo, VirtualFile path) {
89     new HgCommandExecutor(myProject).execute(repo, "resolve", Arrays.asList("--mark", path.getPath()), null);
90   }
91
92   public void markResolved(VirtualFile repo, Collection<FilePath> paths) {
93     for (List<String> chunk : VcsFileUtil.chunkPaths(repo, paths)) {
94       final List<String> args = new ArrayList<String>();
95       args.add("--mark");
96       args.addAll(chunk);
97       new HgCommandExecutor(myProject).execute(repo, "resolve", args, null);
98     }
99   }
100
101 }