15
15
*/
16
16
package com .gitblit .models ;
17
17
18
+ import java .io .IOException ;
18
19
import java .io .Serializable ;
19
20
20
21
import org .eclipse .jgit .diff .DiffEntry ;
21
22
import org .eclipse .jgit .diff .DiffEntry .ChangeType ;
23
+ import org .eclipse .jgit .errors .IncorrectObjectTypeException ;
24
+ import org .eclipse .jgit .errors .MissingObjectException ;
25
+ import org .eclipse .jgit .lib .Constants ;
22
26
import org .eclipse .jgit .lib .FileMode ;
27
+ import org .eclipse .jgit .lib .Repository ;
28
+ import org .eclipse .jgit .revwalk .RevWalk ;
29
+
30
+ import com .gitblit .manager .FilestoreManager ;
31
+ import com .gitblit .utils .JGitUtils ;
23
32
24
33
/**
25
34
* PathModel is a serializable model class that represents a file or a folder,
@@ -34,16 +43,18 @@ public class PathModel implements Serializable, Comparable<PathModel> {
34
43
35
44
public final String name ;
36
45
public final String path ;
46
+ private final FilestoreModel filestoreItem ;
37
47
public final long size ;
38
48
public final int mode ;
39
49
public final String objectId ;
40
50
public final String commitId ;
41
51
public boolean isParentPath ;
42
-
43
- public PathModel (String name , String path , long size , int mode , String objectId , String commitId ) {
52
+
53
+ public PathModel (String name , String path , FilestoreModel filestoreItem , long size , int mode , String objectId , String commitId ) {
44
54
this .name = name ;
45
55
this .path = path ;
46
- this .size = size ;
56
+ this .filestoreItem = filestoreItem ;
57
+ this .size = (filestoreItem == null ) ? size : filestoreItem .getSize ();
47
58
this .mode = mode ;
48
59
this .objectId = objectId ;
49
60
this .commitId = commitId ;
@@ -66,6 +77,18 @@ public boolean isFile() {
66
77
|| FileMode .EXECUTABLE_FILE .equals (mode )
67
78
|| (FileMode .MISSING .equals (mode ) && !isSymlink () && !isSubmodule () && !isTree ());
68
79
}
80
+
81
+ public boolean isFilestoreItem () {
82
+ return filestoreItem != null ;
83
+ }
84
+
85
+ public String getFilestoreOid () {
86
+ if (filestoreItem != null ) {
87
+ return filestoreItem .oid ;
88
+ }
89
+
90
+ return null ;
91
+ }
69
92
70
93
@ Override
71
94
public int hashCode () {
@@ -119,9 +142,9 @@ public static class PathChangeModel extends PathModel {
119
142
120
143
public int deletions ;
121
144
122
- public PathChangeModel (String name , String path , long size , int mode , String objectId ,
145
+ public PathChangeModel (String name , String path , FilestoreModel filestoreItem , long size , int mode , String objectId ,
123
146
String commitId , ChangeType type ) {
124
- super (name , path , size , mode , objectId , commitId );
147
+ super (name , path , filestoreItem , size , mode , objectId , commitId );
125
148
this .changeType = type ;
126
149
}
127
150
@@ -148,18 +171,33 @@ public boolean equals(Object o) {
148
171
return super .equals (o );
149
172
}
150
173
151
- public static PathChangeModel from (DiffEntry diff , String commitId ) {
174
+ public static PathChangeModel from (DiffEntry diff , String commitId , Repository repository ) {
152
175
PathChangeModel pcm ;
176
+ FilestoreModel filestoreItem = null ;
177
+ long size = 0 ;
178
+
179
+ if (repository != null ) {
180
+ try (RevWalk revWalk = new RevWalk (repository )) {
181
+ size = revWalk .getObjectReader ().getObjectSize (diff .getNewId ().toObjectId (), Constants .OBJ_BLOB );
182
+
183
+ if (JGitUtils .isPossibleFilestoreItem (size )) {
184
+ filestoreItem = JGitUtils .getFilestoreItem (revWalk .getObjectReader ().open (diff .getNewId ().toObjectId ()));
185
+ }
186
+ } catch (Exception e ) {
187
+ e .printStackTrace ();
188
+ }
189
+ }
190
+
153
191
if (diff .getChangeType ().equals (ChangeType .DELETE )) {
154
- pcm = new PathChangeModel (diff .getOldPath (), diff .getOldPath (), 0 , diff
192
+ pcm = new PathChangeModel (diff .getOldPath (), diff .getOldPath (), filestoreItem , size , diff
155
193
.getNewMode ().getBits (), diff .getOldId ().name (), commitId , diff
156
194
.getChangeType ());
157
195
} else if (diff .getChangeType ().equals (ChangeType .RENAME )) {
158
- pcm = new PathChangeModel (diff .getOldPath (), diff .getNewPath (), 0 , diff
196
+ pcm = new PathChangeModel (diff .getOldPath (), diff .getNewPath (), filestoreItem , size , diff
159
197
.getNewMode ().getBits (), diff .getNewId ().name (), commitId , diff
160
198
.getChangeType ());
161
199
} else {
162
- pcm = new PathChangeModel (diff .getNewPath (), diff .getNewPath (), 0 , diff
200
+ pcm = new PathChangeModel (diff .getNewPath (), diff .getNewPath (), filestoreItem , size , diff
163
201
.getNewMode ().getBits (), diff .getNewId ().name (), commitId , diff
164
202
.getChangeType ());
165
203
}
0 commit comments