Skip to content

Commit 87bc0d8

Browse files
committed
better new git_askpasswd
1 parent 4e54ff9 commit 87bc0d8

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

gitx_askpasswd_main.m

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,20 @@
2929
#define PASSLABELHEIGHT 16.0
3030
#define WINDOWAUTOSAVENAME @"GitXAskPasswordWindowFrame"
3131

32+
// In 10.6, some NSObject categories (like NSWindowDelegate) were changed to
33+
// protocols. Thus to avoid warnings we need to add protocol specifiers, but
34+
// only when compiling for 10.6+.
35+
#ifndef MAC_OS_X_VERSION_10_6
36+
#define MAC_OS_X_VERSION_10_6 1060
37+
#endif
3238

33-
@interface GAPAppDelegate : NSObject /*<NSApplicationDelegate>*/
39+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
40+
#define PROTOCOL_10_6(...) <__VA_ARGS__>
41+
#else
42+
#define PROTOCOL_10_6(...)
43+
#endif
44+
45+
@interface GAPAppDelegate : NSObject PROTOCOL_10_6(NSApplicationDelegate)
3446
{
3547
}
3648

@@ -50,7 +62,7 @@ -(void)yesNo:(NSString *)prompt url:(NSString *)url{
5062
[alert setInformativeText:prompt];
5163
[alert setAlertStyle:NSWarningAlertStyle];
5264
NSInteger result = [alert runModal];
53-
65+
5466
Boolean yes=NO;
5567
if ( result == NSAlertFirstButtonReturn ) {
5668
yes=YES;
@@ -213,38 +225,49 @@ int main( int argc, const char* argv[] )
213225

214226
NSApplication *app = [NSApplication sharedApplication];
215227
GAPAppDelegate *appDel = [[GAPAppDelegate alloc] init];
216-
[app setDelegate: appDel];
228+
[app setDelegate:appDel];
217229

218230

219-
char args[4024];
220-
getproclline(getppid(),args);
221-
NSString *cmd=[NSString stringWithFormat:@"%@",[NSString stringWithUTF8String:args]];
231+
char c_args[4024];
232+
getproclline(getppid(),c_args);
233+
NSString *cmd=[NSString stringWithFormat:@"%@",[NSString stringWithUTF8String:c_args]];
222234

223235
NSLog(@"cmd: '%@'",cmd);
224236

225-
if([cmd hasPrefix:@"git-remote"]){
226-
NSArray *args=[cmd componentsSeparatedByString:@" "];
227-
NSString *url=[args objectAtIndex:[args count]-1];
228-
229-
void *passwordData = nil;
230-
SecKeychainItemRef itemRef = nil;
231-
UInt32 passwordLength = 0;
232-
233-
OSStatus status = GetPasswordKeychain ([url cStringUsingEncoding:NSASCIIStringEncoding],[url lengthOfBytesUsingEncoding:NSASCIIStringEncoding],&passwordData,&passwordLength,&itemRef);
234-
if (status == noErr) {
235-
SecKeychainItemFreeContent (NULL,passwordData);
236-
NSString *pas=[[NSString stringWithCString:passwordData encoding:NSASCIIStringEncoding] substringToIndex:passwordLength];
237-
printf( "%s", [pas UTF8String] );
238-
return 0;
237+
NSString *prompt;
238+
NSString *url;
239+
BOOL yesno=NO;
240+
NSArray *args=[cmd componentsSeparatedByString:@" "];
241+
242+
if(argc<1){
243+
prompt=@"Enter your OpenSSH passphrase:";
244+
url=@"private key";
245+
}else{
246+
prompt=[NSString stringWithFormat:@"%@",[NSString stringWithCString:argv[1] encoding:NSASCIIStringEncoding]];
247+
if([[prompt lowercaseString] rangeOfString:@"yes/no"].location==NSNotFound){
248+
url=[args objectAtIndex:[args count]-1];
249+
}else{
250+
yesno=YES;
251+
url=[args objectAtIndex:1];
239252
}
240-
241-
NSString *prompt=[NSString stringWithFormat:@"%@",[NSString stringWithCString:argv[1] encoding:NSASCIIStringEncoding]];
242-
[appDel pasword:prompt url:url];
243-
}else{ // yes/no?
244-
NSString *prompt=[NSString stringWithFormat:@"%@",[NSString stringWithCString:argv[1] encoding:NSASCIIStringEncoding]];
245-
NSArray *args=[cmd componentsSeparatedByString:@" "];
246-
NSString *url=[args objectAtIndex:1];
253+
}
254+
255+
void *passwordData = nil;
256+
SecKeychainItemRef itemRef = nil;
257+
UInt32 passwordLength = 0;
258+
259+
OSStatus status = GetPasswordKeychain ([url cStringUsingEncoding:NSASCIIStringEncoding],[url lengthOfBytesUsingEncoding:NSASCIIStringEncoding],&passwordData,&passwordLength,&itemRef);
260+
if (status == noErr) {
261+
SecKeychainItemFreeContent (NULL,passwordData);
262+
NSString *pas=[[NSString stringWithCString:passwordData encoding:NSASCIIStringEncoding] substringToIndex:passwordLength];
263+
printf( "%s", [pas UTF8String] );
264+
return 0;
265+
}
266+
267+
if(yesno){
247268
[appDel yesNo:prompt url:url];
269+
}else{
270+
[appDel pasword:prompt url:url];
248271
}
249272

250273
return 0;

0 commit comments

Comments
 (0)