Repository navigation
Expand file tree
/
Copy pathgit.c
More file actions
225 lines (197 loc) · 6.42 KB
/
Copy pathgit.c
File metadata and controls
225 lines (197 loc) · 6.42 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "git.h"
#include "command.h"
#include "window.h"
#include "mode.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Run a git command and return output (caller frees) */
static char *git_run(const char *cmd) {
FILE *fp = popen(cmd, "r");
if (!fp) return NULL;
size_t cap = 4096;
size_t len = 0;
char *buf = hmalloc(cap);
char line[1024];
while (fgets(line, sizeof(line), fp)) {
size_t llen = strlen(line);
if (len + llen >= cap) {
cap *= 2;
buf = hrealloc(buf, cap);
}
memcpy(buf + len, line, llen);
len += llen;
}
buf[len] = '\0';
pclose(fp);
return buf;
}
GitStatus git_parse_status(void) {
GitStatus status = {.
8000
count = 0};
/* Get branch */
char *branch = git_run("git branch --show-current 2>/dev/null");
if (branch) {
/* Trim newline */
size_t blen = strlen(branch);
if (blen > 0 && branch[blen - 1] == '\n') branch[blen - 1] = '\0';
strncpy(status.branch, branch, sizeof(status.branch) - 1);
free(branch);
}
/* Get status */
char *output = git_run("git status --porcelain=v1 2>/dev/null");
if (!output) return status;
char *line = output;
while (*line && status.count < MAX_GIT_FILES) {
char *eol = strchr(line, '\n');
if (eol) *eol = '\0';
if (strlen(line) >= 4) {
status.files[status.count].index_status = line[0];
status.files[status.count].work_status = line[1];
strncpy(status.files[status.count].path, line + 3,
sizeof(status.files[status.count].path) - 1);
status.count++;
}
if (eol)
line = eol + 1;
else
break;
}
free(output);
return status;
}
char *git_format_status(GitStatus *status) {
size_t cap = 4096;
size_t len = 0;
char *buf = hmalloc(cap);
#define APPEND_FMT(...) do { \
int n = snprintf(buf + len, cap - len, __VA_ARGS__); \
if (n < 0) break; \
if (len + (size_t)n >= cap) { cap *= 2; buf = hrealloc(buf, cap); \
snprintf(buf + len, cap - len, __VA_ARGS__); } \
len += (size_t)n; \
} while(0)
APPEND_FMT("Head: %s\n\n", status->branch[0] ? status->branch : "(detached)");
/* Staged changes */
bool has_staged = false;
for (int i = 0; i < status->count; i++) {
if (status->files[i].index_status != ' ' && status->files[i].index_status != '?') {
if (!has_staged) {
APPEND_FMT("Staged changes:\n");
has_staged = true;
}
char st = status->files[i].index_status;
const char *desc = "modified";
if (st == 'A') desc = "new file";
else if (st == 'D') desc = "deleted";
else if (st == 'R') desc = "renamed";
APPEND_FMT(" %s: %s\n", desc, status->files[i].path);
}
}
if (has_staged) APPEND_FMT("\n");
/* Unstaged changes */
bool has_unstaged = false;
for (int i = 0; i < status->count; i++) {
if (status->files[i].work_status != ' ' && status->files[i].work_status != '?') {
if (!has_unstaged) {
APPEND_FMT("Unstaged changes:\n");
has_unstaged = true;
}
char st = status->files[i].work_status;
const char *desc = "modified";
if (st == 'D') desc = "deleted";
APPEND_FMT(" %s: %s\n", desc, status->files[i].path);
}
}
if (has_unstaged) APPEND_FMT("\n");
/* Untracked files */
bool has_untracked = false;
for (int i = 0; i < status->count; i++) {
if (status->files[i].index_status == '?' && status->files[i].work_status == '?') {
if (!has_untracked) {
APPEND_FMT("Untracked files:\n");
has_untracked = true;
}
APPEND_FMT(" %s\n", status->files[i].path);
}
}
if (!has_staged && !has_unstaged && !has_untracked) {
APPEND_FMT("Nothing to commit, working tree clean\n");
}
#undef APPEND_FMT
buf[len] = '\0';
return buf;
}
void git_stage_file(const char *path) {
char cmd[1024];
snprintf(cmd, sizeof(cmd), "git add '%s' 2>&1", path);
char *output = git_run(cmd);
if (output && output[0]) {
message("git add: %s", output);
}
free(output);
}
void git_unstage_file(const char *path) {
char cmd[1024];
snprintf(cmd, sizeof(cmd), "git restore --staged '%s' 2>&1", path);
char *output = git_run(cmd);
if (output && output[0]) {
message("git restore: %s", output);
}
free(output);
}
void git_commit(const char *msg) {
char cmd[2048];
snprintf(cmd, sizeof(cmd), "git commit -m '%s' 2>&1", msg);
char *output = git_run(cmd);
if (output) {
message("%s", output);
}
free(output);
}
char *git_diff_file(const char *path, bool cached) {
char cmd[1024];
const char *flag = cached ? " --cached" : "";
if (path) {
snprintf(cmd, sizeof(cmd), "git diff%s '%s' 2>/dev/null", flag, path);
} else {
snprintf(cmd, sizeof(cmd), "git diff%s 2>/dev/null", flag);
}
return git_run(cmd);
}
char *git_log(int count) {
char cmd[256];
snprintf(cmd, sizeof(cmd), "git log --oneline -%d 2>/dev/null", count);
return git_run(cmd);
}
char *git_file_at_point(Buffer *buf) {
int line_num, col;
buffer_point_to_line_col(buf, buf->point, &line_num, &col);
char *line_text = buffer_line_text(buf, line_num);
if (!line_text) return NULL;
/* Parse file path from status line format " desc: path" */
char *colon = strchr(line_text, ':');
if (colon) {
char *path = colon + 1;
while (*path == ' ') path++;
/* Trim trailing whitespace */
char *end = path + strlen(path) - 1;
while (end > path && (*end == ' ' || *end == '\n')) *end-- = '\0';
char *result = hstrdup(path);
free(line_text);
return result;
}
/* Try bare filename (untracked section) */
char *path = line_text;
while (*path == ' ') path++;
if (*path && *path != '\n') {
char *end = path + strlen(path) - 1;
while (end > path && (*end == ' ' || *end == '\n')) *end-- = '\0';
char *result = hstrdup(path);
free(line_text);
return result;
}
free(line_text);
return NULL;
}
/* Git commands moved to clj/commands.clj */