Created
April 24, 2014 22:59
-
-
Save rais38/11272383 to your computer and use it in GitHub Desktop.
Retrieve current branch OSX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSString *)projectDirectoryPath | |
{ | |
NSWindowController *currentWindowController = [[NSApp mainWindow] windowController]; | |
id document = [currentWindowController document]; | |
if (currentWindowController && [document isKindOfClass:NSClassFromString(@"IDEWorkspaceDocument")]) { | |
NSURL *workspaceDirectoryURL = [[[document valueForKeyPath:@"_workspace.representingFilePath.fileURL"] URLByDeletingLastPathComponent] filePathURL]; | |
if(workspaceDirectoryURL) { | |
return [workspaceDirectoryURL path]; | |
} | |
} | |
return nil; | |
} | |
- (void)retrieveCurrentBranch | |
{ | |
NSTask *task = [[NSTask alloc] init]; | |
[task setLaunchPath:@"/usr/bin/git"]; | |
[task setCurrentDirectoryPath:[self projectDirectoryPath]]; | |
NSMutableArray *arguments = [NSMutableArray array]; | |
[arguments addObject:@"describe"]; | |
[arguments addObject:@"--contains"]; | |
[arguments addObject:@"--all"]; | |
[arguments addObject:@"HEAD"]; | |
[task setArguments:arguments]; | |
NSPipe *outputPipe = [NSPipe pipe]; | |
[task setStandardOutput:outputPipe]; | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(currentBranchReadCompleted:) name:NSFileHandleReadToEndOfFileCompletionNotification object:[outputPipe fileHandleForReading]]; | |
[[outputPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify]; | |
[task launch]; | |
} | |
#pragma mark - Notifications | |
- (void)currentBranchReadCompleted:(NSNotification *)notification | |
{ | |
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleReadToEndOfFileCompletionNotification object:[notification object]]; | |
NSData *data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]; | |
NSString *currentBranchName = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | |
NSLog(@"Current Branch: %@", currentBranchName); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment