Skip to content

Commit 1a803bd

Browse files
committed
Merge branch 'refs/heads/FileMerge_on_diffs'
2 parents 0b63765 + 5f85c82 commit 1a803bd

File tree

8 files changed

+135
-71
lines changed

8 files changed

+135
-71
lines changed

GLFileView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
+(BOOL)isBinaryFile:(NSString *)line;
4141
+(NSString*)mimeTypeForFileName:(NSString*)file;
4242
+(BOOL)isImage:(NSString*)file;
43+
+(BOOL)isDiffHeader:(NSString*)line;
4344

4445
@property(retain) NSMutableArray *groups;
4546
@property(retain) NSString *logFormat;

GLFileView.m

Lines changed: 64 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -274,61 +274,27 @@ + (NSString *)parseDiff:(NSString *)txt
274274
NSArray *lines = [txt componentsSeparatedByString:@"\n"];
275275
NSString *line;
276276
NSMutableString *res=[NSMutableString string];
277-
BOOL inDiff=FALSE;
278-
BOOL inBlock=FALSE;
279-
277+
280278
int l_line,l_end;
281279
int r_line,r_end;
282-
int i;
283-
for (i=0; i<[lines count]; i++) {
280+
281+
int i=0;
282+
do {
284283
line=[lines objectAtIndex:i];
285-
286-
if([GLFileView isStartBlock:line]){
287-
[res appendString:@"</td></tr></thead><tbody>"];
288-
inDiff=FALSE;
289-
NSString *header=[line substringFromIndex:3];
290-
NSRange hr = NSMakeRange(0, [header rangeOfString:@" @@"].location);
291-
header=[header substringWithRange:hr];
292-
293-
NSArray *pos=[header componentsSeparatedByString:@" "];
294-
NSArray *pos_l=[[pos objectAtIndex:0] componentsSeparatedByString:@","];
295-
NSArray *pos_r=[[pos objectAtIndex:1] componentsSeparatedByString:@","];
296-
297-
l_end=l_line=abs([[pos_l objectAtIndex:0]integerValue]);
298-
if ([pos_l count]>1) {
299-
l_end=l_line+[[pos_l objectAtIndex:1]integerValue];
300-
}
301-
302-
r_end=r_line=[[pos_r objectAtIndex:0]integerValue];
303-
if ([pos_r count]>1) {
304-
r_end=r_line+[[pos_r objectAtIndex:1]integerValue];
305-
}
306-
307-
[res appendString:[NSString stringWithFormat:@"<tr class='header'><td colspan='3'>%@</td></tr>",line]];
308-
inBlock=TRUE;
309-
}else if(inBlock){
310-
NSString *s=[line substringToIndex:1];
311-
if([s isEqualToString:@" "]){
312-
[res appendString:[NSString stringWithFormat:@"<tr><td class='l'>%d</td><td class='r'>%d</td>",l_line++,r_line++]];
313-
}else if([s isEqualToString:@"-"]){
314-
[res appendString:[NSString stringWithFormat:@"<tr class='l'><td class='l'>%d</td><td class='r'></td>",l_line++]];
315-
}else if([s isEqualToString:@"+"]){
316-
[res appendString:[NSString stringWithFormat:@"<tr class='r'><td class='l'></td><td class='r'>%d</td>",r_line++]];
317-
}
318-
[res appendString:[NSString stringWithFormat:@"<td class='code'>%@</td></tr>",[line substringFromIndex:1]]];
319-
if(!(l_line<l_end) && !(r_line<r_end))
320-
inBlock=FALSE;
321-
}else if([GLFileView isStartDiff:line]){
322-
if(inDiff)
323-
[res appendString:@"</tbody></table>"];
324-
inDiff=TRUE;
284+
if([GLFileView isStartDiff:line]){
325285
NSString *fileName=[self getFileName:line];
326-
[res appendString:[NSString stringWithFormat:@"<table id='%@' class='diff'><thead><tr><td colspan='3'>",fileName]];
327-
[res appendString:[NSString stringWithFormat:@"<p>%@</p>",line]];
328-
}else if(inDiff){
329-
[res appendString:[NSString stringWithFormat:@"<p>%@</p>",line]];
286+
[res appendString:[NSString stringWithFormat:@"<table id='%@' class='diff'><thead><tr><td colspan='3'><div style='float:left;'>",fileName]];
287+
do{
288+
[res appendString:[NSString stringWithFormat:@"<p>%@</p>",line]];
289+
line=[lines objectAtIndex:++i];
290+
}while([GLFileView isDiffHeader:line]);
291+
[res appendString:@"</div>"];
292+
if(![self isBinaryFile:line]){
293+
[res appendString:[NSString stringWithFormat:@"<div class='filemerge'><a href='' onclick='openFileMerge(\"%@\",\"{SHA}\"); return false;'><img src='GitX://app:/filemerge' width='32' height='32'/><br/>open in<br/>FileMerge</a></div>",fileName]];
294+
}
295+
[res appendString:@"</td></tr></thead><tbody>"];
296+
330297
if([self isBinaryFile:line]){
331-
[res appendString:@"</td></tr></thead><tbody>"];
332298
NSArray *files=[self getFilesNames:line];
333299
if(![[files objectAtIndex:0] isAbsolutePath]){
334300
[res appendString:[NSString stringWithFormat:@"<tr><td colspan='3'>%@</td></tr>",[files objectAtIndex:0]]];
@@ -342,11 +308,50 @@ + (NSString *)parseDiff:(NSString *)txt
342308
[res appendString:[NSString stringWithFormat:@"<tr><td colspan='3'><img src='GitX://{SHA}/%@'/></td></tr>",[files objectAtIndex:1]]];
343309
}
344310
}
311+
}else{
312+
do{
313+
NSString *header=[line substringFromIndex:3];
314+
NSRange hr = NSMakeRange(0, [header rangeOfString:@" @@"].location);
315+
header=[header substringWithRange:hr];
316+
317+
NSArray *pos=[header componentsSeparatedByString:@" "];
318+
NSArray *pos_l=[[pos objectAtIndex:0] componentsSeparatedByString:@","];
319+
NSArray *pos_r=[[pos objectAtIndex:1] componentsSeparatedByString:@","];
320+
321+
l_end=l_line=abs([[pos_l objectAtIndex:0]integerValue]);
322+
if ([pos_l count]>1) {
323+
l_end=l_line+[[pos_l objectAtIndex:1]integerValue];
324+
}
325+
326+
r_end=r_line=[[pos_r objectAtIndex:0]integerValue];
327+
if ([pos_r count]>1) {
328+
r_end=r_line+[[pos_r objectAtIndex:1]integerValue];
329+
}
330+
331+
[res appendString:[NSString stringWithFormat:@"<tr class='header'><td colspan='3'>%@</td></tr>",line]];
332+
do{
333+
line=[lines objectAtIndex:++i];
334+
NSString *s=[line substringToIndex:1];
335+
if([s isEqualToString:@" "]){
336+
[res appendString:[NSString stringWithFormat:@"<tr><td class='l'>%d</td><td class='r'>%d</td>",l_line++,r_line++]];
337+
}else if([s isEqualToString:@"-"]){
338+
[res appendString:[NSString stringWithFormat:@"<tr class='l'><td class='l'>%d</td><td class='r'></td>",l_line++]];
339+
}else if([s isEqualToString:@"+"]){
340+
[res appendString:[NSString stringWithFormat:@"<tr class='r'><td class='l'></td><td class='r'>%d</td>",r_line++]];
341+
}
342+
[res appendString:[NSString stringWithFormat:@"<td class='code'>%@</td></tr>",[line substringFromIndex:1]]];
343+
}while((l_line<l_end) || (r_line<r_end));
344+
if(i<([lines count]-1)){
345+
line=[lines objectAtIndex:++i];
346+
}
347+
}while([GLFileView isStartBlock:line]);
345348
}
349+
[res appendString:@"</tbody></table>"];
350+
}else {
351+
i++;
346352
}
347-
}
348-
if(inDiff)
349-
[res appendString:@"</tbody></table>"];
353+
}while(i<[lines count]);
354+
350355
return res;
351356
}
352357

@@ -395,6 +400,12 @@ +(NSString*)mimeTypeForFileName:(NSString*)name
395400
return mimeType;
396401
}
397402

403+
+(BOOL)isDiffHeader:(NSString*)line
404+
{
405+
unichar c=[line characterAtIndex:0];
406+
return (c=='i') || (c=='m') || (c=='n') || (c=='d') || (c=='-') || (c=='+');
407+
}
408+
398409
+(BOOL)isImage:(NSString*)file
399410
{
400411
NSString *mimeType=[GLFileView mimeTypeForFileName:file];

PBGitXProtocol.m

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111

1212
@implementation PBGitXProtocol
1313

14+
- (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id <NSURLProtocolClient>)client
15+
{
16+
// work around for NSURLProtocol bug
17+
// note that this leaks!
18+
CFRetain(client);
19+
20+
if (self = [super initWithRequest:request cachedResponse:cachedResponse client:client])
21+
{
22+
}
23+
24+
return self;
25+
}
26+
1427
+ (BOOL) canInitWithRequest:(NSURLRequest *)request
1528
{
1629
return [[[request URL] scheme] isEqualToString:@"GitX"];
@@ -31,25 +44,40 @@ -(void)startLoading
3144
return;
3245
}
3346

34-
NSString *path=[[url path] substringFromIndex:1];
35-
NSString *v=@"";
36-
if ([[path substringToIndex:5] isEqualToString:@"prev/"]) {
37-
path=[path substringFromIndex:5];
38-
v=@"^";
47+
if ([[url host] isEqualToString:@"app"]) {
48+
NSString *app=[[url path] substringFromIndex:1];
49+
NSString *appPath=[[NSWorkspace sharedWorkspace] fullPathForApplication:app];
50+
NSLog(@"app=%@ appPath=%@",app,appPath);
51+
if(appPath){
52+
NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:appPath];
53+
NSLog(@"icon=%@",icon);
54+
[[self client] URLProtocol:self didLoadData:[icon TIFFRepresentation]];
55+
[[self client] URLProtocolDidFinishLoading:self];
56+
}else{
57+
[[self client] URLProtocol:self didFailWithError:[NSError errorWithDomain:@"gitx" code:404 userInfo:nil]];
58+
}
59+
}else {
60+
61+
NSString *path=[[url path] substringFromIndex:1];
62+
NSString *v=@"";
63+
if ([[path substringToIndex:5] isEqualToString:@"prev/"]) {
64+
path=[path substringFromIndex:5];
65+
v=@"^";
66+
}
67+
NSString *specifier = [NSString stringWithFormat:@"%@%@:%@", [url host], v,path];
68+
handle = [repo handleInWorkDirForArguments:[NSArray arrayWithObjects:@"cat-file", @"blob", specifier, nil]];
69+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishFileLoad:) name:NSFileHandleReadToEndOfFileCompletionNotification object:handle];
70+
[handle readToEndOfFileInBackgroundAndNotify];
71+
72+
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[[self request] URL]
73+
MIMEType:nil
74+
expectedContentLength:-1
75+
textEncodingName:nil];
76+
77+
[[self client] URLProtocol:self
78+
didReceiveResponse:response
79+
cacheStoragePolicy:NSURLCacheStorageNotAllowed];
3980
}
40-
NSString *specifier = [NSString stringWithFormat:@"%@%@:%@", [url host], v,path];
41-
handle = [repo handleInWorkDirForArguments:[NSArray arrayWithObjects:@"cat-file", @"blob", specifier, nil]];
42-
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishFileLoad:) name:NSFileHandleReadToEndOfFileCompletionNotification object:handle];
43-
[handle readToEndOfFileInBackgroundAndNotify];
44-
45-
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:[[self request] URL]
46-
MIMEType:nil
47-
expectedContentLength:-1
48-
textEncodingName:nil];
49-
50-
[[self client] URLProtocol:self
51-
didReceiveResponse:response
52-
cacheStoragePolicy:NSURLCacheStorageNotAllowed];
5381
}
5482

5583
- (void) didFinishFileLoad:(NSNotification *)notification

PBWebController.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ - (void)webView:(WebView *)webView addMessageToConsole:(NSDictionary *)dictionar
7676
NSLog(@"Error from webkit: %@", dictionary);
7777
}
7878

79+
- (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame
80+
{
81+
NSLog(@"Message from webkit: %@", message);
82+
}
83+
7984
- (NSURLRequest *)webView:(WebView *)sender
8085
resource:(id)identifier
8186
willSendRequest:(NSURLRequest *)request

PBWebHistoryController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- (NSString *)parseHeader:(NSString *)txt withRefs:(NSString *)badges;
3030
- (NSMutableDictionary *)parseStats:(NSString *)txt;
3131
- (NSString *) someMethodThatReturnsSomeHashForSomeString:(NSString*)concat;
32+
- (void) openFileMerge:(NSString*)file sha:(NSString *)sha;
3233

3334
@property (readonly) NSString* diff;
3435
@end

PBWebHistoryController.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ - (void)selectCommit:(NSString *)sha
224224
[historyController selectCommit:[PBGitSHA shaWithString:sha]];
225225
}
226226

227+
- (void) openFileMerge:(NSString*)file sha:(NSString *)sha
228+
{
229+
NSArray *args=[NSArray arrayWithObjects:@"difftool",@"--no-prompt",@"--tool=opendiff",[NSString stringWithFormat:@"%@^",sha],sha,file,nil];
230+
[historyController.repository handleForArguments:args];
231+
}
232+
233+
227234
- (void) sendKey: (NSString*) key
228235
{
229236
id script = [view windowScriptObject];

html/views/history/history.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,9 @@ a.showdiff {
317317
-webkit-box-shadow: 5px 5px 5px #ccc;
318318
width: 98%;
319319
margin: auto auto 20px 1%;
320+
}
321+
322+
.filemerge {
323+
float: right;
324+
text-align: center;
320325
}

html/views/history/history.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ var selectCommit = function(a) {
22
Controller.selectCommit_(a);
33
}
44

5+
var openFileMerge = function(file,sha) {
6+
alert(file);
7+
alert(sha);
8+
Controller.openFileMerge_sha_(file,sha);
9+
}
10+
511
var showImage = function(element, filename)
612
{
713
element.outerHTML = '<img src="GitX://' + commit.sha + '/' + filename + '">';

0 commit comments

Comments
 (0)