Merge branch 'pu/pb/index_refactor'
[GitX.git] / PBGitConfig.m
blob9166cc4c870faf5d4e0bd2e11c94394d10e1e019
1 //
2 //  PBGitConfig.m
3 //  GitX
4 //
5 //  Created by Pieter de Bie on 14-10-08.
6 //  Copyright 2008 Pieter de Bie. All rights reserved.
7 //
9 #import "PBGitConfig.h"
12 @implementation PBGitConfig
14 - init
16         repositoryPath = nil;
17         return self;
20 - initWithRepository:(NSString *)path
22         repositoryPath = path;
23         return self;
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"];
31         if (global)
32                 [array addObject:@"--global"];
33         else {
34                 [array addObject:@"-f"];
35                 [array addObject:[repositoryPath stringByAppendingPathComponent:@"config"]];
36         }
38         [array addObject:key];
39         [array addObject:value];
41         int ret;
42         [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:array inDir:nil retValue:&ret];
43         if (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];
51         if (repositoryPath)
52                 [arguments addObject:[NSString stringWithFormat:@"--git-dir=%@", repositoryPath]];
54         [arguments addObject:@"config"];
55         [arguments addObject:@"--get"];
56         [arguments addObject:path];
58         int ret;
59         NSString *value = [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir:nil retValue:&ret];
61         if (ret)
62                 return nil;
64         return value;
67 - (void) setValue:(id)value forKeyPath:(NSString *)path
69         // Check if the config option is local. In that case,
70         // write it local
71         if (repositoryPath) {
72                 NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"config", @"-f", [repositoryPath stringByAppendingPathComponent:@"config"], @"--get", path, nil];
73                 int ret;
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];
78         }
80         // Check if it exists globally. In that case, write it as a global
82         NSArray *arguments = [NSArray arrayWithObjects:@"config", @"--global", @"--get", path, nil];
83         int ret;
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];
91 @end