Skip to content

Commit afb3d45

Browse files
committed
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.
1 parent b816165 commit afb3d45

File tree

5 files changed

+121
-1
lines changed

5 files changed

+121
-1
lines changed

GitX.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
F5B721C40E05CF7E00AF29DC /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = F5B721C20E05CF7E00AF29DC /* MainMenu.xib */; };
5959
F5C007750E731B48007B84B2 /* PBGitRef.m in Sources */ = {isa = PBXBuildFile; fileRef = F5C007740E731B48007B84B2 /* PBGitRef.m */; };
6060
F5C6F68D0E65FF9300478D97 /* PBGitLane.m in Sources */ = {isa = PBXBuildFile; fileRef = F5C6F68C0E65FF9300478D97 /* PBGitLane.m */; };
61+
F5D2DC870EA401A80034AD24 /* PBGitConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = F5D2DC860EA401A80034AD24 /* PBGitConfig.m */; };
6162
F5DFFA6C0E075D8800617813 /* PBEasyFS.m in Sources */ = {isa = PBXBuildFile; fileRef = F5DFFA6B0E075D8800617813 /* PBEasyFS.m */; };
6263
F5E926060E8827D300056E75 /* PBViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F5E926050E8827D300056E75 /* PBViewController.m */; };
6364
F5E927F80E883E7200056E75 /* PBChangedFile.m in Sources */ = {isa = PBXBuildFile; fileRef = F5E927F70E883E7200056E75 /* PBChangedFile.m */; };
@@ -176,6 +177,8 @@
176177
F5C007740E731B48007B84B2 /* PBGitRef.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitRef.m; sourceTree = "<group>"; };
177178
F5C6F68B0E65FF9300478D97 /* PBGitLane.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitLane.h; sourceTree = "<group>"; };
178179
F5C6F68C0E65FF9300478D97 /* PBGitLane.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitLane.m; sourceTree = "<group>"; };
180+
F5D2DC850EA401A80034AD24 /* PBGitConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitConfig.h; sourceTree = "<group>"; };
181+
F5D2DC860EA401A80034AD24 /* PBGitConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitConfig.m; sourceTree = "<group>"; };
179182
F5DFFA6A0E075D8800617813 /* PBEasyFS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBEasyFS.h; sourceTree = "<group>"; };
180183
F5DFFA6B0E075D8800617813 /* PBEasyFS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBEasyFS.m; sourceTree = "<group>"; };
181184
F5E926040E8827D300056E75 /* PBViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBViewController.h; sourceTree = "<group>"; };
@@ -336,6 +339,8 @@
336339
F5945E160E02B0C200706420 /* PBGitRepository.m */,
337340
F53C4DF50E97FC630022AD59 /* PBGitBinary.h */,
338341
F53C4DF60E97FC630022AD59 /* PBGitBinary.m */,
342+
F5D2DC850EA401A80034AD24 /* PBGitConfig.h */,
343+
F5D2DC860EA401A80034AD24 /* PBGitConfig.m */,
339344
);
340345
name = Git;
341346
sourceTree = "<group>";
@@ -596,6 +601,7 @@
596601
F56244090E9684B0002B6C44 /* PBUnsortableTableHeader.m in Sources */,
597602
F53C4DF70E97FC630022AD59 /* PBGitBinary.m in Sources */,
598603
F593DF780E9E636C003A8559 /* PBFileChangesTableView.m in Sources */,
604+
F5D2DC870EA401A80034AD24 /* PBGitConfig.m in Sources */,
599605
);
600606
runOnlyForDeploymentPostprocessing = 0;
601607
};

PBGitConfig.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// PBGitConfig.h
3+
// GitX
4+
//
5+
// Created by Pieter de Bie on 14-10-08.
6+
// Copyright 2008 Pieter de Bie. All rights reserved.
7+
//
8+
9+
#import <Cocoa/Cocoa.h>
10+
#import "PBGitBinary.h"
11+
#import "PBEasyPipe.h"
12+
13+
@interface PBGitConfig : NSObject {
14+
NSString *repositoryPath;
15+
}
16+
17+
- init;
18+
- initWithRepository:(NSString *)path;
19+
@end

PBGitConfig.m

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
//
8+
9+
#import "PBGitConfig.h"
10+
11+
12+
@implementation PBGitConfig
13+
14+
- init
15+
{
16+
repositoryPath = nil;
17+
return self;
18+
}
19+
20+
- initWithRepository:(NSString *)path
21+
{
22+
repositoryPath = path;
23+
return self;
24+
}
25+
26+
- (void) writeValue:(NSString *)value forKey:(NSString *)key global:(BOOL)global
27+
{
28+
[self willChangeValueForKey:[key substringToIndex:[key rangeOfString:@"."].location]];
29+
30+
NSMutableArray *array = [NSMutableArray arrayWithObject:@"config"];
31+
if (global)
32+
[array addObject:@"--global"];
33+
else {
34+
[array addObject:@"-f"];
35+
[array addObject:[repositoryPath stringByAppendingString:@"/config"]];
36+
}
37+
38+
[array addObject:key];
39+
[array addObject:value];
40+
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]];
46+
}
47+
48+
- valueForKeyPath:(NSString *)path
49+
{
50+
NSMutableArray *arguments = [NSMutableArray array];
51+
if (repositoryPath)
52+
[arguments addObject:[NSString stringWithFormat:@"--git-dir=%@", repositoryPath]];
53+
54+
[arguments addObject:@"config"];
55+
[arguments addObject:@"--get"];
56+
[arguments addObject:path];
57+
58+
int ret;
59+
NSString *value = [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir:nil retValue:&ret];
60+
61+
if (ret)
62+
return nil;
63+
64+
return value;
65+
}
66+
67+
- (void) setValue:(id)value forKeyPath:(NSString *)path
68+
{
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 stringByAppendingString:@"/config"], @"--get", path, nil];
73+
int ret;
74+
[PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir:nil retValue:&ret];
75+
76+
if (!ret) // it's local
77+
return [self writeValue:value forKey:path global:NO];
78+
}
79+
80+
// Check if it exists globally. In that case, write it as a global
81+
82+
NSArray *arguments = [NSArray arrayWithObjects:@"config", @"--global", @"--get", path];
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];
87+
88+
// It doesn't exist at all. Write it locally.
89+
[self writeValue:value forKey:path global:NO];
90+
}
91+
@end

PBGitRepository.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
#import <Cocoa/Cocoa.h>
1010
#import "PBGitRevList.h"
1111
#import "PBGitRevSpecifier.h"
12+
#import "PBGitConfig.h"
1213

1314
extern NSString* PBGitRepositoryErrorDomain;
1415

1516
@interface PBGitRepository : NSDocument {
1617
PBGitRevList* revisionList;
18+
PBGitConfig *config;
1719

1820
BOOL hasChanged;
1921
NSMutableArray* branches;
@@ -54,6 +56,7 @@ extern NSString* PBGitRepositoryErrorDomain;
5456

5557
@property (assign) BOOL hasChanged;
5658
@property (readonly) NSWindowController *windowController;
59+
@property (readonly) PBGitConfig *config;
5760
@property (retain) PBGitRevList* revisionList;
5861
@property (assign) NSMutableArray* branches;
5962
@property (assign) NSIndexSet* currentBranch;

PBGitRepository.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
@implementation PBGitRepository
2222

23-
@synthesize revisionList, branches, currentBranch, refs, hasChanged;
23+
@synthesize revisionList, branches, currentBranch, refs, hasChanged, config;
2424

2525
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
2626
{
@@ -114,6 +114,7 @@ - (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)type
114114

115115
- (void) setup
116116
{
117+
config = [[PBGitConfig alloc] initWithRepository:self.fileURL.path];
117118
self.branches = [NSMutableArray array];
118119
[self reloadRefs];
119120
revisionList = [[PBGitRevList alloc] initWithRepository:self];

0 commit comments

Comments
 (0)