Skip to content

Commit 4192d6a

Browse files
committed
GitIndex: add commit notifications
1 parent c6d2b61 commit 4192d6a

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

PBGitCommitController.m

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
@interface PBGitCommitController ()
1818
- (void)refreshFinished:(NSNotification *)notification;
19+
- (void)commitStatusUpdated:(NSNotification *)notification;
20+
- (void)commitFinished:(NSNotification *)notification;
1921
@end
2022

2123
@implementation PBGitCommitController
@@ -29,7 +31,11 @@ - (id)initWithRepository:(PBGitRepository *)theRepository superController:(PBGit
2931

3032
index = [[PBGitIndex alloc] initWithRepository:theRepository workingDirectory:[NSURL fileURLWithPath:[theRepository workingDirectory]]];
3133
[index refresh];
34+
3235
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshFinished:) name:PBGitIndexFinishedIndexRefresh object:index];
36+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(commitStatusUpdated:) name:PBGitIndexCommitStatus object:index];
37+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(commitFinished:) name:PBGitIndexFinishedCommit object:index];
38+
3339
return self;
3440
}
3541

@@ -118,11 +124,10 @@ - (IBAction) commit:(id) sender
118124
[cachedFilesController setSelectionIndexes:[NSIndexSet indexSet]];
119125
[unstagedFilesController setSelectionIndexes:[NSIndexSet indexSet]];
120126

121-
[index commitWithMessage:commitMessage];
122-
123-
[webController setStateMessage:[NSString stringWithFormat:@"Successfully created commit"]];
127+
self.busy = YES;
124128

125-
[commitMessageView setString:@""];
129+
[index commitWithMessage:commitMessage];
130+
[commitMessageView setEditable:NO];
126131
}
127132

128133

@@ -131,4 +136,19 @@ - (void)refreshFinished:(NSNotification *)notification
131136
self.busy = NO;
132137
self.status = @"Index refresh finished";
133138
}
139+
140+
- (void)commitStatusUpdated:(NSNotification *)notification
141+
{
142+
self.status = [[notification userInfo] objectForKey:@"description"];
143+
}
144+
145+
- (void)commitFinished:(NSNotification *)notification
146+
{
147+
[webController setStateMessage:[NSString stringWithFormat:[[notification userInfo] objectForKey:@"description"]]];
148+
149+
BOOL success = [[[notification userInfo] objectForKey:@"success"] boolValue];
150+
if (success)
151+
[commitMessageView setString:@""];
152+
[commitMessageView setEditable:YES];
153+
}
134154
@end

PBGitIndex.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
extern NSString *PBGitIndexIndexRefreshStatus;
1515
extern NSString *PBGitIndexIndexRefreshFailed;
1616
extern NSString *PBGitIndexFinishedIndexRefresh;
17+
18+
extern NSString *PBGitIndexCommitStatus;
1719
extern NSString *PBGitIndexCommitFailed;
1820
extern NSString *PBGitIndexFinishedCommit;
1921

PBGitIndex.m

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
NSString *PBGitIndexIndexRefreshStatus = @"PBGitIndexIndexRefreshStatus";
1717
NSString *PBGitIndexIndexRefreshFailed = @"PBGitIndexIndexRefreshFailed";
1818
NSString *PBGitIndexFinishedIndexRefresh = @"PBGitIndexFinishedIndexRefresh";
19+
20+
NSString *PBGitIndexCommitStatus = @"PBGitIndexCommitStatus";
1921
NSString *PBGitIndexCommitFailed = @"PBGitIndexCommitFailed";
2022
NSString *PBGitIndexFinishedCommit = @"PBGitIndexFinishedCommit";
2123

@@ -40,6 +42,7 @@ @interface PBGitIndex ()
4042
// Returns the tree to compare the index to, based
4143
// on whether amend is set or not.
4244
- (NSString *) parentTree;
45+
- (void)postCommitUpdate:(NSString *)update;
4346

4447
@end
4548

@@ -138,8 +141,9 @@ - (void)commitWithMessage:(NSString *)commitMessage
138141
commitMessageFile = [repository.fileURL.path stringByAppendingPathComponent:@"COMMIT_EDITMSG"];
139142

140143
[commitMessage writeToFile:commitMessageFile atomically:YES encoding:NSUTF8StringEncoding error:nil];
144+
141145

142-
// TODO: Notification: @"Creating tree..";
146+
[self postCommitUpdate:@"Creating tree"];
143147
NSString *tree = [repository outputForCommand:@"write-tree"];
144148
if ([tree length] != 40)
145149
return; //TODO: commitFailedBecause:@"Could not create a tree";
@@ -152,6 +156,7 @@ - (void)commitWithMessage:(NSString *)commitMessage
152156
[arguments addObject:parent];
153157
}
154158

159+
[self postCommitUpdate:@"Creating commit"];
155160
int ret = 1;
156161
NSString *commit = [repository outputForArguments:arguments
157162
inputString:commitMessage
@@ -161,23 +166,38 @@ - (void)commitWithMessage:(NSString *)commitMessage
161166
if (ret || [commit length] != 40)
162167
return; // TODO: [self commitFailedBecause:@"Could not create a commit object"];
163168

169+
[self postCommitUpdate:@"Running hooks"];
164170
if (![repository executeHook:@"pre-commit" output:nil])
165171
return; // TODO: [self commitFailedBecause:@"Pre-commit hook failed"];
166172

167173
if (![repository executeHook:@"commit-msg" withArgs:[NSArray arrayWithObject:commitMessageFile] output:nil])
168174
return; // TODO: [self commitFailedBecause:@"Commit-msg hook failed"];
169175

176+
[self postCommitUpdate:@"Updating HEAD"];
170177
[repository outputForArguments:[NSArray arrayWithObjects:@"update-ref", @"-m", commitSubject, @"HEAD", commit, nil]
171178
retValue: &ret];
172179
if (ret)
173180
return; // TODO: [self commitFailedBecause:@"Could not update HEAD"];
174181

175-
if (![repository executeHook:@"post-commit" output:nil])
176-
return; // [webController setStateMessage:[NSString stringWithFormat:@"Post-commit hook failed, however, successfully created commit %@", commit]];
182+
[self postCommitUpdate:@"Running post-commit hook"];
183+
184+
BOOL success = [repository executeHook:@"post-commit" output:nil];
185+
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObject:[NSNumber numberWithBool:success] forKey:@"success"];
186+
NSString *description;
187+
if (success)
188+
description = [NSString stringWithFormat:@"Successfull created commit %@", commit];
177189
else
178-
//[webController setStateMessage:[NSString stringWithFormat:@"Successfully created commit %@", commit]];
179-
;
190+
description = [NSString stringWithFormat:@"Post-commit hook failed, but successfully created commit %@", commit];
180191

192+
[userInfo setObject:description forKey:@"description"];
193+
[userInfo setObject:commit forKey:@"sha"];
194+
195+
[[NSNotificationCenter defaultCenter] postNotificationName:PBGitIndexFinishedCommit
196+
object:self
197+
userInfo:userInfo];
198+
if (!success)
199+
return;
200+
181201
repository.hasChanged = YES;
182202

183203
amendEnvironment = nil;
@@ -188,6 +208,13 @@ - (void)commitWithMessage:(NSString *)commitMessage
188208

189209
}
190210

211+
- (void)postCommitUpdate:(NSString *)update
212+
{
213+
[[NSNotificationCenter defaultCenter] postNotificationName:PBGitIndexCommitStatus
214+
object:self
215+
userInfo:[NSDictionary dictionaryWithObject:update forKey:@"description"]];
216+
}
217+
191218
- (BOOL)stageFiles:(NSArray *)stageFiles
192219
{
193220
// Input string for update-index

0 commit comments

Comments
 (0)