Blame


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