-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathgit_context.c
More file actions
411 lines (370 loc) · 12.3 KB
/
Copy pathgit_context.c
File metadata and controls
411 lines (370 loc) · 12.3 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "git/git_context.h"
#include "foundation/compat_fs.h"
#include "foundation/constants.h"
#include "foundation/str_util.h"
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
enum {
GIT_CMD_MAX = 1024,
GIT_OUTPUT_MAX = 4096,
};
static char *git_strdup(const char *s) {
if (!s) {
s = "";
}
size_t n = strlen(s) + 1;
char *out = (char *)malloc(n);
if (!out) {
return NULL;
}
memcpy(out, s, n);
return out;
}
static void trim_newlines(char *s) {
if (!s) {
return;
}
size_t n = strlen(s);
while (n > 0 && (s[n - 1] == '\n' || s[n - 1] == '\r')) {
s[--n] = '\0';
}
}
static bool git_validate_repo_path(const char *repo_path) {
return cbm_validate_shell_path_arg(repo_path);
}
static int git_capture(const char *repo_path, const char *git_args, char **out) {
if (!out) {
return CBM_NOT_FOUND;
}
*out = NULL;
if (!repo_path || !git_args || !git_validate_repo_path(repo_path)) {
return CBM_NOT_FOUND;
}
char cmd[GIT_CMD_MAX];
#ifdef _WIN32
const char *null_dev = "NUL";
#else
const char *null_dev = "/dev/null";
#endif
/* Double quotes work for POSIX shells and cmd.exe. cbm_validate_shell_arg()
* rejects quote/backslash/substitution metacharacters before interpolation. */
int n = snprintf(cmd, sizeof(cmd), "git -C \"%s\" %s 2>%s", repo_path, git_args, null_dev);
if (n < 0 || n >= (int)sizeof(cmd)) {
return CBM_NOT_FOUND;
}
FILE *fp = cbm_popen(cmd, "r");
if (!fp) {
return CBM_NOT_FOUND;
}
char buf[GIT_OUTPUT_MAX];
if (!fgets(buf, sizeof(buf), fp)) {
cbm_pclose(fp);
return CBM_NOT_FOUND;
}
trim_newlines(buf);
int rc = cbm_pclose(fp);
if (rc != 0 || buf[0] == '\0') {
return CBM_NOT_FOUND;
}
*out = git_strdup(buf);
return *out ? 0 : CBM_NOT_FOUND;
}
static bool path_is_absolute(const char *path) {
if (!path || !path[0]) {
return false;
}
if (path[0] == '/') {
return true;
}
#ifdef _WIN32
return isalpha((unsigned char)path[0]) && path[1] == ':';
#else
return false;
#endif
}
static char *join_root_relative(const char *root, const char *rel) {
if (!root || !root[0]) {
return git_strdup(rel);
}
int n = snprintf(NULL, 0, "%s/%s", root, rel);
if (n < 0) {
return NULL;
}
char *out = (char *)malloc((size_t)n + 1);
if (!out) {
return NULL;
}
snprintf(out, (size_t)n + 1, "%s/%s", root, rel);
return out;
}
/* Derive the canonical repo root.
*
* Preferred (git 2.31+): abs_common_dir is `git rev-parse --path-format=absolute
* --git-common-dir` — git's OWN absolute, canonical common-dir. Because a main
* repo and its linked worktree both ask the same git binary, they resolve to the
* IDENTICAL path, so canonical_root is consistent across worktrees AND platforms
* with no manual join or realpath/_fullpath — which diverged under msys vs native
* path representations (#659 root cause, and the worktree==main-root breakage in
* test_pipeline.c git_context_linked_worktree). git also resolves the relative
* ".." internally, so the subdirectory case (#659) is fixed here too.
*
* Fallback (git < 2.31, no --path-format → abs_common_dir empty): the relative
* --git-common-dir is relative to input_path (the -C dir), so join against it and
* realpath-normalize the "..". Unix only — on Windows git emits an absolute
* common-dir so this branch isn't reached in practice, and _fullpath there
* reintroduces the msys divergence. */
static char *derive_canonical_root(const char *input_path, const char *worktree_root,
const char *git_common_dir, const char *abs_common_dir) {
char *root = NULL;
if (abs_common_dir && abs_common_dir[0] && path_is_absolute(abs_common_dir)) {
root = git_strdup(abs_common_dir);
if (!root) {
return NULL;
}
} else {
const char *src = git_common_dir && git_common_dir[0] ? git_common_dir : worktree_root;
if (!src) {
return git_strdup("");
}
#ifndef _WIN32
root = path_is_absolute(src) ? git_strdup(src) : join_root_relative(input_path, src);
if (!root) {
return NULL;
}
{
char resolved[4096];
if (realpath(root, resolved) != NULL) {
free(root);
root = git_strdup(resolved);
if (!root) {
return NULL;
}
}
}
#else
(void)input_path;
root = path_is_absolute(src) ? git_strdup(src) : join_root_relative(worktree_root, src);
if (!root) {
return NULL;
}
#endif
}
size_t len = strlen(root);
while (len > 1 && (root[len - 1] == '/' || root[len - 1] == '\\')) {
root[--len] = '\0';
}
if (len >= 5 && strcmp(root + len - 5, "/.git") == 0) {
root[len - 5] = '\0';
}
#ifdef _WIN32
else if (len >= 5 && strcmp(root + len - 5, "\\.git") == 0) {
root[len - 5] = '\0';
}
#endif
return root;
}
static char *slug_from_branch(const char *branch, bool detached) {
const char *fallback = detached ? "detached" : "working-tree";
const char *src = detached ? fallback : (branch && branch[0] ? branch : fallback);
size_t len = strlen(src);
char *slug = (char *)malloc(len + 1);
if (!slug) {
return NULL;
}
size_t j = 0;
bool in_dash = false;
for (size_t i = 0; i < len; i++) {
unsigned char c = (unsigned char)src[i];
if (isalnum(c) || c == '-' || c == '_' || c == '.') {
if (j == 0 && c == '-') {
in_dash = true;
continue;
}
slug[j++] = (char)c;
in_dash = false;
} else if (j > 0 && !in_dash) {
slug[j++] = '-';
in_dash = true;
}
}
while (j > 0 && slug[j - 1] == '-') {
j--;
}
slug[j] = '\0';
if (slug[0] == '\0') {
free(slug);
return git_strdup(fallback);
}
return slug;
}
void cbm_git_context_free(cbm_git_context_t *ctx) {
if (!ctx) {
return;
}
free(ctx->input_path);
free(ctx->worktree_root);
free(ctx->git_dir);
free(ctx->git_common_dir);
free(ctx->canonical_root);
free(ctx->branch);
free(ctx->branch_slug);
free(ctx->head_sha);
free(ctx->base_sha);
memset(ctx, 0, sizeof(*ctx));
}
int cbm_git_context_resolve(const char *path, cbm_git_context_t *out) {
if (!out) {
return CBM_NOT_FOUND;
}
memset(out, 0, sizeof(*out));
if (!path || !path[0]) {
return CBM_NOT_FOUND;
}
out->input_path = git_strdup(path);
if (!out->input_path) {
return CBM_NOT_FOUND;
}
struct stat st;
out->root_exists = (stat(path, &st) == 0);
if (!out->root_exists) {
return 0;
}
if (git_capture(path, "rev-parse --show-toplevel", &out->worktree_root) != 0) {
out->is_git = false;
return 0;
}
out->is_git = true;
if (git_capture(path, "rev-parse --git-dir", &out->git_dir) != 0) {
out->git_dir = git_strdup("");
}
if (git_capture(path, "rev-parse --git-common-dir", &out->git_common_dir) != 0) {
out->git_common_dir = git_strdup("");
}
if (git_capture(path, "rev-parse --verify HEAD", &out->head_sha) != 0) {
out->head_sha = git_strdup("");
}
if (git_capture(path, "symbolic-ref --quiet --short HEAD", &out->branch) != 0) {
out->branch = git_strdup("DETACHED");
out->is_detached = true;
}
out->is_worktree =
out->git_dir && out->git_common_dir && strcmp(out->git_dir, out->git_common_dir) != 0;
/* git 2.31+ canonical absolute common-dir (best-effort; NULL on older git,
* where derive_canonical_root falls back to the relative common-dir). */
char *abs_common_dir = NULL;
(void)git_capture(path, "rev-parse --path-format=absolute --git-common-dir", &abs_common_dir);
out->canonical_root =
derive_canonical_root(path, out->worktree_root, out->git_common_dir, abs_common_dir);
free(abs_common_dir);
out->branch_slug = slug_from_branch(out->branch, out->is_detached);
if (git_capture(path, "merge-base HEAD @{upstream}", &out->base_sha) != 0) {
out->base_sha = git_strdup("");
}
if (!out->git_dir || !out->git_common_dir || !out->head_sha || !out->branch ||
!out->canonical_root || !out->branch_slug || !out->base_sha) {
cbm_git_context_free(out);
return CBM_NOT_FOUND;
}
return 0;
}
char *cbm_git_context_branch_qn(const char *project_name, const cbm_git_context_t *ctx) {
const char *project = project_name && project_name[0] ? project_name : "project";
const char *slug = "working-tree";
if (ctx) {
if (ctx->is_detached) {
slug = "detached";
} else if (ctx->is_git && ctx->branch_slug && ctx->branch_slug[0]) {
slug = ctx->branch_slug;
}
}
int n = snprintf(NULL, 0, "%s.__branch__.%s", project, slug);
if (n < 0) {
return NULL;
}
char *out = (char *)malloc((size_t)n + 1);
if (!out) {
return NULL;
}
snprintf(out, (size_t)n + 1, "%s.__branch__.%s", project, slug);
return out;
}
static bool append_fmt_checked(char *buf, int buf_size, int *off, const char *fmt, ...) {
if (!buf || !off || buf_size <= 0 || *off < 0 || *off >= buf_size) {
return false;
}
va_list ap;
va_start(ap, fmt);
int n = vsnprintf(buf + *off, (size_t)(buf_size - *off), fmt, ap);
va_end(ap);
if (n < 0 || n >= buf_size - *off) {
buf[buf_size - 1] = '\0';
return false;
}
*off += n;
return true;
}
static int json_escaped_len(const char *src) {
if (!src) {
return 0;
}
int len = 0;
for (int i = 0; src[i]; i++) {
unsigned char c = (unsigned char)src[i];
if (c == '"' || c == '\\' || c == '\n' || c == '\r' || c == '\t') {
len += 2;
} else if (c < 0x20) {
len += 6; /* \u00XX */
} else {
len++;
}
}
return len;
}
static bool json_append_bool(char *buf, int buf_size, int *off, const char *name, bool value,
bool comma) {
return append_fmt_checked(buf, buf_size, off, "\"%s\":%s%s", name, value ? "true" : "false",
comma ? "," : "");
}
static bool json_append_string(char *buf, int buf_size, int *off, const char *name,
const char *value, bool comma) {
int needed = json_escaped_len(value ? value : "");
char *escaped = malloc((size_t)needed + 1);
if (!escaped) {
return false;
}
int actual = cbm_json_escape(escaped, needed + 1, value ? value : "");
bool ok = actual == needed && append_fmt_checked(buf, buf_size, off, "\"%s\":\"%s\"%s", name,
escaped, comma ? "," : "");
free(escaped);
return ok;
}
int cbm_git_context_props_json(const cbm_git_context_t *ctx, char *buf, int buf_size) {
if (!ctx || !buf || buf_size <= 0) {
return 0;
}
int off = 0;
bool ok =
append_fmt_checked(buf, buf_size, &off, "{") &&
json_append_bool(buf, buf_size, &off, "is_git", ctx->is_git, true) &&
json_append_bool(buf, buf_size, &off, "is_worktree", ctx->is_worktree, true) &&
json_append_bool(buf, buf_size, &off, "is_detached", ctx->is_detached, true) &&
json_append_bool(buf, buf_size, &off, "root_exists", ctx->root_exists, true) &&
json_append_string(buf, buf_size, &off, "canonical_root", ctx->canonical_root, true) &&
json_append_string(buf, buf_size, &off, "worktree_root", ctx->worktree_root, true) &&
json_append_string(buf, buf_size, &off, "git_common_dir", ctx->git_common_dir, true) &&
json_append_string(buf, buf_size, &off, "branch", ctx->branch, true) &&
json_append_string(buf, buf_size, &off, "head_sha", ctx->head_sha, true) &&
json_append_string(buf, buf_size, &off, "base_sha", ctx->base_sha, false) &&
append_fmt_checked(buf, buf_size, &off, "}");
if (!ok) {
if (buf_size > 0) {
buf[0] = '\0';
}
return 0;
}
return off;
}