git4idea: Minor improvements to collect changes
[fedora-idea.git] / plugins / git4idea / src / git4idea / changes / GitCommittedChangeListProvider.java
blobe3d74f9dbe0053b0427978f394fec9fffd685602
1 package git4idea.changes;
3 import com.intellij.openapi.diagnostic.Logger;
4 import com.intellij.openapi.project.Project;
5 import com.intellij.openapi.util.text.StringUtil;
6 import com.intellij.openapi.vcs.*;
7 import com.intellij.openapi.vcs.changes.committed.DecoratorManager;
8 import com.intellij.openapi.vcs.changes.committed.VcsCommittedListsZipper;
9 import com.intellij.openapi.vcs.changes.committed.VcsCommittedViewAuxiliary;
10 import com.intellij.openapi.vcs.history.VcsRevisionNumber;
11 import com.intellij.openapi.vcs.versionBrowser.ChangeBrowserSettings;
12 import com.intellij.openapi.vcs.versionBrowser.ChangesBrowserSettingsEditor;
13 import com.intellij.openapi.vcs.versionBrowser.CommittedChangeList;
14 import com.intellij.openapi.vfs.LocalFileSystem;
15 import com.intellij.openapi.vfs.VirtualFile;
16 import git4idea.GitBranch;
17 import git4idea.GitRemote;
18 import git4idea.GitUtil;
19 import git4idea.commands.GitHandler;
20 import git4idea.commands.GitSimpleHandler;
21 import git4idea.commands.StringScanner;
22 import org.jetbrains.annotations.Nls;
23 import org.jetbrains.annotations.Nullable;
25 import java.io.DataInput;
26 import java.io.DataOutput;
27 import java.io.File;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Date;
32 import java.util.List;
34 /**
35 * The provider for committed change lists
37 public class GitCommittedChangeListProvider implements CachingCommittedChangesProvider<CommittedChangeList, ChangeBrowserSettings> {
38 /**
39 * the logger
41 private static final Logger LOG = Logger.getInstance(GitCommittedChangeListProvider.class.getName());
44 /**
45 * The project for the service
47 private Project myProject;
49 /**
50 * The constructor
52 * @param project the project instance for this provider
54 public GitCommittedChangeListProvider(Project project) {
55 myProject = project;
58 /**
59 * {@inheritDoc}
61 public ChangeBrowserSettings createDefaultSettings() {
62 return new ChangeBrowserSettings();
65 /**
66 * {@inheritDoc}
68 public ChangesBrowserSettingsEditor<ChangeBrowserSettings> createFilterUI(boolean showDateFilter) {
69 return new GitVersionFilterComponent(showDateFilter);
72 /**
73 * {@inheritDoc}
75 public RepositoryLocation getLocationFor(FilePath root) {
76 VirtualFile gitRoot = GitUtil.getGitRootOrNull(root);
77 if (gitRoot == null) {
78 return null;
80 try {
81 GitBranch c = GitBranch.current(myProject, gitRoot);
82 if (c == null) {
83 return null;
85 String remote = c.getTrackedRemoteName(myProject, gitRoot);
86 if (StringUtil.isEmpty(remote)) {
87 return null;
89 File rootFile = new File(gitRoot.getPath());
90 if (".".equals(remote)) {
91 return new GitRepositoryLocation(gitRoot.getUrl(), rootFile);
93 else {
94 GitRemote r = GitRemote.find(myProject, gitRoot, remote);
95 return r == null ? null : new GitRepositoryLocation(r.fetchUrl(), rootFile);
98 catch (VcsException e) {
99 if (LOG.isDebugEnabled()) {
100 LOG.debug("Exception for determining repository location", e);
102 return null;
107 * {@inheritDoc}
109 public RepositoryLocation getLocationFor(FilePath root, String repositoryPath) {
110 return getLocationFor(root);
114 * {@inheritDoc}
116 public VcsCommittedListsZipper getZipper() {
117 return null;
121 * {@inheritDoc}
123 public List<CommittedChangeList> getCommittedChanges(ChangeBrowserSettings settings, RepositoryLocation location, int maxCount)
124 throws VcsException {
125 ArrayList<CommittedChangeList> rc = new ArrayList<CommittedChangeList>();
126 GitRepositoryLocation l = (GitRepositoryLocation)location;
127 Long beforeRev = settings.getChangeBeforeFilter();
128 Long afterRev = settings.getChangeBeforeFilter();
129 Date beforeDate = settings.getDateBeforeFilter();
130 Date afterDate = settings.getDateBeforeFilter();
131 String author = settings.getUserFilter();
132 VirtualFile root = LocalFileSystem.getInstance().findFileByIoFile(l.getRoot());
133 if (root == null) {
134 throw new VcsException("The repository does not exists anymore: " + l.getRoot());
136 GitSimpleHandler h = new GitSimpleHandler(myProject, root, GitHandler.LOG);
137 h.addParameters("--pretty=format:%x0C%n" + GitChangeUtils.COMMITTED_CHANGELIST_FORMAT);
138 if (!StringUtil.isEmpty(author)) {
139 h.addParameters("--author=" + author);
141 if (beforeDate != null) {
142 h.addParameters("--before=" + GitUtil.gitTime(beforeDate));
144 if (afterDate != null) {
145 h.addParameters("--after=" + GitUtil.gitTime(afterDate));
147 if (maxCount != getUnlimitedCountValue()) {
148 h.addParameters("-n" + maxCount);
150 if (beforeRev != null && afterRev != null) {
151 h.addParameters(GitUtil.formatLongRev(afterRev) + ".." + GitUtil.formatLongRev(beforeRev));
153 else if (beforeRev != null) {
154 h.addParameters(GitUtil.formatLongRev(beforeRev));
156 else if (afterRev != null) {
157 h.addParameters(GitUtil.formatLongRev(afterRev) + "..");
159 String output = h.run();
160 StringScanner s = new StringScanner(output);
161 while (s.hasMoreData() && s.startsWith('\u000C')) {
162 s.nextLine();
163 rc.add(GitChangeUtils.parseChangeList(myProject, root, s));
165 if (s.hasMoreData()) {
166 throw new IllegalStateException("More input is avaialble: " + s.line());
168 return rc;
173 * {@inheritDoc}
175 public ChangeListColumn[] getColumns() {
176 return new ChangeListColumn[]{ChangeListColumn.NUMBER, ChangeListColumn.DATE, ChangeListColumn.DESCRIPTION, ChangeListColumn.NAME};
180 * {@inheritDoc}
182 public VcsCommittedViewAuxiliary createActions(DecoratorManager manager, RepositoryLocation location) {
183 return null;
187 * {@inheritDoc}
189 public int getUnlimitedCountValue() {
190 return -1;
193 public int getFormatVersion() {
194 return 0; //To change body of implemented methods use File | Settings | File Templates.
197 public void writeChangeList(DataOutput stream, CommittedChangeList list) throws IOException {
198 //To change body of implemented methods use File | Settings | File Templates.
201 public CommittedChangeList readChangeList(RepositoryLocation location, DataInput stream) throws IOException {
202 return null; //To change body of implemented methods use File | Settings | File Templates.
205 public boolean isMaxCountSupported() {
206 return false; //To change body of implemented methods use File | Settings | File Templates.
209 public Collection<FilePath> getIncomingFiles(RepositoryLocation location) throws VcsException {
210 return null; //To change body of implemented methods use File | Settings | File Templates.
213 public boolean refreshCacheByNumber() {
214 return false; //To change body of implemented methods use File | Settings | File Templates.
217 @Nls
218 public String getChangelistTitle() {
219 return null; //To change body of implemented methods use File | Settings | File Templates.
222 public boolean isChangeLocallyAvailable(FilePath filePath,
223 @Nullable VcsRevisionNumber localRevision,
224 VcsRevisionNumber changeRevision,
225 CommittedChangeList changeList) {
226 return false; //To change body of implemented methods use File | Settings | File Templates.
229 public boolean refreshIncomingWithCommitted() {
230 return false; //To change body of implemented methods use File | Settings | File Templates.