From afb3d45656887ee1bef52b7f25e3781391346da5 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Tue, 14 Oct 2008 00:59:43 +0200 Subject: [PATCH] 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. --- GitX.xcodeproj/project.pbxproj | 6 +++ PBGitConfig.h | 19 +++++++ PBGitConfig.m | 91 ++++++++++++++++++++++++++++++++++ PBGitRepository.h | 3 ++ PBGitRepository.m | 3 +- 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 PBGitConfig.h create mode 100644 PBGitConfig.m diff --git a/GitX.xcodeproj/project.pbxproj b/GitX.xcodeproj/project.pbxproj index edaa979..af8bcee 100644 --- a/GitX.xcodeproj/project.pbxproj +++ b/GitX.xcodeproj/project.pbxproj @@ -58,6 +58,7 @@ F5B721C40E05CF7E00AF29DC /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = F5B721C20E05CF7E00AF29DC /* MainMenu.xib */; }; F5C007750E731B48007B84B2 /* PBGitRef.m in Sources */ = {isa = PBXBuildFile; fileRef = F5C007740E731B48007B84B2 /* PBGitRef.m */; }; F5C6F68D0E65FF9300478D97 /* PBGitLane.m in Sources */ = {isa = PBXBuildFile; fileRef = F5C6F68C0E65FF9300478D97 /* PBGitLane.m */; }; + F5D2DC870EA401A80034AD24 /* PBGitConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = F5D2DC860EA401A80034AD24 /* PBGitConfig.m */; }; F5DFFA6C0E075D8800617813 /* PBEasyFS.m in Sources */ = {isa = PBXBuildFile; fileRef = F5DFFA6B0E075D8800617813 /* PBEasyFS.m */; }; F5E926060E8827D300056E75 /* PBViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F5E926050E8827D300056E75 /* PBViewController.m */; }; F5E927F80E883E7200056E75 /* PBChangedFile.m in Sources */ = {isa = PBXBuildFile; fileRef = F5E927F70E883E7200056E75 /* PBChangedFile.m */; }; @@ -176,6 +177,8 @@ F5C007740E731B48007B84B2 /* PBGitRef.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitRef.m; sourceTree = ""; }; F5C6F68B0E65FF9300478D97 /* PBGitLane.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitLane.h; sourceTree = ""; }; F5C6F68C0E65FF9300478D97 /* PBGitLane.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitLane.m; sourceTree = ""; }; + F5D2DC850EA401A80034AD24 /* PBGitConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitConfig.h; sourceTree = ""; }; + F5D2DC860EA401A80034AD24 /* PBGitConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitConfig.m; sourceTree = ""; }; F5DFFA6A0E075D8800617813 /* PBEasyFS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBEasyFS.h; sourceTree = ""; }; F5DFFA6B0E075D8800617813 /* PBEasyFS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBEasyFS.m; sourceTree = ""; }; F5E926040E8827D300056E75 /* PBViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBViewController.h; sourceTree = ""; }; @@ -336,6 +339,8 @@ F5945E160E02B0C200706420 /* PBGitRepository.m */, F53C4DF50E97FC630022AD59 /* PBGitBinary.h */, F53C4DF60E97FC630022AD59 /* PBGitBinary.m */, + F5D2DC850EA401A80034AD24 /* PBGitConfig.h */, + F5D2DC860EA401A80034AD24 /* PBGitConfig.m */, ); name = Git; sourceTree = ""; @@ -596,6 +601,7 @@ F56244090E9684B0002B6C44 /* PBUnsortableTableHeader.m in Sources */, F53C4DF70E97FC630022AD59 /* PBGitBinary.m in Sources */, F593DF780E9E636C003A8559 /* PBFileChangesTableView.m in Sources */, + F5D2DC870EA401A80034AD24 /* PBGitConfig.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/PBGitConfig.h b/PBGitConfig.h new file mode 100644 index 0000000..9875a3c --- /dev/null +++ b/PBGitConfig.h @@ -0,0 +1,19 @@ +// +// PBGitConfig.h +// GitX +// +// Created by Pieter de Bie on 14-10-08. +// Copyright 2008 Pieter de Bie. All rights reserved. +// + +#import +#import "PBGitBinary.h" +#import "PBEasyPipe.h" + +@interface PBGitConfig : NSObject { + NSString *repositoryPath; +} + +- init; +- initWithRepository:(NSString *)path; +@end diff --git a/PBGitConfig.m b/PBGitConfig.m new file mode 100644 index 0000000..8b51013 --- /dev/null +++ b/PBGitConfig.m @@ -0,0 +1,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 stringByAppendingString:@"/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 stringByAppendingString:@"/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]; + 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 diff --git a/PBGitRepository.h b/PBGitRepository.h index 9d743d9..8f14d17 100644 --- a/PBGitRepository.h +++ b/PBGitRepository.h @@ -9,11 +9,13 @@ #import #import "PBGitRevList.h" #import "PBGitRevSpecifier.h" +#import "PBGitConfig.h" extern NSString* PBGitRepositoryErrorDomain; @interface PBGitRepository : NSDocument { PBGitRevList* revisionList; + PBGitConfig *config; BOOL hasChanged; NSMutableArray* branches; @@ -54,6 +56,7 @@ extern NSString* PBGitRepositoryErrorDomain; @property (assign) BOOL hasChanged; @property (readonly) NSWindowController *windowController; +@property (readonly) PBGitConfig *config; @property (retain) PBGitRevList* revisionList; @property (assign) NSMutableArray* branches; @property (assign) NSIndexSet* currentBranch; diff --git a/PBGitRepository.m b/PBGitRepository.m index 5ccb5e4..60020bf 100644 --- a/PBGitRepository.m +++ b/PBGitRepository.m @@ -20,7 +20,7 @@ NSString* PBGitRepositoryErrorDomain = @"GitXErrorDomain"; @implementation PBGitRepository -@synthesize revisionList, branches, currentBranch, refs, hasChanged; +@synthesize revisionList, branches, currentBranch, refs, hasChanged, config; - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError { @@ -114,6 +114,7 @@ NSString* PBGitRepositoryErrorDomain = @"GitXErrorDomain"; - (void) setup { + config = [[PBGitConfig alloc] initWithRepository:self.fileURL.path]; self.branches = [NSMutableArray array]; [self reloadRefs]; revisionList = [[PBGitRevList alloc] initWithRepository:self];