1 /*******************************************************************************
2 * Copyright (C) 2008, Google Inc.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.egit
.core
;
13 import java
.io
.IOException
;
14 import java
.io
.InputStream
;
16 import org
.eclipse
.core
.resources
.IContainer
;
17 import org
.eclipse
.core
.resources
.IFile
;
18 import org
.eclipse
.core
.resources
.IResource
;
19 import org
.eclipse
.core
.resources
.IWorkspaceRoot
;
20 import org
.eclipse
.core
.runtime
.CoreException
;
21 import org
.eclipse
.egit
.core
.project
.RepositoryMapping
;
22 import org
.spearce
.jgit
.errors
.IncorrectObjectTypeException
;
23 import org
.spearce
.jgit
.lib
.Constants
;
24 import org
.spearce
.jgit
.lib
.FileMode
;
25 import org
.spearce
.jgit
.lib
.ObjectId
;
26 import org
.spearce
.jgit
.lib
.Repository
;
27 import org
.spearce
.jgit
.treewalk
.AbstractTreeIterator
;
28 import org
.spearce
.jgit
.treewalk
.WorkingTreeIterator
;
29 import org
.spearce
.jgit
.util
.FS
;
32 * Adapts an Eclipse {@link IContainer} for use in a <code>TreeWalk</code>.
34 * This iterator converts an Eclipse IContainer object into something that a
35 * TreeWalk instance can iterate over in parallel with any other Git tree data
36 * structure, such as another working directory tree from outside of the
37 * workspace or a stored tree from a Repository object database.
39 * Modification times provided by this iterator are obtained from the cache
40 * Eclipse uses to track external resource modification. This can be faster, but
41 * requires the user refresh their workspace when external modifications take
42 * place. This is not really a concern as it is common practice to need to do a
43 * workspace refresh after externally modifying a file.
45 * @see org.spearce.jgit.treewalk.TreeWalk
47 public class ContainerTreeIterator
extends WorkingTreeIterator
{
48 private static String
computePrefix(final IContainer base
) {
49 final RepositoryMapping rm
= RepositoryMapping
.getMapping(base
);
51 throw new IllegalArgumentException("Not in a Git project: " + base
);
52 return rm
.getRepoRelativePath(base
);
55 private final IContainer node
;
58 * Construct a new iterator from a container in the workspace.
60 * The iterator will support traversal over the named container, but only if
61 * it is contained within a project which has the Git repository provider
62 * connected and this resource is mapped into a Git repository. During the
63 * iteration the paths will be automatically generated to match the proper
64 * repository paths for this container's children.
67 * the part of the workspace the iterator will walk over.
69 public ContainerTreeIterator(final IContainer base
) {
70 super(computePrefix(base
));
76 * Construct a new iterator from the workspace root.
78 * The iterator will support traversal over workspace projects that have
79 * a Git repository provider connected and is mapped into a Git repository.
80 * During the iteration the paths will be automatically generated to match
81 * the proper repository paths for this container's children.
84 * the workspace root to walk over.
86 public ContainerTreeIterator(final IWorkspaceRoot root
) {
93 * Construct a new iterator from a container in the workspace, with a given
96 * The iterator will support traversal over the named container, but only if
97 * it is contained within a project which has the Git repository provider
98 * connected and this resource is mapped into a Git repository. During the
99 * iteration the paths will be automatically generated to match the proper
100 * repository paths for this container's children.
103 * the parent iterator we were created from.
105 * the part of the workspace the iterator will walk over.
107 public ContainerTreeIterator(final WorkingTreeIterator p
,
108 final IContainer base
) {
115 public AbstractTreeIterator
createSubtreeIterator(final Repository db
)
116 throws IncorrectObjectTypeException
, IOException
{
117 if (FileMode
.TREE
.equals(mode
))
118 return new ContainerTreeIterator(this,
119 (IContainer
) ((ResourceEntry
) current()).rsrc
);
121 throw new IncorrectObjectTypeException(ObjectId
.zeroId(),
122 Constants
.TYPE_TREE
);
126 * Get the ResourceEntry for the current entry.
128 * @return the current entry
130 public ResourceEntry
getResourceEntry() {
131 return (ResourceEntry
) current();
134 private Entry
[] entries() {
135 final IResource
[] all
;
137 all
= node
.members(IContainer
.INCLUDE_HIDDEN
);
138 } catch (CoreException err
) {
142 final Entry
[] r
= new Entry
[all
.length
];
143 for (int i
= 0; i
< r
.length
; i
++)
144 r
[i
] = new ResourceEntry(all
[i
]);
149 * Wrapper for a resource in the Eclipse workspace
151 static public class ResourceEntry
extends Entry
{
152 final IResource rsrc
;
154 private final FileMode mode
;
156 private long length
= -1;
158 ResourceEntry(final IResource f
) {
161 switch (f
.getType()) {
163 if (FS
.INSTANCE
.canExecute(asFile()))
164 mode
= FileMode
.EXECUTABLE_FILE
;
166 mode
= FileMode
.REGULAR_FILE
;
168 case IResource
.PROJECT
:
169 case IResource
.FOLDER
: {
170 final IContainer c
= (IContainer
) f
;
171 if (c
.findMember(".git") != null)
172 mode
= FileMode
.GITLINK
;
174 mode
= FileMode
.TREE
;
178 mode
= FileMode
.MISSING
;
184 public FileMode
getMode() {
189 public String
getName() {
190 if (rsrc
.getType() == IResource
.PROJECT
)
191 return rsrc
.getLocation().lastSegment();
193 return rsrc
.getName();
197 public long getLength() {
199 if (rsrc
instanceof IFile
)
200 length
= asFile().length();
208 public long getLastModified() {
209 return rsrc
.getLocalTimeStamp();
213 public InputStream
openInputStream() throws IOException
{
214 if (rsrc
instanceof IFile
) {
216 return ((IFile
) rsrc
).getContents(true);
217 } catch (CoreException err
) {
218 final IOException ioe
= new IOException(err
.getMessage());
223 throw new IOException("Not a regular file: " + rsrc
);
227 * Get the underlying resource of this entry.
229 * @return the underlying resource
231 public IResource
getResource() {
235 private File
asFile() {
236 return ((IFile
) rsrc
).getLocation().toFile();