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
;
37 * Git content revision
39 public class GitContentRevision
implements ContentRevision
{
43 @NotNull private final FilePath myFile
;
47 @NotNull private final GitRevisionNumber myRevision
;
51 @NotNull private final Project myProject
;
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
) {
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);
69 public String
getContent() throws VcsException
{
70 if (myFile
.isDirectory()) {
73 VirtualFile root
= GitUtil
.getGitRoot(myFile
);
74 GitSimpleHandler h
= new GitSimpleHandler(myProject
, root
, GitHandler
.SHOW
);
75 h
.setCharset(myCharset
);
78 h
.addParameters(myRevision
.getRev() + ":" + GitUtil
.relativePath(root
, myFile
));
83 public FilePath
getFile() {
88 public VcsRevisionNumber
getRevisionNumber() {
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();
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
,
118 VcsRevisionNumber revisionNumber
,
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
);
127 return CurrentContentRevision
.create(file
);