/
PBChangedFile.m
113 lines (93 loc) · 2.74 KB
/
PBChangedFile.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// PBChangedFile.m
// GitX
//
// Created by Pieter de Bie on 22-09-08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "PBChangedFile.h"
#import "PBEasyPipe.h"
@implementation PBChangedFile
@synthesize path, status, hasCachedChanges, hasUnstagedChanges;
- (id) initWithPath:(NSString *)p andRepository:(PBGitRepository *)r
{
path = p;
repository = r;
return self;
}
- (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]];
}
- (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]];
}
- (NSImage *) icon
{
NSString *filename;
switch (status) {
case NEW:
filename = @"new_file";
break;
case DELETED:
filename = @"deleted_file";
break;
default:
filename = @"empty_file";
break;
}
NSString *p = [[NSBundle mainBundle] pathForResource:filename ofType:@"png"];
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;
}
+ (BOOL)isKeyExcludedFromWebScript:(const char *)name {
return NO;
}
@end