Permalink
Browse files

GitIndex: Add methods to stage and unstage files

These are mostly copies from PBGitIndexController, and
they can be refactored to a common method. I'm not sure of a name
for that yet, so I'll keep it like this for now :)
  • Loading branch information...
1 parent 64a52ad commit 35a4dc37e2181a029a5326085f421f8fec849951 @pieter committed Sep 13, 2009
Showing with 68 additions and 2 deletions.
  1. +2 −2 PBGitIndex.h
  2. +66 −0 PBGitIndex.m
View
@@ -44,8 +44,8 @@
//- (void)commit;
// Inter-file changes:
-//- (void)stageFiles:(NSArray *)files;
-//- (void)unstageFiles:(NSArray *)files;
+- (BOOL)stageFiles:(NSArray *)stageFiles;
+- (BOOL)unstageFiles:(NSArray *)unstageFiles;
// Intra-file changes
//- (void)applyPatch:(NSString *)hunk stage:(BOOL)stage reverse:(BOOL)reverse;
View
@@ -118,6 +118,71 @@ - (NSString *) parentTree
return parent;
}
+- (BOOL)stageFiles:(NSArray *)stageFiles
+{
+ // Input string for update-index
+ // This will be a list of filenames that
+ // should be updated. It's similar to
+ // "git add -- <files>
+ NSMutableString *input = [NSMutableString string];
+
+ for (PBChangedFile *file in stageFiles) {
+ [input appendFormat:@"%@\0", file.path];
+ }
+
+ int ret = 1;
+ [repository outputForArguments:[NSArray arrayWithObjects:@"update-index", @"--add", @"--remove", @"-z", @"--stdin", nil]
+ inputString:input
+ retValue:&ret];
+
+ if (ret) {
+ // FIXME: failed notification?
+ NSLog(@"Error when updating index. Retvalue: %i", ret);
+ return NO;
+ }
+
+ // TODO: Stop Tracking
+ for (PBChangedFile *file in stageFiles)
+ {
+ file.hasUnstagedChanges = NO;
+ file.hasStagedChanges = YES;
+ }
+ // TODO: Resume tracking
+ return YES;
+}
+
+// TODO: Refactor with above. What's a better name for this?
+- (BOOL)unstageFiles:(NSArray *)unstageFiles
+{
+ NSMutableString *input = [NSMutableString string];
+
+ for (PBChangedFile *file in unstageFiles) {
+ [input appendString:[file indexInfo]];
+ }
+
+ int ret = 1;
+ [repository outputForArguments:[NSArray arrayWithObjects:@"update-index", @"-z", @"--index-info", nil]
+ inputString:input
+ retValue:&ret];
+
+ if (ret)
+ {
+ // FIXME: Failed notification
+ NSLog(@"Error when updating index. Retvalue: %i", ret);
+ return NO;
+ }
+
+ // TODO: stop tracking
+ for (PBChangedFile *file in unstageFiles)
+ {
+ file.hasUnstagedChanges = YES;
+ file.hasStagedChanges = NO;
+ }
+ // TODO: resume tracking
+
+ return YES;
+}
+
- (NSString *)diffForFile:(PBChangedFile *)file staged:(BOOL)staged contextLines:(NSUInteger)context
{
NSString *parameter = [NSString stringWithFormat:@"-U%u", context];
@@ -147,6 +212,7 @@ - (NSString *)diffForFile:(PBChangedFile *)file staged:(BOOL)staged contextLines
return [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"diff-files", parameter, @"--", file.path, nil]];
}
+
# pragma mark WebKit Accessibility
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector

0 comments on commit 35a4dc3

Please sign in to comment.