From 8df614cb53deb9f1fe7e66e6a9db82ff69cbc155 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Sun, 13 Sep 2009 02:47:39 +0200 Subject: [PATCH] GitIndex: add basic commit method --- PBGitIndex.h | 2 +- PBGitIndex.m | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/PBGitIndex.h b/PBGitIndex.h index 19bfe43..261b226 100644 --- a/PBGitIndex.h +++ b/PBGitIndex.h @@ -41,7 +41,7 @@ // Refresh the index - (void)refresh; -//- (void)commit; +- (void)commitWithMessage:(NSString *)commitMessage; // Inter-file changes: - (BOOL)stageFiles:(NSArray *)stageFiles; diff --git a/PBGitIndex.m b/PBGitIndex.m index 1109140..5097158 100644 --- a/PBGitIndex.m +++ b/PBGitIndex.m @@ -118,6 +118,69 @@ return parent; } +- (void)commitWithMessage:(NSString *)commitMessage +{ + NSMutableString *commitSubject = [@"commit: " mutableCopy]; + NSRange newLine = [commitMessage rangeOfString:@"\n"]; + if (newLine.location == NSNotFound) + [commitSubject appendString:commitMessage]; + else + [commitSubject appendString:[commitMessage substringToIndex:newLine.location]]; + + NSString *commitMessageFile; + commitMessageFile = [repository.fileURL.path stringByAppendingPathComponent:@"COMMIT_EDITMSG"]; + + [commitMessage writeToFile:commitMessageFile atomically:YES encoding:NSUTF8StringEncoding error:nil]; + + // TODO: Notification: @"Creating tree.."; + NSString *tree = [repository outputForCommand:@"write-tree"]; + if ([tree length] != 40) + return; //TODO: commitFailedBecause:@"Could not create a tree"; + + + NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"commit-tree", tree, nil]; + NSString *parent = amend ? @"HEAD^" : @"HEAD"; + if ([repository parseReference:parent]) { + [arguments addObject:@"-p"]; + [arguments addObject:parent]; + } + + int ret = 1; + NSString *commit = [repository outputForArguments:arguments + inputString:commitMessage + byExtendingEnvironment:amendEnvironment + retValue: &ret]; + + if (ret || [commit length] != 40) + return; // TODO: [self commitFailedBecause:@"Could not create a commit object"]; + + 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"]; + + [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]]; + ; + + repository.hasChanged = YES; + + amendEnvironment = nil; + if (amend) + self.amend = NO; + else + [self refresh]; + +} + - (BOOL)stageFiles:(NSArray *)stageFiles { // Input string for update-index