From 08757ad610fb12e727ae5ce11cb7d2085fe86211 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Mon, 16 Jun 2008 01:52:12 +0200 Subject: [PATCH] Add easy pipe handling library. Use it to find real git repository path This adds a new class, PBEasyPipe, which can do most of the pipe handling in an easy way. We use this to call `git rev-parse --git-dir` to find our current git repository. This means that we can now call GitX within a subdirectory :) --- ApplicationController.m | 8 +++-- GitX.xcodeproj/project.pbxproj | 22 +++++++++++--- PBEasyPipe.h | 22 ++++++++++++++ PBEasyPipe.m | 53 ++++++++++++++++++++++++++++++++++ PBGitRepository.m | 45 ++++++++++++----------------- 5 files changed, 116 insertions(+), 34 deletions(-) create mode 100644 PBEasyPipe.h create mode 100644 PBEasyPipe.m diff --git a/ApplicationController.m b/ApplicationController.m index 1edba66..c1ce453 100644 --- a/ApplicationController.m +++ b/ApplicationController.m @@ -14,14 +14,16 @@ - (ApplicationController*) init { - char* a = getenv("PWD"); self.selectedTab = [NSNumber numberWithInt:0]; + + // Find the current repository + char* a = getenv("PWD"); NSString* path; if (a == nil) // TODO: Add a check here to see if the directory exists. - path = @"/Users/Pieter/projects/bonnenteller/.git"; + path = @"/Users/Pieter/projects/Cocoa/GitTest/.git"; else - path = [[NSString stringWithCString:a] stringByAppendingString:@"/.git"]; + path = [NSString stringWithCString:a]; self.repository = [PBGitRepository repositoryWithPath:path]; return self; diff --git a/GitX.xcodeproj/project.pbxproj b/GitX.xcodeproj/project.pbxproj index bbd392c..b7d90fa 100644 --- a/GitX.xcodeproj/project.pbxproj +++ b/GitX.xcodeproj/project.pbxproj @@ -23,6 +23,7 @@ F565265A0E03E71B00F03B52 /* commit.html in Resources */ = {isa = PBXBuildFile; fileRef = F56526590E03E71B00F03B52 /* commit.html */; }; F57ABE0B0E0442DD00A088B8 /* commit.js in Resources */ = {isa = PBXBuildFile; fileRef = F57ABDDE0E0441DE00A088B8 /* commit.js */; }; F57ABE2B0E04435100A088B8 /* prototype.js in Resources */ = {isa = PBXBuildFile; fileRef = F57ABE180E04431D00A088B8 /* prototype.js */; }; + F57CC3910E05DDF2000472E2 /* PBEasyPipe.m in Sources */ = {isa = PBXBuildFile; fileRef = F57CC3900E05DDF2000472E2 /* PBEasyPipe.m */; }; F58A8F280E043698007E3FC0 /* commits.css in Resources */ = {isa = PBXBuildFile; fileRef = F58A8F270E043698007E3FC0 /* commits.css */; }; F5945E170E02B0C200706420 /* PBGitRepository.m in Sources */ = {isa = PBXBuildFile; fileRef = F5945E160E02B0C200706420 /* PBGitRepository.m */; }; F5B721C40E05CF7E00AF29DC /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = F5B721C20E05CF7E00AF29DC /* MainMenu.xib */; }; @@ -56,6 +57,8 @@ F56526590E03E71B00F03B52 /* commit.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; name = commit.html; path = html/commit.html; sourceTree = ""; }; F57ABDDE0E0441DE00A088B8 /* commit.js */ = {isa = PBXFileReference; explicitFileType = sourcecode.javascript; fileEncoding = 4; name = commit.js; path = html/commit.js; sourceTree = ""; }; F57ABE180E04431D00A088B8 /* prototype.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = prototype.js; path = html/prototype.js; sourceTree = ""; }; + F57CC38F0E05DDF2000472E2 /* PBEasyPipe.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBEasyPipe.h; sourceTree = ""; }; + F57CC3900E05DDF2000472E2 /* PBEasyPipe.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBEasyPipe.m; sourceTree = ""; }; F58A8F270E043698007E3FC0 /* commits.css */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.css; name = commits.css; path = html/commits.css; sourceTree = ""; }; F5945E150E02B0C200706420 /* PBGitRepository.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitRepository.h; sourceTree = ""; }; F5945E160E02B0C200706420 /* PBGitRepository.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitRepository.m; sourceTree = ""; }; @@ -78,12 +81,8 @@ 080E96DDFE201D6D7F000001 /* Classes */ = { isa = PBXGroup; children = ( - F56526290E03D89B00F03B52 /* PBWebGitController.h */, - F565262A0E03D89B00F03B52 /* PBWebGitController.m */, F56524B90E02D22D00F03B52 /* NSFileHandleExt.m */, F56524BA0E02D22D00F03B52 /* NSFileHandleExt.h */, - 77C8280B06725ACE000B614F /* ApplicationController.h */, - 77C8280C06725ACE000B614F /* ApplicationController.m */, ); name = Classes; sourceTree = ""; @@ -118,6 +117,7 @@ 29B97314FDCFA39411CA2CEA /* GitTest */ = { isa = PBXGroup; children = ( + F57CC3850E05DDC1000472E2 /* Controllers */, F561777C0E05C88E001DCD79 /* DetailView.png */, F56174540E05887E001DCD79 /* Git */, F58A8F250E04368A007E3FC0 /* HTML */, @@ -180,6 +180,19 @@ name = Git; sourceTree = ""; }; + F57CC3850E05DDC1000472E2 /* Controllers */ = { + isa = PBXGroup; + children = ( + F56526290E03D89B00F03B52 /* PBWebGitController.h */, + F565262A0E03D89B00F03B52 /* PBWebGitController.m */, + 77C8280B06725ACE000B614F /* ApplicationController.h */, + 77C8280C06725ACE000B614F /* ApplicationController.m */, + F57CC38F0E05DDF2000472E2 /* PBEasyPipe.h */, + F57CC3900E05DDF2000472E2 /* PBEasyPipe.m */, + ); + name = Controllers; + sourceTree = ""; + }; F58A8F250E04368A007E3FC0 /* HTML */ = { isa = PBXGroup; children = ( @@ -263,6 +276,7 @@ F56524F00E02D45200F03B52 /* PBGitCommit.m in Sources */, F565262B0E03D89B00F03B52 /* PBWebGitController.m in Sources */, F56174570E058893001DCD79 /* PBGitTree.m in Sources */, + F57CC3910E05DDF2000472E2 /* PBEasyPipe.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/PBEasyPipe.h b/PBEasyPipe.h new file mode 100644 index 0000000..40a6a51 --- /dev/null +++ b/PBEasyPipe.h @@ -0,0 +1,22 @@ +// +// PBEasyPipe.h +// GitX +// +// Created by Pieter de Bie on 16-06-08. +// Copyright 2008 __MyCompanyName__. All rights reserved. +// + +#import + + +@interface PBEasyPipe : NSObject { + +} + ++ (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args; ++ (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir; + ++ (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args; ++ (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir; + +@end diff --git a/PBEasyPipe.m b/PBEasyPipe.m new file mode 100644 index 0000000..2a2eff4 --- /dev/null +++ b/PBEasyPipe.m @@ -0,0 +1,53 @@ +// +// PBEasyPipe.m +// GitX +// +// Created by Pieter de Bie on 16-06-08. +// Copyright 2008 __MyCompanyName__. All rights reserved. +// + +#import "PBEasyPipe.h" + + +@implementation PBEasyPipe + ++ (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args +{ + return [self handleForCommand:cmd withArgs:args inDir:nil]; +} + ++ (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir +{ + NSTask* task = [[NSTask alloc] init]; + task.launchPath = cmd; + task.arguments = args; + if (dir) + task.currentDirectoryPath = dir; + NSLog(@"Starting cmd %@ in dir %@", cmd, dir); + NSPipe* pipe = [NSPipe pipe]; + task.standardOutput = pipe; + + NSFileHandle* handle = [NSFileHandle fileHandleWithStandardOutput]; + handle = [pipe fileHandleForReading]; + + [task launch]; + return handle; +} + + + ++ (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir +{ + NSFileHandle* handle = [self handleForCommand:cmd withArgs: args inDir: dir]; + NSData* data = [handle readDataToEndOfFile]; + NSString* string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; + if ([string hasSuffix:@"\n"]) + string = [string substringToIndex:[string length]-1]; + + return string; +} ++ (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args +{ + return [self outputForCommand:cmd withArgs:args inDir:nil]; +} +@end diff --git a/PBGitRepository.m b/PBGitRepository.m index 85ae98d..49a2b88 100644 --- a/PBGitRepository.m +++ b/PBGitRepository.m @@ -10,6 +10,7 @@ #import "PBGitCommit.h" #import "NSFileHandleExt.h" +#import "PBEasyPipe.h" @implementation PBGitRepository @@ -25,7 +26,18 @@ static NSString* gitPath = @"/usr/bin/env"; - (PBGitRepository*) initWithPath: (NSString*) p { - self.path = p; + if ([p hasSuffix:@".git"]) + self.path = p; + else { + NSString* newPath = [PBEasyPipe outputForCommand:gitPath withArgs:[NSArray arrayWithObjects:@"rev-parse", @"--git-dir", nil] inDir:p]; + if ([newPath isEqualToString:@".git"]) + self.path = [p stringByAppendingPathComponent:@".git"]; + else + self.path = newPath; + } + + NSLog(@"Git path is: %@", self.path); + NSThread * commitThread = [[NSThread alloc] initWithTarget: self selector: @selector(initializeCommits) object:nil]; [commitThread start]; return self; @@ -41,17 +53,9 @@ static NSString* gitPath = @"/usr/bin/env"; } // No explicit path. Try it with "which" - NSTask* task = [[NSTask alloc] init]; - task.launchPath = @"/usr/bin/which"; - task.arguments = [NSArray arrayWithObject:@"git"]; - NSPipe* pipe = [NSPipe pipe]; - NSFileHandle* handle = [pipe fileHandleForReading]; - task.standardOutput = pipe; - [task launch]; - NSString* a = [handle readLine]; - gitPath = a; + gitPath = [PBEasyPipe outputForCommand:@"/usr/bin/which" withArgs:[NSArray arrayWithObject:@"git"]]; - if (a.length == 0) { + if (gitPath.length == 0) { NSLog(@"Git path not found. Defaulting to /opt/pieter/bin/git"); gitPath = @"/opt/pieter/bin/git"; } @@ -99,22 +103,9 @@ static NSString* gitPath = @"/usr/bin/env"; - (NSFileHandle*) handleForArguments:(NSArray *)args { NSString* gitDirArg = [@"--git-dir=" stringByAppendingString:path]; - NSArray* arguments = [NSArray arrayWithObject: gitDirArg]; - arguments = [arguments arrayByAddingObjectsFromArray: args]; - - NSTask* task = [[NSTask alloc] init]; - task.launchPath = gitPath; - task.arguments = arguments; - - NSPipe* pipe = [NSPipe pipe]; - task.standardOutput = pipe; - - NSFileHandle* handle = [NSFileHandle fileHandleWithStandardOutput]; - handle = [pipe fileHandleForReading]; - - [task launch]; - - return handle; + NSMutableArray* arguments = [NSMutableArray arrayWithObject: gitDirArg]; + [arguments addObjectsFromArray: args]; + return [PBEasyPipe handleForCommand:gitPath withArgs:arguments]; } - (NSFileHandle*) handleForCommand:(NSString *)cmd