Remove System.out.println from RevWalkFilterTest
[jgit.git] / org.spearce.egit.core / src / org / spearce / egit / core / internal / storage / CommitFileRevision.java
blobe364027d6a79aca50669301f35814270d3f41812
1 /*******************************************************************************
2 * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * See LICENSE for the full license text, also available.
8 *******************************************************************************/
9 package org.spearce.egit.core.internal.storage;
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Map;
16 import org.eclipse.core.internal.resources.ResourceException;
17 import org.eclipse.core.resources.IResourceStatus;
18 import org.eclipse.core.resources.IStorage;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.team.core.history.IFileRevision;
23 import org.eclipse.team.core.history.ITag;
24 import org.spearce.egit.core.GitTag;
25 import org.spearce.jgit.lib.AnyObjectId;
26 import org.spearce.jgit.lib.ObjectId;
27 import org.spearce.jgit.lib.PersonIdent;
28 import org.spearce.jgit.lib.Ref;
29 import org.spearce.jgit.lib.Repository;
30 import org.spearce.jgit.revwalk.RevCommit;
31 import org.spearce.jgit.treewalk.TreeWalk;
33 /**
34 * An {@link IFileRevision} for a version of a specified resource in the
35 * specified commit (revision).
37 class CommitFileRevision extends GitFileRevision {
38 private final Repository db;
40 private final RevCommit commit;
42 private final PersonIdent author;
44 private final String path;
46 private ObjectId blobId;
48 CommitFileRevision(final Repository repo, final RevCommit rc,
49 final String fileName) {
50 this(repo, rc, fileName, null);
53 CommitFileRevision(final Repository repo, final RevCommit rc,
54 final String fileName, final ObjectId blob) {
55 super(fileName);
56 db = repo;
57 commit = rc;
58 author = rc.getAuthorIdent();
59 path = fileName;
60 blobId = blob;
63 String getGitPath() {
64 return path;
67 public IStorage getStorage(final IProgressMonitor monitor)
68 throws CoreException {
69 if (blobId == null)
70 blobId = locateBlobObjectId();
71 return new BlobStorage(db, path, blobId);
74 public long getTimestamp() {
75 return author != null ? author.getWhen().getTime() : 0;
78 public String getContentIdentifier() {
79 return commit.getId().name();
82 public String getAuthor() {
83 return author != null ? author.getName() : null;
86 public String getComment() {
87 return commit.getShortMessage();
90 public String toString() {
91 return commit.getId() + ":" + path;
94 public ITag[] getTags() {
95 final Collection<GitTag> ret = new ArrayList<GitTag>();
96 for (final Map.Entry<String, Ref> tag : db.getTags().entrySet()) {
97 final ObjectId ref = tag.getValue().getPeeledObjectId();
98 if (ref == null)
99 continue;
100 if (!AnyObjectId.equals(ref, commit))
101 continue;
102 ret.add(new GitTag(tag.getKey()));
104 return ret.toArray(new ITag[ret.size()]);
108 * Get the commit that introduced this file revision.
110 * @return the commit we most recently noticed this file in.
112 public RevCommit getRevCommit() {
113 return commit;
116 private ObjectId locateBlobObjectId() throws CoreException {
117 try {
118 final TreeWalk w = TreeWalk.forPath(db, path, commit.getTree());
119 if (w == null)
120 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL,
121 Path.fromPortableString(path), "Path not in "
122 + commit.getId() + ".", null);
123 return w.getObjectId(0);
124 } catch (IOException e) {
125 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, Path
126 .fromPortableString(path), "IO error looking up path in "
127 + commit.getId() + ".", e);