Permalink
Browse files

GitIndex: add commit notifications

  • Loading branch information...
1 parent c6d2b61 commit 4192d6a03f08fa347d4600071eb08042cca594a5 @pieter committed Sep 13, 2009
Showing with 58 additions and 9 deletions.
  1. +24 −4 PBGitCommitController.m
  2. +2 −0 PBGitIndex.h
  3. +32 −5 PBGitIndex.m
View
@@ -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 @@ - (id)initWithRepository:(PBGitRepository *)theRepository superController:(PBGit
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 @@ - (IBAction) commit:(id) sender
[cachedFilesController setSelectionIndexes:[NSIndexSet indexSet]];
[unstagedFilesController setSelectionIndexes:[NSIndexSet indexSet]];
- [index commitWithMessage:commitMessage];
-
- [webController setStateMessage:[NSString stringWithFormat:@"Successfully created commit"]];
+ self.busy = YES;
- [commitMessageView setString:@""];
+ [index commitWithMessage:commitMessage];
+ [commitMessageView setEditable:NO];
}
@@ -131,4 +136,19 @@ - (void)refreshFinished:(NSNotification *)notification
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
View
@@ -14,6 +14,8 @@
extern NSString *PBGitIndexIndexRefreshStatus;
extern NSString *PBGitIndexIndexRefreshFailed;
extern NSString *PBGitIndexFinishedIndexRefresh;
+
+extern NSString *PBGitIndexCommitStatus;
extern NSString *PBGitIndexCommitFailed;
extern NSString *PBGitIndexFinishedCommit;
View
@@ -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 @@ @interface PBGitIndex ()
// 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 @@ - (void)commitWithMessage:(NSString *)commitMessage
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 @@ - (void)commitWithMessage:(NSString *)commitMessage
[arguments addObject:parent];
}
+ [self postCommitUpdate:@"Creating commit"];
int ret = 1;
NSString *commit = [repository outputForArguments:arguments
inputString:commitMessage
@@ -161,23 +166,38 @@ - (void)commitWithMessage:(NSString *)commitMessage
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]];
+ [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
- //[webController setStateMessage:[NSString stringWithFormat:@"Successfully created commit %@", commit]];
- ;
+ 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 @@ - (void)commitWithMessage:(NSString *)commitMessage
}
+- (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

0 comments on commit 4192d6a

Please sign in to comment.