GitIndex: Add a few notifications
[GitX.git] / PBGitCommitController.m
blob38ecc13eafcd8c0a79a397acf5b28a34ad3abf53
1 //
2 //  PBGitCommitController.m
3 //  GitX
4 //
5 //  Created by Pieter de Bie on 19-09-08.
6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
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;
19 @end
21 @implementation PBGitCommitController
23 @synthesize status, index;
25 - (id)initWithRepository:(PBGitRepository *)theRepository superController:(PBGitWindowController *)controller
27         if (!(self = [super initWithRepository:theRepository superController:controller]))
28                 return nil;
30         index = [[PBGitIndex alloc] initWithRepository:theRepository workingDirectory:[NSURL fileURLWithPath:[theRepository workingDirectory]]];
31         [index refresh];
32         return self;
35 - (BOOL)busy
37         return NO;
40 - (void)awakeFromNib
42         [super awakeFromNib];
44         [commitMessageView setTypingAttributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Monaco" size:12.0] forKey:NSFontAttributeName]];
45         
46         [unstagedFilesController setFilterPredicate:[NSPredicate predicateWithFormat:@"hasUnstagedChanges == 1"]];
47         [cachedFilesController setFilterPredicate:[NSPredicate predicateWithFormat:@"hasStagedChanges == 1"]];
48         
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]]];
55 - (void) removeView
57         [webController closeView];
58         [super finalize];
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];
78         }
81 - (void) refresh:(id) sender
83         [index refresh];
85         // Reload refs (in case HEAD changed)
86         [repository reloadRefs];
89 - (void) updateView
91         [self refresh:nil];
94 - (void) commitFailedBecause:(NSString *)reason
96         //self.busy--;
97         self.status = [@"Commit failed: " stringByAppendingString:reason];
98         [[repository windowController] showMessageSheet:@"Commit failed" infoText:reason];
99         return;
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."];
106                 return;
107         }
109         if ([[cachedFilesController arrangedObjects] count] == 0) {
110                 [[repository windowController] showMessageSheet:@"No changes to commit" infoText:@"You must first stage some changes before committing"];
111                 return;
112         }               
113         
114         NSString *commitMessage = [commitMessageView string];
115         if ([commitMessage length] < 3) {
116                 [[repository windowController] showMessageSheet:@"Commitmessage missing" infoText:@"Please enter a commit message before committing"];
117                 return;
118         }
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:@""];
131 @end