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);
308 dd4a6547 2019-08-17 stsp got_object_tree_entry_close(new);
314 6af1ccbd 2019-08-16 stsp free(*sorted);
315 6af1ccbd 2019-08-16 stsp *sorted = NULL;
318 6af1ccbd 2019-08-16 stsp return err;
321 f91abf81 2019-04-14 stsp const struct got_error *
322 f91abf81 2019-04-14 stsp got_object_tree_create(struct got_object_id **id,
323 6af1ccbd 2019-08-16 stsp struct got_tree_entries *tree_entries, struct got_repository *repo)
325 f91abf81 2019-04-14 stsp const struct got_error *err = NULL;
326 f91abf81 2019-04-14 stsp char modebuf[sizeof("100644 ")];
327 f91abf81 2019-04-14 stsp SHA1_CTX sha1_ctx;
328 f91abf81 2019-04-14 stsp char *header = NULL;
329 f91abf81 2019-04-14 stsp size_t headerlen, len = 0, n;
330 f91abf81 2019-04-14 stsp FILE *treefile = NULL;
331 6af1ccbd 2019-08-16 stsp struct got_tree_entries *entries; /* sorted according to Git rules */
332 f91abf81 2019-04-14 stsp struct got_tree_entry *te;
334 f91abf81 2019-04-14 stsp *id = NULL;
336 51c32763 2019-05-09 stsp SHA1Init(&sha1_ctx);
338 6af1ccbd 2019-08-16 stsp err = sort_tree_entries_the_way_git_likes_it(&entries, tree_entries);
340 6af1ccbd 2019-08-16 stsp return err;
342 f91abf81 2019-04-14 stsp SIMPLEQ_FOREACH(te, &entries->head, entry) {
343 f91abf81 2019-04-14 stsp err = mode2str(modebuf, sizeof(modebuf), te->mode);
346 f91abf81 2019-04-14 stsp len += strlen(modebuf) + strlen(te->name) + 1 +
347 f91abf81 2019-04-14 stsp SHA1_DIGEST_LENGTH;
350 f91abf81 2019-04-14 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TREE, len) == -1) {
351 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
354 f91abf81 2019-04-14 stsp headerlen = strlen(header) + 1;
355 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, header, headerlen);
357 f91abf81 2019-04-14 stsp treefile = got_opentemp();
358 f91abf81 2019-04-14 stsp if (treefile == NULL) {
359 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
363 f91abf81 2019-04-14 stsp n = fwrite(header, 1, headerlen, treefile);
364 f91abf81 2019-04-14 stsp if (n != headerlen) {
365 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
369 f91abf81 2019-04-14 stsp SIMPLEQ_FOREACH(te, &entries->head, entry) {
370 f91abf81 2019-04-14 stsp err = mode2str(modebuf, sizeof(modebuf), te->mode);
373 f91abf81 2019-04-14 stsp len = strlen(modebuf);
374 f91abf81 2019-04-14 stsp n = fwrite(modebuf, 1, len, treefile);
375 f91abf81 2019-04-14 stsp if (n != len) {
376 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
379 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, modebuf, len);
381 f91abf81 2019-04-14 stsp len = strlen(te->name) + 1; /* must include NUL */
382 f91abf81 2019-04-14 stsp n = fwrite(te->name, 1, len, treefile);
383 f91abf81 2019-04-14 stsp if (n != len) {
384 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
387 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, te->name, len);
389 f91abf81 2019-04-14 stsp len = SHA1_DIGEST_LENGTH;
390 f91abf81 2019-04-14 stsp n = fwrite(te->id->sha1, 1, len, treefile);
391 f91abf81 2019-04-14 stsp if (n != len) {
392 f91abf81 2019-04-14 stsp err = got_ferror(treefile, GOT_ERR_IO);
395 f91abf81 2019-04-14 stsp SHA1Update(&sha1_ctx, te->id->sha1, len);
398 f91abf81 2019-04-14 stsp *id = malloc(sizeof(**id));
399 f91abf81 2019-04-14 stsp if (*id == NULL) {
400 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
403 4be2a0b4 2019-04-14 stsp SHA1Final((*id)->sha1, &sha1_ctx);
405 f91abf81 2019-04-14 stsp if (fflush(treefile) != 0) {
406 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
409 f91abf81 2019-04-14 stsp rewind(treefile);
411 76f564d5 2019-04-14 stsp err = create_object_file(*id, treefile, repo);
413 f91abf81 2019-04-14 stsp free(header);
414 6af1ccbd 2019-08-16 stsp got_object_tree_entries_close(entries);
415 6af1ccbd 2019-08-16 stsp free(entries);
416 f91abf81 2019-04-14 stsp if (treefile && fclose(treefile) != 0 && err == NULL)
417 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
420 de18fc63 2019-05-09 stsp *id = NULL;
422 de18fc63 2019-05-09 stsp return err;
425 de18fc63 2019-05-09 stsp const struct got_error *
426 de18fc63 2019-05-09 stsp got_object_commit_create(struct got_object_id **id,
427 de18fc63 2019-05-09 stsp struct got_object_id *tree_id, struct got_object_id_queue *parent_ids,
428 de18fc63 2019-05-09 stsp int nparents, const char *author, time_t author_time,
429 de18fc63 2019-05-09 stsp const char *committer, time_t committer_time,
430 de18fc63 2019-05-09 stsp const char *logmsg, struct got_repository *repo)
432 de18fc63 2019-05-09 stsp const struct got_error *err = NULL;
433 de18fc63 2019-05-09 stsp SHA1_CTX sha1_ctx;
434 de18fc63 2019-05-09 stsp char *header = NULL, *tree_str = NULL;
435 de18fc63 2019-05-09 stsp char *author_str = NULL, *committer_str = NULL;
436 de18fc63 2019-05-09 stsp char *id_str = NULL;
437 de18fc63 2019-05-09 stsp size_t headerlen, len = 0, n;
438 de18fc63 2019-05-09 stsp FILE *commitfile = NULL;
439 de18fc63 2019-05-09 stsp struct got_object_qid *qid;
440 787c8eb6 2019-07-11 stsp char *msg0, *msg;
442 de18fc63 2019-05-09 stsp *id = NULL;
444 de18fc63 2019-05-09 stsp SHA1Init(&sha1_ctx);
446 787c8eb6 2019-07-11 stsp msg0 = strdup(logmsg);
447 787c8eb6 2019-07-11 stsp if (msg0 == NULL)
448 787c8eb6 2019-07-11 stsp return got_error_from_errno("strdup");
449 787c8eb6 2019-07-11 stsp msg = msg0;
451 10796104 2019-07-11 stsp while (isspace((unsigned char)msg[0]))
453 787c8eb6 2019-07-11 stsp len = strlen(msg);
454 10796104 2019-07-11 stsp while (len > 0 && isspace((unsigned char)msg[len - 1])) {
455 787c8eb6 2019-07-11 stsp msg[len - 1] = '\0';
459 de18fc63 2019-05-09 stsp if (asprintf(&author_str, "%s%s %lld +0000\n",
460 de18fc63 2019-05-09 stsp GOT_COMMIT_LABEL_AUTHOR, author, author_time) == -1)
461 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
463 de18fc63 2019-05-09 stsp if (asprintf(&committer_str, "%s%s %lld +0000\n",
464 de18fc63 2019-05-09 stsp GOT_COMMIT_LABEL_COMMITTER, committer ? committer : author,
465 de18fc63 2019-05-09 stsp committer ? committer_time : author_time)
467 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
471 de18fc63 2019-05-09 stsp len = strlen(GOT_COMMIT_LABEL_TREE) + SHA1_DIGEST_STRING_LENGTH +
473 de18fc63 2019-05-09 stsp (strlen(GOT_COMMIT_LABEL_PARENT) + SHA1_DIGEST_STRING_LENGTH) +
474 787c8eb6 2019-07-11 stsp + strlen(author_str) + strlen(committer_str) + 2 + strlen(msg);
476 de18fc63 2019-05-09 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_COMMIT, len) == -1) {
477 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
480 de18fc63 2019-05-09 stsp headerlen = strlen(header) + 1;
481 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, header, headerlen);
483 de18fc63 2019-05-09 stsp commitfile = got_opentemp();
484 de18fc63 2019-05-09 stsp if (commitfile == NULL) {
485 638f9024 2019-05-13 stsp err = got_error_from_errno("got_opentemp");
489 de18fc63 2019-05-09 stsp n = fwrite(header, 1, headerlen, commitfile);
490 de18fc63 2019-05-09 stsp if (n != headerlen) {
491 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
495 de18fc63 2019-05-09 stsp err = got_object_id_str(&id_str, tree_id);
498 de18fc63 2019-05-09 stsp if (asprintf(&tree_str, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str)
500 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
503 de18fc63 2019-05-09 stsp len = strlen(tree_str);
504 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, tree_str, len);
505 de18fc63 2019-05-09 stsp n = fwrite(tree_str, 1, len, commitfile);
506 de18fc63 2019-05-09 stsp if (n != len) {
507 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
511 3ce1b845 2019-07-15 stsp if (parent_ids) {
512 3ce1b845 2019-07-15 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
513 3ce1b845 2019-07-15 stsp char *parent_str = NULL;
515 3ce1b845 2019-07-15 stsp free(id_str);
517 3ce1b845 2019-07-15 stsp err = got_object_id_str(&id_str, qid->id);
520 3ce1b845 2019-07-15 stsp if (asprintf(&parent_str, "%s%s\n",
521 3ce1b845 2019-07-15 stsp GOT_COMMIT_LABEL_PARENT, id_str) == -1) {
522 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
525 3ce1b845 2019-07-15 stsp len = strlen(parent_str);
526 3ce1b845 2019-07-15 stsp SHA1Update(&sha1_ctx, parent_str, len);
527 3ce1b845 2019-07-15 stsp n = fwrite(parent_str, 1, len, commitfile);
528 3ce1b845 2019-07-15 stsp if (n != len) {
529 3ce1b845 2019-07-15 stsp err = got_ferror(commitfile, GOT_ERR_IO);
530 3ce1b845 2019-07-15 stsp free(parent_str);
533 de18fc63 2019-05-09 stsp free(parent_str);
537 de18fc63 2019-05-09 stsp len = strlen(author_str);
538 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, author_str, len);
539 de18fc63 2019-05-09 stsp n = fwrite(author_str, 1, len, commitfile);
540 de18fc63 2019-05-09 stsp if (n != len) {
541 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
545 de18fc63 2019-05-09 stsp len = strlen(committer_str);
546 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, committer_str, len);
547 de18fc63 2019-05-09 stsp n = fwrite(committer_str, 1, len, commitfile);
548 de18fc63 2019-05-09 stsp if (n != len) {
549 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
553 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
554 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
555 de18fc63 2019-05-09 stsp if (n != 1) {
556 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
560 787c8eb6 2019-07-11 stsp len = strlen(msg);
561 787c8eb6 2019-07-11 stsp SHA1Update(&sha1_ctx, msg, len);
562 787c8eb6 2019-07-11 stsp n = fwrite(msg, 1, len, commitfile);
563 de18fc63 2019-05-09 stsp if (n != len) {
564 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
568 de18fc63 2019-05-09 stsp SHA1Update(&sha1_ctx, "\n", 1);
569 de18fc63 2019-05-09 stsp n = fwrite("\n", 1, 1, commitfile);
570 de18fc63 2019-05-09 stsp if (n != 1) {
571 de18fc63 2019-05-09 stsp err = got_ferror(commitfile, GOT_ERR_IO);
575 de18fc63 2019-05-09 stsp *id = malloc(sizeof(**id));
576 de18fc63 2019-05-09 stsp if (*id == NULL) {
577 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
580 de18fc63 2019-05-09 stsp SHA1Final((*id)->sha1, &sha1_ctx);
582 de18fc63 2019-05-09 stsp if (fflush(commitfile) != 0) {
583 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
586 de18fc63 2019-05-09 stsp rewind(commitfile);
588 de18fc63 2019-05-09 stsp err = create_object_file(*id, commitfile, repo);
590 787c8eb6 2019-07-11 stsp free(msg0);
591 de18fc63 2019-05-09 stsp free(header);
592 de18fc63 2019-05-09 stsp free(tree_str);
593 de18fc63 2019-05-09 stsp free(author_str);
594 de18fc63 2019-05-09 stsp free(committer_str);
595 de18fc63 2019-05-09 stsp if (commitfile && fclose(commitfile) != 0 && err == NULL)
596 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("fclose");
599 8e7bd50a 2019-08-22 stsp *id = NULL;
601 8e7bd50a 2019-08-22 stsp return err;
604 8e7bd50a 2019-08-22 stsp const struct got_error *
605 8e7bd50a 2019-08-22 stsp got_object_tag_create(struct got_object_id **id,
606 8e7bd50a 2019-08-22 stsp const char *tag_name, struct got_object_id *object_id, const char *tagger,
607 8e7bd50a 2019-08-22 stsp time_t tagger_time, const char *tagmsg, struct got_repository *repo)
609 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
610 8e7bd50a 2019-08-22 stsp SHA1_CTX sha1_ctx;
611 8e7bd50a 2019-08-22 stsp char *header = NULL;
612 8e7bd50a 2019-08-22 stsp char *tag_str = NULL, *tagger_str = NULL;
613 8e7bd50a 2019-08-22 stsp char *id_str = NULL, *obj_str = NULL, *type_str = NULL;
614 8e7bd50a 2019-08-22 stsp size_t headerlen, len = 0, n;
615 8e7bd50a 2019-08-22 stsp FILE *tagfile = NULL;
616 8e7bd50a 2019-08-22 stsp char *msg0 = NULL, *msg;
617 8e7bd50a 2019-08-22 stsp const char *obj_type_str;
618 8e7bd50a 2019-08-22 stsp int obj_type;
620 8e7bd50a 2019-08-22 stsp *id = NULL;
622 8e7bd50a 2019-08-22 stsp SHA1Init(&sha1_ctx);
624 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str, object_id);
627 8e7bd50a 2019-08-22 stsp if (asprintf(&obj_str, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str) == -1) {
628 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
632 8e7bd50a 2019-08-22 stsp err = got_object_get_type(&obj_type, repo, object_id);
636 8e7bd50a 2019-08-22 stsp switch (obj_type) {
637 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
638 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_BLOB;
640 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
641 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_TREE;
643 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
644 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_COMMIT;
646 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
647 8e7bd50a 2019-08-22 stsp obj_type_str = GOT_OBJ_LABEL_TAG;
650 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_OBJ_TYPE);
654 8e7bd50a 2019-08-22 stsp if (asprintf(&type_str, "%s%s\n", GOT_TAG_LABEL_TYPE,
655 8e7bd50a 2019-08-22 stsp obj_type_str) == -1) {
656 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
660 8e7bd50a 2019-08-22 stsp if (asprintf(&tag_str, "%s%s\n", GOT_TAG_LABEL_TAG, tag_name) == -1) {
661 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
665 8e7bd50a 2019-08-22 stsp if (asprintf(&tagger_str, "%s%s %lld +0000\n",
666 2575b0eb 2019-08-22 stsp GOT_TAG_LABEL_TAGGER, tagger, tagger_time) == -1)
667 8e7bd50a 2019-08-22 stsp return got_error_from_errno("asprintf");
669 8e7bd50a 2019-08-22 stsp msg0 = strdup(tagmsg);
670 8e7bd50a 2019-08-22 stsp if (msg0 == NULL) {
671 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
674 8e7bd50a 2019-08-22 stsp msg = msg0;
676 8e7bd50a 2019-08-22 stsp while (isspace((unsigned char)msg[0]))
679 8e7bd50a 2019-08-22 stsp len = strlen(obj_str) + strlen(type_str) + strlen(tag_str) +
680 8e7bd50a 2019-08-22 stsp strlen(tagger_str) + 1 + strlen(msg) + 1;
682 8e7bd50a 2019-08-22 stsp if (asprintf(&header, "%s %zd", GOT_OBJ_LABEL_TAG, len) == -1) {
683 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
687 8e7bd50a 2019-08-22 stsp headerlen = strlen(header) + 1;
688 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, header, headerlen);
690 8e7bd50a 2019-08-22 stsp tagfile = got_opentemp();
691 8e7bd50a 2019-08-22 stsp if (tagfile == NULL) {
692 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_opentemp");
696 8e7bd50a 2019-08-22 stsp n = fwrite(header, 1, headerlen, tagfile);
697 8e7bd50a 2019-08-22 stsp if (n != headerlen) {
698 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
701 8e7bd50a 2019-08-22 stsp len = strlen(obj_str);
702 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, obj_str, len);
703 8e7bd50a 2019-08-22 stsp n = fwrite(obj_str, 1, len, tagfile);
704 8e7bd50a 2019-08-22 stsp if (n != len) {
705 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
708 8e7bd50a 2019-08-22 stsp len = strlen(type_str);
709 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, type_str, len);
710 8e7bd50a 2019-08-22 stsp n = fwrite(type_str, 1, len, tagfile);
711 8e7bd50a 2019-08-22 stsp if (n != len) {
712 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
716 8e7bd50a 2019-08-22 stsp len = strlen(tag_str);
717 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, tag_str, len);
718 8e7bd50a 2019-08-22 stsp n = fwrite(tag_str, 1, len, tagfile);
719 8e7bd50a 2019-08-22 stsp if (n != len) {
720 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
724 8e7bd50a 2019-08-22 stsp len = strlen(tagger_str);
725 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, tagger_str, len);
726 8e7bd50a 2019-08-22 stsp n = fwrite(tagger_str, 1, len, tagfile);
727 8e7bd50a 2019-08-22 stsp if (n != len) {
728 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
732 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, "\n", 1);
733 8e7bd50a 2019-08-22 stsp n = fwrite("\n", 1, 1, tagfile);
734 8e7bd50a 2019-08-22 stsp if (n != 1) {
735 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
739 8e7bd50a 2019-08-22 stsp len = strlen(msg);
740 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, msg, len);
741 8e7bd50a 2019-08-22 stsp n = fwrite(msg, 1, len, tagfile);
742 8e7bd50a 2019-08-22 stsp if (n != len) {
743 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
747 8e7bd50a 2019-08-22 stsp SHA1Update(&sha1_ctx, "\n", 1);
748 8e7bd50a 2019-08-22 stsp n = fwrite("\n", 1, 1, tagfile);
749 8e7bd50a 2019-08-22 stsp if (n != 1) {
750 8e7bd50a 2019-08-22 stsp err = got_ferror(tagfile, GOT_ERR_IO);
754 8e7bd50a 2019-08-22 stsp *id = malloc(sizeof(**id));
755 8e7bd50a 2019-08-22 stsp if (*id == NULL) {
756 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("malloc");
759 8e7bd50a 2019-08-22 stsp SHA1Final((*id)->sha1, &sha1_ctx);
761 8e7bd50a 2019-08-22 stsp if (fflush(tagfile) != 0) {
762 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("fflush");
765 8e7bd50a 2019-08-22 stsp rewind(tagfile);
767 8e7bd50a 2019-08-22 stsp err = create_object_file(*id, tagfile, repo);
769 8e7bd50a 2019-08-22 stsp free(msg0);
770 8e7bd50a 2019-08-22 stsp free(header);
771 8e7bd50a 2019-08-22 stsp free(obj_str);
772 8e7bd50a 2019-08-22 stsp free(tagger_str);
773 8e7bd50a 2019-08-22 stsp if (tagfile && fclose(tagfile) != 0 && err == NULL)
774 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
777 f91abf81 2019-04-14 stsp *id = NULL;
779 f91abf81 2019-04-14 stsp return err;