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
;
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
;
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
) {
61 author
= rc
.getAuthorIdent();
70 public IStorage
getStorage(final IProgressMonitor monitor
)
71 throws CoreException
{
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();
103 if (!AnyObjectId
.equals(ref
, commit
))
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() {
119 private ObjectId
locateBlobObjectId() throws CoreException
{
121 final TreeWalk w
= TreeWalk
.forPath(db
, path
, commit
.getTree());
123 throw new ResourceException(IResourceStatus
.FAILED_READ_LOCAL
,
124 Path
.fromPortableString(path
), NLS
.bind(
125 CoreText
.CommitFileRevision_pathNotIn
, commit
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