-
Notifications
You must be signed in to change notification settings - Fork 601
/
PBGitConfig.m
91 lines (72 loc) · 2.39 KB
/
PBGitConfig.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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 stringByAppendingPathComponent:@"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 stringByAppendingPathComponent:@"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, nil];
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