diff --git a/PBGitCommitController.h b/PBGitCommitController.h index 1126b07..7a69381 100644 --- a/PBGitCommitController.h +++ b/PBGitCommitController.h @@ -30,6 +30,7 @@ // And decrease it after you have finished. int busy; BOOL amend; + NSDictionary *amendEnvironment; } diff --git a/PBGitCommitController.m b/PBGitCommitController.m index 8a276d3..7bde848 100644 --- a/PBGitCommitController.m +++ b/PBGitCommitController.m @@ -10,6 +10,7 @@ #import "NSFileHandleExt.h" #import "PBChangedFile.h" #import "PBWebChangesController.h" +#import "NSString_RegEx.h" @interface PBGitCommitController (PrivateMethods) @@ -51,20 +52,34 @@ { if (newAmend == amend) return; + amend = newAmend; + amendEnvironment = nil; - // Replace commit message with the old one if it's less than 3 characters long. - // This is just a random number. - if (amend && [[commitMessageView string] length] <= 3) { + // If we amend, we want to keep the author information for the previous commit + // We do this by reading in the previous commit, and storing the information + // in a dictionary. This dictionary will then later be read by [self commit:] + if (amend) { NSString *message = [repository outputForCommand:@"cat-file commit HEAD"]; - NSRange r = [message rangeOfString:@"\n\n"]; - if (r.location != NSNotFound) - message = [message substringFromIndex:r.location + 2]; + NSArray *match = [message substringsMatchingRegularExpression:@"\nauthor ([^\n]*) <([^\n>]*)> ([0-9]+[^\n]*)\n" count:3 options:0 ranges:nil error:nil]; + if (match) + amendEnvironment = [NSDictionary dictionaryWithObjectsAndKeys:[match objectAtIndex:1], @"GIT_AUTHOR_NAME", + [match objectAtIndex:2], @"GIT_AUTHOR_EMAIL", + [match objectAtIndex:3], @"GIT_AUTHOR_DATE", + nil]; - commitMessageView.string = message; + // Replace commit message with the old one if it's less than 3 characters long. + // This is just a random number. + if ([[commitMessageView string] length] <= 3) { + // Find the commit message + NSRange r = [message rangeOfString:@"\n\n"]; + if (r.location != NSNotFound) + message = [message substringFromIndex:r.location + 2]; + + commitMessageView.string = message; + } } - [self refresh:self]; } @@ -336,6 +351,7 @@ NSString *commit = [repository outputForArguments:arguments inputString:commitMessage + byExtendingEnvironment:amendEnvironment retValue: &ret]; if (ret || [commit length] != 40) @@ -361,6 +377,7 @@ self.busy--; [commitMessageView setString:@""]; amend = NO; + amendEnvironment = nil; [self refresh:self]; self.amend = NO; }