Fix incorrect editor updates with changes from diff-viewer
[egit.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / internal / storage / CommitFileRevision.java
blobd219aabe08e97f0066d5c5c1b8a10de9d1d695d7
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 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
9 *******************************************************************************/
10 package org.eclipse.egit.core.internal.storage;
12 import java.io.IOException;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Map;
17 import org.eclipse.core.internal.resources.ResourceException;
18 import org.eclipse.core.resources.IResourceStatus;
19 import org.eclipse.core.resources.IStorage;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.egit.core.CoreText;
24 import org.eclipse.egit.core.GitTag;
25 import org.eclipse.osgi.util.NLS;
26 import org.eclipse.team.core.history.IFileRevision;
27 import org.eclipse.team.core.history.ITag;
28 import org.eclipse.jgit.lib.AnyObjectId;
29 import org.eclipse.jgit.lib.ObjectId;
30 import org.eclipse.jgit.lib.PersonIdent;
31 import org.eclipse.jgit.lib.Ref;
32 import org.eclipse.jgit.lib.Repository;
33 import org.eclipse.jgit.revwalk.RevCommit;
34 import org.eclipse.jgit.treewalk.TreeWalk;
36 /**
37 * An {@link IFileRevision} for a version of a specified resource in the
38 * specified commit (revision).
40 class CommitFileRevision extends GitFileRevision {
41 private final Repository db;
43 private final RevCommit commit;
45 private final PersonIdent author;
47 private final String path;
49 private ObjectId blobId;
51 CommitFileRevision(final Repository repo, final RevCommit rc,
52 final String fileName) {
53 this(repo, rc, fileName, null);
56 CommitFileRevision(final Repository repo, final RevCommit rc,
57 final String fileName, final ObjectId blob) {
58 super(fileName);
59 db = repo;
60 commit = rc;
61 author = rc.getAuthorIdent();
62 path = fileName;
63 blobId = blob;
66 String getGitPath() {
67 return path;
70 public IStorage getStorage(final IProgressMonitor monitor)
71 throws CoreException {
72 if (blobId == null)
73 blobId = locateBlobObjectId();
74 return new CommitBlobStorage(db, path, blobId, commit);
77 public long getTimestamp() {
78 return author != null ? author.getWhen().getTime() : 0;
81 public String getContentIdentifier() {
82 return commit.getId().name();
85 public String getAuthor() {
86 return author != null ? author.getName() : null;
89 public String getComment() {
90 return commit.getShortMessage();
93 public String toString() {
94 return commit.getId() + ":" + path; //$NON-NLS-1$
97 public ITag[] getTags() {
98 final Collection<GitTag> ret = new ArrayList<GitTag>();
99 for (final Map.Entry<String, Ref> tag : db.getTags().entrySet()) {
100 final ObjectId ref = tag.getValue().getPeeledObjectId();
101 if (ref == null)
102 continue;
103 if (!AnyObjectId.equals(ref, commit))
104 continue;
105 ret.add(new GitTag(tag.getKey()));
107 return ret.toArray(new ITag[ret.size()]);
111 * Get the commit that introduced this file revision.
113 * @return the commit we most recently noticed this file in.
115 public RevCommit getRevCommit() {
116 return commit;
119 private ObjectId locateBlobObjectId() throws CoreException {
120 try {
121 final TreeWalk w = TreeWalk.forPath(db, path, commit.getTree());
122 if (w == null)
123 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL,
124 Path.fromPortableString(path), NLS.bind(
125 CoreText.CommitFileRevision_pathNotIn, commit
126 .getId()), null);
127 return w.getObjectId(0);
128 } catch (IOException e) {
129 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL, Path
130 .fromPortableString(path), NLS.bind(
131 CoreText.CommitFileRevision_errorLookingUpPath, commit
132 .getId()), e);