Directory: | src/ |
---|---|
File: | src/ci.c |
Date: | 2024-04-25 03:45:42 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 37 | 47 | 78.7% |
Branches: | 9 | 18 | 50.0% |
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 "ci.h" | ||
9 | #include "file.h" | ||
10 | #include "git.h" | ||
11 | #include "log.h" | ||
12 | #include "process.h" | ||
13 | |||
14 | #include <git2.h> | ||
15 | #include <stddef.h> | ||
16 | #include <stdlib.h> | ||
17 | |||
18 | /* clang-format off */ | ||
19 | static const char *ci_scripts[] = { | ||
20 | "./.ci.sh", | ||
21 | "./.ci", | ||
22 | "./ci.sh", | ||
23 | "./ci", | ||
24 | NULL, | ||
25 | }; | ||
26 | |||
27 | static const char *ci_env[] = { | ||
28 | "CI=y", | ||
29 | "CIMPLE=y", | ||
30 | NULL, | ||
31 | }; | ||
32 | /* clang-format on */ | ||
33 | |||
34 | 9180 | static int ci_run_script(const char *script, struct proc_output *result) | |
35 | { | ||
36 | 9180 | const char *args[] = {script, NULL}; | |
37 | 9180 | return proc_capture(args, ci_env, result); | |
38 | } | ||
39 | |||
40 | 9180 | int ci_run(struct proc_output *result) | |
41 | { | ||
42 |
1/2✓ Branch 0 taken 36720 times.
✗ Branch 1 not taken.
|
36720 | for (const char **script = ci_scripts; *script; ++script) { |
43 |
2/2✓ Branch 1 taken 27540 times.
✓ Branch 2 taken 9180 times.
|
36720 | if (!file_exists(*script)) |
44 | 27540 | continue; | |
45 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 9180 times.
|
9180 | log("Going to run: %s\n", *script); |
46 | 9180 | return ci_run_script(*script, result); | |
47 | } | ||
48 | |||
49 | ✗ | log("Couldn't find any CI scripts to run\n"); | |
50 | ✗ | return -1; | |
51 | } | ||
52 | |||
53 | 9180 | static void ci_cleanup_git_repo(git_repository *repo) | |
54 | { | ||
55 | 9180 | rm_rf(git_repository_workdir(repo)); | |
56 | 9180 | libgit_repository_free(repo); | |
57 | 9180 | } | |
58 | |||
59 | 9180 | static int ci_prepare_git_repo(git_repository **repo, const char *url, const char *rev) | |
60 | { | ||
61 | 9180 | int ret = 0; | |
62 | |||
63 | 9180 | ret = libgit_clone_to_tmp(repo, url); | |
64 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
65 | ✗ | return ret; | |
66 | |||
67 | 9180 | ret = libgit_checkout(*repo, rev); | |
68 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
69 | ✗ | goto cleanup_repo; | |
70 | |||
71 | 9180 | return ret; | |
72 | |||
73 | ✗ | cleanup_repo: | |
74 | ✗ | ci_cleanup_git_repo(*repo); | |
75 | |||
76 | ✗ | return ret; | |
77 | } | ||
78 | |||
79 | 9180 | int ci_run_git_repo(const char *url, const char *rev, struct proc_output *output) | |
80 | { | ||
81 | 9180 | char *oldpwd = NULL; | |
82 | 9180 | git_repository *repo = NULL; | |
83 | 9180 | int ret = 0; | |
84 | |||
85 | 9180 | ret = ci_prepare_git_repo(&repo, url, rev); | |
86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
87 | ✗ | goto exit; | |
88 | |||
89 | 9180 | ret = my_chdir(git_repository_workdir(repo), &oldpwd); | |
90 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
|
9180 | if (ret < 0) |
91 | ✗ | goto free_repo; | |
92 | |||
93 | 9180 | ret = ci_run(output); | |
94 |
1/2✓ Branch 0 taken 9180 times.
✗ Branch 1 not taken.
|
9180 | if (ret < 0) |
95 | ✗ | goto oldpwd; | |
96 | |||
97 | 9180 | oldpwd: | |
98 | 9180 | my_chdir(oldpwd, NULL); | |
99 | 9180 | free(oldpwd); | |
100 | |||
101 | 9180 | free_repo: | |
102 | 9180 | ci_cleanup_git_repo(repo); | |
103 | |||
104 | 9180 | exit: | |
105 | 9180 | return ret; | |
106 | } | ||
107 |