2 // PBGitCommitController.m
5 // Created by Pieter de Bie on 19-09-08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
9 #import "PBGitCommitController.h"
10 #import "NSFileHandleExt.h"
11 #import "PBChangedFile.h"
12 #import "PBWebChangesController.h"
13 #import "PBGitIndex.h"
14 #import "NSString_RegEx.h"
17 @interface PBGitCommitController (PrivateMethods)
18 - (void)processHunk:(NSString *)hunk stage:(BOOL)stage reverse:(BOOL)reverse;
21 @implementation PBGitCommitController
23 @synthesize status, index;
25 - (id)initWithRepository:(PBGitRepository *)theRepository superController:(PBGitWindowController *)controller
27 if (!(self = [super initWithRepository:theRepository superController:controller]))
30 index = [[PBGitIndex alloc] initWithRepository:theRepository workingDirectory:[NSURL fileURLWithPath:[theRepository workingDirectory]]];
44 [commitMessageView setTypingAttributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Monaco" size:12.0] forKey:NSFontAttributeName]];
46 [unstagedFilesController setFilterPredicate:[NSPredicate predicateWithFormat:@"hasUnstagedChanges == 1"]];
47 [cachedFilesController setFilterPredicate:[NSPredicate predicateWithFormat:@"hasStagedChanges == 1"]];
49 [unstagedFilesController setSortDescriptors:[NSArray arrayWithObjects:
50 [[NSSortDescriptor alloc] initWithKey:@"status" ascending:false],
51 [[NSSortDescriptor alloc] initWithKey:@"path" ascending:true], nil]];
52 [cachedFilesController setSortDescriptors:[NSArray arrayWithObject:
53 [[NSSortDescriptor alloc] initWithKey:@"path" ascending:true]]];
57 [webController closeView];
60 - (NSResponder *)firstResponder;
62 return commitMessageView;
65 - (IBAction)signOff:(id)sender
67 if (![repository.config valueForKeyPath:@"user.name"] || ![repository.config valueForKeyPath:@"user.email"])
68 return [[repository windowController] showMessageSheet:@"User's name not set" infoText:@"Signing off a commit requires setting user.name and user.email in your git config"];
69 NSString *SOBline = [NSString stringWithFormat:@"Signed-off-by: %@ <%@>",
70 [repository.config valueForKeyPath:@"user.name"],
71 [repository.config valueForKeyPath:@"user.email"]];
73 if([commitMessageView.string rangeOfString:SOBline].location == NSNotFound) {
74 NSArray *selectedRanges = [commitMessageView selectedRanges];
75 commitMessageView.string = [NSString stringWithFormat:@"%@\n\n%@",
76 commitMessageView.string, SOBline];
77 [commitMessageView setSelectedRanges: selectedRanges];
81 - (void) refresh:(id) sender
85 // Reload refs (in case HEAD changed)
86 [repository reloadRefs];
94 - (void) commitFailedBecause:(NSString *)reason
97 self.status = [@"Commit failed: " stringByAppendingString:reason];
98 [[repository windowController] showMessageSheet:@"Commit failed" infoText:reason];
102 - (IBAction) commit:(id) sender
104 if ([[NSFileManager defaultManager] fileExistsAtPath:[repository.fileURL.path stringByAppendingPathComponent:@"MERGE_HEAD"]]) {
105 [[repository windowController] showMessageSheet:@"Cannot commit merges" infoText:@"GitX cannot commit merges yet. Please commit your changes from the command line."];
109 if ([[cachedFilesController arrangedObjects] count] == 0) {
110 [[repository windowController] showMessageSheet:@"No changes to commit" infoText:@"You must first stage some changes before committing"];
114 NSString *commitMessage = [commitMessageView string];
115 if ([commitMessage length] < 3) {
116 [[repository windowController] showMessageSheet:@"Commitmessage missing" infoText:@"Please enter a commit message before committing"];
120 [cachedFilesController setSelectionIndexes:[NSIndexSet indexSet]];
121 [unstagedFilesController setSelectionIndexes:[NSIndexSet indexSet]];
123 [index commitWithMessage:commitMessage];
125 [webController setStateMessage:[NSString stringWithFormat:@"Successfully created commit"]];
127 [commitMessageView setString:@""];