Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 156 lines (124 sloc) 4.493 kb
08757ad @pieter Add easy pipe handling library. Use it to find real git repository path
pieter authored
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 //
8
9 #import "PBEasyPipe.h"
10
11 @implementation PBEasyPipe
12
13 + (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args
14 {
15 return [self handleForCommand:cmd withArgs:args inDir:nil];
16 }
17
faef971 @pieter EasyPipe: Add functions to ask for the return value of commands
pieter authored
18 + (NSTask *) taskForCommand:(NSString *)cmd withArgs:(NSArray *)args inDir:(NSString *)dir
08757ad @pieter Add easy pipe handling library. Use it to find real git repository path
pieter authored
19 {
cc5c93c @coresystems-krt - stashes can be created with optional message
coresystems-krt authored
20 NSMutableArray *filteredArguments = [[NSMutableArray alloc] init];
21 for (NSString *param in args) {
22 if ([param length] > 0) {
23 [filteredArguments addObject:param];
24 }
25 }
26
08757ad @pieter Add easy pipe handling library. Use it to find real git repository path
pieter authored
27 NSTask* task = [[NSTask alloc] init];
1b3f4ea @brotherbard Remove uses of dot notation for non properties of NSTask.
brotherbard authored
28 [task setLaunchPath:cmd];
cc5c93c @coresystems-krt - stashes can be created with optional message
coresystems-krt authored
29 [task setArguments:filteredArguments];
08757ad @pieter Add easy pipe handling library. Use it to find real git repository path
pieter authored
30 if (dir)
1b3f4ea @brotherbard Remove uses of dot notation for non properties of NSTask.
brotherbard authored
31 [task setCurrentDirectoryPath:dir];
5a7d1e9 @pieter EasyPipe: Only show debug when user pref is set
pieter authored
32
33 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"Show Debug Messages"])
656cb82 @laullon NSLog => Dlog (Log only on debug build)
authored
34 DLog(@"Starting command `%@ %@` in dir %@", cmd, [args componentsJoinedByString:@" "], dir);
3d64d83 @pieter Create a new SpeedTest target
pieter authored
35 #ifdef CLI
656cb82 @laullon NSLog => Dlog (Log only on debug build)
authored
36 DLog(@"Starting command `%@ %@` in dir %@", cmd, [args componentsJoinedByString:@" "], dir);
3d64d83 @pieter Create a new SpeedTest target
pieter authored
37 #endif
5a7d1e9 @pieter EasyPipe: Only show debug when user pref is set
pieter authored
38
08757ad @pieter Add easy pipe handling library. Use it to find real git repository path
pieter authored
39 NSPipe* pipe = [NSPipe pipe];
1b3f4ea @brotherbard Remove uses of dot notation for non properties of NSTask.
brotherbard authored
40 [task setStandardOutput:pipe];
41 [task setStandardError:pipe];
faef971 @pieter EasyPipe: Add functions to ask for the return value of commands
pieter authored
42 return task;
43 }
44
45 + (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir
46 {
47 NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
1b3f4ea @brotherbard Remove uses of dot notation for non properties of NSTask.
brotherbard authored
48 NSFileHandle* handle = [[task standardOutput] fileHandleForReading];
08757ad @pieter Add easy pipe handling library. Use it to find real git repository path
pieter authored
49
50 [task launch];
51 return handle;
52 }
53
54
55
faef971 @pieter EasyPipe: Add functions to ask for the return value of commands
pieter authored
56 + (NSString*) outputForCommand:(NSString *) cmd
57 withArgs:(NSArray *) args
58 inDir:(NSString *) dir
59 retValue:(int *) ret
08757ad @pieter Add easy pipe handling library. Use it to find real git repository path
pieter authored
60 {
57168e1 @pieter PBEasyPipe: Add method that allows you to specify the environment stuff
pieter authored
61 return [self outputForCommand:cmd withArgs:args inDir:dir byExtendingEnvironment:nil inputString:nil retValue:ret];
4024753 @pieter PBEasyPipe: Add a new method to provide input
pieter authored
62 }
faef971 @pieter EasyPipe: Add functions to ask for the return value of commands
pieter authored
63
4024753 @pieter PBEasyPipe: Add a new method to provide input
pieter authored
64 + (NSString*) outputForCommand:(NSString *) cmd
65 withArgs:(NSArray *) args
66 inDir:(NSString *) dir
57168e1 @pieter PBEasyPipe: Add method that allows you to specify the environment stuff
pieter authored
67 inputString:(NSString *) input
4024753 @pieter PBEasyPipe: Add a new method to provide input
pieter authored
68 retValue:(int *) ret
69 {
57168e1 @pieter PBEasyPipe: Add method that allows you to specify the environment stuff
pieter authored
70 return [self outputForCommand:cmd withArgs:args inDir:dir byExtendingEnvironment:nil inputString:input retValue:ret];
71 }
72
73 + (NSString*) outputForCommand:(NSString *) cmd
74 withArgs:(NSArray *) args
75 inDir:(NSString *) dir
76 byExtendingEnvironment:(NSDictionary *)dict
77 inputString:(NSString *) input
78 retValue:(int *) ret
79 {
4024753 @pieter PBEasyPipe: Add a new method to provide input
pieter authored
80 NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
57168e1 @pieter PBEasyPipe: Add method that allows you to specify the environment stuff
pieter authored
81
82 if (dict) {
83 NSMutableDictionary *env = [[[NSProcessInfo processInfo] environment] mutableCopy];
84 [env addEntriesFromDictionary:dict];
1b3f4ea @brotherbard Remove uses of dot notation for non properties of NSTask.
brotherbard authored
85 [task setEnvironment:env];
57168e1 @pieter PBEasyPipe: Add method that allows you to specify the environment stuff
pieter authored
86 }
87
1b3f4ea @brotherbard Remove uses of dot notation for non properties of NSTask.
brotherbard authored
88 NSFileHandle* handle = [[task standardOutput] fileHandleForReading];
530377a @pieter PBEasyPipe: Fall back to ASCII encoding when UTF-8 fails
pieter authored
89
90 if (input) {
1b3f4ea @brotherbard Remove uses of dot notation for non properties of NSTask.
brotherbard authored
91 [task setStandardInput:[NSPipe pipe]];
92 NSFileHandle *inHandle = [[task standardInput] fileHandleForWriting];
530377a @pieter PBEasyPipe: Fall back to ASCII encoding when UTF-8 fails
pieter authored
93 [inHandle writeData:[input dataUsingEncoding:NSUTF8StringEncoding]];
94 [inHandle closeFile];
95 }
4024753 @pieter PBEasyPipe: Add a new method to provide input
pieter authored
96
97 [task launch];
98
99 NSData* data = [handle readDataToEndOfFile];
530377a @pieter PBEasyPipe: Fall back to ASCII encoding when UTF-8 fails
pieter authored
100 NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
101 if (!string)
f94ab0f @pieter Use NSISOLatin1 encoding instead of ASCII
pieter authored
102 string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
4024753 @pieter PBEasyPipe: Add a new method to provide input
pieter authored
103
104 // Strip trailing newline
105 if ([string hasSuffix:@"\n"])
106 string = [string substringToIndex:[string length]-1];
107
faef971 @pieter EasyPipe: Add functions to ask for the return value of commands
pieter authored
108 [task waitUntilExit];
109 if (ret)
110 *ret = [task terminationStatus];
08757ad @pieter Add easy pipe handling library. Use it to find real git repository path
pieter authored
111 return string;
112 }
faef971 @pieter EasyPipe: Add functions to ask for the return value of commands
pieter authored
113
114 // We don't use the above function because then we'd have to wait until the program was finished
115 // with running
4024753 @pieter PBEasyPipe: Add a new method to provide input
pieter authored
116
117 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir
faef971 @pieter EasyPipe: Add functions to ask for the return value of commands
pieter authored
118 {
119 NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
1b3f4ea @brotherbard Remove uses of dot notation for non properties of NSTask.
brotherbard authored
120 NSFileHandle* handle = [[task standardOutput] fileHandleForReading];
4024753 @pieter PBEasyPipe: Add a new method to provide input
pieter authored
121
faef971 @pieter EasyPipe: Add functions to ask for the return value of commands
pieter authored
122 [task launch];
1b3f4ea @brotherbard Remove uses of dot notation for non properties of NSTask.
brotherbard authored
123 // This can cause a "Bad file descriptor"... when?
cc6a915 @pieter PBEasyPipe: Might catch an exception
pieter authored
124 NSData *data;
125 @try {
126 data = [handle readDataToEndOfFile];
127 }
128 @catch (NSException * e) {
656cb82 @laullon NSLog => Dlog (Log only on debug build)
authored
129 DLog(@"Got a bad file descriptor in %@!", NSStringFromSelector(_cmd));
cc6a915 @pieter PBEasyPipe: Might catch an exception
pieter authored
130 if ([NSThread currentThread] != [NSThread mainThread])
131 [task waitUntilExit];
132
133 return nil;
134 }
530377a @pieter PBEasyPipe: Fall back to ASCII encoding when UTF-8 fails
pieter authored
135 NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
136 if (!string)
f94ab0f @pieter Use NSISOLatin1 encoding instead of ASCII
pieter authored
137 string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
b0caa34 @pieter Revert "Wait for a child to clear it out."
pieter authored
138
faef971 @pieter EasyPipe: Add functions to ask for the return value of commands
pieter authored
139 // Strip trailing newline
140 if ([string hasSuffix:@"\n"])
141 string = [string substringToIndex:[string length]-1];
8fde62f @pieter PBEasyPipe: Wait until exit if we're not in the main thread
pieter authored
142
143 if ([NSThread currentThread] != [NSThread mainThread])
144 [task waitUntilExit];
145
faef971 @pieter EasyPipe: Add functions to ask for the return value of commands
pieter authored
146 return string;
147 }
148
4024753 @pieter PBEasyPipe: Add a new method to provide input
pieter authored
149
08757ad @pieter Add easy pipe handling library. Use it to find real git repository path
pieter authored
150 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args
151 {
152 return [self outputForCommand:cmd withArgs:args inDir:nil];
153 }
cc6ac22 @pieter Allow double click to open file
pieter authored
154
08757ad @pieter Add easy pipe handling library. Use it to find real git repository path
pieter authored
155 @end
Something went wrong with that request. Please try again.