5 // Created by Pieter de Bie on 14-10-08.
6 // Copyright 2008 Pieter de Bie. All rights reserved.
9 #import "PBGitConfig.h"
12 @implementation PBGitConfig
20 - initWithRepository:(NSString *)path
22 repositoryPath = path;
26 - (void) writeValue:(NSString *)value forKey:(NSString *)key global:(BOOL)global
28 [self willChangeValueForKey:[key substringToIndex:[key rangeOfString:@"."].location]];
30 NSMutableArray *array = [NSMutableArray arrayWithObject:@"config"];
32 [array addObject:@"--global"];
34 [array addObject:@"-f"];
35 [array addObject:[repositoryPath stringByAppendingPathComponent:@"config"]];
38 [array addObject:key];
39 [array addObject:value];
42 [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:array inDir:nil retValue:&ret];
44 NSLog(@"Writing to config file failed!");
45 [self didChangeValueForKey:[key substringToIndex:[key rangeOfString:@"."].location]];
48 - valueForKeyPath:(NSString *)path
50 NSMutableArray *arguments = [NSMutableArray array];
52 [arguments addObject:[NSString stringWithFormat:@"--git-dir=%@", repositoryPath]];
54 [arguments addObject:@"config"];
55 [arguments addObject:@"--get"];
56 [arguments addObject:path];
59 NSString *value = [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir:nil retValue:&ret];
67 - (void) setValue:(id)value forKeyPath:(NSString *)path
69 // Check if the config option is local. In that case,
72 NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"config", @"-f", [repositoryPath stringByAppendingPathComponent:@"config"], @"--get", path, nil];
74 [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir:nil retValue:&ret];
76 if (!ret) // it's local
77 return [self writeValue:value forKey:path global:NO];
80 // Check if it exists globally. In that case, write it as a global
82 NSArray *arguments = [NSArray arrayWithObjects:@"config", @"--global", @"--get", path, nil];
84 [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir:nil retValue:&ret];
85 if (!ret) // It exists globally
86 return [self writeValue:value forKey:path global:YES];
88 // It doesn't exist at all. Write it locally.
89 [self writeValue:value forKey:path global:NO];