From 4192d6a03f08fa347d4600071eb08042cca594a5 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Sun, 13 Sep 2009 03:37:30 +0200 Subject: [PATCH] GitIndex: add commit notifications --- PBGitCommitController.m | 28 ++++++++++++++++++++++++---- PBGitIndex.h | 2 ++ PBGitIndex.m | 39 +++++++++++++++++++++++++++++++++------ 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/PBGitCommitController.m b/PBGitCommitController.m index 0643402..b5163be 100644 --- a/PBGitCommitController.m +++ b/PBGitCommitController.m @@ -16,6 +16,8 @@ @interface PBGitCommitController () - (void)refreshFinished:(NSNotification *)notification; +- (void)commitStatusUpdated:(NSNotification *)notification; +- (void)commitFinished:(NSNotification *)notification; @end @implementation PBGitCommitController @@ -29,7 +31,11 @@ index = [[PBGitIndex alloc] initWithRepository:theRepository workingDirectory:[NSURL fileURLWithPath:[theRepository workingDirectory]]]; [index refresh]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshFinished:) name:PBGitIndexFinishedIndexRefresh object:index]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(commitStatusUpdated:) name:PBGitIndexCommitStatus object:index]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(commitFinished:) name:PBGitIndexFinishedCommit object:index]; + return self; } @@ -118,11 +124,10 @@ [cachedFilesController setSelectionIndexes:[NSIndexSet indexSet]]; [unstagedFilesController setSelectionIndexes:[NSIndexSet indexSet]]; + self.busy = YES; + [index commitWithMessage:commitMessage]; - - [webController setStateMessage:[NSString stringWithFormat:@"Successfully created commit"]]; - - [commitMessageView setString:@""]; + [commitMessageView setEditable:NO]; } @@ -131,4 +136,19 @@ self.busy = NO; self.status = @"Index refresh finished"; } + +- (void)commitStatusUpdated:(NSNotification *)notification +{ + self.status = [[notification userInfo] objectForKey:@"description"]; +} + +- (void)commitFinished:(NSNotification *)notification +{ + [webController setStateMessage:[NSString stringWithFormat:[[notification userInfo] objectForKey:@"description"]]]; + + BOOL success = [[[notification userInfo] objectForKey:@"success"] boolValue]; + if (success) + [commitMessageView setString:@""]; + [commitMessageView setEditable:YES]; +} @end diff --git a/PBGitIndex.h b/PBGitIndex.h index 7dd0e93..f1fc8c9 100644 --- a/PBGitIndex.h +++ b/PBGitIndex.h @@ -14,6 +14,8 @@ extern NSString *PBGitIndexIndexRefreshStatus; extern NSString *PBGitIndexIndexRefreshFailed; extern NSString *PBGitIndexFinishedIndexRefresh; + +extern NSString *PBGitIndexCommitStatus; extern NSString *PBGitIndexCommitFailed; extern NSString *PBGitIndexFinishedCommit; diff --git a/PBGitIndex.m b/PBGitIndex.m index 2ce84ab..e641102 100644 --- a/PBGitIndex.m +++ b/PBGitIndex.m @@ -16,6 +16,8 @@ NSString *PBGitIndexIndexRefreshStatus = @"PBGitIndexIndexRefreshStatus"; NSString *PBGitIndexIndexRefreshFailed = @"PBGitIndexIndexRefreshFailed"; NSString *PBGitIndexFinishedIndexRefresh = @"PBGitIndexFinishedIndexRefresh"; + +NSString *PBGitIndexCommitStatus = @"PBGitIndexCommitStatus"; NSString *PBGitIndexCommitFailed = @"PBGitIndexCommitFailed"; NSString *PBGitIndexFinishedCommit = @"PBGitIndexFinishedCommit"; @@ -40,6 +42,7 @@ NSString *PBGitIndexFinishedCommit = @"PBGitIndexFinishedCommit"; // Returns the tree to compare the index to, based // on whether amend is set or not. - (NSString *) parentTree; +- (void)postCommitUpdate:(NSString *)update; @end @@ -138,8 +141,9 @@ NSString *PBGitIndexFinishedCommit = @"PBGitIndexFinishedCommit"; commitMessageFile = [repository.fileURL.path stringByAppendingPathComponent:@"COMMIT_EDITMSG"]; [commitMessage writeToFile:commitMessageFile atomically:YES encoding:NSUTF8StringEncoding error:nil]; + - // TODO: Notification: @"Creating tree.."; + [self postCommitUpdate:@"Creating tree"]; NSString *tree = [repository outputForCommand:@"write-tree"]; if ([tree length] != 40) return; //TODO: commitFailedBecause:@"Could not create a tree"; @@ -152,6 +156,7 @@ NSString *PBGitIndexFinishedCommit = @"PBGitIndexFinishedCommit"; [arguments addObject:parent]; } + [self postCommitUpdate:@"Creating commit"]; int ret = 1; NSString *commit = [repository outputForArguments:arguments inputString:commitMessage @@ -161,23 +166,38 @@ NSString *PBGitIndexFinishedCommit = @"PBGitIndexFinishedCommit"; if (ret || [commit length] != 40) return; // TODO: [self commitFailedBecause:@"Could not create a commit object"]; + [self postCommitUpdate:@"Running hooks"]; if (![repository executeHook:@"pre-commit" output:nil]) return; // TODO: [self commitFailedBecause:@"Pre-commit hook failed"]; if (![repository executeHook:@"commit-msg" withArgs:[NSArray arrayWithObject:commitMessageFile] output:nil]) return; // TODO: [self commitFailedBecause:@"Commit-msg hook failed"]; + [self postCommitUpdate:@"Updating HEAD"]; [repository outputForArguments:[NSArray arrayWithObjects:@"update-ref", @"-m", commitSubject, @"HEAD", commit, nil] retValue: &ret]; if (ret) return; // TODO: [self commitFailedBecause:@"Could not update HEAD"]; - if (![repository executeHook:@"post-commit" output:nil]) - return; // [webController setStateMessage:[NSString stringWithFormat:@"Post-commit hook failed, however, successfully created commit %@", commit]]; - else - //[webController setStateMessage:[NSString stringWithFormat:@"Successfully created commit %@", commit]]; - ; + [self postCommitUpdate:@"Running post-commit hook"]; + BOOL success = [repository executeHook:@"post-commit" output:nil]; + NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObject:[NSNumber numberWithBool:success] forKey:@"success"]; + NSString *description; + if (success) + description = [NSString stringWithFormat:@"Successfull created commit %@", commit]; + else + description = [NSString stringWithFormat:@"Post-commit hook failed, but successfully created commit %@", commit]; + + [userInfo setObject:description forKey:@"description"]; + [userInfo setObject:commit forKey:@"sha"]; + + [[NSNotificationCenter defaultCenter] postNotificationName:PBGitIndexFinishedCommit + object:self + userInfo:userInfo]; + if (!success) + return; + repository.hasChanged = YES; amendEnvironment = nil; @@ -188,6 +208,13 @@ NSString *PBGitIndexFinishedCommit = @"PBGitIndexFinishedCommit"; } +- (void)postCommitUpdate:(NSString *)update +{ + [[NSNotificationCenter defaultCenter] postNotificationName:PBGitIndexCommitStatus + object:self + userInfo:[NSDictionary dictionaryWithObject:update forKey:@"description"]]; +} + - (BOOL)stageFiles:(NSArray *)stageFiles { // Input string for update-index