GCC Code Coverage Report


Directory: src/
File: src/git.c
Date: 2024-04-25 03:45:42
Exec Total Coverage
Lines: 39 55 70.9%
Branches: 10 64 15.6%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2022 Egor Tensin <egor@tensin.name>
3 * This file is part of the "cimple" project.
4 * For details, see https://github.com/egor-tensin/cimple.
5 * Distributed under the MIT License.
6 */
7
8 #include "git.h"
9 #include "log.h"
10
11 #include <git2.h>
12
13 #include <stdlib.h>
14
15 #define git_log_err(fn) \
16 do { \
17 const git_error *error = git_error_last(); \
18 const char *msg = error && error->message ? error->message : "???"; \
19 log_err("%s: %s\n", fn, msg); \
20 } while (0)
21
22 54 int libgit_init(void)
23 {
24 54 int ret = 0;
25
26 54 ret = git_libgit2_init();
27
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
54 if (ret < 0) {
28 git_log_err("git_libgit2_init");
29 return ret;
30 }
31
32 54 return 0;
33 }
34
35 54 void libgit_shutdown(void)
36 {
37 54 git_libgit2_shutdown();
38 54 }
39
40 9180 int libgit_clone(git_repository **repo, const char *url, const char *dir)
41 {
42 git_clone_options opts;
43 9180 int ret = 0;
44
45
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9180 times.
9180 log("Cloning git repository from %s to %s\n", url, dir);
46
47 9180 ret = git_clone_options_init(&opts, GIT_CLONE_OPTIONS_VERSION);
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0) {
49 git_log_err("git_clone_options_init");
50 return ret;
51 }
52 9180 opts.checkout_opts.checkout_strategy = GIT_CHECKOUT_NONE;
53
54 9180 ret = git_clone(repo, url, dir, &opts);
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0) {
56 git_log_err("git_clone");
57 return ret;
58 }
59
60 9180 return 0;
61 }
62
63 9180 int libgit_clone_to_tmp(git_repository **repo, const char *url)
64 {
65 9180 char dir[] = "/tmp/git.XXXXXX";
66
67
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9180 times.
9180 if (!mkdtemp(dir)) {
68 log_errno("mkdtemp");
69 return -1;
70 }
71
72 9180 return libgit_clone(repo, url, dir);
73 }
74
75 9180 void libgit_repository_free(git_repository *repo)
76 {
77 9180 git_repository_free(repo);
78 9180 }
79
80 9180 int libgit_checkout(git_repository *repo, const char *rev)
81 {
82 git_checkout_options opts;
83 git_object *obj;
84 9180 int ret = 0;
85
86
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9180 times.
9180 log("Checking out revision %s\n", rev);
87
88 9180 ret = git_revparse_single(&obj, repo, rev);
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0) {
90 git_log_err("git_revparse_single");
91 return ret;
92 }
93
94 9180 ret = git_checkout_options_init(&opts, GIT_CHECKOUT_OPTIONS_VERSION);
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0) {
96 git_log_err("git_checkout_options_init");
97 goto free_obj;
98 }
99 9180 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
100
101 9180 ret = git_checkout_tree(repo, obj, &opts);
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0) {
103 git_log_err("git_checkout_tree");
104 goto free_obj;
105 }
106
107 9180 ret = git_repository_set_head_detached(repo, git_object_id(obj));
108
1/2
✓ Branch 0 taken 9180 times.
✗ Branch 1 not taken.
9180 if (ret < 0) {
109 git_log_err("git_repository_set_head_detached");
110 goto free_obj;
111 }
112
113 9180 free_obj:
114 9180 git_object_free(obj);
115
116 9180 return ret;
117 }
118