Adding Git source, NetBeans project files, GPL v2 LICENSE, ant build file.
[nbgit.git] / src / org / netbeans / modules / git / ui / status / StatusAction.java
blobe0fb147aceebcdf1b85037b86f7554c69af06a59
1 package org.netbeans.modules.git.ui.status;
4 import java.awt.event.ActionEvent;
5 import java.io.File;
6 import java.util.Calendar;
7 import java.util.Collection;
8 import java.util.Map;
9 import java.util.logging.Level;
10 import javax.swing.Action;
11 import org.netbeans.modules.git.FileInformation;
12 import org.netbeans.modules.git.FileStatusCache;
13 import org.netbeans.modules.git.Git;
14 import org.netbeans.modules.git.GitException;
15 import org.netbeans.modules.git.GitProgressSupport;
16 import org.netbeans.modules.git.ui.actions.ContextAction;
17 import org.netbeans.modules.git.util.GitCommand;
18 import org.netbeans.modules.git.util.GitUtils;
19 import org.netbeans.modules.versioning.spi.VCSContext;
20 import org.netbeans.modules.versioning.util.Utils;
22 /**
23 * Status action for Git:
24 * git status - show changed files in the working directory
26 * @author John Rice
28 public class StatusAction extends ContextAction {
30 private final VCSContext context;
32 public StatusAction(String name, VCSContext context) {
33 this.context = context;
34 putValue(Action.NAME, name);
37 public void performAction(ActionEvent ev) {
38 File [] files = context.getRootFiles().toArray(new File[context.getRootFiles().size()]);
39 if (files == null || files.length == 0) return;
41 final GitVersioningTopComponent stc = GitVersioningTopComponent.findInstance();
42 stc.setContentTitle(Utils.getContextDisplayName(context));
43 stc.setContext(context);
44 stc.open();
45 stc.requestActive();
46 stc.performRefreshAction();
49 @Override
50 public boolean isEnabled() {
51 return GitUtils.getRootFile(context) != null;
54 /**
55 * Connects to repository and gets recent status.
57 public static void executeStatus(final VCSContext context, GitProgressSupport support) {
59 if (context == null || context.getRootFiles().size() == 0) {
60 return;
62 File repository = GitUtils.getRootFile(context);
63 if (repository == null) {
64 return;
67 try {
68 FileStatusCache cache = Git.getInstance().getFileStatusCache();
69 Calendar start = Calendar.getInstance();
70 cache.refreshCached(context);
71 Calendar end = Calendar.getInstance();
72 Git.LOG.log(Level.FINE, "executeStatus: refreshCached took {0} millisecs", end.getTimeInMillis() - start.getTimeInMillis()); // NOI18N
74 for (File root : context.getRootFiles()) {
75 if(support.isCanceled()) {
76 return;
78 if (root.isDirectory()) {
79 Map<File, FileInformation> interestingFiles;
80 interestingFiles = GitCommand.getInterestingStatus(repository, root);
81 if (!interestingFiles.isEmpty()){
82 Collection<File> files = interestingFiles.keySet();
84 Map<File, Map<File,FileInformation>> interestingDirs =
85 GitUtils.getInterestingDirs(interestingFiles, files);
87 start = Calendar.getInstance();
88 for (File file : files) {
89 if(support.isCanceled()) {
90 return;
92 FileInformation fi = interestingFiles.get(file);
94 cache.refreshFileStatus(file, fi,
95 interestingDirs.get(file.isDirectory()? file: file.getParentFile()));
97 end = Calendar.getInstance();
98 Git.LOG.log(Level.FINE, "executeStatus: process interesting files took {0} millisecs", end.getTimeInMillis() - start.getTimeInMillis()); // NOI18N
100 } else {
101 cache.refresh(root, FileStatusCache.REPOSITORY_STATUS_UNKNOWN);
104 } catch (GitException ex) {
105 support.annotate(ex);