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
|
/*-
* SPDX-License-Identifier: AGPL-3.0-only
* SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org>
*/
#include "x.h"
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;
}
|