forked from laullon/gitx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PBGitConfig.m
125 lines (99 loc) · 3.51 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// 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;
}
- initWithRepositoryPath:(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)
DLog(@"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];
}
/**
runs `git config -l` returning a dict of key-value pairs from the result
passing nil as directory passes '--global' flag
*/
- (NSDictionary*) listConfigValuesInDir:(NSString*)inDir
{
NSArray* arguments;
if (inDir == nil) {
arguments = [NSArray arrayWithObjects:@"config", @"--global", @"-l", @"-z", nil];
} else {
arguments = [NSArray arrayWithObjects:@"config", @"-l", @"-z", nil];
}
int ret = 1;
NSString* output = [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir:inDir retValue:&ret];
NSMutableDictionary *result = [NSMutableDictionary dictionary];
if (ret==0) {
NSArray *lines = [output componentsSeparatedByString:@"\0"];
for (NSString* line in lines) {
if([line length] == 0) continue;
NSRange equalsPos = [line rangeOfString:@"\n"];
NSString* key = [line substringToIndex:equalsPos.location];
NSString* value = [line substringFromIndex:equalsPos.location+1];
[result setObject:value forKey:key];
}
}
return [NSDictionary dictionaryWithDictionary:result];
}
@end