aboutsummaryrefslogtreecommitdiffstats
path: root/src/r-lg2.c
blob: 915b2108722e401676f46731e100290c09c31330 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#if defined(LIGBIT)

#include <stdio.h>
#include "r-lg2.h"
#include "xstd.h"
#include "output.h"

git_repository *g_repo = NULL;

static int lg2_commit(git_index *index, char *comment)
{
	int error;
	git_oid commit_oid, tree_oid;
	git_tree *tree = NULL;
	git_object *parent = NULL;
	git_reference *ref = NULL;
	git_signature *author_signature = NULL, *committer_signature = NULL;

	error = git_revparse_ext(&parent, &ref, g_repo, "HEAD");
	if (error == GIT_ENOTFOUND) {
		printf("HEAD not found. Creating first commit\n");
		error = 0;
	} else if (error != 0) {
		const git_error *err = git_error_last();
		if (err) printf("ERROR %d: %s\n", err->klass, err->message);
		else printf("ERROR %d: no detailed info\n", error);
		return error;
	}

	error = git_index_write_tree(&tree_oid, index);
	if (error) {
		print_error("git: Could not write tree\n");
		return error;
	}
	error = git_index_write(index);
	if (error) {
		print_error("git: Could not write index\n");
		return error;
	}

	error = git_tree_lookup(&tree, g_repo, &tree_oid);
	if (error) {
		print_error("git: Error looking up tree\n");
		return error;
	}

	error = git_signature_default_from_env(&author_signature, &committer_signature, g_repo);
	if (error) {
		print_error("git: Error creating signatures\n");
		return error;
	}

	error = git_commit_create_v(
		&commit_oid,
		g_repo,
		"HEAD",
		author_signature,
		committer_signature,
		NULL,
		comment,
		tree,
		parent ? 1 : 0, parent);
	if (error) {
		print_error("git: Error creating commit\n");
	} else {
		char *commit_hash = git_oid_tostr_s(&commit_oid); 
		printf("git: [HEAD %s] %s\n", commit_hash, comment);
		error = 0;
	}

	git_signature_free(author_signature);
	git_signature_free(committer_signature);
	git_tree_free(tree);
	git_object_free(parent);
	git_reference_free(ref);
	return error;
}

int lg2_open_repo(const char *path)
{
	int error = 0;
	error = git_libgit2_init();
	if (error < 0) {
		const git_error *e = git_error_last();
		print_error("git_libgit2_init(): %s\n", e->message);
		return error;
	}

	error = git_repository_open(&g_repo, path);
	if (error < 0) {
		print_error("Warning: could not open repository, trying git init\n");

		error = git_repository_init(&g_repo, path, 0);
		if (error < 0) {
			const git_error *e = git_error_last();
			print_error("git_repository_init(): %s\n", e->message);
			return error;
		}

		git_index *idx = NULL;
		error = git_repository_index(&idx, g_repo);
		if (error < 0) {
			const git_error *e = git_error_last();
			print_error("git_repository_index(): %s\n", e->message);
			git_repository_free(g_repo);
			return error;
		}

		// Add all files in the repository
		char *paths[] = {"*", ".*"};
		git_strarray arr = {paths, 2};

		error = git_index_add_all(idx, &arr, GIT_INDEX_ADD_DEFAULT, NULL, NULL);
		if (error < 0) {
			const git_error *e = git_error_last();
			print_error("git_index_add_all(): %s\n", e->message);
			git_index_free(idx);
			git_repository_free(g_repo);
			return error;
		}

		error = lg2_commit(idx, "init commit");
		git_index_free(idx);
	}
	return error;
}

int lg2_close_repo()
{
	if (g_repo != NULL)
		git_repository_free(g_repo);
	git_libgit2_shutdown();
	return 0;
}

int lg2_simple_action(git_action_t action, int is_overwrite, const char *path, const char *new_path)
{
	int error = 0;
	git_index *idx = NULL;
	char *message = NULL;
	const char *action_str = "";
	const char *overwrite_str = NULL;

	error = git_repository_index(&idx, g_repo);
	if (error < 0) {
		const git_error *e = git_error_last();
		print_error("git_repository_index(): %s\n", e->message);
		return error;
	}

	switch (action) {
	case GIT_ACTION_INSERT:
		action_str = "insert";
		error = git_index_add_bypath(idx, path);
		break;
	case GIT_ACTION_GENERATE:
		action_str = "generate";
		error = git_index_add_bypath(idx, path);
		break;
	case GIT_ACTION_EDIT:
		action_str = "edit";
		error = git_index_add_bypath(idx, path);
		break;
	    
	case GIT_ACTION_DELETE:
		action_str = "delete";
		error = git_index_remove_bypath(idx, path);
		break;
	    
	case GIT_ACTION_MOVE:
		error = git_index_remove_bypath(idx, path);
		if (!error)
			error = git_index_add_bypath(idx, new_path);

		if (is_overwrite)
			overwrite_str = " (overwrite)";
		message = xstrcat(path, " -> ", new_path, overwrite_str, NULL);
		break;
	}

	if (error) {
		const git_error *e = git_error_last();
		print_error("Index operation failed: %s\n", e->message);
		git_index_free(idx);
		return error;
	}

	// Generate commit message if not already set (for move)
	if (!message) {
		if (is_overwrite)
			overwrite_str = " (overwrite)";
		message = xstrcat(path, ": ", action_str, overwrite_str, NULL);
	}

	if (!message) {
		git_index_free(idx);
		return 1;
	}

	error = lg2_commit(idx, message);

	free(message);
	git_index_free(idx);
	return error;
}

#endif /* LIGBIT */