Use ObjectId[] instead of List for parents
[egit.git] / org.spearce.jgit / src / org / spearce / jgit / lib / Walker.java
blob86c8a1eaf437d1bbd96b9069da55cb0210083aa9
1 /**
2 *
3 */
4 package org.spearce.jgit.lib;
6 import java.io.IOException;
7 import java.util.ArrayList;
8 import java.util.Arrays;
9 import java.util.Collection;
10 import java.util.Collections;
12 public abstract class Walker {
13 private String[] relativeResourceName;
14 private boolean leafIsBlob;
15 private boolean followMainOnly;
16 private Repository repository;
17 private ObjectId activeDiffLeafId;
18 private final Commit start;
20 protected abstract void collect(Collection ret,Commit commit, int count);
22 protected Walker(Repository repostory, Commit start, String[] relativeResourceName,boolean leafIsBlob,boolean followMainOnly, ObjectId activeDiffLeafId) {
23 this.repository = repostory;
24 this.start = start;
25 this.relativeResourceName = relativeResourceName;
26 this.leafIsBlob = leafIsBlob;
27 this.followMainOnly = followMainOnly;
28 this.activeDiffLeafId = activeDiffLeafId;
31 public Collection collectHistory() {
32 try {
33 Commit commit = start;
34 ObjectId[] initialResourceHash = new ObjectId[relativeResourceName.length];
35 Arrays.fill(initialResourceHash, ObjectId.zeroId());
36 if (activeDiffLeafId != null)
37 initialResourceHash[initialResourceHash.length-1] = activeDiffLeafId;
38 return collectHistory(0, initialResourceHash, null,
39 repository, commit);
40 } catch (IOException e) {
41 e.printStackTrace();
42 return Collections.EMPTY_LIST;
46 Collection collectHistory(int count, ObjectId[] lastResourceHash, TreeEntry lastEntry,
47 Repository repository, Commit top) throws IOException {
48 if (top == null)
49 return Collections.EMPTY_LIST;
50 Collection ret = new ArrayList(10000);
51 Commit current = top;
52 Commit previous = top;
54 do {
55 TreeEntry currentEntry = lastEntry;
56 ObjectId[] currentResourceHash = new ObjectId[lastResourceHash.length];
57 Tree t = current.getTree();
58 for (int i = 0; i < currentResourceHash.length; ++i) {
59 TreeEntry m;
60 if (i == relativeResourceName.length-1 && leafIsBlob)
61 m = t.findBlobMember(relativeResourceName[i]);
62 else
63 m = t.findTreeMember(relativeResourceName[i]);
64 if (m != null) {
65 ObjectId id = m.getId();
66 currentResourceHash[i] = id;
67 if (id.equals(lastResourceHash[i])) {
68 while (++i < currentResourceHash.length) {
69 currentResourceHash[i] = lastResourceHash[i];
71 } else {
72 if (m instanceof Tree) {
73 t = (Tree)m;
74 } else {
75 if (i == currentResourceHash.length - 1) {
76 currentEntry = m;
77 } else {
78 currentEntry = null;
79 while (++i < currentResourceHash.length) {
80 currentResourceHash[i] = ObjectId.zeroId();
85 } else {
86 for (; i < currentResourceHash.length; ++i) {
87 currentResourceHash[i] = ObjectId.zeroId();
92 if (currentResourceHash.length == 0 || !currentResourceHash[currentResourceHash.length-1].equals(lastResourceHash[currentResourceHash.length-1])) {
93 collect(ret, previous, count);
95 lastResourceHash = currentResourceHash;
96 previous = current;
98 // TODO: we may need to list more revisions when traversing
99 // branches
100 ObjectId[] parents = current.getParentIds();
101 if (!followMainOnly) {
102 for (int i = 1; i < parents.length; ++i) {
103 ObjectId mergeParentId = parents[i];
104 Commit mergeParent;
105 try {
106 mergeParent = repository.mapCommit(mergeParentId);
107 ret.addAll(collectHistory(0, lastResourceHash, currentEntry, repository,
108 mergeParent));
109 // TODO: this gets us a lot of duplicates that we need
110 // to filter out
111 // Leave that til we get a GUI.
112 } catch (IOException e) {
113 e.printStackTrace();
117 if (parents.length > 0) {
118 ObjectId parentId = parents[0];
119 try {
120 current = repository.mapCommit(parentId);
121 } catch (IOException e) {
122 e.printStackTrace();
123 current = null;
125 } else
126 current = null;
127 if (count>=0)
128 count++;
129 } while (current != null);
131 return ret;