Skip to content

Commit

Permalink
CommitView: Add option to amend commits
Browse files Browse the repository at this point in the history
  • Loading branch information
pieter committed Oct 10, 2008
1 parent 5010511 commit e659e63
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 257 deletions.
6 changes: 4 additions & 2 deletions PBChangedFile.h
Expand Up @@ -29,9 +29,11 @@ typedef enum {
@property (assign) BOOL hasCachedChanges;
@property (assign) BOOL hasUnstagedChanges;
- (NSImage *)icon;
- (NSString *)changes;
- (NSString *)cachedChangesAmend:(BOOL)amend;
- (NSString *)unstagedChanges;

- (void) stageChanges;
- (void) unstageChanges;
- (void) unstageChangesAmend:(BOOL)amend;

- (id) initWithPath:(NSString *)p andRepository:(PBGitRepository *)r;
@end
12 changes: 9 additions & 3 deletions PBChangedFile.m
Expand Up @@ -20,8 +20,11 @@ - (id) initWithPath:(NSString *)p andRepository:(PBGitRepository *)r
return self;
}

- (NSString *) cachedChanges
- (NSString *) cachedChangesAmend:(BOOL) amend
{
if (amend)
return [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"diff", @"--cached", @"HEAD^", @"--", path, nil]];

return [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"diff", @"--cached", @"--", path, nil]];
}

Expand Down Expand Up @@ -63,9 +66,12 @@ - (void) stageChanges
self.hasCachedChanges = YES;
}

- (void) unstageChanges
- (void) unstageChangesAmend:(BOOL) amend
{
[repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"reset", @"--", path, nil]];
if (amend)
[repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"reset", @"HEAD^", @"--", path, nil]];
else
[repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"reset", @"--", path, nil]];
self.hasCachedChanges = NO;
self.hasUnstagedChanges = YES;
}
Expand Down
2 changes: 2 additions & 0 deletions PBGitCommitController.h
Expand Up @@ -23,6 +23,7 @@
// You can increase it when your process start
// And decrease it after you have finished.
int busy;
BOOL amend;

IBOutlet PBIconAndTextCell* unstagedButtonCell;
IBOutlet PBIconAndTextCell* cachedButtonCell;
Expand All @@ -34,6 +35,7 @@
@property (retain) NSMutableArray *files;
@property (copy) NSString *status;
@property (assign) int busy;
@property (assign) BOOL amend;

- (void) readCachedFiles:(NSNotification *)notification;
- (void) readOtherFiles:(NSNotification *)notification;
Expand Down
33 changes: 26 additions & 7 deletions PBGitCommitController.m
Expand Up @@ -12,13 +12,15 @@

@implementation PBGitCommitController

@synthesize files, status, busy;
@synthesize files, status, busy, amend;

- (void)awakeFromNib
{
[super awakeFromNib];
self.busy = 0;

amend = NO;

[unstagedButtonCell setAction:@selector(rowClicked:)];
[cachedButtonCell setAction:@selector(rowClicked:)];

Expand All @@ -39,6 +41,18 @@ - (void)awakeFromNib
[[NSSortDescriptor alloc] initWithKey:@"path" ascending:true]]];
}

- (void) setAmend:(BOOL)newAmend
{
if (newAmend == amend)
return;
amend = newAmend;

if (amend && [[commitMessageView string] length] <= 3)
commitMessageView.string = [repository outputForCommand:@"log -1 --pretty=format:%s%n%n%b HEAD"];

[self refresh:self];
}

- (NSArray *) linesFromNotification:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
Expand All @@ -60,16 +74,18 @@ - (NSArray *) linesFromNotification:(NSNotification *)notification

- (NSString *) parentTree
{
id a = [repository parseReference:@"HEAD"];
NSString *parent = amend ? @"HEAD^" : @"HEAD";

NSString *a = [repository parseReference:@"HEAD^"];

// TODO: parseReference should exit nil if it errors out. For
// now, compare to "HEAD"
if ([a isEqualToString:@"HEAD"]) {
if ([a isEqualToString:parent]) {
// We don't have a head ref. Return the empty tree.
return @"4b825dc642cb6eb9a060e54bf8d69288fbee4904";
}

return @"HEAD";
return parent;
}

- (void) refresh:(id) sender
Expand Down Expand Up @@ -233,9 +249,10 @@ - (IBAction) commit:(id) sender
int ret;

NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"commit-tree", tree, nil];
if ([repository parseSymbolicReference:@"HEAD"]) {
NSString *parent = amend ? @"HEAD^" : @"HEAD";
if ([repository parseReference:parent]) {
[arguments addObject:@"-p"];
[arguments addObject:@"HEAD"];
[arguments addObject:parent];
}

NSString *commit = [repository outputForArguments:arguments
Expand All @@ -259,7 +276,9 @@ - (IBAction) commit:(id) sender
repository.hasChanged = YES;
self.busy--;
[commitMessageView setString:@""];
amend = NO;
[self refresh:self];
self.amend = NO;
}

- (void) tableClicked:(NSTableView *) tableView
Expand All @@ -280,7 +299,7 @@ - (void) tableClicked:(NSTableView *) tableView
if ([tableView tag] == 0)
[selectedItem stageChanges];
else
[selectedItem unstageChanges];
[selectedItem unstageChangesAmend:amend];

// Add the file to the other controller if it's not there yet
for (PBChangedFile *object in [otherController arrangedObjects])
Expand Down

0 comments on commit e659e63

Please sign in to comment.