Load commit list in a separate thread

This makes the initial startup much faster. Commits get loaded in a separate
thread, and are displayed every 1000 commits.

There is a bug in here that makes fails a click while it is loading the commit
list. Not sure how to fix this, perhaps send the arrayController?
This commit is contained in:
Pieter de Bie
2008-06-15 16:10:09 +02:00
parent 378016c84f
commit 2e4ea6edab
5 changed files with 38 additions and 20 deletions
+1 -5
View File
@@ -18,9 +18,7 @@
F56526240E03D85900F03B52 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F56526230E03D85900F03B52 /* WebKit.framework */; };
F565262B0E03D89B00F03B52 /* PBWebGitController.m in Sources */ = {isa = PBXBuildFile; fileRef = F565262A0E03D89B00F03B52 /* PBWebGitController.m */; };
F565265A0E03E71B00F03B52 /* commit.html in Resources */ = {isa = PBXBuildFile; fileRef = F56526590E03E71B00F03B52 /* commit.html */; };
F57ABDDF0E0441DE00A088B8 /* commit.js in Sources */ = {isa = PBXBuildFile; fileRef = F57ABDDE0E0441DE00A088B8 /* commit.js */; };
F57ABE0B0E0442DD00A088B8 /* commit.js in Resources */ = {isa = PBXBuildFile; fileRef = F57ABDDE0E0441DE00A088B8 /* commit.js */; };
F57ABE190E04431D00A088B8 /* prototype.js in Sources */ = {isa = PBXBuildFile; fileRef = F57ABE180E04431D00A088B8 /* prototype.js */; };
F57ABE2B0E04435100A088B8 /* prototype.js in Resources */ = {isa = PBXBuildFile; fileRef = F57ABE180E04431D00A088B8 /* prototype.js */; };
F58A8F280E043698007E3FC0 /* commits.css in Resources */ = {isa = PBXBuildFile; fileRef = F58A8F270E043698007E3FC0 /* commits.css */; };
F5945E170E02B0C200706420 /* PBGitRepository.m in Sources */ = {isa = PBXBuildFile; fileRef = F5945E160E02B0C200706420 /* PBGitRepository.m */; };
@@ -48,7 +46,7 @@
F56526290E03D89B00F03B52 /* PBWebGitController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBWebGitController.h; sourceTree = "<group>"; };
F565262A0E03D89B00F03B52 /* PBWebGitController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBWebGitController.m; sourceTree = "<group>"; };
F56526590E03E71B00F03B52 /* commit.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = commit.html; path = html/commit.html; sourceTree = "<group>"; };
F57ABDDE0E0441DE00A088B8 /* commit.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = commit.js; path = html/commit.js; sourceTree = "<group>"; };
F57ABDDE0E0441DE00A088B8 /* commit.js */ = {isa = PBXFileReference; explicitFileType = sourcecode.javascript; fileEncoding = 4; name = commit.js; path = html/commit.js; sourceTree = "<group>"; };
F57ABE180E04431D00A088B8 /* prototype.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = prototype.js; path = html/prototype.js; sourceTree = "<group>"; };
F58A8F270E043698007E3FC0 /* commits.css */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.css; name = commits.css; path = html/commits.css; sourceTree = "<group>"; };
F5945E150E02B0C200706420 /* PBGitRepository.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitRepository.h; sourceTree = "<group>"; };
@@ -239,8 +237,6 @@
F56524BB0E02D22D00F03B52 /* NSFileHandleExt.m in Sources */,
F56524F00E02D45200F03B52 /* PBGitCommit.m in Sources */,
F565262B0E03D89B00F03B52 /* PBWebGitController.m in Sources */,
F57ABDDF0E0441DE00A088B8 /* commit.js in Sources */,
F57ABE190E04431D00A088B8 /* prototype.js in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
+5 -1
View File
@@ -13,9 +13,13 @@
NSArray* commits;
}
+ (PBGitRepository*) repositoryWithPath:(NSString*) path;
+ (void) setGitPath;
+ (PBGitRepository*) repositoryWithPath:(NSString*) path;
- (PBGitRepository*) initWithPath:(NSString*) path;
- (NSFileHandle*) handleForCommand:(NSString*) cmd;
- (void) initializeCommits;
- (void) addCommit: (id)obj;
@property (copy) NSString* path;
+25 -9
View File
@@ -13,16 +13,25 @@
@implementation PBGitRepository
@synthesize commits;
static NSString* gitPath = @"/usr/bin/env";
+ (PBGitRepository*) repositoryWithPath:(NSString*) path
{
[self setGitPath];
PBGitRepository* repo = [[PBGitRepository alloc] init];
repo.path = path;
PBGitRepository* repo = [[PBGitRepository alloc] initWithPath: path];
return repo;
}
- (PBGitRepository*) initWithPath: (NSString*) p
{
self.path = p;
NSThread * commitThread = [[NSThread alloc] initWithTarget: self selector: @selector(initializeCommits) object:nil];
[commitThread start];
return self;
}
+ (void) setGitPath
{
char* path = getenv("GIT_PATH");
@@ -60,26 +69,33 @@ static NSString* gitPath = @"/usr/bin/env";
commits = obj;
}
- (NSArray*) commits
- (void) initializeCommits
{
if (commits != nil)
return commits;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSFileHandle* handle = [self handleForCommand:@"log --pretty=format:%H\01%s\01%an HEAD"];
NSMutableArray * newArray = [NSMutableArray array];
NSDate* start = [NSDate date];
NSFileHandle* handle = [self handleForCommand:@"log --pretty=format:%H\01%s\01%an HEAD"];
NSString* currentLine = [handle readLine];
int num = 0;
while (currentLine.length > 0) {
NSArray* components = [currentLine componentsSeparatedByString:@"\01"];
PBGitCommit* newCommit = [[PBGitCommit alloc] initWithRepository: self andSha: [components objectAtIndex:0]];
newCommit.subject = [components objectAtIndex:1];
newCommit.author = [components objectAtIndex:2];
[newArray addObject: newCommit];
num++;
if (num % 1000 == 0)
[self performSelectorOnMainThread:@selector(setCommits:) withObject:newArray waitUntilDone:NO];
currentLine = [handle readLine];
}
commits = newArray;
return commits;
[self performSelectorOnMainThread:@selector(setCommits:) withObject:newArray waitUntilDone:YES];
NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:start];
NSLog(@"Loaded %i commits in %f seconds", num, duration);
[pool release];
[NSThread exit];
}
- (NSFileHandle*) handleForCommand:(NSString *)cmd
+3 -1
View File
@@ -8,15 +8,17 @@
#import <Cocoa/Cocoa.h>
#import "GitTest_AppDelegate.h"
#import "PBGitCommit.h"
#import <WebKit/WebKit.h>
@interface PBWebGitController : NSObject {
IBOutlet GitTest_AppDelegate* controller;
IBOutlet WebView* view;
IBOutlet NSArrayController* commitsController;
NSString* currentSha;
NSString* diff;
}
- (void) changeContentTo: (id) content;
- (void) changeContentTo: (PBGitCommit *) content;
@property (readonly) NSString* diff;
@end
+4 -4
View File
@@ -41,13 +41,13 @@
}
}
- (void) changeContentTo: (id) content
- (void) changeContentTo: (PBGitCommit *) content
{
NSLog(@"Starting Change");
if ([currentSha isEqualToString: content.sha])
return;
currentSha = content.sha;
id script = [view windowScriptObject];
[script setValue: content forKey:@"CommitObject"];
NSLog(@"Done 1");
[script callWebScriptMethod:@"doeHet" withArguments: nil];
NSLog(@"Change done");
}
@end