Refactor Repository's ref reading/writing for aggressive caching
[egit/zawir.git] / org.spearce.egit.core / src / org / spearce / egit / core / internal / storage / CommitFileRevision.java
blob680f820f2e284b9d1cdeb87de3ecf5803ffa4a0a
1 /*
2 * Copyright (C) 2007 Robin Rosenberg
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License, version 2.1, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org.spearce.egit.core.internal.storage;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Map;
24 import org.eclipse.core.internal.resources.ResourceException;
25 import org.eclipse.core.resources.IResourceStatus;
26 import org.eclipse.core.resources.IStorage;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.Path;
30 import org.eclipse.team.core.history.IFileRevision;
31 import org.eclipse.team.core.history.ITag;
32 import org.spearce.egit.core.GitTag;
33 import org.spearce.jgit.lib.AnyObjectId;
34 import org.spearce.jgit.lib.ObjectId;
35 import org.spearce.jgit.lib.PersonIdent;
36 import org.spearce.jgit.lib.Ref;
37 import org.spearce.jgit.lib.Repository;
38 import org.spearce.jgit.revwalk.RevCommit;
39 import org.spearce.jgit.treewalk.TreeWalk;
41 /**
42 * An {@link IFileRevision} for a version of a specified resource in the
43 * specified commit (revision).
45 class CommitFileRevision extends GitFileRevision {
46 private final Repository db;
48 private final RevCommit commit;
50 private final PersonIdent author;
52 private final String path;
54 private ObjectId blobId;
56 CommitFileRevision(final Repository repo, final RevCommit rc,
57 final String fileName) {
58 this(repo, rc, fileName, null);
61 CommitFileRevision(final Repository repo, final RevCommit rc,
62 final String fileName, final ObjectId blob) {
63 super(fileName);
64 db = repo;
65 commit = rc;
66 author = rc.getAuthorIdent();
67 path = fileName;
68 blobId = blob;
71 String getGitPath() {
72 return path;
75 public IStorage getStorage(final IProgressMonitor monitor)
76 throws CoreException {
77 if (blobId == null)
78 blobId = locateBlobObjectId();
79 return new BlobStorage(db, path, blobId);
82 public long getTimestamp() {
83 return author != null ? author.getWhen().getTime() : 0;
86 public String getContentIdentifier() {
87 return commit.getId().toString();
90 public String getAuthor() {
91 return author != null ? author.getName() : null;
94 public String getComment() {
95 return commit.getShortMessage();
98 public String toString() {
99 return commit.getId() + ":" + path;
102 public ITag[] getTags() {
103 final Collection<GitTag> ret = new ArrayList<GitTag>();
104 for (final Map.Entry<String, Ref> tag : db.getTags().entrySet()) {
105 final ObjectId ref = tag.getValue().getPeeledObjectId();
106 if (ref == null)
107 continue;
108 if (!AnyObjectId.equals(ref, commit))
109 continue;
110 ret.add(new GitTag(tag.getKey()));
112 return ret.toArray(new ITag[ret.size()]);
116 * Get the commit that introduced this file revision.
118 * @return the commit we most recently noticed this file in.
120 public RevCommit getRevCommit() {
121 return commit;
124 private ObjectId locateBlobObjectId() throws CoreException {
125 try {
126 final TreeWalk w = TreeWalk.forPath(db, path, commit.getTree());
127 if (w == null)
128 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL,
129 Path.fromPortableString(path), "Path not in "
130 + commit.getId() + ".", null);
131 return w.getObjectId(0);
132 } catch (IOException e) {
133 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, Path
134 .fromPortableString(path), "IO error looking up path in "
135 + commit.getId() + ".", e);