Blob


1 /*
2 * Copyright (c) 2017 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sha1.h>
23 #include "got_error.h"
24 #include "got_object.h"
25 #include "got_refs.h"
26 #include "got_repository.h"
28 #define RUN_TEST(expr, name) \
29 if (!(expr)) { printf("test %s failed", (name)); failure = 1; }
31 #define GOT_REPO_PATH "../../../"
33 static int
34 repo_read_object_header(const char *repo_path)
35 {
36 const struct got_error *err;
37 struct got_repository *repo;
38 struct got_reference *head_ref;
39 struct got_object_id *id;
40 struct got_object *obj;
41 char buf[SHA1_DIGEST_STRING_LENGTH];
42 int ret;
44 err = got_repo_open(&repo, repo_path);
45 if (err != NULL || repo == NULL)
46 return 0;
47 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
48 if (err != NULL || head_ref == NULL)
49 return 0;
50 err = got_ref_resolve(&id, repo, head_ref);
51 if (err != NULL || head_ref == NULL)
52 return 0;
53 printf("HEAD is at %s\n", got_object_id_str(id, buf, sizeof(buf)));
54 err = got_object_open(&obj, repo, id);
55 if (err != NULL || obj == NULL)
56 return 0;
57 printf("object type=%d size=%lu\n", obj->type, obj->size);
58 if (obj->type == GOT_OBJ_TYPE_COMMIT) {
59 struct got_commit_object *commit;
60 struct got_parent_id *pid;
62 err = got_object_commit_open(&commit, repo, obj);
63 if (err != NULL || commit == NULL)
64 return 0;
65 printf("tree: %s\n",
66 got_object_id_str(&commit->tree_id, buf, sizeof(buf)));
67 printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
68 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
69 printf("%s\n",
70 got_object_id_str(&pid->id, buf, sizeof(buf)));
71 }
72 printf("author: %s\n", commit->author);
73 printf("committer: %s\n", commit->committer);
74 printf("log: %s\n", commit->logmsg);
75 }
76 got_object_close(obj);
77 free(id);
78 got_ref_close(head_ref);
79 got_repo_close(repo);
80 return 1;
81 }
83 int
84 main(int argc, const char *argv[])
85 {
86 int failure = 0;
87 const char *repo_path;
89 if (argc == 1)
90 repo_path = GOT_REPO_PATH;
91 else if (argc == 2)
92 repo_path = argv[1];
93 else {
94 fprintf(stderr, "usage: repository_test [REPO_PATH]\n");
95 return 1;
96 }
98 RUN_TEST(repo_read_object_header(repo_path), "read_object_header");
100 return failure ? 1 : 0;