git4idea: Added support for checking locally modified files
[fedora-idea.git] / plugins / git4idea / src / git4idea / GitContentRevision.java
blobbe0e77c32f04dcbbfc6dfb9fa0f3d5211f3d645a
1 package git4idea;
2 /*
3 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
4 * with the License. You may obtain a copy of the License at:
6 * http://www.apache.org/licenses/LICENSE-2.0
8 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
9 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for
10 * the specific language governing permissions and limitations under the License.
12 * Copyright 2007 Decentrix Inc
13 * Copyright 2007 Aspiro AS
14 * Copyright 2008 MQSoftware
15 * Copyright 2008 JetBrains s.r.o.
16 * Authors: gevession, Erlend Simonsen & Mark Scott
18 * This code was originally derived from the MKS & Mercurial IDEA VCS plugins
21 import com.intellij.openapi.project.Project;
22 import com.intellij.openapi.vcs.FilePath;
23 import com.intellij.openapi.vcs.VcsException;
24 import com.intellij.openapi.vcs.changes.ContentRevision;
25 import com.intellij.openapi.vcs.changes.CurrentContentRevision;
26 import com.intellij.openapi.vcs.history.VcsRevisionNumber;
27 import com.intellij.openapi.vfs.VirtualFile;
28 import com.intellij.vcsUtil.VcsUtil;
29 import git4idea.commands.GitHandler;
30 import git4idea.commands.GitSimpleHandler;
31 import org.jetbrains.annotations.NotNull;
32 import org.jetbrains.annotations.Nullable;
34 import java.nio.charset.Charset;
36 /**
37 * Git content revision
39 public class GitContentRevision implements ContentRevision {
40 /**
41 * the file path
43 @NotNull private final FilePath myFile;
44 /**
45 * the revision number
47 @NotNull private final GitRevisionNumber myRevision;
48 /**
49 * the context project
51 @NotNull private final Project myProject;
52 /**
53 * The charset for the file
55 @NotNull private final Charset myCharset;
57 public GitContentRevision(@NotNull FilePath file, @NotNull GitRevisionNumber revision, @NotNull Project project, Charset charset) {
58 myProject = project;
59 myFile = file;
60 myRevision = revision;
61 myCharset = charset != null ? charset : file.getCharset(project);
64 public GitContentRevision(@NotNull FilePath file, @NotNull GitRevisionNumber revision, @NotNull Project project) {
65 this(file, revision, project, null);
68 @Nullable
69 public String getContent() throws VcsException {
70 if (myFile.isDirectory()) {
71 return null;
73 VirtualFile root = GitUtil.getGitRoot(myFile);
74 GitSimpleHandler h = new GitSimpleHandler(myProject, root, GitHandler.SHOW);
75 h.setCharset(myCharset);
76 h.setNoSSH(true);
77 h.setSilent(true);
78 h.addParameters(myRevision.getRev() + ":" + GitUtil.relativePath(root, myFile));
79 return h.run();
82 @NotNull
83 public FilePath getFile() {
84 return myFile;
87 @NotNull
88 public VcsRevisionNumber getRevisionNumber() {
89 return myRevision;
92 public boolean equals(Object obj) {
93 if (this == obj) return true;
94 if ((obj == null) || (obj.getClass() != getClass())) return false;
96 GitContentRevision test = (GitContentRevision)obj;
97 return (myFile.equals(test.myFile) && myRevision.equals(test.myRevision));
100 public int hashCode() {
101 return myFile.hashCode() + myRevision.hashCode();
105 * Create revision
107 * @param vcsRoot a vcs root for the repository
108 * @param path an path inside with possibly escape sequences
109 * @param revisionNumber a revision number, if null the current revision will be created
110 * @param project the context project
111 * @param isDeleted if true, the file is deleted
112 * @return a created revision
113 * @throws com.intellij.openapi.vcs.VcsException
114 * if there is a problem with creating revision
116 public static ContentRevision createRevision(VirtualFile vcsRoot,
117 String path,
118 VcsRevisionNumber revisionNumber,
119 Project project,
120 boolean isDeleted) throws VcsException {
121 final String name = vcsRoot.getPath() + "/" + GitUtil.unescapePath(path);
122 final FilePath file = isDeleted ? VcsUtil.getFilePathForDeletedFile(name, false) : VcsUtil.getFilePath(name, false);
123 if (revisionNumber != null) {
124 return new GitContentRevision(file, (GitRevisionNumber)revisionNumber, project);
126 else {
127 return CurrentContentRevision.create(file);