Skip to content

Instantly share code, notes, and snippets.

@rais38
Created April 24, 2014 22:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rais38/11272383 to your computer and use it in GitHub Desktop.
Save rais38/11272383 to your computer and use it in GitHub Desktop.
Retrieve current branch OSX
- (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