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
|
/*-
* SPDX-License-Identifier: AGPL-3.0-only
* SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
*/
#include "x.h"
static int append_buf(char **data, size_t *len, size_t *cap, const char *src, size_t n)
{
if (n == 0)
return 0;
size_t need = *len + n;
if (need > *cap) {
size_t newcap = *cap ? *cap * 2 : 256;
while (newcap < need)
newcap *= 2;
char *p = (char *)realloc(*data, newcap);
if (!p)
return -1;
*data = p;
*cap = newcap;
}
memcpy(*data + *len, src, n);
*len += n;
return 0;
}
int cmd_commit_tree_oid(git_repository *repo, struct bare_reader *reader, struct bare_writer *writer)
{
char hex[64] = { 0 };
if (bare_get_data(reader, (uint8_t *) hex, sizeof(hex) - 1) != BARE_ERROR_NONE) {
bare_put_uint(writer, 11);
return -1;
}
git_oid oid;
if (git_oid_fromstr(&oid, hex) != 0) {
bare_put_uint(writer, 14);
return -1;
}
git_commit *commit = NULL;
if (git_commit_lookup(&commit, repo, &oid) != 0) {
bare_put_uint(writer, 14);
return -1;
}
git_tree *tree = NULL;
if (git_commit_tree(&tree, commit) != 0) {
git_commit_free(commit);
bare_put_uint(writer, 14);
return -1;
}
const git_oid *toid = git_tree_id(tree);
bare_put_uint(writer, 0);
bare_put_data(writer, toid->id, GIT_OID_RAWSZ);
git_tree_free(tree);
git_commit_free(commit);
return 0;
}
int cmd_commit_create(git_repository *repo, struct bare_reader *reader, struct bare_writer *writer)
{
char treehex[64] = { 0 };
if (bare_get_data(reader, (uint8_t *) treehex, sizeof(treehex) - 1) != BARE_ERROR_NONE) {
bare_put_uint(writer, 11);
return -1;
}
git_oid tree_oid;
if (git_oid_fromstr(&tree_oid, treehex) != 0) {
bare_put_uint(writer, 15);
return -1;
}
uint64_t pcnt = 0;
if (bare_get_uint(reader, &pcnt) != BARE_ERROR_NONE) {
bare_put_uint(writer, 11);
return -1;
}
git_commit **parents = NULL;
if (pcnt > 0) {
parents = (git_commit **) calloc(pcnt, sizeof(git_commit *));
if (!parents) {
bare_put_uint(writer, 15);
return -1;
}
for (uint64_t i = 0; i < pcnt; i++) {
char phex[64] = { 0 };
if (bare_get_data(reader, (uint8_t *) phex, sizeof(phex) - 1) != BARE_ERROR_NONE) {
bare_put_uint(writer, 11);
goto fail;
}
git_oid poid;
if (git_oid_fromstr(&poid, phex) != 0) {
bare_put_uint(writer, 15);
goto fail;
}
if (git_commit_lookup(&parents[i], repo, &poid) != 0) {
bare_put_uint(writer, 15);
goto fail;
}
}
}
char aname[512] = { 0 };
char aemail[512] = { 0 };
if (bare_get_data(reader, (uint8_t *) aname, sizeof(aname) - 1) != BARE_ERROR_NONE) {
bare_put_uint(writer, 11);
goto fail;
}
if (bare_get_data(reader, (uint8_t *) aemail, sizeof(aemail) - 1) != BARE_ERROR_NONE) {
bare_put_uint(writer, 11);
goto fail;
}
int64_t when = 0;
int64_t tzoff = 0;
if (bare_get_i64(reader, &when) != BARE_ERROR_NONE) {
bare_put_uint(writer, 11);
goto fail;
}
if (bare_get_i64(reader, &tzoff) != BARE_ERROR_NONE) {
bare_put_uint(writer, 11);
goto fail;
}
char *message = NULL;
{
uint64_t msz = 0;
if (bare_get_uint(reader, &msz) != BARE_ERROR_NONE) {
bare_put_uint(writer, 11);
goto fail;
}
message = (char *)malloc(msz + 1);
if (!message) {
bare_put_uint(writer, 15);
goto fail;
}
if (bare_get_fixed_data(reader, (uint8_t *) message, msz) != BARE_ERROR_NONE) {
free(message);
bare_put_uint(writer, 11);
goto fail;
}
message[msz] = '\0';
}
git_signature *sig = NULL;
if (git_signature_new(&sig, aname, aemail, (git_time_t) when, (int)tzoff) != 0) {
free(message);
bare_put_uint(writer, 19);
goto fail;
}
git_tree *tree = NULL;
if (git_tree_lookup(&tree, repo, &tree_oid) != 0) {
git_signature_free(sig);
free(message);
bare_put_uint(writer, 19);
goto fail;
}
git_oid out;
int rc = git_commit_create(&out, repo, NULL, sig, sig, NULL, message, tree,
(int)pcnt, (const git_commit **)parents);
git_tree_free(tree);
git_signature_free(sig);
free(message);
if (rc != 0) {
bare_put_uint(writer, 19);
goto fail;
}
bare_put_uint(writer, 0);
bare_put_data(writer, out.id, GIT_OID_RAWSZ);
if (parents) {
for (uint64_t i = 0; i < pcnt; i++)
if (parents[i])
git_commit_free(parents[i]);
free(parents);
}
return 0;
fail:
if (parents) {
for (uint64_t i = 0; i < pcnt; i++)
if (parents[i])
git_commit_free(parents[i]);
free(parents);
}
return -1;
}
int cmd_update_ref(git_repository *repo, struct bare_reader *reader, struct bare_writer *writer)
{
char refname[4096] = { 0 };
char commithex[64] = { 0 };
if (bare_get_data(reader, (uint8_t *) refname, sizeof(refname) - 1) != BARE_ERROR_NONE) {
bare_put_uint(writer, 11);
return -1;
}
if (bare_get_data(reader, (uint8_t *) commithex, sizeof(commithex) - 1)
!= BARE_ERROR_NONE) {
bare_put_uint(writer, 11);
return -1;
}
git_oid oid;
if (git_oid_fromstr(&oid, commithex) != 0) {
bare_put_uint(writer, 18);
return -1;
}
git_reference *out = NULL;
int rc = git_reference_create(&out, repo, refname, &oid, 1, NULL);
if (rc != 0) {
bare_put_uint(writer, 18);
return -1;
}
git_reference_free(out);
bare_put_uint(writer, 0);
return 0;
}
int cmd_commit_info(git_repository *repo, struct bare_reader *reader, struct bare_writer *writer)
{
char hex[64] = { 0 };
if (bare_get_data(reader, (uint8_t *) hex, sizeof(hex) - 1) != BARE_ERROR_NONE) {
bare_put_uint(writer, 11);
return -1;
}
git_oid oid;
if (git_oid_fromstr(&oid, hex) != 0) {
bare_put_uint(writer, 14);
return -1;
}
git_commit *commit = NULL;
if (git_commit_lookup(&commit, repo, &oid) != 0) {
bare_put_uint(writer, 14);
return -1;
}
const git_signature *author = git_commit_author(commit);
const git_signature *committer = git_commit_committer(commit);
const char *aname = author && author->name ? author->name : "";
const char *aemail = author && author->email ? author->email : "";
git_time_t awhen = author ? author->when.time : 0;
int aoffset = author ? author->when.offset : 0;
const char *cname = committer && committer->name ? committer->name : "";
const char *cemail = committer && committer->email ? committer->email : "";
git_time_t cwhen = committer ? committer->when.time : 0;
int coffset = committer ? committer->when.offset : 0;
const char *message = git_commit_message(commit);
if (!message) message = "";
bare_put_uint(writer, 0);
/* Commit ID */
const git_oid *cid = git_commit_id(commit);
bare_put_data(writer, cid->id, GIT_OID_RAWSZ);
/* Author */
bare_put_data(writer, (const uint8_t *)aname, strlen(aname));
bare_put_data(writer, (const uint8_t *)aemail, strlen(aemail));
bare_put_i64(writer, (int64_t)awhen);
bare_put_i64(writer, (int64_t)aoffset);
/* Committer */
bare_put_data(writer, (const uint8_t *)cname, strlen(cname));
bare_put_data(writer, (const uint8_t *)cemail, strlen(cemail));
bare_put_i64(writer, (int64_t)cwhen);
bare_put_i64(writer, (int64_t)coffset);
/* Message */
bare_put_data(writer, (const uint8_t *)message, strlen(message));
/* Parents */
uint32_t pcnt = git_commit_parentcount(commit);
bare_put_uint(writer, (uint64_t)pcnt);
for (uint32_t i = 0; i < pcnt; i++) {
const git_commit *p = NULL;
if (git_commit_parent((git_commit **)&p, commit, i) == 0 && p) {
const git_oid *po = git_commit_id(p);
bare_put_data(writer, po->id, GIT_OID_RAWSZ);
git_commit_free((git_commit *)p);
} else {
uint8_t zero[GIT_OID_RAWSZ] = {0};
bare_put_data(writer, zero, GIT_OID_RAWSZ);
}
}
/* Structured diff */
git_tree *tree = NULL;
if (git_commit_tree(&tree, commit) != 0) {
git_commit_free(commit);
bare_put_uint(writer, 15);
return -1;
}
git_diff *diff = NULL;
if (pcnt == 0) {
if (git_diff_tree_to_tree(&diff, repo, NULL, tree, NULL) != 0) {
git_tree_free(tree);
git_commit_free(commit);
bare_put_uint(writer, 15);
return -1;
}
} else {
git_commit *parent = NULL;
git_tree *ptree = NULL;
if (git_commit_parent(&parent, commit, 0) != 0 || git_commit_tree(&ptree, parent) != 0) {
if (parent) git_commit_free(parent);
git_tree_free(tree);
git_commit_free(commit);
bare_put_uint(writer, 15);
return -1;
}
if (git_diff_tree_to_tree(&diff, repo, ptree, tree, NULL) != 0) {
git_tree_free(ptree);
git_commit_free(parent);
git_tree_free(tree);
git_commit_free(commit);
bare_put_uint(writer, 15);
return -1;
}
git_tree_free(ptree);
git_commit_free(parent);
}
size_t files = git_diff_num_deltas(diff);
bare_put_uint(writer, (uint64_t)files);
for (size_t i = 0; i < files; i++) {
git_patch *patch = NULL;
if (git_patch_from_diff(&patch, diff, i) != 0) {
/* empty diff */
bare_put_uint(writer, 0);
bare_put_uint(writer, 0);
bare_put_data(writer, (const uint8_t *)"", 0);
bare_put_data(writer, (const uint8_t *)"", 0);
bare_put_uint(writer, 0);
continue;
}
const git_diff_delta *delta = git_patch_get_delta(patch);
uint32_t from_mode = delta ? delta->old_file.mode : 0;
uint32_t to_mode = delta ? delta->new_file.mode : 0;
const char *from_path = (delta && delta->old_file.path) ? delta->old_file.path : "";
const char *to_path = (delta && delta->new_file.path) ? delta->new_file.path : "";
bare_put_uint(writer, (uint64_t)from_mode);
bare_put_uint(writer, (uint64_t)to_mode);
bare_put_data(writer, (const uint8_t *)from_path, strlen(from_path));
bare_put_data(writer, (const uint8_t *)to_path, strlen(to_path));
size_t hunks = git_patch_num_hunks(patch);
uint64_t chunk_count = 0;
for (size_t h = 0; h < hunks; h++) {
const git_diff_hunk *hunk = NULL;
size_t lines = 0;
if (git_patch_get_hunk(&hunk, &lines, patch, h) != 0) continue;
int prev = -2;
for (size_t ln = 0; ln < lines; ln++) {
const git_diff_line *line = NULL;
if (git_patch_get_line_in_hunk(&line, patch, h, ln) != 0 || !line) continue;
int op = 0;
if (line->origin == '+') op = 1;
else if (line->origin == '-') op = 2;
else op = 0;
if (op != prev) { chunk_count++; prev = op; }
}
}
bare_put_uint(writer, chunk_count);
for (size_t h = 0; h < hunks; h++) {
const git_diff_hunk *hunk = NULL;
size_t lines = 0;
if (git_patch_get_hunk(&hunk, &lines, patch, h) != 0) continue;
int prev = -2;
struct {
char *data;
size_t len;
size_t cap;
} buf = {0};
for (size_t ln = 0; ln < lines; ln++) {
const git_diff_line *line = NULL;
if (git_patch_get_line_in_hunk(&line, patch, h, ln) != 0 || !line) continue;
int op = 0;
if (line->origin == '+') op = 1;
else if (line->origin == '-') op = 2;
else op = 0;
if (prev == -2) prev = op;
if (op != prev) {
bare_put_uint(writer, (uint64_t)prev);
bare_put_data(writer, (const uint8_t *)buf.data, buf.len);
free(buf.data);
buf.data = NULL; buf.len = 0; buf.cap = 0;
prev = op;
}
if (line->content && line->content_len > 0) {
if (append_buf(&buf.data, &buf.len, &buf.cap, line->content, line->content_len) != 0) {
free(buf.data);
git_patch_free(patch);
git_diff_free(diff);
git_tree_free(tree);
git_commit_free(commit);
bare_put_uint(writer, 15);
return -1;
}
}
}
if (prev != -2) {
bare_put_uint(writer, (uint64_t)prev);
bare_put_data(writer, (const uint8_t *)buf.data, buf.len);
free(buf.data);
}
}
git_patch_free(patch);
}
git_diff_free(diff);
git_tree_free(tree);
git_commit_free(commit);
return 0;
}
|