Skip to content
This repository has been archived by the owner on Sep 4, 2018. It is now read-only.

Add proxy support and "Force HTTPS for GitHub" setting #272

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use Environment variables instead of git config to set proxy
  • Loading branch information
Max Demian committed May 20, 2015
commit 7d05453fd4b1bcb061cc5cfdf469d9b6366c4956
12 changes: 3 additions & 9 deletions Alcatraz/Helpers/ATZGit.m
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ + (void)cloneRepository:(NSString *)remotePath toLocalPath:(NSString *)localPath
completion:(void (^)(NSString *output, NSError *))completion {
NSLog(@"Cloning Repo: %@", localPath);
if ([ATZConfig forceHttps]) {
remotePath = [self convertRemotePathToHttps:remotePath];
remotePath = [self forceHttpsForGithub:remotePath];
}
[self clone:remotePath to:localPath completion:completion];
}
@@ -73,25 +73,19 @@ + (BOOL)areCommandLineToolsAvailable {
return areAvailable;
}

+ (NSString *)convertRemotePathToHttps:(NSString *)remotePath {
+ (NSString *)forceHttpsForGithub:(NSString *)remotePath {
if ([remotePath containsString:GITHUB_GIT_ADRESS]) {
return [remotePath stringByReplacingOccurrencesOfString:GITHUB_GIT_ADRESS
withString:GITHUB_HTTPS_ADRESS];
}
return remotePath;
}

+ (NSArray *)addProxyToArguments:(NSArray *)arguments {
NSArray *proxy = @[[NSString stringWithFormat:@"-c http.proxy=%@", [ATZConfig httpProxy]]];

return [arguments arrayByAddingObjectsFromArray:proxy];
}

#pragma mark - Private

+ (void)clone:(NSString *)remotePath to:(NSString *)localPath completion:(void (^)(NSString *, NSError *))completion {
ATZShell *shell = [ATZShell new];
NSArray *cloneArguments = [self addProxyToArguments:@[CLONE, remotePath, localPath, IGNORE_PUSH_CONFIG]];
NSArray *cloneArguments = @[CLONE, remotePath, localPath, IGNORE_PUSH_CONFIG];

[shell executeCommand:GIT withArguments:cloneArguments completion:^(NSString *output, NSError *error) {
NSLog(@"Git Clone output: %@", output);
75 changes: 40 additions & 35 deletions Alcatraz/Helpers/ATZShell.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Shell.m
//
//
// Copyright (c) 2013 Marin Usalj | supermar.in
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -8,10 +8,10 @@
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -21,6 +21,7 @@
// THE SOFTWARE.

#import "ATZShell.h"
#import "ATZConfig.h"

@interface ATZShell (){}
@property (nonatomic, retain) NSMutableData *taskOutput;
@@ -29,69 +30,73 @@ @interface ATZShell (){}
@implementation ATZShell

- (void)executeCommand:(NSString *)command withArguments:(NSArray *)arguments
completion:(void(^)(NSString *taskOutput, NSError *error))completion {

completion:(void (^)(NSString *taskOutput, NSError *error))completion {
[self executeCommand:command withArguments:arguments inWorkingDirectory:nil completion:completion];
}

- (void)executeCommand:(NSString *)command withArguments:(NSArray *)arguments inWorkingDirectory:(NSString *)path
completion:(void(^)(NSString *taskOutput, NSError *error))completion {

completion:(void (^)(NSString *taskOutput, NSError *error))completion {
_taskOutput = [NSMutableData new];
NSTask *shellTask = [NSTask new];

if (path) [shellTask setCurrentDirectoryPath:path];

// Set proxy environment variables for the task.
NSMutableDictionary *environment = [NSMutableDictionary new ];
[environment addEntriesFromDictionary:[[NSProcessInfo processInfo] environment]];
[environment setObject:[ATZConfig httpProxy] forKey:@"http_proxy"];
[environment setObject:[ATZConfig httpProxy] forKey:@"HTTPS_PROXY"];
[shellTask setEnvironment:environment];

if (path) {
[shellTask setCurrentDirectoryPath:path];
}
[shellTask setLaunchPath:command];
[shellTask setArguments:arguments];

[self setUpShellOutputForTask:shellTask];
[self setUpStdErrorOutputForTask:shellTask];

[self setUpTerminationHandlerForTask:shellTask completion:completion];
[self tryToLaunchTask:shellTask completionIfFailed:completion];
}


#pragma mark - Private

- (void)setUpShellOutputForTask:(NSTask *)task {
task.standardOutput = [NSPipe pipe];
[[task.standardOutput fileHandleForReading] setReadabilityHandler:^(NSFileHandle *file) {
[self.taskOutput appendData:[file availableData]];
}];
[self.taskOutput appendData:[file availableData]];
}];
}

- (void)setUpStdErrorOutputForTask:(NSTask *)task {
task.standardError = [NSPipe pipe];
[[task.standardError fileHandleForReading] setReadabilityHandler:^(NSFileHandle *file) {
[self.taskOutput appendData:[file availableData]];
}];
[self.taskOutput appendData:[file availableData]];
}];
}

- (void)setUpTerminationHandlerForTask:(NSTask *)task completion:(void(^)(NSString *taskOutput, NSError *error))completion {
- (void)setUpTerminationHandlerForTask:(NSTask *)task completion:(void (^)(NSString *taskOutput, NSError *error))completion {
[task setTerminationHandler:^(NSTask *task) {

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSString* output = [[NSString alloc] initWithData:self.taskOutput encoding:NSUTF8StringEncoding];

if (task.terminationStatus == 0) {
completion(output, nil);
} else {
NSString* reason = [NSString stringWithFormat:@"Task exited with status %d", task.terminationStatus];
completion(output, [NSError errorWithDomain:reason code:666 userInfo:@{ NSLocalizedDescriptionKey: reason }]);
}
}];

[task.standardOutput fileHandleForReading].readabilityHandler = nil;
[task.standardError fileHandleForReading].readabilityHandler = nil;
}];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSString *output = [[NSString alloc] initWithData:self.taskOutput encoding:NSUTF8StringEncoding];

if (task.terminationStatus == 0) {
completion(output, nil);
} else {
NSString *reason = [NSString stringWithFormat:@"Task exited with status %d", task.terminationStatus];
completion(output, [NSError errorWithDomain:reason code:666 userInfo:@{ NSLocalizedDescriptionKey: reason }]);
}
}];

[task.standardOutput fileHandleForReading].readabilityHandler = nil;
[task.standardError fileHandleForReading].readabilityHandler = nil;
}];
}

- (void)tryToLaunchTask:(NSTask *)shellTask completionIfFailed:(void(^)(NSString *taskOutput, NSError *error))completion {
- (void)tryToLaunchTask:(NSTask *)shellTask completionIfFailed:(void (^)(NSString *taskOutput, NSError *error))completion {
@try {
[shellTask launch];
}
@catch (NSException *exception) {
}@catch (NSException *exception) {
NSLog(@"Shell command execution failed! %@", exception);
completion(nil, [NSError errorWithDomain:exception.reason code:667 userInfo:nil]);
}