-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.cpp
More file actions
59 lines (47 loc) · 1.38 KB
/
git.cpp
File metadata and controls
59 lines (47 loc) · 1.38 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "shell.h"
#include <cstdio>
#include <sys/resource.h>
#include <libgen.h>
int find_git_root(const char *cwd, char *git_path) {
char current[1024];
strncpy(current, cwd, sizeof(current));
while (strcmp(current, "/") != 0) {
snprintf(git_path, 1024, "%s/.git", current);
struct stat st;
if (stat(git_path, &st) == 0)
return 1;
char temp[1024];
strncpy(temp, current, sizeof(temp));
char *parent = dirname(temp);
if (strcmp(parent, current) == 0) break;
strncpy(current, parent, sizeof(current));
}
return 0;
}
void get_git_branch(const char *git_path, char *branch){
char head_path[1024];
snprintf(head_path, sizeof(head_path), "%s/HEAD", git_path);
FILE *file = fopen(head_path, "r");
if (!file) return;
char line[256];
fgets(line, sizeof(line), file);
fclose(file);
strncpy(branch, line + 16, 256);
branch[strcspn(branch, "\n")] = '\0';
}
void get_time(char *buf){
time_t now = time(NULL);
struct tm *t = localtime(&now);
strftime(buf, 6, "%H:%M", t);
}
int get_git_status(const char *git_path) {
FILE *file = popen("git status --porcelain", "r");
if (!file) return 1;
char buf[256];
if (fgets(buf, sizeof(buf), file) == NULL) {
pclose(file);
return 1; // clean
}
pclose(file);
return 0; // changes
}