Skip to content

Commit 0975426

Browse files
committed
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.
1 parent 874737f commit 0975426

14 files changed

+505
-219
lines changed

GitX.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
F50FE0E30E07BE9600854FCD /* PBGitRevisionCell.m in Sources */ = {isa = PBXBuildFile; fileRef = F50FE0E20E07BE9600854FCD /* PBGitRevisionCell.m */; };
3030
F513085B0E0740F2000C8BCD /* PBQLOutlineView.m in Sources */ = {isa = PBXBuildFile; fileRef = F513085A0E0740F2000C8BCD /* PBQLOutlineView.m */; };
3131
F5140DC90E8A8EB20091E9F3 /* RoundedRectangle.m in Sources */ = {isa = PBXBuildFile; fileRef = F5140DC80E8A8EB20091E9F3 /* RoundedRectangle.m */; };
32+
F523CEB60ED3399200DDD714 /* PBGitIndexController.m in Sources */ = {isa = PBXBuildFile; fileRef = F523CEB50ED3399200DDD714 /* PBGitIndexController.m */; };
3233
F52BCE030E84208300AA3741 /* PBGitHistoryView.xib in Resources */ = {isa = PBXBuildFile; fileRef = F52BCE020E84208300AA3741 /* PBGitHistoryView.xib */; };
3334
F52BCE070E84211300AA3741 /* PBGitHistoryController.m in Sources */ = {isa = PBXBuildFile; fileRef = F52BCE060E84211300AA3741 /* PBGitHistoryController.m */; };
3435
F53C4DF70E97FC630022AD59 /* PBGitBinary.m in Sources */ = {isa = PBXBuildFile; fileRef = F53C4DF60E97FC630022AD59 /* PBGitBinary.m */; };
@@ -141,6 +142,8 @@
141142
F513085A0E0740F2000C8BCD /* PBQLOutlineView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBQLOutlineView.m; sourceTree = "<group>"; };
142143
F5140DC70E8A8EB20091E9F3 /* RoundedRectangle.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RoundedRectangle.h; sourceTree = "<group>"; };
143144
F5140DC80E8A8EB20091E9F3 /* RoundedRectangle.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RoundedRectangle.m; sourceTree = "<group>"; };
145+
F523CEB40ED3399100DDD714 /* PBGitIndexController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitIndexController.h; sourceTree = "<group>"; };
146+
F523CEB50ED3399200DDD714 /* PBGitIndexController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitIndexController.m; sourceTree = "<group>"; };
144147
F52BCE020E84208300AA3741 /* PBGitHistoryView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = PBGitHistoryView.xib; sourceTree = "<group>"; };
145148
F52BCE050E84211300AA3741 /* PBGitHistoryController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitHistoryController.h; sourceTree = "<group>"; };
146149
F52BCE060E84211300AA3741 /* PBGitHistoryController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitHistoryController.m; sourceTree = "<group>"; };
@@ -503,6 +506,8 @@
503506
F59116E80E843BCB0072CCB1 /* PBGitCommitController.m */,
504507
F5E927FA0E883F0700056E75 /* PBWebChangesController.h */,
505508
F5E927FB0E883F0700056E75 /* PBWebChangesController.m */,
509+
F523CEB40ED3399100DDD714 /* PBGitIndexController.h */,
510+
F523CEB50ED3399200DDD714 /* PBGitIndexController.m */,
506511
);
507512
name = Commit;
508513
sourceTree = "<group>";
@@ -671,6 +676,7 @@
671676
F5FC41F40EBCBD4300191D80 /* PBGitXProtocol.m in Sources */,
672677
F574A2850EAE2EAC003F2CB1 /* PBRefController.m in Sources */,
673678
F5FC43FE0EBD08EE00191D80 /* PBRefMenuItem.m in Sources */,
679+
F523CEB60ED3399200DDD714 /* PBGitIndexController.m in Sources */,
674680
);
675681
runOnlyForDeploymentPostprocessing = 0;
676682
};

PBChangedFile.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,22 @@ typedef enum {
1919
NSString *path;
2020
BOOL hasCachedChanges;
2121
BOOL hasUnstagedChanges;
22-
__weak PBGitRepository *repository;
22+
23+
// Index and HEAD stuff, to be used to revert changes
24+
NSString *commitBlobSHA;
25+
NSString *commitBlobMode;
26+
2327
PBChangedFileStatus status;
2428
}
2529

2630

27-
@property (readonly) NSString *path;
31+
@property (copy) NSString *path, *commitBlobSHA, *commitBlobMode;
2832
@property (assign) PBChangedFileStatus status;
2933
@property (assign) BOOL hasCachedChanges;
3034
@property (assign) BOOL hasUnstagedChanges;
31-
- (NSImage *)icon;
32-
- (NSString *)cachedChangesAmend:(BOOL)amend;
33-
- (NSString *)unstagedChanges;
3435

35-
- (void) stageChanges;
36-
- (void) unstageChangesAmend:(BOOL)amend;
36+
- (NSImage *)icon;
37+
- (NSString *)indexInfo;
3738

38-
- (id) initWithPath:(NSString *)p andRepository:(PBGitRepository *)r;
39+
- (id) initWithPath:(NSString *)p;
3940
@end

PBChangedFile.m

Lines changed: 10 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,25 @@
1111

1212
@implementation PBChangedFile
1313

14-
@synthesize path, status, hasCachedChanges, hasUnstagedChanges;
14+
@synthesize path, status, hasCachedChanges, hasUnstagedChanges, commitBlobSHA, commitBlobMode;
1515

16-
- (id) initWithPath:(NSString *)p andRepository:(PBGitRepository *)r
16+
- (id) initWithPath:(NSString *)p
1717
{
18+
if (![super init])
19+
return nil;
20+
1821
path = p;
19-
repository = r;
2022
return self;
2123
}
2224

23-
- (NSString *) cachedChangesAmend:(BOOL) amend
25+
- (NSString *)indexInfo
2426
{
25-
if (amend)
26-
return [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"diff", @"--cached", @"HEAD^", @"--", path, nil]];
27-
28-
return [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"diff", @"--cached", @"--", path, nil]];
29-
}
30-
31-
- (NSString *)unstagedChanges
32-
{
33-
if (status == NEW) {
34-
NSStringEncoding encoding;
35-
NSError *error = nil;
36-
NSString *contents = [NSString stringWithContentsOfFile:[[repository workingDirectory] stringByAppendingPathComponent:path] usedEncoding:&encoding error:&error];
37-
if (error)
38-
return nil;
39-
40-
return contents;
41-
}
42-
43-
return [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"diff", @"--", path, nil]];
27+
if (!self.commitBlobSHA)
28+
return [NSString stringWithFormat:@"0 0000000000000000000000000000000000000000\t%@\0", self.path];
29+
else
30+
return [NSString stringWithFormat:@"%@ %@\t%@\0", self.commitBlobMode, self.commitBlobSHA, self.path];
4431
}
4532

46-
4733
- (NSImage *) icon
4834
{
4935
NSString *filename;
@@ -62,45 +48,6 @@ - (NSImage *) icon
6248
return [[NSImage alloc] initByReferencingFile: p];
6349
}
6450

65-
- (void) stageChanges
66-
{
67-
if (status == DELETED)
68-
[repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"rm", path, nil]];
69-
else
70-
[repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"add", path, nil]];
71-
72-
self.hasUnstagedChanges = NO;
73-
self.hasCachedChanges = YES;
74-
}
75-
76-
- (void) unstageChangesAmend:(BOOL) amend
77-
{
78-
if (amend)
79-
[repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"reset", @"HEAD^", @"--", path, nil]];
80-
else
81-
[repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"reset", @"--", path, nil]];
82-
self.hasCachedChanges = NO;
83-
self.hasUnstagedChanges = YES;
84-
}
85-
86-
- (void) forceRevertChanges
87-
{
88-
[repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"checkout", @"--", path, nil]];
89-
self.hasUnstagedChanges = NO;
90-
}
91-
92-
- (void) revertChanges
93-
{
94-
int ret = [[NSAlert alertWithMessageText:@"Revert changes"
95-
defaultButton:nil
96-
alternateButton:@"Cancel"
97-
otherButton:nil
98-
informativeTextWithFormat:@"Are you sure you wish to revert the changes in '%@'?\n\n You cannot undo this operation.", path] runModal];
99-
100-
if (ret == NSAlertDefaultReturn)
101-
[self forceRevertChanges];
102-
}
103-
10451
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
10552
{
10653
return NO;

PBFileChangesTableView.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111

1212
@interface PBFileChangesTableView : NSTableView {
13-
id controller;
1413
}
1514

16-
@property (retain) id controller;
1715
@end

PBFileChangesTableView.m

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@
77
//
88

99
#import "PBFileChangesTableView.h"
10-
#import "PBGitCommitController.h"
1110

1211
@implementation PBFileChangesTableView
1312

14-
@synthesize controller;
15-
1613
#pragma mark NSTableView overrides
1714
- (NSMenu *)menuForEvent:(NSEvent *)theEvent
1815
{
19-
if (controller)
20-
return [(PBGitCommitController *)controller menuForTable: self];
16+
if ([self delegate])
17+
return [[self delegate] menuForTable: self];
2118

2219
return nil;
2320
}

PBGitCommitController.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,18 @@
1717
IBOutlet NSTextView *commitMessageView;
1818
IBOutlet NSArrayController *unstagedFilesController;
1919
IBOutlet NSArrayController *cachedFilesController;
20-
NSString *status;
2120

21+
IBOutlet id indexController;
2222
IBOutlet id webController;
2323

24+
NSString *status;
25+
2426
// We use busy as a count of active processes.
2527
// You can increase it when your process start
2628
// And decrease it after you have finished.
2729
int busy;
2830
BOOL amend;
2931

30-
IBOutlet PBIconAndTextCell* unstagedButtonCell;
31-
IBOutlet PBIconAndTextCell* cachedButtonCell;
32-
33-
IBOutlet NSTableView *unstagedTable;
34-
IBOutlet NSTableView *cachedTable;
3532
}
3633

3734
@property (retain) NSMutableArray *files;
@@ -44,8 +41,6 @@
4441
- (void) readUnstagedFiles:(NSNotification *)notification;
4542
- (void) stageHunk: (NSString *)hunk reverse:(BOOL)reverse;
4643

47-
- (NSMenu *) menuForTable:(NSTableView *)table;
48-
4944
- (IBAction) refresh:(id) sender;
5045
- (IBAction) commit:(id) sender;
5146
@end

PBGitCommitController.m

Lines changed: 15 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,6 @@ @implementation PBGitCommitController
1717
- (void)awakeFromNib
1818
{
1919
[super awakeFromNib];
20-
self.busy = 0;
21-
22-
amend = NO;
23-
24-
[unstagedButtonCell setAction:@selector(rowClicked:)];
25-
[cachedButtonCell setAction:@selector(rowClicked:)];
26-
27-
[unstagedTable setDoubleAction:@selector(tableClicked:)];
28-
[cachedTable setDoubleAction:@selector(tableClicked:)];
29-
30-
[unstagedTable setController: self];
31-
3220
[self refresh:self];
3321

3422
[commitMessageView setTypingAttributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Monaco" size:12.0] forKey:NSFontAttributeName]];
@@ -133,7 +121,7 @@ - (void) readOtherFiles:(NSNotification *)notification;
133121
for (NSString *line in lines) {
134122
if ([line length] == 0)
135123
continue;
136-
PBChangedFile *file =[[PBChangedFile alloc] initWithPath:line andRepository:repository];
124+
PBChangedFile *file =[[PBChangedFile alloc] initWithPath:line];
137125
file.status = NEW;
138126
file.hasCachedChanges = NO;
139127
file.hasUnstagedChanges = YES;
@@ -155,15 +143,22 @@ - (void) addFilesFromLines:(NSArray *)lines cached:(BOOL) cached
155143
}
156144
even = 0;
157145

146+
NSString *mode = [[fileStatus objectAtIndex:0] substringFromIndex:1];
147+
NSString *sha = [fileStatus objectAtIndex:2];
148+
158149
BOOL isNew = YES;
159150
// If the file is already added, we shouldn't add it again
160151
// but rather update it to incorporate our changes
161152
for (PBChangedFile *file in files) {
162153
if ([file.path isEqualToString:line]) {
163-
if (cached)
154+
if (cached) {
155+
file.commitBlobSHA = sha;
156+
file.commitBlobMode = mode;
164157
file.hasCachedChanges = YES;
158+
}
165159
else
166160
file.hasUnstagedChanges = YES;
161+
167162
isNew = NO;
168163
break;
169164
}
@@ -172,12 +167,17 @@ - (void) addFilesFromLines:(NSArray *)lines cached:(BOOL) cached
172167
if (!isNew)
173168
continue;
174169

175-
PBChangedFile *file = [[PBChangedFile alloc] initWithPath:line andRepository:repository];
170+
PBChangedFile *file = [[PBChangedFile alloc] initWithPath:line];
176171
if ([[fileStatus objectAtIndex:4] isEqualToString:@"D"])
177172
file.status = DELETED;
173+
else if([[fileStatus objectAtIndex:0] isEqualToString:@":000000"])
174+
file.status = NEW;
178175
else
179176
file.status = MODIFIED;
180177

178+
file.commitBlobSHA = sha;
179+
file.commitBlobMode = mode;
180+
181181
file.hasCachedChanges = cached;
182182
file.hasUnstagedChanges = !cached;
183183

@@ -279,60 +279,6 @@ - (IBAction) commit:(id) sender
279279
self.amend = NO;
280280
}
281281

282-
- (void) tableClicked:(NSTableView *) tableView
283-
{
284-
NSUInteger selectionIndex = [[tableView selectedRowIndexes] firstIndex];
285-
NSArrayController *controller = [tableView tag] == 0 ? unstagedFilesController : cachedFilesController;
286-
PBChangedFile *selectedItem = [[controller arrangedObjects] objectAtIndex:selectionIndex];
287-
288-
if ([tableView tag] == 0)
289-
[selectedItem stageChanges];
290-
else
291-
[selectedItem unstageChangesAmend:amend];
292-
293-
}
294-
295-
- (void) rowClicked:(NSCell *)sender
296-
{
297-
NSTableView *tableView = (NSTableView *)[sender controlView];
298-
if([tableView numberOfSelectedRows] != 1)
299-
return;
300-
[self tableClicked: tableView];
301-
}
302-
303-
- (void)tableView:(NSTableView*)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn*)tableColumn row:(int)rowIndex
304-
{
305-
[[tableColumn dataCell] setImage:[[[(([tableView tag] == 0) ? unstagedFilesController : cachedFilesController) arrangedObjects] objectAtIndex:rowIndex] icon]];
306-
}
307-
308-
- (NSMenu *) menuForTable:(NSTableView *)table
309-
{
310-
NSUInteger selectionIndex = [[table selectedRowIndexes] firstIndex];
311-
PBChangedFile *selectedItem = [[unstagedFilesController arrangedObjects] objectAtIndex:selectionIndex];
312-
313-
NSMenu *a = [[NSMenu alloc] init];
314-
NSMenuItem *stageItem = [[NSMenuItem alloc] initWithTitle:@"Stage Changes" action:@selector(stageChanges) keyEquivalent:@""];
315-
[stageItem setTarget:selectedItem];
316-
[a addItem:stageItem];
317-
318-
// Do not add "revert" options for untracked files
319-
if (selectedItem.status == NEW)
320-
return a;
321-
322-
NSMenuItem *revertItem = [[NSMenuItem alloc] initWithTitle:@"Revert Changes…" action:@selector(revertChanges) keyEquivalent:@""];
323-
[revertItem setTarget:selectedItem];
324-
[revertItem setAlternate:NO];
325-
[a addItem:revertItem];
326-
327-
NSMenuItem *revertForceItem = [[NSMenuItem alloc] initWithTitle:@"Revert Changes" action:@selector(forceRevertChanges) keyEquivalent:@""];
328-
[revertForceItem setTarget:selectedItem];
329-
[revertForceItem setAlternate:YES];
330-
[revertForceItem setKeyEquivalentModifierMask:NSAlternateKeyMask];
331-
[a addItem:revertForceItem];
332-
333-
return a;
334-
}
335-
336282
- (void) stageHunk:(NSString *)hunk reverse:(BOOL)reverse
337283
{
338284
NSMutableArray *array = [NSMutableArray arrayWithObjects:@"apply", @"--cached", nil];

0 commit comments

Comments
 (0)