-
Notifications
You must be signed in to change notification settings - Fork 603
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a way to retrieve values from .git/config files
This introduces the PBGitConfig class. It is KVC compliant as far as I can see, in that you can actually bind to it in IB and use ValueForKeyPath to retrieve values. It currently only handles strings; it should be possible to add functions to process booleans and numbers to it.
- Loading branch information
Showing
5 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// PBGitConfig.h | ||
// GitX | ||
// | ||
// Created by Pieter de Bie on 14-10-08. | ||
// Copyright 2008 Pieter de Bie. All rights reserved. | ||
// | ||
|
||
#import <Cocoa/Cocoa.h> | ||
#import "PBGitBinary.h" | ||
#import "PBEasyPipe.h" | ||
|
||
@interface PBGitConfig : NSObject { | ||
NSString *repositoryPath; | ||
} | ||
|
||
- init; | ||
- initWithRepository:(NSString *)path; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// | ||
// PBGitConfig.m | ||
// GitX | ||
// | ||
// Created by Pieter de Bie on 14-10-08. | ||
// Copyright 2008 Pieter de Bie. All rights reserved. | ||
// | ||
|
||
#import "PBGitConfig.h" | ||
|
||
|
||
@implementation PBGitConfig | ||
|
||
- init | ||
{ | ||
repositoryPath = nil; | ||
return self; | ||
} | ||
|
||
- initWithRepository:(NSString *)path | ||
{ | ||
repositoryPath = path; | ||
return self; | ||
} | ||
|
||
- (void) writeValue:(NSString *)value forKey:(NSString *)key global:(BOOL)global | ||
{ | ||
[self willChangeValueForKey:[key substringToIndex:[key rangeOfString:@"."].location]]; | ||
|
||
NSMutableArray *array = [NSMutableArray arrayWithObject:@"config"]; | ||
if (global) | ||
[array addObject:@"--global"]; | ||
else { | ||
[array addObject:@"-f"]; | ||
[array addObject:[repositoryPath stringByAppendingString:@"/config"]]; | ||
} | ||
|
||
[array addObject:key]; | ||
[array addObject:value]; | ||
|
||
int ret; | ||
[PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:array inDir:nil retValue:&ret]; | ||
if (ret) | ||
NSLog(@"Writing to config file failed!"); | ||
[self didChangeValueForKey:[key substringToIndex:[key rangeOfString:@"."].location]]; | ||
} | ||
|
||
- valueForKeyPath:(NSString *)path | ||
{ | ||
NSMutableArray *arguments = [NSMutableArray array]; | ||
if (repositoryPath) | ||
[arguments addObject:[NSString stringWithFormat:@"--git-dir=%@", repositoryPath]]; | ||
|
||
[arguments addObject:@"config"]; | ||
[arguments addObject:@"--get"]; | ||
[arguments addObject:path]; | ||
|
||
int ret; | ||
NSString *value = [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir:nil retValue:&ret]; | ||
|
||
if (ret) | ||
return nil; | ||
|
||
return value; | ||
} | ||
|
||
- (void) setValue:(id)value forKeyPath:(NSString *)path | ||
{ | ||
// Check if the config option is local. In that case, | ||
// write it local | ||
if (repositoryPath) { | ||
NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"config", @"-f", [repositoryPath stringByAppendingString:@"/config"], @"--get", path, nil]; | ||
int ret; | ||
[PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir:nil retValue:&ret]; | ||
|
||
if (!ret) // it's local | ||
return [self writeValue:value forKey:path global:NO]; | ||
} | ||
|
||
// Check if it exists globally. In that case, write it as a global | ||
|
||
NSArray *arguments = [NSArray arrayWithObjects:@"config", @"--global", @"--get", path]; | ||
int ret; | ||
[PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir:nil retValue:&ret]; | ||
if (!ret) // It exists globally | ||
return [self writeValue:value forKey:path global:YES]; | ||
|
||
// It doesn't exist at all. Write it locally. | ||
[self writeValue:value forKey:path global:NO]; | ||
} | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters