aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/git.c
blob: 4acf0e9e1ecd2759f6263ed965d69596514c9e43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com>
 * This file is part of the "cimple" project.
 * For details, see https://github.com/egor-tensin/cimple.
 * Distributed under the MIT License.
 */

#include "git.h"
#include "log.h"

#include <git2.h>

#include <stdlib.h>

#define git_log_err(fn)                                                                            \
	do {                                                                                       \
		const git_error *error = git_error_last();                                         \
		const char *msg = error && error->message ? error->message : "???";                \
		log_err("%s: %s\n", fn, msg);                                                      \
	} while (0)

int libgit_init(void)
{
	int ret = 0;

	ret = git_libgit2_init();
	if (ret < 0) {
		git_log_err("git_libgit2_init");
		return ret;
	}

	return 0;
}

void libgit_shutdown(void)
{
	git_libgit2_shutdown();
}

int libgit_clone(git_repository **repo, const char *url, const char *dir)
{
	git_clone_options opts;
	int ret = 0;

	log("Cloning git repository from %s to %s\n", url, dir);

	ret = git_clone_options_init(&opts, GIT_CLONE_OPTIONS_VERSION);
	if (ret < 0) {
		git_log_err("git_clone_options_init");
		return ret;
	}
	opts.checkout_opts.checkout_strategy = GIT_CHECKOUT_NONE;

	ret = git_clone(repo, url, dir, &opts);
	if (ret < 0) {
		git_log_err("git_clone");
		return ret;
	}

	return 0;
}

int libgit_clone_to_tmp(git_repository **repo, const char *url)
{
	char dir[] = "/tmp/git.XXXXXX";

	if (!mkdtemp(dir)) {
		log_errno("mkdtemp");
		return -1;
	}

	return libgit_clone(repo, url, dir);
}

void libgit_repository_free(git_repository *repo)
{
	git_repository_free(repo);
}

int libgit_checkout(git_repository *repo, const char *rev)
{
	git_checkout_options opts;
	git_object *obj;
	int ret = 0;

	log("Checking out revision %s\n", rev);

	ret = git_revparse_single(&obj, repo, rev);
	if (ret < 0) {
		git_log_err("git_revparse_single");
		return ret;
	}

	ret = git_checkout_options_init(&opts, GIT_CHECKOUT_OPTIONS_VERSION);
	if (ret < 0) {
		git_log_err("git_checkout_options_init");
		goto free_obj;
	}
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	ret = git_checkout_tree(repo, obj, &opts);
	if (ret < 0) {
		git_log_err("git_checkout_tree");
		goto free_obj;
	}

	ret = git_repository_set_head_detached(repo, git_object_id(obj));
	if (ret < 0) {
		git_log_err("git_repository_set_head_detached");
		goto free_obj;
	}

free_obj:
	git_object_free(obj);

	return ret;
}