Skip to content

Commit

Permalink
GitIndex: Add methods to stage and unstage files
Browse files Browse the repository at this point in the history
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
pieter committed Sep 13, 2009
1 parent 64a52ad commit 35a4dc3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
4 changes: 2 additions & 2 deletions PBGitIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
66 changes: 66 additions & 0 deletions PBGitIndex.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 35a4dc3

Please sign in to comment.