Files
gitx/PBEasyPipe.m
T
Pieter de Bie 08757ad610 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 :)
2008-06-16 01:52:12 +02:00

54 lines
1.4 KiB
Objective-C

//
// 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