Skip to content

Commit 77b8431

Browse files
committed
better diff process
1 parent 875e7cf commit 77b8431

File tree

3 files changed

+53
-23
lines changed

3 files changed

+53
-23
lines changed

GLFileView.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
- (void)showFile;
2929
- (void)didLoad;
3030
- (NSString *)parseBlame:(NSString *)txt;
31-
- (NSString *)parseHTML:(NSString *)txt;
31+
+ (NSString *)parseHTML:(NSString *)txt;
32+
+ (NSString *) parseDiff:(NSString *)txt;
33+
+(BOOL)isStartDiff:(NSString *)line;
34+
+(BOOL)isStartBlock:(NSString *)line;
3235

3336
@property(retain) NSMutableArray *groups;
3437
@property(retain) NSString *logFormat;

GLFileView.m

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ - (void) showFile
103103
if(startFile==@"fileview"){
104104
fileTxt=[file textContents:&theError];
105105
if(!theError)
106-
fileTxt=[self parseHTML:fileTxt];
106+
fileTxt=[GLFileView parseHTML:fileTxt];
107107
}else if(startFile==@"blame"){
108108
fileTxt=[file blame:&theError];
109109
if(!theError)
@@ -113,7 +113,7 @@ - (void) showFile
113113
}else if(startFile==@"diff"){
114114
fileTxt=[file diff:diffType error:&theError];
115115
if(!theError)
116-
fileTxt=[self parseDiff:fileTxt];
116+
fileTxt=[GLFileView parseDiff:fileTxt];
117117
}
118118

119119
id script = [view windowScriptObject];
@@ -214,7 +214,7 @@ - (void)closeView
214214
[super closeView];
215215
}
216216

217-
- (NSString *) parseHTML:(NSString *)txt
217+
+ (NSString *) parseHTML:(NSString *)txt
218218
{
219219
txt=[txt stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
220220
txt=[txt stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"];
@@ -223,23 +223,21 @@ - (NSString *) parseHTML:(NSString *)txt
223223
return txt;
224224
}
225225

226-
- (NSString *) parseDiff:(NSString *)txt
226+
+ (NSString *) parseDiff:(NSString *)txt
227227
{
228228
txt=[self parseHTML:txt];
229229

230230
NSArray *lines = [txt componentsSeparatedByString:@"\n"];
231231
NSString *line;
232232
NSMutableString *res=[NSMutableString string];
233-
234-
[res appendString:@"<table class='diff'><thead><tr><td colspan='3'>"];
233+
BOOL inDiff=FALSE;
235234
int i=0;
235+
line=[lines objectAtIndex:i++];
236236
while(i<[lines count]){
237-
line=[lines objectAtIndex:i];
238-
if([[line substringToIndex:2] isEqualToString:@"@@"]){
239-
[res appendString:@"</td></tr></thead><tbody>"];
237+
if([GLFileView isStartBlock:line]){
240238

241-
int l_int,l_count,l_line;
242-
int r_int,r_count,r_line;
239+
int l_int,l_line,l_end;
240+
int r_int,r_line,r_end;
243241

244242
NSString *header=[line substringFromIndex:3];
245243
NSRange hr = NSMakeRange(0, [header rangeOfString:@" @@"].location);
@@ -248,15 +246,17 @@ - (NSString *) parseDiff:(NSString *)txt
248246
NSArray *pos=[header componentsSeparatedByString:@" "];
249247
NSArray *pos_l=[[pos objectAtIndex:0] componentsSeparatedByString:@","];
250248
NSArray *pos_r=[[pos objectAtIndex:1] componentsSeparatedByString:@","];
249+
251250
l_line=l_int=abs([[pos_l objectAtIndex:0]integerValue]);
252-
l_count=[[pos_l objectAtIndex:1]integerValue];
251+
l_end=l_line+[[pos_l objectAtIndex:1]integerValue];
252+
253253
r_line=r_int=[[pos_r objectAtIndex:0]integerValue];
254-
r_count=[[pos_r objectAtIndex:1]integerValue];
254+
r_end=r_line+[[pos_r objectAtIndex:1]integerValue];
255255

256256
[res appendString:[NSString stringWithFormat:@"<tr class='header'><td colspan='3'>%@</td></tr>",line]];
257257

258258
do{
259-
line=[lines objectAtIndex:++i];
259+
line=[lines objectAtIndex:i++];
260260
NSString *s=[line substringToIndex:1];
261261

262262
if([s isEqualToString:@" "]){
@@ -266,21 +266,42 @@ - (NSString *) parseDiff:(NSString *)txt
266266
}else if([s isEqualToString:@"+"]){
267267
[res appendString:[NSString stringWithFormat:@"<tr class='r'><td class='l'></td><td class='r'>%d</td>",r_line++]];
268268
}
269-
[res appendString:[NSString stringWithFormat:@"<td class='code'>%@</td></tr>",line]];
270-
}while(l_line<(l_int+l_count) && r_line<(r_int+r_count));
271-
269+
[res appendString:[NSString stringWithFormat:@"<td class='code'>%@</td></tr>",line]];
270+
//NSLog(@"%@ %d(%d)-%d(%d)",s,l_line,(l_int+l_count),r_line,(r_int+r_count));
271+
}while((l_line<l_end) || (r_line<r_end));
272+
273+
}else if([GLFileView isStartDiff:line]){
274+
if(inDiff)
275+
[res appendString:@"</tbody></table>"];
276+
inDiff=TRUE;
277+
[res appendString:@"<table class='diff'><thead><tr><td colspan='3'>"];
278+
do{
279+
[res appendString:[NSString stringWithFormat:@"<p>%@</p>",line]];
280+
line=[lines objectAtIndex:i++];
281+
}while(![GLFileView isStartBlock:line]);
282+
[res appendString:@"</td></tr></thead><tbody>"];
272283
}else{
273-
[res appendString:[NSString stringWithFormat:@"<p>%@</p>",line]];
284+
line=[lines objectAtIndex:i++];
274285
}
275-
i++;
276286
}
277-
[res appendString:@"</tbody></table>"];
287+
if(inDiff)
288+
[res appendString:@"</tbody></table>"];
278289
return res;
279290
}
280291

292+
+(BOOL)isStartDiff:(NSString *)line
293+
{
294+
return (([line length]>10) && [[line substringToIndex:10] isEqualToString:@"diff --git"]);
295+
}
296+
297+
+(BOOL)isStartBlock:(NSString *)line
298+
{
299+
return (([line length]>2) && [[line substringToIndex:2] isEqualToString:@"@@"]);
300+
}
301+
281302
- (NSString *) parseBlame:(NSString *)txt
282303
{
283-
txt=[self parseHTML:txt];
304+
txt=[GLFileView parseHTML:txt];
284305

285306
NSArray *lines = [txt componentsSeparatedByString:@"\n"];
286307
NSString *line;

PBWebHistoryController.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ - (void)commitDetailsLoaded:(NSNotification *)notification
9696
if (!details)
9797
return;
9898

99-
[[view windowScriptObject] callWebScriptMethod:@"loadCommitDetails" withArguments:[NSArray arrayWithObject:details]];
99+
NSRange range = [details rangeOfString:@"diff --git"];
100+
NSString *commitDetails=[details substringToIndex:range.location];
101+
NSString *diffs=[details substringFromIndex:range.location];
102+
NSString *pd=[GLFileView parseDiff:diffs];
103+
104+
[[view windowScriptObject] callWebScriptMethod:@"loadCommitDetails" withArguments:[NSArray arrayWithObject:commitDetails]];
105+
[[view windowScriptObject] callWebScriptMethod:@"newStyle" withArguments:[NSArray arrayWithObject:pd]];
100106
}
101107

102108
- (void)selectCommit:(NSString *)sha

0 commit comments

Comments
 (0)