Skip to content

Instantly share code, notes, and snippets.

@hugowetterberg
Created January 26, 2011 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugowetterberg/796784 to your computer and use it in GitHub Desktop.
Save hugowetterberg/796784 to your computer and use it in GitHub Desktop.
Reading the reference from HEAD in a git repo at path
-(NSString*)currentBranch {
const char *refAnfang = "ref: ";
NSString *branch;
const char *filename = [[path stringByAppendingPathComponent:@"HEAD"] cStringUsingEncoding:NSUTF8StringEncoding];
FILE *file = fopen (filename, "r");
if (file != NULL) {
char line [256];
if (fgets(line, sizeof line, file) != NULL) {
line[strlen(line)-1] = 0;
if (strncmp(refAnfang, line, 5) == 0) {
const char *reference = line + 5;
branch = [NSString stringWithCString:reference encoding:NSUTF8StringEncoding];
}
}
fclose(file);
}
return branch;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment