Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FileTreeIterator.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (C) 2008, Google Inc.
  3. * Copyright (C) 2007-2010, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * Copyright (C) 2009, Tor Arne Vestbø <torarnv@gmail.com>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.treewalk;
  47. import java.io.File;
  48. import java.io.FileInputStream;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.FileMode;
  54. import org.eclipse.jgit.lib.ObjectReader;
  55. import org.eclipse.jgit.lib.Repository;
  56. import org.eclipse.jgit.util.FS;
  57. /**
  58. * Working directory iterator for standard Java IO.
  59. * <p>
  60. * This iterator uses the standard <code>java.io</code> package to read the
  61. * specified working directory as part of a {@link TreeWalk}.
  62. */
  63. public class FileTreeIterator extends WorkingTreeIterator {
  64. /**
  65. * the starting directory. This directory should correspond to the root of
  66. * the repository.
  67. */
  68. protected final File directory;
  69. /**
  70. * the file system abstraction which will be necessary to perform certain
  71. * file system operations.
  72. */
  73. protected final FS fs;
  74. /**
  75. * Create a new iterator to traverse the work tree and its children.
  76. *
  77. * @param repo
  78. * the repository whose working tree will be scanned.
  79. */
  80. public FileTreeIterator(Repository repo) {
  81. this(repo.getWorkTree(), repo.getFS(),
  82. repo.getConfig().get(WorkingTreeOptions.KEY));
  83. initRootIterator(repo);
  84. }
  85. /**
  86. * Create a new iterator to traverse the given directory and its children.
  87. *
  88. * @param root
  89. * the starting directory. This directory should correspond to
  90. * the root of the repository.
  91. * @param fs
  92. * the file system abstraction which will be necessary to perform
  93. * certain file system operations.
  94. * @param options
  95. * working tree options to be used
  96. */
  97. public FileTreeIterator(final File root, FS fs, WorkingTreeOptions options) {
  98. super(options);
  99. directory = root;
  100. this.fs = fs;
  101. init(entries());
  102. }
  103. /**
  104. * Create a new iterator to traverse a subdirectory.
  105. *
  106. * @param p
  107. * the parent iterator we were created from.
  108. * @param fs
  109. * the file system abstraction which will be necessary to perform
  110. * certain file system operations.
  111. * @param root
  112. * the subdirectory. This should be a directory contained within
  113. * the parent directory.
  114. */
  115. protected FileTreeIterator(final WorkingTreeIterator p, final File root,
  116. FS fs) {
  117. super(p);
  118. directory = root;
  119. this.fs = fs;
  120. init(entries());
  121. }
  122. @Override
  123. public AbstractTreeIterator createSubtreeIterator(final ObjectReader reader)
  124. throws IncorrectObjectTypeException, IOException {
  125. return new FileTreeIterator(this, ((FileEntry) current()).file, fs);
  126. }
  127. private Entry[] entries() {
  128. final File[] all = directory.listFiles();
  129. if (all == null)
  130. return EOF;
  131. final Entry[] r = new Entry[all.length];
  132. for (int i = 0; i < r.length; i++)
  133. r[i] = new FileEntry(all[i], fs);
  134. return r;
  135. }
  136. /**
  137. * Wrapper for a standard Java IO file
  138. */
  139. static public class FileEntry extends Entry {
  140. final File file;
  141. private final FileMode mode;
  142. private long length = -1;
  143. private long lastModified;
  144. /**
  145. * Create a new file entry.
  146. *
  147. * @param f
  148. * file
  149. * @param fs
  150. * file system
  151. */
  152. public FileEntry(final File f, FS fs) {
  153. file = f;
  154. if (f.isDirectory()) {
  155. if (new File(f, Constants.DOT_GIT).exists())
  156. mode = FileMode.GITLINK;
  157. else
  158. mode = FileMode.TREE;
  159. } else if (fs.canExecute(file))
  160. mode = FileMode.EXECUTABLE_FILE;
  161. else
  162. mode = FileMode.REGULAR_FILE;
  163. }
  164. @Override
  165. public FileMode getMode() {
  166. return mode;
  167. }
  168. @Override
  169. public String getName() {
  170. return file.getName();
  171. }
  172. @Override
  173. public long getLength() {
  174. if (length < 0)
  175. length = file.length();
  176. return length;
  177. }
  178. @Override
  179. public long getLastModified() {
  180. if (lastModified == 0)
  181. lastModified = file.lastModified();
  182. return lastModified;
  183. }
  184. @Override
  185. public InputStream openInputStream() throws IOException {
  186. return new FileInputStream(file);
  187. }
  188. /**
  189. * Get the underlying file of this entry.
  190. *
  191. * @return the underlying file of this entry
  192. */
  193. public File getFile() {
  194. return file;
  195. }
  196. }
  197. /**
  198. * @return The root directory of this iterator
  199. */
  200. public File getDirectory() {
  201. return directory;
  202. }
  203. /**
  204. * @return The location of the working file. This is the same as {@code new
  205. * File(getDirectory(), getEntryPath())} but may be faster by
  206. * reusing an internal File instance.
  207. */
  208. public File getEntryFile() {
  209. return ((FileEntry) current()).getFile();
  210. }
  211. @Override
  212. protected byte[] idSubmodule(final Entry e) {
  213. if (repository == null)
  214. return idSubmodule(getDirectory(), e);
  215. return super.idSubmodule(e);
  216. }
  217. }