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
+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