Skip to content

Commit 2fc59d5

Browse files
committed
Load commit diffs in PBWebCommitController asynchronously.
1 parent df50e5e commit 2fc59d5

File tree

1 file changed

+70
-61
lines changed

1 file changed

+70
-61
lines changed

PBWebCommitController.m

Lines changed: 70 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -83,67 +83,76 @@ - (void)commitDetailsLoaded:(NSNotification *)notification
8383
{
8484
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleReadToEndOfFileCompletionNotification object:nil];
8585

86-
NSData *data = [[notification userInfo] valueForKey:NSFileHandleNotificationDataItem];
87-
if (!data)
88-
return;
89-
90-
NSString *details = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
91-
if (!details)
92-
details = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
93-
94-
if (!details)
95-
return;
96-
97-
// Header
98-
NSArray *headerItems = [self parseHeader:details];
99-
NSString *header = [self htmlForHeader:headerItems withRefs:[self refsForCurrentCommit]];
100-
101-
// In case the commit is a merge, we need to explicity give diff-tree the
102-
// list of parents, or else it will yield an empty result.
103-
// If it's not a merge, this won't hurt.
104-
NSMutableArray *allParents = [NSMutableArray array];
105-
106-
for (NSDictionary *item in headerItems)
107-
if ([[item objectForKey:kHeaderKeyName] isEqualToString:@"parent"])
108-
[allParents addObject:[item objectForKey:kHeaderKeyContent]];
109-
110-
NSArray *parents = [self chooseDiffParents:allParents];
111-
112-
// File Stats
113-
NSMutableDictionary *stats = [self parseStats:details];
114-
115-
// File list
116-
NSMutableArray *args = [NSMutableArray arrayWithObjects:@"diff-tree", @"--root", @"-r", @"-C90%", @"-M90%", nil];
117-
[args addObjectsFromArray:parents];
118-
[args addObject:currentSha];
119-
NSString *dt = [repository outputInWorkdirForArguments:args];
120-
NSString *fileList = [GLFileView parseDiffTree:dt withStats:stats];
121-
122-
// Diffs list
123-
args = [NSMutableArray arrayWithObjects:@"diff-tree", @"--root", @"--cc", @"-C90%", @"-M90%", nil];
124-
[args addObjectsFromArray:parents];
125-
[args addObject:currentSha];
126-
NSString *d = [repository outputInWorkdirForArguments:args];
127-
NSString *html;
128-
if(showLongDiffs || [d length] < 200000)
129-
{
130-
showLongDiffs = FALSE;
131-
NSString *diffs = [GLFileView parseDiff:d];
132-
html = [NSString stringWithFormat:@"%@%@<div id='diffs'>%@</div>",header,fileList,diffs];
133-
} else {
134-
html = [NSString stringWithFormat:@"%@%@<div id='diffs'><p>This is a very large commit. It may take a long time to load the diff. Click <a href='' onclick='showFullDiff(); return false;'>here</a> to show anyway.</p></div>",header,fileList,currentSha];
135-
}
136-
137-
html = [html stringByReplacingOccurrencesOfString:@"{SHA_PREV}" withString:[NSString stringWithFormat:@"%@^",currentSha]];
138-
html = [html stringByReplacingOccurrencesOfString:@"{SHA}" withString:currentSha];
139-
140-
[[view windowScriptObject] callWebScriptMethod:@"showCommit" withArguments:[NSArray arrayWithObject:html]];
141-
142-
#ifdef DEBUG_BUILD
143-
NSString *dom = [(DOMHTMLElement*)[[[view mainFrame] DOMDocument] documentElement] outerHTML];
144-
NSString *tmpFile = @"~/tmp/test2.html";
145-
[dom writeToFile:[tmpFile stringByExpandingTildeInPath] atomically:true encoding:NSUTF8StringEncoding error:nil];
146-
#endif
86+
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
87+
NSData *data = [[notification userInfo] valueForKey:NSFileHandleNotificationDataItem];
88+
if (!data)
89+
return;
90+
91+
NSString *details = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
92+
if (!details)
93+
details = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
94+
95+
if (!details)
96+
return;
97+
98+
// Header
99+
NSArray *headerItems = [self parseHeader:details];
100+
NSString *header = [self htmlForHeader:headerItems withRefs:[self refsForCurrentCommit]];
101+
102+
// In case the commit is a merge, we need to explicity give diff-tree the
103+
// list of parents, or else it will yield an empty result.
104+
// If it's not a merge, this won't hurt.
105+
NSMutableArray *allParents = [NSMutableArray array];
106+
107+
for (NSDictionary *item in headerItems)
108+
if ([[item objectForKey:kHeaderKeyName] isEqualToString:@"parent"])
109+
[allParents addObject:[item objectForKey:kHeaderKeyContent]];
110+
111+
NSArray *parents = [self chooseDiffParents:allParents];
112+
113+
// File Stats
114+
NSMutableDictionary *stats = [self parseStats:details];
115+
116+
// File list
117+
NSMutableArray *args = [NSMutableArray arrayWithObjects:@"diff-tree", @"--root", @"-r", @"-C90%", @"-M90%", nil];
118+
[args addObjectsFromArray:parents];
119+
[args addObject:currentSha];
120+
NSString *dt = [repository outputInWorkdirForArguments:args];
121+
NSString *fileList = [GLFileView parseDiffTree:dt withStats:stats];
122+
123+
// Diffs list
124+
NSString *loadingSha = currentSha;
125+
126+
args = [NSMutableArray arrayWithObjects:@"diff-tree", @"--root", @"--cc", @"-C90%", @"-M90%", nil];
127+
[args addObjectsFromArray:parents];
128+
[args addObject:loadingSha];
129+
NSString *d = [repository outputInWorkdirForArguments:args];
130+
NSString *html;
131+
if(showLongDiffs || [d length] < 200000)
132+
{
133+
showLongDiffs = FALSE;
134+
NSString *diffs = [GLFileView parseDiff:d];
135+
html = [NSString stringWithFormat:@"%@%@<div id='diffs'>%@</div>",header,fileList,diffs];
136+
} else {
137+
html = [NSString stringWithFormat:@"%@%@<div id='diffs'><p>This is a very large commit. It may take a long time to load the diff. Click <a href='' onclick='showFullDiff(); return false;'>here</a> to show anyway.</p></div>",header,fileList,currentSha];
138+
}
139+
140+
html = [html stringByReplacingOccurrencesOfString:@"{SHA_PREV}" withString:[NSString stringWithFormat:@"%@^",currentSha]];
141+
html = [html stringByReplacingOccurrencesOfString:@"{SHA}" withString:currentSha];
142+
143+
dispatch_async(dispatch_get_main_queue(), ^{
144+
// If a bunch of commits were selected quickly we check to make sure the commit we requested is still active
145+
if ([currentSha isEqualToString:loadingSha]) {
146+
[[view windowScriptObject] callWebScriptMethod:@"showCommit" withArguments:[NSArray arrayWithObject:html]];
147+
148+
#ifdef DEBUG_BUILD
149+
NSString *dom = [(DOMHTMLElement*)[[[view mainFrame] DOMDocument] documentElement] outerHTML];
150+
NSString *tmpFile = @"~/tmp/test2.html";
151+
[dom writeToFile:[tmpFile stringByExpandingTildeInPath] atomically:true encoding:NSUTF8StringEncoding error:nil];
152+
#endif
153+
}
154+
});
155+
});
147156
}
148157

149158
- (NSString*) refsForCurrentCommit

0 commit comments

Comments
 (0)