Skip to content

Commit

Permalink
Add a way to retrieve values from .git/config files
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pieter committed Oct 13, 2008
1 parent b816165 commit afb3d45
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
6 changes: 6 additions & 0 deletions GitX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -176,6 +177,8 @@
F5C007740E731B48007B84B2 /* PBGitRef.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitRef.m; sourceTree = "<group>"; };
F5C6F68B0E65FF9300478D97 /* PBGitLane.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitLane.h; sourceTree = "<group>"; };
F5C6F68C0E65FF9300478D97 /* PBGitLane.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitLane.m; sourceTree = "<group>"; };
F5D2DC850EA401A80034AD24 /* PBGitConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitConfig.h; sourceTree = "<group>"; };
F5D2DC860EA401A80034AD24 /* PBGitConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitConfig.m; sourceTree = "<group>"; };
F5DFFA6A0E075D8800617813 /* PBEasyFS.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBEasyFS.h; sourceTree = "<group>"; };
F5DFFA6B0E075D8800617813 /* PBEasyFS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBEasyFS.m; sourceTree = "<group>"; };
F5E926040E8827D300056E75 /* PBViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBViewController.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -336,6 +339,8 @@
F5945E160E02B0C200706420 /* PBGitRepository.m */,
F53C4DF50E97FC630022AD59 /* PBGitBinary.h */,
F53C4DF60E97FC630022AD59 /* PBGitBinary.m */,
F5D2DC850EA401A80034AD24 /* PBGitConfig.h */,
F5D2DC860EA401A80034AD24 /* PBGitConfig.m */,
);
name = Git;
sourceTree = "<group>";
Expand Down Expand Up @@ -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;
};
Expand Down
19 changes: 19 additions & 0 deletions PBGitConfig.h
Original file line number Diff line number Diff line change
@@ -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 <Cocoa/Cocoa.h>
#import "PBGitBinary.h"
#import "PBEasyPipe.h"

@interface PBGitConfig : NSObject {
NSString *repositoryPath;
}

- init;
- initWithRepository:(NSString *)path;
@end
91 changes: 91 additions & 0 deletions PBGitConfig.m
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions PBGitRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
#import <Cocoa/Cocoa.h>
#import "PBGitRevList.h"
#import "PBGitRevSpecifier.h"
#import "PBGitConfig.h"

extern NSString* PBGitRepositoryErrorDomain;

@interface PBGitRepository : NSDocument {
PBGitRevList* revisionList;
PBGitConfig *config;

BOOL hasChanged;
NSMutableArray* branches;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion PBGitRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

@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
{
Expand Down Expand Up @@ -114,6 +114,7 @@ - (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)type

- (void) setup
{
config = [[PBGitConfig alloc] initWithRepository:self.fileURL.path];
self.branches = [NSMutableArray array];
[self reloadRefs];
revisionList = [[PBGitRevList alloc] initWithRepository:self];
Expand Down

0 comments on commit afb3d45

Please sign in to comment.