Skip to content

Commit

Permalink
GitIndex: add commit notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
pieter committed Sep 13, 2009
1 parent c6d2b61 commit 4192d6a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
28 changes: 24 additions & 4 deletions PBGitCommitController.m
Expand Up @@ -16,6 +16,8 @@

@interface PBGitCommitController ()
- (void)refreshFinished:(NSNotification *)notification;
- (void)commitStatusUpdated:(NSNotification *)notification;
- (void)commitFinished:(NSNotification *)notification;
@end

@implementation PBGitCommitController
Expand All @@ -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;
}

Expand Down Expand Up @@ -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];
}


Expand All @@ -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
2 changes: 2 additions & 0 deletions PBGitIndex.h
Expand Up @@ -14,6 +14,8 @@
extern NSString *PBGitIndexIndexRefreshStatus;
extern NSString *PBGitIndexIndexRefreshFailed;
extern NSString *PBGitIndexFinishedIndexRefresh;

extern NSString *PBGitIndexCommitStatus;
extern NSString *PBGitIndexCommitFailed;
extern NSString *PBGitIndexFinishedCommit;

Expand Down
37 changes: 32 additions & 5 deletions PBGitIndex.m
Expand Up @@ -16,6 +16,8 @@
NSString *PBGitIndexIndexRefreshStatus = @"PBGitIndexIndexRefreshStatus";
NSString *PBGitIndexIndexRefreshFailed = @"PBGitIndexIndexRefreshFailed";
NSString *PBGitIndexFinishedIndexRefresh = @"PBGitIndexFinishedIndexRefresh";

NSString *PBGitIndexCommitStatus = @"PBGitIndexCommitStatus";
NSString *PBGitIndexCommitFailed = @"PBGitIndexCommitFailed";
NSString *PBGitIndexFinishedCommit = @"PBGitIndexFinishedCommit";

Expand All @@ -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

Expand Down Expand Up @@ -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";
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 4192d6a

Please sign in to comment.