2 44edeea7 2019-04-11 stsp * Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 44edeea7 2019-04-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 44edeea7 2019-04-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 44edeea7 2019-04-11 stsp * copyright notice and this permission notice appear in all copies.
8 44edeea7 2019-04-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 44edeea7 2019-04-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 44edeea7 2019-04-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 44edeea7 2019-04-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 44edeea7 2019-04-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 44edeea7 2019-04-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 44edeea7 2019-04-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 44edeea7 2019-04-11 stsp #include <sys/types.h>
18 44edeea7 2019-04-11 stsp #include <sys/stat.h>
19 44edeea7 2019-04-11 stsp #include <sys/queue.h>
21 787c8eb6 2019-07-11 stsp #include <ctype.h>
22 51130c02 2019-04-13 stsp #include <errno.h>
23 44edeea7 2019-04-11 stsp #include <fcntl.h>
24 44edeea7 2019-04-11 stsp #include <stdio.h>
25 44edeea7 2019-04-11 stsp #include <stdlib.h>
26 44edeea7 2019-04-11 stsp #include <string.h>
27 44edeea7 2019-04-11 stsp #include <stdint.h>
28 44edeea7 2019-04-11 stsp #include <sha1.h>
29 a14a8cf6 2019-04-11 stsp #include <unistd.h>
30 44edeea7 2019-04-11 stsp #include <zlib.h>
32 44edeea7 2019-04-11 stsp #include "got_error.h"
33 44edeea7 2019-04-11 stsp #include "got_object.h"
34 44edeea7 2019-04-11 stsp #include "got_repository.h"
35 44edeea7 2019-04-11 stsp #include "got_opentemp.h"
36 324d37e7 2019-05-11 stsp #include "got_path.h"
38 44edeea7 2019-04-11 stsp #include "got_lib_sha1.h"
39 44edeea7 2019-04-11 stsp #include "got_lib_deflate.h"
40 44edeea7 2019-04-11 stsp #include "got_lib_delta.h"
41 44edeea7 2019-04-11 stsp #include "got_lib_object.h"
42 6af1ccbd 2019-08-16 stsp #include "got_lib_object_parse.h"
43 44edeea7 2019-04-11 stsp #include "got_lib_lockfile.h"
45 44edeea7 2019-04-11 stsp #ifndef nitems
46 44edeea7 2019-04-11 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
49 ac1c5662 2019-04-13 stsp static const struct got_error *
50 76f564d5 2019-04-14 stsp create_object_file(struct got_object_id *id, FILE *content,
51 ac1c5662 2019-04-13 stsp struct got_repository *repo)
53 ac1c5662 2019-04-13 stsp const struct got_error *err = NULL, *unlock_err = NULL;
54 c6f826b4 2019-04-13 stsp char *objpath = NULL, *tmppath = NULL;
55 c6f826b4 2019-04-13 stsp FILE *tmpfile = NULL;
56 ac1c5662 2019-04-13 stsp struct got_lockfile *lf = NULL;
57 c6f826b4 2019-04-13 stsp size_t tmplen = 0;
59 ac1c5662 2019-04-13 stsp err = got_object_get_path(&objpath, id, repo);
63 c6f826b4 2019-04-13 stsp err = got_opentemp_named(&tmppath, &tmpfile, objpath);
65 ac1c5662 2019-04-13 stsp char *parent_path;
66 ac1c5662 2019-04-13 stsp if (!(err->code == GOT_ERR_ERRNO && errno == ENOENT))
68 ac1c5662 2019-04-13 stsp err = got_path_dirname(&parent_path, objpath);
71 ac1c5662 2019-04-13 stsp err = got_path_mkdir(parent_path);
72 ac1c5662 2019-04-13 stsp free(parent_path);
75 c6f826b4 2019-04-13 stsp err = got_opentemp_named(&tmppath, &tmpfile, objpath);
80 c6f826b4 2019-04-13 stsp err = got_deflate_to_file(&tmplen, content, tmpfile);
84 ac1c5662 2019-04-13 stsp err = got_lockfile_lock(&lf, objpath);
88 c6f826b4 2019-04-13 stsp if (rename(tmppath, objpath) != 0) {
89 638f9024 2019-05-13 stsp err = got_error_from_errno3("rename", tmppath, objpath);
92 c6f826b4 2019-04-13 stsp free(tmppath);
93 c6f826b4 2019-04-13 stsp tmppath = NULL;
95 ac1c5662 2019-04-13 stsp if (chmod(objpath, GOT_DEFAULT_FILE_MODE) != 0) {
96 638f9024 2019-05-13 stsp err = got_error_from_errno2("chmod", objpath);
100 ac1c5662 2019-04-13 stsp free(objpath);
101 c6f826b4 2019-04-13 stsp if (tmppath) {
102 c6f826b4 2019-04-13 stsp if (unlink(tmppath) != 0 && err == NULL)
103 638f9024 2019-05-13 stsp err = got_error_from_errno2("unlink", tmppath);
104 c6f826b4 2019-04-13 stsp free(tmppath);
106 c6f826b4 2019-04-13 stsp if (tmpfile && fclose(tmpfile) != 0 && err == NULL)
107 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
109 ac1c5662 2019-04-13 stsp unlock_err = got_lockfile_unlock(lf);
110 ac1c5662 2019-04-13 stsp return err ? err : unlock_err;
113 44edeea7 2019-04-11 stsp const struct got_error *
114 f970685c 2019-04-13 stsp got_object_blob_create(struct got_object_id **id, const char *ondisk_path,
115 f970685c 2019-04-13 stsp struct got_repository *repo)
117 ac1c5662 2019-04-13 stsp const struct got_error *err = NULL;
118 ac1c5662 2019-04-13 stsp char *header = NULL;
119 ac1c5662 2019-04-13 stsp FILE *blobfile = NULL;
120 44edeea7 2019-04-11 stsp int fd = -1;
121 44edeea7 2019-04-11 stsp struct stat sb;
122 44edeea7 2019-04-11 stsp SHA1_CTX sha1_ctx;
123 ac1c5662 2019-04-13 stsp size_t headerlen = 0, n;
125 44edeea7 2019-04-11 stsp *id = NULL;
127 44edeea7 2019-04-11 stsp SHA1Init(&sha1_ctx);
129 44edeea7 2019-04-11 stsp fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
130 44edeea7 2019-04-11 stsp if (fd == -1)
131 638f9024 2019-05-13 stsp return got_error_from_errno2("open", ondisk_path);
133 44edeea7 2019-04-11 stsp if (fstat(fd, &sb) == -1) {
134 638f9024 2019-05-13 stsp err = got_error_from_errno2("fstat", ondisk_path);
138 44edeea7 2019-04-11 stsp if (asprintf(&header, "%s %lld", GOT_OBJ_LABEL_BLOB,
139 44edeea7 2019-04-11 stsp sb.st_size) == -1) {
140 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
143 ffb286fd 2019-04-11 stsp headerlen = strlen(header) + 1;
144 ffb286fd 2019-04-11 stsp SHA1Update(&sha1_ctx, header, headerlen);
146 81984c6b 2019-04-11 stsp blobfile = got_opentemp();
147 81984c6b 2019-04-11 stsp if (blobfile == NULL) {
148 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
152 ac1c5662 2019-04-13 stsp n = fwrite(header, 1, headerlen, blobfile);
153 ac1c5662 2019-04-13 stsp if (n != headerlen) {
154 f16c2465 2019-04-11 stsp err = got_ferror(blobfile, GOT_ERR_IO);
158 44edeea7 2019-04-11 stsp char buf[8192];
159 a14a8cf6 2019-04-11 stsp ssize_t inlen;
161 a14a8cf6 2019-04-11 stsp inlen = read(fd, buf, sizeof(buf));
162 a14a8cf6 2019-04-11 stsp if (inlen == -1) {
163 638f9024 2019-05-13 stsp err = got_error_from_errno("read");
166 a14a8cf6 2019-04-11 stsp if (inlen == 0)
167 a14a8cf6 2019-04-11 stsp break; /* EOF */
168 44edeea7 2019-04-11 stsp SHA1Update(&sha1_ctx, buf, inlen);
169 ac1c5662 2019-04-13 stsp n = fwrite(buf, 1, inlen, blobfile);
170 ac1c5662 2019-04-13 stsp if (n != inlen) {
171 44edeea7 2019-04-11 stsp err = got_ferror(blobfile, GOT_ERR_IO);
176 44edeea7 2019-04-11 stsp *id = malloc(sizeof(**id));
177 44edeea7 2019-04-11 stsp if (*id == NULL) {
178 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
181 4be2a0b4 2019-04-14 stsp SHA1Final((*id)->sha1, &sha1_ctx);
183 44edeea7 2019-04-11 stsp if (fflush(blobfile) != 0) {
184 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
187 44edeea7 2019-04-11 stsp rewind(blobfile);
189 76f564d5 2019-04-14 stsp err = create_object_file(*id, blobfile, repo);
191 44edeea7 2019-04-11 stsp free(header);
192 44edeea7 2019-04-11 stsp if (fd != -1 && close(fd) != 0 && err == NULL)
193 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
194 44edeea7 2019-04-11 stsp if (blobfile && fclose(blobfile) != 0 && err == NULL)
195 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
198 44edeea7 2019-04-11 stsp *id = NULL;
200 ac1c5662 2019-04-13 stsp return err;
203 f91abf81 2019-04-14 stsp static const struct got_error *
204 f91abf81 2019-04-14 stsp mode2str(char *buf, size_t len, mode_t mode)
207 f91abf81 2019-04-14 stsp ret = snprintf(buf, len, "%o ", mode);
208 f91abf81 2019-04-14 stsp if (ret == -1 || ret >= len)
209 f91abf81 2019-04-14 stsp return got_error(GOT_ERR_NO_SPACE);
210 f91abf81 2019-04-14 stsp return NULL;
214 6af1ccbd 2019-08-16 stsp static const struct got_error *
215 6af1ccbd 2019-08-16 stsp te_git_name(char **name, struct got_tree_entry *te)
217 6af1ccbd 2019-08-16 stsp const struct got_error *err = NULL;
219 6af1ccbd 2019-08-16 stsp if (S_ISDIR(te->mode)) {
220 6af1ccbd 2019-08-16 stsp if (asprintf(name, "%s/", te->name) == -1)
221 6af1ccbd 2019-08-16 stsp err = got_error_from_errno("asprintf");
223 6af1ccbd 2019-08-16 stsp *name = strdup(te->name);
224 6af1ccbd 2019-08-16 stsp if (*name == NULL)
225 6af1ccbd 2019-08-16 stsp err = got_error_from_errno("strdup");
227 6af1ccbd 2019-08-16 stsp return err;
230 6af1ccbd 2019-08-16 stsp static const struct got_error *
231 6af1ccbd 2019-08-16 stsp insert_tree_entry(struct got_tree_entries *sorted, struct got_tree_entry *new)
233 6af1ccbd 2019-08-16 stsp const struct got_error *err = NULL;
234 6af1ccbd 2019-08-16 stsp struct got_tree_entry *te = NULL, *prev = NULL;
235 6af1ccbd 2019-08-16 stsp char *name;
238 6af1ccbd 2019-08-16 stsp if (SIMPLEQ_EMPTY(&sorted->head)) {
239 6af1ccbd 2019-08-16 stsp SIMPLEQ_INSERT_HEAD(&sorted->head, new, entry);
240 6af1ccbd 2019-08-16 stsp sorted->nentries++;
241 6af1ccbd 2019-08-16 stsp return NULL;
244 6af1ccbd 2019-08-16 stsp err = te_git_name(&name, new);
246 6af1ccbd 2019-08-16 stsp return err;
248 6af1ccbd 2019-08-16 stsp te = SIMPLEQ_FIRST(&sorted->head);
249 6af1ccbd 2019-08-16 stsp while (te) {
250 6af1ccbd 2019-08-16 stsp char *te_name;
251 6af1ccbd 2019-08-16 stsp if (prev == NULL) {
253 6af1ccbd 2019-08-16 stsp te = SIMPLEQ_NEXT(te, entry);
256 6af1ccbd 2019-08-16 stsp err = te_git_name(&te_name, te);
259 6af1ccbd 2019-08-16 stsp cmp = strcmp(te_name, name);
260 6af1ccbd 2019-08-16 stsp free(te_name);
261 6af1ccbd 2019-08-16 stsp if (cmp == 0) {
262 6af1ccbd 2019-08-16 stsp err = got_error(GOT_ERR_TREE_DUP_ENTRY);
265 6af1ccbd 2019-08-16 stsp if (cmp > 0) {
266 6af1ccbd 2019-08-16 stsp SIMPLEQ_INSERT_AFTER(&sorted->head, prev, new, entry);
267 6af1ccbd 2019-08-16 stsp sorted->nentries++;
271 6af1ccbd 2019-08-16 stsp te = SIMPLEQ_NEXT(te, entry);
274 6af1ccbd 2019-08-16 stsp if (te == NULL) {
275 6af1ccbd 2019-08-16 stsp SIMPLEQ_INSERT_TAIL(&sorted->head, new, entry);
276 6af1ccbd 2019-08-16 stsp sorted->nentries++;
279 6af1ccbd 2019-08-16 stsp free(name);
280 6af1ccbd 2019-08-16 stsp return err;
284 6af1ccbd 2019-08-16 stsp * Git expects directory tree entries to be sorted with an imaginary slash
285 6af1ccbd 2019-08-16 stsp * appended to their name, and will break otherwise. Let's be nice.
287 6af1ccbd 2019-08-16 stsp static const struct got_error *
288 6af1ccbd 2019-08-16 stsp sort_tree_entries_the_way_git_likes_it(struct got_tree_entries **sorted,
289 6af1ccbd 2019-08-16 stsp struct got_tree_entries *tree_entries)
291 6af1ccbd 2019-08-16 stsp const struct got_error *err = NULL;
292 6af1ccbd 2019-08-16 stsp struct got_tree_entry *te;
294 6af1ccbd 2019-08-16 stsp *sorted = malloc(sizeof(**sorted));
295 6af1ccbd 2019-08-16 stsp if (*sorted == NULL)
296 6af1ccbd 2019-08-16 stsp return got_error_from_errno("malloc");
298 6af1ccbd 2019-08-16 stsp (*sorted)->nentries = 0;
299 6af1ccbd 2019-08-16 stsp SIMPLEQ_INIT(&(*sorted)->head);
301 6af1ccbd 2019-08-16 stsp SIMPLEQ_FOREACH(te, &tree_entries->head, entry) {
302 6af1ccbd 2019-08-16 stsp struct got_tree_entry *new;
303 6af1ccbd 2019-08-16 stsp err = got_object_tree_entry_dup(&new, te);
306 6af1ccbd 2019-08-16 stsp err = insert_tree_entry(*sorted, new);
312 6af1ccbd 2019-08-16 stsp free(*sorted);
313 6af1ccbd 2019-08-16 stsp *sorted = NULL;
316 6af1ccbd 2019-08-16 stsp return err;
319 f91abf81 2019-04-14 stsp const struct got_error *
320 f91abf81 2019-04-14 stsp got_object_tree_create(struct got_object_id **id,
321 6af1ccbd 2019-08-16 stsp struct got_tree_entries *tree_entries, struct got_repository *repo)
323 f91abf81 2019-04-14 stsp const struct got_error *err = NULL;
324 f91abf81 2019-04-14 stsp char modebuf[sizeof("100644 ")];
325 f91abf81 2019-04-14 stsp SHA1_CTX sha1_ctx;
326 f91abf81 2019-04-14 stsp char *header = NULL;
327 f91abf81 2019-04-14 stsp size_t headerlen, len = 0, n;
328 f91abf81 2019-04-14 stsp FILE *treefile = NULL;
329 6af1ccbd 2019-08-16 stsp struct got_tree_entries *entries; /* sorted according to Git rules */
330 f91abf81 2019-04-14 stsp struct got_tree_entry *te;
332 f91abf81 2019-04-14 stsp *id = NULL;
334 51c32763 2019-05-09 stsp SHA1Init(&sha1_ctx);
336 6af1ccbd 2019-08-16 stsp err = sort_tree_entries_the_way_git_likes_it(&entries, tree_entries);
338 6af1ccbd 2019-08-16 stsp return err;
340 f91abf81 2019-04-14 stsp SIMPLEQ_FOREACH(te, &entries->head, entry) {
341 f91abf81 2019-04-14 stsp err = mode2str(modebuf, sizeof(modebuf), te->mode);
344 f91abf81 2019-04-14 stsp len += strlen(modebuf) + strlen(te->name) + 1 +
345 f91abf81 2019-04-14 stsp SHA1_DIGEST_LENGTH;
348 f91abf81 2019-04-14 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
349 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
352 f91abf81 2019-04-14 stsp headerlen = strlen(header) + 1;
353 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, header, headerlen);
355 f91abf81 2019-04-14 stsp treefile = got_opentemp();
356 f91abf81 2019-04-14 stsp if (treefile == NULL) {
357 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
361 f91abf81 2019-04-14 stsp n = fwrite(header, 1, headerlen, treefile);
362 f91abf81 2019-04-14 stsp if (n != headerlen) {
363 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
367 f91abf81 2019-04-14 stsp SIMPLEQ_FOREACH(te, &entries->head, entry) {
368 f91abf81 2019-04-14 stsp err = mode2str(modebuf, sizeof(modebuf), te->mode);
371 f91abf81 2019-04-14 stsp len = strlen(modebuf);
372 f91abf81 2019-04-14 stsp n = fwrite(modebuf, 1, len, treefile);
373 f91abf81 2019-04-14 stsp if (n != len) {
374 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
377 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, modebuf, len);
379 f91abf81 2019-04-14 stsp len = strlen(te->name) + 1; /* must include NUL */
380 f91abf81 2019-04-14 stsp n = fwrite(te->name, 1, len, treefile);
381 f91abf81 2019-04-14 stsp if (n != len) {
382 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
385 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, te->name, len);
387 f91abf81 2019-04-14 stsp len = SHA1_DIGEST_LENGTH;
388 f91abf81 2019-04-14 stsp n = fwrite(te->id->sha1, 1, len, treefile);
389 f91abf81 2019-04-14 stsp if (n != len) {
390 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
393 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, te->id->sha1, len);
396 f91abf81 2019-04-14 stsp *id = malloc(sizeof(**id));
397 f91abf81 2019-04-14 stsp if (*id == NULL) {
398 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
401 4be2a0b4 2019-04-14 stsp SHA1Final((*id)->sha1, &sha1_ctx);
403 f91abf81 2019-04-14 stsp if (fflush(treefile) != 0) {
404 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
407 f91abf81 2019-04-14 stsp rewind(treefile);
409 76f564d5 2019-04-14 stsp err = create_object_file(*id, treefile, repo);
411 f91abf81 2019-04-14 stsp free(header);
412 6af1ccbd 2019-08-16 stsp got_object_tree_entries_close(entries);
413 6af1ccbd 2019-08-16 stsp free(entries);
414 f91abf81 2019-04-14 stsp if (treefile && fclose(treefile) != 0 && err == NULL)
415 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
418 de18fc63 2019-05-09 stsp *id = NULL;
420 de18fc63 2019-05-09 stsp return err;
423 de18fc63 2019-05-09 stsp const struct got_error *
424 de18fc63 2019-05-09 stsp got_object_commit_create(struct got_object_id **id,
425 de18fc63 2019-05-09 stsp struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
426 de18fc63 2019-05-09 stsp int nparents, const char *author, time_t author_time,
427 de18fc63 2019-05-09 stsp const char *committer, time_t committer_time,
428 de18fc63 2019-05-09 stsp const char *logmsg, struct got_repository *repo)
430 de18fc63 2019-05-09 stsp const struct got_error *err = NULL;
431 de18fc63 2019-05-09 stsp SHA1_CTX sha1_ctx;
432 de18fc63 2019-05-09 stsp char *header = NULL, *tree_str = NULL;
433 de18fc63 2019-05-09 stsp char *author_str = NULL, *committer_str = NULL;
434 de18fc63 2019-05-09 stsp char *id_str = NULL;
435 de18fc63 2019-05-09 stsp size_t headerlen, len = 0, n;
436 de18fc63 2019-05-09 stsp FILE *commitfile = NULL;
437 de18fc63 2019-05-09 stsp struct got_object_qid *qid;
438 787c8eb6 2019-07-11 stsp char *msg0, *msg;
440 de18fc63 2019-05-09 stsp *id = NULL;
442 de18fc63 2019-05-09 stsp SHA1Init(&sha1_ctx);
444 787c8eb6 2019-07-11 stsp msg0 = strdup(logmsg);
445 787c8eb6 2019-07-11 stsp if (msg0 == NULL)
446 787c8eb6 2019-07-11 stsp return got_error_from_errno("strdup");
447 787c8eb6 2019-07-11 stsp msg = msg0;
449 10796104 2019-07-11 stsp while (isspace((unsigned char)msg[0]))
451 787c8eb6 2019-07-11 stsp len = strlen(msg);
452 10796104 2019-07-11 stsp while (len > 0 && isspace((unsigned char)msg[len - 1])) {
453 787c8eb6 2019-07-11 stsp msg[len - 1] = '\0';
457 de18fc63 2019-05-09 stsp if (asprintf(&author_str, "%s%s %lld +0000\n",
458 de18fc63 2019-05-09 stsp GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
459 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
461 de18fc63 2019-05-09 stsp if (asprintf(&committer_str, "%s%s %lld +0000\n",
462 de18fc63 2019-05-09 stsp GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
463 de18fc63 2019-05-09 stsp committer ? committer_time : author_time)
465 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
469 de18fc63 2019-05-09 stsp len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
471 de18fc63 2019-05-09 stsp (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
472 787c8eb6 2019-07-11 stsp + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
474 de18fc63 2019-05-09 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
475 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
478 de18fc63 2019-05-09 stsp headerlen = strlen(header) + 1;
479 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, header, headerlen);
481 de18fc63 2019-05-09 stsp commitfile = got_opentemp();
482 de18fc63 2019-05-09 stsp if (commitfile == NULL) {
483 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
487 de18fc63 2019-05-09 stsp n = fwrite(header, 1, headerlen, commitfile);
488 de18fc63 2019-05-09 stsp if (n != headerlen) {
489 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
493 de18fc63 2019-05-09 stsp err = got_object_id_str(&id_str, tree_id);
496 de18fc63 2019-05-09 stsp if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
498 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
501 de18fc63 2019-05-09 stsp len = strlen(tree_str);
502 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, tree_str, len);
503 de18fc63 2019-05-09 stsp n = fwrite(tree_str, 1, len, commitfile);
504 de18fc63 2019-05-09 stsp if (n != len) {
505 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
509 3ce1b845 2019-07-15 stsp if (parent_ids) {
510 3ce1b845 2019-07-15 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
511 3ce1b845 2019-07-15 stsp char *parent_str = NULL;
513 3ce1b845 2019-07-15 stsp free(id_str);
515 3ce1b845 2019-07-15 stsp err = got_object_id_str(&id_str, qid->id);
518 3ce1b845 2019-07-15 stsp if (asprintf(&parent_str, "%s%s\n",
519 3ce1b845 2019-07-15 stsp GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
520 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
523 3ce1b845 2019-07-15 stsp len = strlen(parent_str);
524 3ce1b845 2019-07-15 stsp SHA1Update(&sha1_ctx, parent_str, len);
525 3ce1b845 2019-07-15 stsp n = fwrite(parent_str, 1, len, commitfile);
526 3ce1b845 2019-07-15 stsp if (n != len) {
527 3ce1b845 2019-07-15 stsp err = got_ferror(commitfile, GOT_ERR_IO);
528 3ce1b845 2019-07-15 stsp free(parent_str);
531 de18fc63 2019-05-09 stsp free(parent_str);
535 de18fc63 2019-05-09 stsp len = strlen(author_str);
536 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, author_str, len);
537 de18fc63 2019-05-09 stsp n = fwrite(author_str, 1, len, commitfile);
538 de18fc63 2019-05-09 stsp if (n != len) {
539 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
543 de18fc63 2019-05-09 stsp len = strlen(committer_str);
544 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, committer_str, len);
545 de18fc63 2019-05-09 stsp n = fwrite(committer_str, 1, len, commitfile);
546 de18fc63 2019-05-09 stsp if (n != len) {
547 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
551 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
552 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
553 de18fc63 2019-05-09 stsp if (n != 1) {
554 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
558 787c8eb6 2019-07-11 stsp len = strlen(msg);
559 787c8eb6 2019-07-11 stsp SHA1Update(&sha1_ctx, msg, len);
560 787c8eb6 2019-07-11 stsp n = fwrite(msg, 1, len, commitfile);
561 de18fc63 2019-05-09 stsp if (n != len) {
562 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
566 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
567 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
568 de18fc63 2019-05-09 stsp if (n != 1) {
569 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
573 de18fc63 2019-05-09 stsp *id = malloc(sizeof(**id));
574 de18fc63 2019-05-09 stsp if (*id == NULL) {
575 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
578 de18fc63 2019-05-09 stsp SHA1Final((*id)->sha1, &sha1_ctx);
580 de18fc63 2019-05-09 stsp if (fflush(commitfile) != 0) {
581 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
584 de18fc63 2019-05-09 stsp rewind(commitfile);
586 de18fc63 2019-05-09 stsp err = create_object_file(*id, commitfile, repo);
588 787c8eb6 2019-07-11 stsp free(msg0);
589 de18fc63 2019-05-09 stsp free(header);
590 de18fc63 2019-05-09 stsp free(tree_str);
591 de18fc63 2019-05-09 stsp free(author_str);
592 de18fc63 2019-05-09 stsp free(committer_str);
593 de18fc63 2019-05-09 stsp if (commitfile && fclose(commitfile) != 0 && err == NULL)
594 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
597 f91abf81 2019-04-14 stsp *id = NULL;
599 f91abf81 2019-04-14 stsp return err;