Use getRepostoryRelativeName everywhere
[egit.git] / org.spearce.egit.core / src / org / spearce / egit / core / GitStorage.java
blobcdb270ecfd804f994a14c8e723da122cd6b6662f
1 /*
2 * Copyright (C) 2006 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;
19 import java.io.ByteArrayInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.InputStream;
24 import org.eclipse.core.internal.resources.ResourceException;
25 import org.eclipse.core.resources.IFile;
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.resources.IResourceStatus;
28 import org.eclipse.core.resources.IStorage;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.core.runtime.IPath;
31 import org.eclipse.team.core.RepositoryProvider;
32 import org.spearce.egit.core.project.RepositoryMapping;
33 import org.spearce.jgit.lib.ObjectId;
34 import org.spearce.jgit.lib.ObjectLoader;
35 import org.spearce.jgit.lib.Tree;
36 import org.spearce.jgit.lib.TreeEntry;
38 public class GitStorage implements IStorage {
40 private final IResource resource;
42 private TreeEntry entry;
44 private ObjectId treeId;
46 public GitStorage(ObjectId treeId, IResource resource) {
47 this.treeId = treeId;
48 this.resource = resource;
49 if (treeId == null)
50 return;
52 GitProvider provider = (GitProvider) RepositoryProvider
53 .getProvider(resource.getProject());
54 RepositoryMapping repositoryMapping = provider.getData()
55 .getRepositoryMapping(resource.getProject());
56 Tree tree;
57 try {
58 tree = repositoryMapping.getRepository().mapTree(treeId);
59 String name = repositoryMapping.getRepoRelativePath(resource);
60 if (resource.getType() == IResource.FILE)
61 entry = tree.findBlobMember(name);
62 else
63 entry = tree.findTreeMember(name);
64 } catch (IOException e) {
65 // TODO Auto-generated catch block
66 e.printStackTrace();
70 public InputStream getContents() throws CoreException {
71 try {
72 if (treeId == null) {
73 return ((IFile) resource).getContents();
74 } else {
75 if (entry == null)
76 return new ByteArrayInputStream(new byte[0]);
77 else {
78 ObjectId id = entry.getId();
79 ObjectLoader reader = entry.getRepository().openBlob(id);
80 byte[] bytes = reader.getBytes();
81 return new ByteArrayInputStream(bytes);
84 } catch (FileNotFoundException e) {
85 throw new ResourceException(IResourceStatus.FAILED_READ_LOCAL,
86 resource.getFullPath(), "Could not read file", e);
87 } catch (IOException e) {
88 // TODO Auto-generated catch block
89 e.printStackTrace();
90 throw new ResourceException(33, resource.getFullPath(), e
91 .getMessage(), e);
95 public IPath getFullPath() {
96 return resource.getFullPath();
99 public String getName() {
100 return resource.getName();
103 public boolean isReadOnly() {
104 // TODO Auto-generated method stub
105 return false;
108 public Object getAdapter(Class adapter) {
109 // TODO Auto-generated method stub
110 return null;