2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package git4idea.history;
18 import com.intellij.openapi.actionSystem.AnAction;
19 import com.intellij.openapi.diagnostic.Logger;
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.util.text.StringUtil;
22 import com.intellij.openapi.vcs.FilePath;
23 import com.intellij.openapi.vcs.FilePathImpl;
24 import com.intellij.openapi.vcs.VcsException;
25 import com.intellij.openapi.vcs.changes.ContentRevision;
26 import com.intellij.openapi.vcs.history.*;
27 import com.intellij.openapi.vfs.VirtualFile;
28 import com.intellij.util.Consumer;
29 import com.intellij.util.Processor;
30 import com.intellij.util.ui.ColumnInfo;
31 import git4idea.GitFileRevision;
32 import git4idea.GitRevisionNumber;
33 import git4idea.GitUtil;
34 import git4idea.GitVcs;
35 import git4idea.actions.GitShowAllSubmittedFilesAction;
36 import git4idea.changes.GitChangeUtils;
37 import git4idea.config.GitExecutableValidator;
38 import git4idea.history.browser.SHAHash;
39 import git4idea.repo.GitRepository;
40 import git4idea.repo.GitRepositoryManager;
41 import org.jetbrains.annotations.NotNull;
42 import org.jetbrains.annotations.Nullable;
45 import java.util.Collections;
46 import java.util.List;
49 * Git history provider implementation
51 public class GitHistoryProvider implements VcsHistoryProvider, VcsCacheableHistorySessionFactory<Boolean, VcsAbstractHistorySession>,
52 VcsBaseRevisionAdviser {
53 private static final Logger log = Logger.getInstance(GitHistoryProvider.class.getName());
55 @NotNull private final Project myProject;
57 public GitHistoryProvider(@NotNull Project project) {
61 public VcsDependentHistoryComponents getUICustomization(final VcsHistorySession session, JComponent forShortcutRegistration) {
62 return VcsDependentHistoryComponents.createOnlyColumns(new ColumnInfo[0]);
65 public AnAction[] getAdditionalActions(Runnable refresher) {
66 return new AnAction[]{new GitShowAllSubmittedFilesAction(), new GitCopyHistoryRevisionNumberAction()};
69 public boolean isDateOmittable() {
74 public String getHelpId() {
79 public FilePath getUsedFilePath(VcsAbstractHistorySession session) {
84 public Boolean getAddinionallyCachedData(VcsAbstractHistorySession session) {
89 public VcsAbstractHistorySession createFromCachedData(Boolean aBoolean,
90 @NotNull List<VcsFileRevision> revisions,
91 @NotNull FilePath filePath,
92 VcsRevisionNumber currentRevision) {
93 return createSession(filePath, revisions, currentRevision);
97 public VcsHistorySession createSessionFor(final FilePath filePath) throws VcsException {
98 List<VcsFileRevision> revisions = null;
100 revisions = GitHistoryUtils.history(myProject, filePath);
101 } catch (VcsException e) {
102 GitVcs.getInstance(myProject).getExecutableValidator().showNotificationOrThrow(e);
104 return createSession(filePath, revisions, null);
107 private VcsAbstractHistorySession createSession(final FilePath filePath, final List<VcsFileRevision> revisions,
108 @Nullable final VcsRevisionNumber number) {
109 return new VcsAbstractHistorySession(revisions, number) {
111 protected VcsRevisionNumber calcCurrentRevisionNumber() {
113 return GitHistoryUtils.getCurrentRevision(myProject, filePath, "HEAD");
115 catch (VcsException e) {
116 // likely the file is not under VCS anymore.
117 if (log.isDebugEnabled()) {
118 log.debug("Unable to retrieve the current revision number", e);
124 public HistoryAsTreeProvider getHistoryAsTreeProvider() {
129 public VcsHistorySession copy() {
130 return createSession(filePath, getRevisionList(), getCurrentRevisionNumber());
136 public boolean getBaseVersionContent(FilePath filePath,
137 Processor<CharSequence> processor,
138 final String beforeVersionId,
139 List<String> warnings)
140 throws VcsException {
141 if (StringUtil.isEmptyOrSpaces(beforeVersionId) || filePath.getVirtualFile() == null) return false;
142 // apply if base revision id matches revision
143 final VirtualFile root = GitUtil.getGitRoot(filePath);
144 if (root == null) return false;
146 final SHAHash shaHash = GitChangeUtils.commitExists(myProject, root, beforeVersionId, null, "HEAD");
147 if (shaHash == null) {
148 throw new VcsException("Can not apply patch to " + filePath.getPath() + ".\nCan not find revision '" + beforeVersionId + "'.");
151 final ContentRevision content = GitVcs.getInstance(myProject).getDiffProvider()
152 .createFileContent(new GitRevisionNumber(shaHash.getValue()), filePath.getVirtualFile());
153 if (content == null) {
154 throw new VcsException("Can not load content of '" + filePath.getPath() + "' for revision '" + shaHash.getValue() + "'");
156 return ! processor.process(content.getContent());
159 public void reportAppendableHistory(final FilePath path, final VcsAppendableHistorySessionPartner partner) throws VcsException {
160 final VcsAbstractHistorySession emptySession = createSession(path, Collections.<VcsFileRevision>emptyList(), null);
161 partner.reportCreatedEmptySession(emptySession);
162 final GitExecutableValidator validator = GitVcs.getInstance(myProject).getExecutableValidator();
164 GitHistoryUtils.history(myProject, refreshPath(path), null, new Consumer<GitFileRevision>() {
165 public void consume(GitFileRevision gitFileRevision) {
166 partner.acceptRevision(gitFileRevision);
168 }, new Consumer<VcsException>() {
169 public void consume(VcsException e) {
170 if (validator.checkExecutableAndNotifyIfNeeded()) {
171 partner.reportException(e);
175 } catch (VcsException e) {
176 validator.showNotificationOrThrow(e);
181 * Refreshes the IO File inside this FilePath to let it survive moves.
184 private static FilePath refreshPath(@NotNull FilePath path) {
185 VirtualFile virtualFile = path.getVirtualFile();
186 if (virtualFile == null) {
189 return new FilePathImpl(virtualFile);
192 public boolean supportsHistoryForDirectories() {
197 public DiffFromHistoryHandler getHistoryDiffHandler() {
198 return new GitDiffFromHistoryHandler(myProject);
202 public boolean canShowHistoryFor(@NotNull VirtualFile file) {
203 GitRepositoryManager manager = GitUtil.getRepositoryManager(myProject);
204 GitRepository repository = manager.getRepositoryForFile(file);
205 return repository != null && !repository.isFresh();