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
;
25 this.relativeResourceName
= relativeResourceName
;
26 this.leafIsBlob
= leafIsBlob
;
27 this.followMainOnly
= followMainOnly
;
28 this.activeDiffLeafId
= activeDiffLeafId
;
31 public Collection
collectHistory() {
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,
40 } catch (IOException e
) {
42 return Collections
.EMPTY_LIST
;
46 Collection
collectHistory(int count
, ObjectId
[] lastResourceHash
, TreeEntry lastEntry
,
47 Repository repository
, Commit top
) throws IOException
{
49 return Collections
.EMPTY_LIST
;
50 Collection ret
= new ArrayList(10000);
52 Commit previous
= top
;
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
) {
60 if (i
== relativeResourceName
.length
-1 && leafIsBlob
)
61 m
= t
.findBlobMember(relativeResourceName
[i
]);
63 m
= t
.findTreeMember(relativeResourceName
[i
]);
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
];
72 if (m
instanceof Tree
) {
75 if (i
== currentResourceHash
.length
- 1) {
79 while (++i
< currentResourceHash
.length
) {
80 currentResourceHash
[i
] = ObjectId
.zeroId();
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
;
98 // TODO: we may need to list more revisions when traversing
100 ObjectId
[] parents
= current
.getParentIds();
101 if (!followMainOnly
) {
102 for (int i
= 1; i
< parents
.length
; ++i
) {
103 ObjectId mergeParentId
= parents
[i
];
106 mergeParent
= repository
.mapCommit(mergeParentId
);
107 ret
.addAll(collectHistory(0, lastResourceHash
, currentEntry
, repository
,
109 // TODO: this gets us a lot of duplicates that we need
111 // Leave that til we get a GUI.
112 } catch (IOException e
) {
117 if (parents
.length
> 0) {
118 ObjectId parentId
= parents
[0];
120 current
= repository
.mapCommit(parentId
);
121 } catch (IOException e
) {
129 } while (current
!= null);