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 :)
This commit is contained in:
Pieter de Bie
2008-06-16 01:52:12 +02:00
parent 0171c6d065
commit 08757ad610
5 changed files with 116 additions and 34 deletions
+5 -3
View File
@@ -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;
+18 -4
View File
@@ -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 = "<group>"; };
F57ABDDE0E0441DE00A088B8 /* commit.js */ = {isa = PBXFileReference; explicitFileType = sourcecode.javascript; fileEncoding = 4; name = commit.js; path = html/commit.js; sourceTree = "<group>"; };
F57ABE180E04431D00A088B8 /* prototype.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; name = prototype.js; path = html/prototype.js; sourceTree = "<group>"; };
F57CC38F0E05DDF2000472E2 /* PBEasyPipe.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBEasyPipe.h; sourceTree = "<group>"; };
F57CC3900E05DDF2000472E2 /* PBEasyPipe.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBEasyPipe.m; sourceTree = "<group>"; };
F58A8F270E043698007E3FC0 /* commits.css */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.css; name = commits.css; path = html/commits.css; sourceTree = "<group>"; };
F5945E150E02B0C200706420 /* PBGitRepository.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitRepository.h; sourceTree = "<group>"; };
F5945E160E02B0C200706420 /* PBGitRepository.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitRepository.m; sourceTree = "<group>"; };
@@ -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 = "<group>";
@@ -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 = "<group>";
};
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 = "<group>";
};
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;
};
+22
View File
@@ -0,0 +1,22 @@
//
// PBEasyPipe.h
// GitX
//
// Created by Pieter de Bie on 16-06-08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@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
+53
View File
@@ -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
+18 -27
View File
@@ -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