HistoryView: don't load in commit information in a separate thread anymore
[GitX.git] / PBEasyPipe.m
blob3b80184d6a7c8dc42915ff171ea5dd3943c81682
1 //
2 //  PBEasyPipe.m
3 //  GitX
4 //
5 //  Created by Pieter de Bie on 16-06-08.
6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
9 #import "PBEasyPipe.h"
12 @implementation PBEasyPipe
14 + (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args
16         return [self handleForCommand:cmd withArgs:args inDir:nil];
19 + (NSTask *) taskForCommand:(NSString *)cmd withArgs:(NSArray *)args inDir:(NSString *)dir
21         NSTask* task = [[NSTask alloc] init];
22         task.launchPath = cmd;
23         task.arguments = args;
24         if (dir)
25                 task.currentDirectoryPath = dir;
27         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"Show Debug Messages"])
28                 NSLog(@"Starting command `%@ %@` in dir %@", cmd, [args componentsJoinedByString:@" "], dir);
29 #ifdef CLI
30         NSLog(@"Starting command `%@ %@` in dir %@", cmd, [args componentsJoinedByString:@" "], dir);
31 #endif
33         NSPipe* pipe = [NSPipe pipe];
34         task.standardOutput = pipe;
35         return task;
38 + (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir
40         NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
41         NSFileHandle* handle = [task.standardOutput fileHandleForReading];
42         
43         [task launch];
44         return handle;
49 + (NSString*) outputForCommand:(NSString *) cmd
50                                           withArgs:(NSArray *)  args
51                                                  inDir:(NSString *) dir
52                                       retValue:(int *)      ret
54         return [self outputForCommand:cmd withArgs:args inDir:dir byExtendingEnvironment:nil inputString:nil retValue:ret];
55 }       
57 + (NSString*) outputForCommand:(NSString *) cmd
58                                           withArgs:(NSArray *)  args
59                                                  inDir:(NSString *) dir
60                                    inputString:(NSString *) input
61                                       retValue:(int *)      ret
63         return [self outputForCommand:cmd withArgs:args inDir:dir byExtendingEnvironment:nil inputString:input retValue:ret];
66 + (NSString*) outputForCommand:(NSString *)    cmd
67                                           withArgs:(NSArray *)     args
68                                                  inDir:(NSString *)    dir
69                 byExtendingEnvironment:(NSDictionary *)dict
70                                    inputString:(NSString *)    input
71                                           retValue:(int *)         ret
73         NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
75         if (dict) {
76                 NSMutableDictionary *env = [[[NSProcessInfo processInfo] environment] mutableCopy];
77                 [env addEntriesFromDictionary:dict];
78                 task.environment = env;
79         }
81         NSFileHandle* handle = [task.standardOutput fileHandleForReading];
83         if (input) {
84                 task.standardInput = [NSPipe pipe];
85                 NSFileHandle *inHandle = [task.standardInput fileHandleForWriting];
86                 [inHandle writeData:[input dataUsingEncoding:NSUTF8StringEncoding]];
87                 [inHandle closeFile];
88         }
89         
90         [task launch];
91         
92         NSData* data = [handle readDataToEndOfFile];
93         NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
94         if (!string)
95                 string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
96         
97         // Strip trailing newline
98         if ([string hasSuffix:@"\n"])
99                 string = [string substringToIndex:[string length]-1];
100         
101         [task waitUntilExit];
102         if (ret)
103                 *ret = [task terminationStatus];
104         return string;
105 }       
107 // We don't use the above function because then we'd have to wait until the program was finished
108 // with running
110 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args  inDir: (NSString*) dir
112         NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
113         NSFileHandle* handle = [task.standardOutput fileHandleForReading];
114         
115         [task launch];
116 #warning This can cause a "Bad file descriptor"... when?
117         NSData *data;
118         @try {
119                 data = [handle readDataToEndOfFile];
120         }
121         @catch (NSException * e) {
122                 NSLog(@"Got a bad file descriptor in %s!", _cmd);
123                 if ([NSThread currentThread] != [NSThread mainThread])
124                         [task waitUntilExit];
126                 return nil;
127         }
128         NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
129         if (!string)
130                 string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
131         
132         // Strip trailing newline
133         if ([string hasSuffix:@"\n"])
134                 string = [string substringToIndex:[string length]-1];
136         if ([NSThread currentThread] != [NSThread mainThread])
137                 [task waitUntilExit];
139         return string;
143 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args
145         return [self outputForCommand:cmd withArgs:args inDir:nil];
148 @end