git4idea: Added support for checking locally modified files
[fedora-idea.git] / plugins / git4idea / src / git4idea / GitFileRevision.java
blob3a3c63e219a78af11544fd5d11653a3ef31a6ca9
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 * Authors: gevession, Erlend Simonsen & Mark Scott
17 * This code was originally derived from the MKS & Mercurial IDEA VCS plugins
20 import com.intellij.openapi.project.Project;
21 import com.intellij.openapi.vcs.FilePath;
22 import com.intellij.openapi.vcs.VcsException;
23 import com.intellij.openapi.vcs.history.VcsFileRevision;
24 import com.intellij.openapi.vcs.history.VcsRevisionNumber;
25 import com.intellij.openapi.vfs.VirtualFile;
26 import git4idea.commands.GitHandler;
27 import git4idea.commands.GitSimpleHandler;
28 import org.jetbrains.annotations.NotNull;
29 import org.jetbrains.annotations.Nullable;
31 import java.io.IOException;
32 import java.io.UnsupportedEncodingException;
33 import java.nio.charset.Charset;
34 import java.util.Date;
36 /**
37 * Git file revision
39 public class GitFileRevision implements VcsFileRevision, Comparable<VcsFileRevision> {
40 /**
41 * encoding to be used for binary output
43 @SuppressWarnings({"HardCodedStringLiteral"}) private final static Charset BIN_ENCODING = Charset.forName("ISO-8859-1");
44 private final FilePath path;
45 private final GitRevisionNumber revision;
46 private final String author;
47 private final String message;
48 private byte[] content;
49 private final Project project;
50 private final String branch;
52 public GitFileRevision(@NotNull Project project, @NotNull FilePath path, @NotNull GitRevisionNumber revision) {
53 this(project, path, revision, null, null, null);
56 public GitFileRevision(@NotNull Project project,
57 @NotNull FilePath path,
58 @NotNull GitRevisionNumber revision,
59 @Nullable String author,
60 @Nullable String message,
61 @Nullable String branch) {
62 this.project = project;
63 this.path = path;
64 this.revision = revision;
65 this.author = author;
66 this.message = message;
67 this.branch = branch;
70 /**
71 * @return file path
73 public FilePath getPath() {
74 return path;
77 public VcsRevisionNumber getRevisionNumber() {
78 return revision;
81 public Date getRevisionDate() {
82 return revision.getTimestamp();
85 public String getAuthor() {
86 return author;
89 public String getCommitMessage() {
90 return message;
93 public String getBranchName() {
94 return branch;
97 public synchronized void loadContent() throws VcsException {
98 final VirtualFile root = GitUtil.getGitRoot(path);
99 GitSimpleHandler h = new GitSimpleHandler(project, root, GitHandler.SHOW);
100 h.setNoSSH(true);
101 h.setCharset(BIN_ENCODING);
102 h.setSilent(true);
103 h.addParameters(revision.getRev() + ":" + GitUtil.relativePath(root, path));
104 String result = h.run();
105 try {
106 content = result.getBytes(BIN_ENCODING.name());
108 catch (UnsupportedEncodingException e) {
109 throw new VcsException("Unable to locate encoding: " + BIN_ENCODING.name() + " Reason: " + e.toString(), e);
113 public synchronized byte[] getContent() throws IOException {
114 if (content == null) {
115 try {
116 loadContent();
118 catch (VcsException e) {
119 throw new IOException(e.getMessage());
122 return content;
125 public int compareTo(VcsFileRevision rev) {
126 if (rev instanceof GitFileRevision) return revision.compareTo(((GitFileRevision)rev).revision);
127 return getRevisionDate().compareTo(rev.getRevisionDate());