summaryrefslogtreecommitdiff
path: root/lib/gall/ll/git2.c
blob: 4eecb9147da21d9342b4150f5339c4762878b456 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* git2.c
 *
 * Simple binding to the parts of libgit2 in use by Gall.
 *
 * Copyright 2014 Daniel Silverstone <dsilvers@digital-scurf.org>
 */

#include <lua.h>
#include <lauxlib.h>
#include "git2.h"

typedef struct {
	git_repository *repo;
	git_odb *odb;
} repo_et_al_t;

static inline git_repository *to_repo(lua_State *L, int idx)
{
	repo_et_al_t *real = lua_touserdata(L, idx);
	if (real == NULL)
		return NULL;
	return real->repo;
}

static inline git_odb *to_odb(lua_State *L, int idx)
{
	repo_et_al_t *real = lua_touserdata(L, idx);
	if (real == NULL)
		return NULL;
	return real->odb;
}

/* Push the most recent error from libgit2 */
static int push_git2_error(lua_State *L)
{
	const git_error *err = giterr_last();
	lua_pushnil(L);
	lua_pushstring(L, err->message);
	return 2;
}



static int L_gc_repo(lua_State *L)
{
	repo_et_al_t *real = lua_touserdata(L, 1);
	if (real->odb != NULL)
		git_odb_free(real->odb);
	if (real->repo != NULL)
		git_repository_free(real->repo);
	real->repo = NULL;
	real->odb = NULL;
	return 0;
}

/* Trivial Repository binding, GC will free the repo */
static int L_open_repo(lua_State *L)
{
	const char *repopath = luaL_checkstring(L, 1);
	int ret;
	repo_et_al_t *real = lua_newuserdata(L, sizeof(*real));
	real->repo = NULL; real->odb = NULL;
	
	ret = git_repository_open(&real->repo, repopath);
	if (ret != 0) {
		return push_git2_error(L);
	}
	
	ret = git_repository_odb(&real->odb, real->repo);
	if (ret != 0) {
		push_git2_error(L);
		git_repository_free(real->repo);
		return 2;
	}
	
	lua_pushvalue(L, lua_upvalueindex(1));
	lua_setmetatable(L, -2);
	return 1;
}

static int L_lookup_symbolic_ref(lua_State *L)
{
	git_repository *repo = to_repo(L, 1);
	const char *refname = luaL_checkstring(L, 2);
	git_reference *ref = NULL;
	if (git_reference_lookup(&ref, repo, refname) != 0) {
		return push_git2_error(L);
	}
	if (git_reference_type(ref) != GIT_REF_SYMBOLIC) {
		git_reference_free(ref);
		lua_pushnil(L);
		lua_pushfstring(L, "%s is not symbolic", refname);
		return 2;
	}
	lua_pushstring(L, git_reference_symbolic_target(ref));
	git_reference_free(ref);
	return 1;
}

static int format_oid(lua_State *L, git_oid *oid)
{
	char oidstr[40];
	git_oid_fmt(oidstr, oid);
	lua_pushlstring(L, oidstr, 40);
	return 1;
}

static int L_lookup_sha_from_ref(lua_State *L)
{
	git_repository *repo = to_repo(L, 1);
	const char *refname = luaL_checkstring(L, 2);
	git_oid ref;
	if (git_reference_name_to_id(&ref, repo, refname) != 0) {
		return push_git2_error(L);
	}
	return format_oid(L, &ref);
}

static int parse_oid(lua_State *L, int spos, git_oid *oid)
{
	const char *oid_s = luaL_checkstring(L, spos);
	int ret = git_oid_fromstr(oid, oid_s);
	if (ret != 0)
		return push_git2_error(L);
	return 0;
}

static int L_merge_base(lua_State *L)
{
	git_repository *repo = to_repo(L, 1);
	git_oid left, right, out;
	int ret;	
	if (parse_oid(L, 2, &left) != 0)
		return 2;
	if (parse_oid(L, 3, &right) != 0)
		return 2;
	ret = git_merge_base(&out, repo, &left, &right);
	if (ret == 0)
		return format_oid(L, &out);
	if (ret == GIT_ENOTFOUND) {
		lua_pushnil(L);
		lua_pushliteral(L, "ENOTFOUND");
		return 2;
	}
	return push_git2_error(L);
}

static int L_gc_odb_object(lua_State *L)
{
	git_odb_object **obj = lua_touserdata(L, 1);
	if (*obj != NULL)
		git_odb_object_free(*obj);
	*obj = NULL;
	return 0;
}

static int L_get_object(lua_State *L)
{
	git_odb *odb = to_odb(L, 1);
	git_odb_object **obj = lua_newuserdata(L, sizeof(*obj));
	git_oid oid;
	if (parse_oid(L, 2, &oid) != 0)
		return 2;
	if (git_odb_read(obj, odb, &oid) != 0)
		return push_git2_error(L);
	lua_pushvalue(L, lua_upvalueindex(1));
	lua_setmetatable(L, -2);
	return 1;
}

static int L_get_object_size(lua_State *L)
{
	git_odb_object **obj = lua_touserdata(L, 1);
	lua_pushnumber(L, git_odb_object_size(*obj));
	return 1;
}

static void push_object_type_str(lua_State *L, git_otype ty)
{
	switch(ty) {
	case GIT_OBJ_ANY:
		lua_pushliteral(L, "any");
		break;
	case GIT_OBJ_BAD:
		lua_pushliteral(L, "bad");
		break;
	case GIT_OBJ__EXT1:
		lua_pushliteral(L, "reserved");
		break;
	case GIT_OBJ_COMMIT:
		lua_pushliteral(L, "commit");
		break;
	case GIT_OBJ_TREE:
		lua_pushliteral(L, "tree");
		break;
	case GIT_OBJ_BLOB:
		lua_pushliteral(L, "blob");
		break;
	case GIT_OBJ_TAG:
		lua_pushliteral(L, "tag");
		break;
	case GIT_OBJ__EXT2:
		lua_pushliteral(L, "reserved");
		break;
	case GIT_OBJ_OFS_DELTA:
		lua_pushliteral(L, "delta");
		break;
	case GIT_OBJ_REF_DELTA:
		lua_pushliteral(L, "refdelta");
		break;
	default:
		lua_pushliteral(L, "unknown");
		break;
	}
}

static int L_get_object_type(lua_State *L)
{
	git_odb_object **obj = lua_touserdata(L, 1);
	push_object_type_str(L, git_odb_object_type(*obj));
	return 1;
}

static int L_get_object_raw(lua_State *L)
{
	git_odb_object **obj = lua_touserdata(L, 1);
	lua_pushlstring(L, git_odb_object_data(*obj),
			git_odb_object_size(*obj));
	return 1;
}

int luaopen_git2(lua_State *L)
{
	lua_newtable(L);
	lua_pushliteral(L, "LIBGIT2_VERSION");
	lua_pushliteral(L, LIBGIT2_VERSION);
	lua_settable(L, -3);
	
	lua_pushliteral(L, "open_repo");
	lua_newtable(L);
	lua_pushliteral(L, "__gc");
	lua_pushcclosure(L, L_gc_repo, 0);
	lua_settable(L, -3);
	lua_pushcclosure(L, L_open_repo, 1);
	lua_settable(L, -3);

	lua_pushliteral(L, "get_object");
	lua_newtable(L);
	lua_pushliteral(L, "__gc");
	lua_pushcclosure(L, L_gc_odb_object, 0);
	lua_settable(L, -3);
	lua_pushcclosure(L, L_get_object, 1);
	lua_settable(L, -3);

#define BASIC_FUNC(FN)				\
	do {					\
		lua_pushliteral(L, #FN);	\
		lua_pushcclosure(L, L_##FN, 0);	\
		lua_settable(L, -3);		\
	} while (0)
	BASIC_FUNC(lookup_symbolic_ref);
	BASIC_FUNC(lookup_sha_from_ref);
	BASIC_FUNC(merge_base);
	BASIC_FUNC(get_object_size);
	BASIC_FUNC(get_object_type);
	BASIC_FUNC(get_object_raw);
	return 1;
}