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 <stdio.h>
18 #include <stdlib.h>
19 #include <sha1.h>
21 #include "got_error.h"
22 #include "got_object.h"
23 #include "got_refs.h"
24 #include "got_repository.h"
26 #define RUN_TEST(expr, name) \
27 if (!(expr)) { printf("test %s failed", (name)); failure = 1; }
29 #define GOT_REPO_PATH "../../../"
31 static int
32 repo_resolve_head_ref(const char *repo_path)
33 {
34 const struct got_error *err;
35 struct got_repository *repo;
36 struct got_reference *head_ref;
37 struct got_object_id *id;
38 int ret;
40 err = got_repo_open(&repo, repo_path);
41 if (err != NULL || repo == NULL)
42 return 0;
43 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
44 if (err != NULL || head_ref == NULL)
45 return 0;
46 err = got_ref_resolve(&id, repo, head_ref);
47 if (err != NULL || head_ref == NULL)
48 return 0;
49 free(id);
50 got_ref_close(head_ref);
51 got_repo_close(repo);
52 return 1;
53 }
55 int
56 main(int argc, const char *argv[])
57 {
58 int failure = 0;
59 const char *repo_path;
61 if (argc == 1)
62 repo_path = GOT_REPO_PATH;
63 else if (argc == 2)
64 repo_path = argv[1];
65 else {
66 fprintf(stderr, "usage: repository_test [REPO_PATH]\n");
67 return 1;
68 }
70 RUN_TEST(repo_resolve_head_ref(repo_path), "resolve_head_ref");
72 return failure ? 1 : 0;
73 }