Permalink
Browse files

CommitView: move index functions to separate controller

This merges functionality that was previously stored in the
combination of PBGitCommitController / PBChangedFile to a
dedicated controller, PBGitIndexController.
  • Loading branch information...
1 parent 874737f commit 0975426f2f5119335c00768006b1ede0252ac448 @pieter committed Nov 18, 2008
@@ -29,6 +29,7 @@
F50FE0E30E07BE9600854FCD /* PBGitRevisionCell.m in Sources */ = {isa = PBXBuildFile; fileRef = F50FE0E20E07BE9600854FCD /* PBGitRevisionCell.m */; };
F513085B0E0740F2000C8BCD /* PBQLOutlineView.m in Sources */ = {isa = PBXBuildFile; fileRef = F513085A0E0740F2000C8BCD /* PBQLOutlineView.m */; };
F5140DC90E8A8EB20091E9F3 /* RoundedRectangle.m in Sources */ = {isa = PBXBuildFile; fileRef = F5140DC80E8A8EB20091E9F3 /* RoundedRectangle.m */; };
+ F523CEB60ED3399200DDD714 /* PBGitIndexController.m in Sources */ = {isa = PBXBuildFile; fileRef = F523CEB50ED3399200DDD714 /* PBGitIndexController.m */; };
F52BCE030E84208300AA3741 /* PBGitHistoryView.xib in Resources */ = {isa = PBXBuildFile; fileRef = F52BCE020E84208300AA3741 /* PBGitHistoryView.xib */; };
F52BCE070E84211300AA3741 /* PBGitHistoryController.m in Sources */ = {isa = PBXBuildFile; fileRef = F52BCE060E84211300AA3741 /* PBGitHistoryController.m */; };
F53C4DF70E97FC630022AD59 /* PBGitBinary.m in Sources */ = {isa = PBXBuildFile; fileRef = F53C4DF60E97FC630022AD59 /* PBGitBinary.m */; };
@@ -141,6 +142,8 @@
F513085A0E0740F2000C8BCD /* PBQLOutlineView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBQLOutlineView.m; sourceTree = "<group>"; };
F5140DC70E8A8EB20091E9F3 /* RoundedRectangle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RoundedRectangle.h; sourceTree = "<group>"; };
F5140DC80E8A8EB20091E9F3 /* RoundedRectangle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RoundedRectangle.m; sourceTree = "<group>"; };
+ F523CEB40ED3399100DDD714 /* PBGitIndexController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitIndexController.h; sourceTree = "<group>"; };
+ F523CEB50ED3399200DDD714 /* PBGitIndexController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitIndexController.m; sourceTree = "<group>"; };
F52BCE020E84208300AA3741 /* PBGitHistoryView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = PBGitHistoryView.xib; sourceTree = "<group>"; };
F52BCE050E84211300AA3741 /* PBGitHistoryController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitHistoryController.h; sourceTree = "<group>"; };
F52BCE060E84211300AA3741 /* PBGitHistoryController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitHistoryController.m; sourceTree = "<group>"; };
@@ -503,6 +506,8 @@
F59116E80E843BCB0072CCB1 /* PBGitCommitController.m */,
F5E927FA0E883F0700056E75 /* PBWebChangesController.h */,
F5E927FB0E883F0700056E75 /* PBWebChangesController.m */,
+ F523CEB40ED3399100DDD714 /* PBGitIndexController.h */,
+ F523CEB50ED3399200DDD714 /* PBGitIndexController.m */,
);
name = Commit;
sourceTree = "<group>";
@@ -671,6 +676,7 @@
F5FC41F40EBCBD4300191D80 /* PBGitXProtocol.m in Sources */,
F574A2850EAE2EAC003F2CB1 /* PBRefController.m in Sources */,
F5FC43FE0EBD08EE00191D80 /* PBRefMenuItem.m in Sources */,
+ F523CEB60ED3399200DDD714 /* PBGitIndexController.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -19,21 +19,22 @@ typedef enum {
NSString *path;
BOOL hasCachedChanges;
BOOL hasUnstagedChanges;
- __weak PBGitRepository *repository;
+
+ // Index and HEAD stuff, to be used to revert changes
+ NSString *commitBlobSHA;
+ NSString *commitBlobMode;
+
PBChangedFileStatus status;
}
-@property (readonly) NSString *path;
+@property (copy) NSString *path, *commitBlobSHA, *commitBlobMode;
@property (assign) PBChangedFileStatus status;
@property (assign) BOOL hasCachedChanges;
@property (assign) BOOL hasUnstagedChanges;
-- (NSImage *)icon;
-- (NSString *)cachedChangesAmend:(BOOL)amend;
-- (NSString *)unstagedChanges;
-- (void) stageChanges;
-- (void) unstageChangesAmend:(BOOL)amend;
+- (NSImage *)icon;
+- (NSString *)indexInfo;
-- (id) initWithPath:(NSString *)p andRepository:(PBGitRepository *)r;
+- (id) initWithPath:(NSString *)p;
@end
View
@@ -11,39 +11,25 @@
@implementation PBChangedFile
-@synthesize path, status, hasCachedChanges, hasUnstagedChanges;
+@synthesize path, status, hasCachedChanges, hasUnstagedChanges, commitBlobSHA, commitBlobMode;
-- (id) initWithPath:(NSString *)p andRepository:(PBGitRepository *)r
+- (id) initWithPath:(NSString *)p
{
+ if (![super init])
+ return nil;
+
path = p;
- repository = r;
return self;
}
-- (NSString *) cachedChangesAmend:(BOOL) amend
+- (NSString *)indexInfo
{
- if (amend)
- return [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"diff", @"--cached", @"HEAD^", @"--", path, nil]];
-
- return [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"diff", @"--cached", @"--", path, nil]];
-}
-
-- (NSString *)unstagedChanges
-{
- if (status == NEW) {
- NSStringEncoding encoding;
- NSError *error = nil;
- NSString *contents = [NSString stringWithContentsOfFile:[[repository workingDirectory] stringByAppendingPathComponent:path] usedEncoding:&encoding error:&error];
- if (error)
- return nil;
-
- return contents;
- }
-
- return [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"diff", @"--", path, nil]];
+ if (!self.commitBlobSHA)
+ return [NSString stringWithFormat:@"0 0000000000000000000000000000000000000000\t%@\0", self.path];
+ else
+ return [NSString stringWithFormat:@"%@ %@\t%@\0", self.commitBlobMode, self.commitBlobSHA, self.path];
}
-
- (NSImage *) icon
{
NSString *filename;
@@ -62,45 +48,6 @@ - (NSImage *) icon
return [[NSImage alloc] initByReferencingFile: p];
}
-- (void) stageChanges
-{
- if (status == DELETED)
- [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"rm", path, nil]];
- else
- [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"add", path, nil]];
-
- self.hasUnstagedChanges = NO;
- self.hasCachedChanges = YES;
-}
-
-- (void) unstageChangesAmend:(BOOL) amend
-{
- if (amend)
- [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"reset", @"HEAD^", @"--", path, nil]];
- else
- [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"reset", @"--", path, nil]];
- self.hasCachedChanges = NO;
- self.hasUnstagedChanges = YES;
-}
-
-- (void) forceRevertChanges
-{
- [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"checkout", @"--", path, nil]];
- self.hasUnstagedChanges = NO;
-}
-
-- (void) revertChanges
-{
- int ret = [[NSAlert alertWithMessageText:@"Revert changes"
- defaultButton:nil
- alternateButton:@"Cancel"
- otherButton:nil
- informativeTextWithFormat:@"Are you sure you wish to revert the changes in '%@'?\n\n You cannot undo this operation.", path] runModal];
-
- if (ret == NSAlertDefaultReturn)
- [self forceRevertChanges];
-}
-
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
{
return NO;
View
@@ -10,8 +10,6 @@
@interface PBFileChangesTableView : NSTableView {
- id controller;
}
-@property (retain) id controller;
@end
View
@@ -7,17 +7,14 @@
//
#import "PBFileChangesTableView.h"
-#import "PBGitCommitController.h"
@implementation PBFileChangesTableView
-@synthesize controller;
-
#pragma mark NSTableView overrides
- (NSMenu *)menuForEvent:(NSEvent *)theEvent
{
- if (controller)
- return [(PBGitCommitController *)controller menuForTable: self];
+ if ([self delegate])
+ return [[self delegate] menuForTable: self];
return nil;
}
View
@@ -17,21 +17,18 @@
IBOutlet NSTextView *commitMessageView;
IBOutlet NSArrayController *unstagedFilesController;
IBOutlet NSArrayController *cachedFilesController;
- NSString *status;
+ IBOutlet id indexController;
IBOutlet id webController;
+ NSString *status;
+
// We use busy as a count of active processes.
// 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;
-
- IBOutlet NSTableView *unstagedTable;
- IBOutlet NSTableView *cachedTable;
}
@property (retain) NSMutableArray *files;
@@ -44,8 +41,6 @@
- (void) readUnstagedFiles:(NSNotification *)notification;
- (void) stageHunk: (NSString *)hunk reverse:(BOOL)reverse;
-- (NSMenu *) menuForTable:(NSTableView *)table;
-
- (IBAction) refresh:(id) sender;
- (IBAction) commit:(id) sender;
@end
View
@@ -17,18 +17,6 @@ @implementation PBGitCommitController
- (void)awakeFromNib
{
[super awakeFromNib];
- self.busy = 0;
-
- amend = NO;
-
- [unstagedButtonCell setAction:@selector(rowClicked:)];
- [cachedButtonCell setAction:@selector(rowClicked:)];
-
- [unstagedTable setDoubleAction:@selector(tableClicked:)];
- [cachedTable setDoubleAction:@selector(tableClicked:)];
-
- [unstagedTable setController: self];
-
[self refresh:self];
[commitMessageView setTypingAttributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Monaco" size:12.0] forKey:NSFontAttributeName]];
@@ -133,7 +121,7 @@ - (void) readOtherFiles:(NSNotification *)notification;
for (NSString *line in lines) {
if ([line length] == 0)
continue;
- PBChangedFile *file =[[PBChangedFile alloc] initWithPath:line andRepository:repository];
+ PBChangedFile *file =[[PBChangedFile alloc] initWithPath:line];
file.status = NEW;
file.hasCachedChanges = NO;
file.hasUnstagedChanges = YES;
@@ -155,15 +143,22 @@ - (void) addFilesFromLines:(NSArray *)lines cached:(BOOL) cached
}
even = 0;
+ NSString *mode = [[fileStatus objectAtIndex:0] substringFromIndex:1];
+ NSString *sha = [fileStatus objectAtIndex:2];
+
BOOL isNew = YES;
// If the file is already added, we shouldn't add it again
// but rather update it to incorporate our changes
for (PBChangedFile *file in files) {
if ([file.path isEqualToString:line]) {
- if (cached)
+ if (cached) {
+ file.commitBlobSHA = sha;
+ file.commitBlobMode = mode;
file.hasCachedChanges = YES;
+ }
else
file.hasUnstagedChanges = YES;
+
isNew = NO;
break;
}
@@ -172,12 +167,17 @@ - (void) addFilesFromLines:(NSArray *)lines cached:(BOOL) cached
if (!isNew)
continue;
- PBChangedFile *file = [[PBChangedFile alloc] initWithPath:line andRepository:repository];
+ PBChangedFile *file = [[PBChangedFile alloc] initWithPath:line];
if ([[fileStatus objectAtIndex:4] isEqualToString:@"D"])
file.status = DELETED;
+ else if([[fileStatus objectAtIndex:0] isEqualToString:@":000000"])
+ file.status = NEW;
else
file.status = MODIFIED;
+ file.commitBlobSHA = sha;
+ file.commitBlobMode = mode;
+
file.hasCachedChanges = cached;
file.hasUnstagedChanges = !cached;
@@ -279,60 +279,6 @@ - (IBAction) commit:(id) sender
self.amend = NO;
}
-- (void) tableClicked:(NSTableView *) tableView
-{
- NSUInteger selectionIndex = [[tableView selectedRowIndexes] firstIndex];
- NSArrayController *controller = [tableView tag] == 0 ? unstagedFilesController : cachedFilesController;
- PBChangedFile *selectedItem = [[controller arrangedObjects] objectAtIndex:selectionIndex];
-
- if ([tableView tag] == 0)
- [selectedItem stageChanges];
- else
- [selectedItem unstageChangesAmend:amend];
-
-}
-
-- (void) rowClicked:(NSCell *)sender
-{
- NSTableView *tableView = (NSTableView *)[sender controlView];
- if([tableView numberOfSelectedRows] != 1)
- return;
- [self tableClicked: tableView];
-}
-
-- (void)tableView:(NSTableView*)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn*)tableColumn row:(int)rowIndex
-{
- [[tableColumn dataCell] setImage:[[[(([tableView tag] == 0) ? unstagedFilesController : cachedFilesController) arrangedObjects] objectAtIndex:rowIndex] icon]];
-}
-
-- (NSMenu *) menuForTable:(NSTableView *)table
-{
- NSUInteger selectionIndex = [[table selectedRowIndexes] firstIndex];
- PBChangedFile *selectedItem = [[unstagedFilesController arrangedObjects] objectAtIndex:selectionIndex];
-
- NSMenu *a = [[NSMenu alloc] init];
- NSMenuItem *stageItem = [[NSMenuItem alloc] initWithTitle:@"Stage Changes" action:@selector(stageChanges) keyEquivalent:@""];
- [stageItem setTarget:selectedItem];
- [a addItem:stageItem];
-
- // Do not add "revert" options for untracked files
- if (selectedItem.status == NEW)
- return a;
-
- NSMenuItem *revertItem = [[NSMenuItem alloc] initWithTitle:@"Revert Changes…" action:@selector(revertChanges) keyEquivalent:@""];
- [revertItem setTarget:selectedItem];
- [revertItem setAlternate:NO];
- [a addItem:revertItem];
-
- NSMenuItem *revertForceItem = [[NSMenuItem alloc] initWithTitle:@"Revert Changes" action:@selector(forceRevertChanges) keyEquivalent:@""];
- [revertForceItem setTarget:selectedItem];
- [revertForceItem setAlternate:YES];
- [revertForceItem setKeyEquivalentModifierMask:NSAlternateKeyMask];
- [a addItem:revertForceItem];
-
- return a;
-}
-
- (void) stageHunk:(NSString *)hunk reverse:(BOOL)reverse
{
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"apply", @"--cached", nil];
Oops, something went wrong.

0 comments on commit 0975426

Please sign in to comment.