forked from ivanchenkodmitry/git_branch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_branch.c
More file actions
executable file
·40 lines (34 loc) · 881 Bytes
/
Copy pathgit_branch.c
File metadata and controls
executable file
·40 lines (34 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <libgen.h>
#define CWD_LEN 1000
#define ERROR(str) {fprintf(stderr,str);exit(1);}
int main(int argc, char *argv[])
{
FILE *head_file;
char head_str[CWD_LEN];
char cwd[CWD_LEN];
if (getcwd(cwd, CWD_LEN) == NULL)
ERROR("error: current pathname is too long\n");
char *dirname_ptr = cwd;
int dirname_length;
while((dirname_length = strlen(dirname_ptr)) > 1) {
strcat(dirname_ptr, "/.git/HEAD");
if((head_file = fopen(dirname_ptr, "r")) != NULL) {
if (!fgets(head_str, CWD_LEN*sizeof(char), head_file))
ERROR("HEAD is empty\n");
char *ptr = strrchr(head_str,'/');
if (ptr != NULL) {
printf("%s",ptr + 1);
break;
}
fclose(head_file);
}
dirname_ptr[dirname_length] = '\0';
dirname_ptr = dirname(dirname_ptr);
}
return 0;
}