/
gitteh.h
133 lines (114 loc) · 3 KB
/
gitteh.h
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
#ifndef GITTEH_H
#define GITTEH_H
#include <v8.h>
#include "cvv8/convert.hpp"
#include <node.h>
#include <git2.h>
#include <node_object_wrap.h>
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include "thread.h"
#include "baton.h"
using namespace v8;
using namespace node;
using namespace cvv8;
using std::string;
#define REQ_EXT_ARG(I, VAR) \
if (args.Length() <= (I) || !args[I]->IsExternal()) \
return ThrowException(Exception::TypeError( \
String::New("Argument " #I " invalid"))); \
Local<External> VAR = Local<External>::Cast(args[I]);
namespace gitteh {
Handle<Object> GetModule();
static inline Handle<Value> CreateGitError() {
const git_error *err = giterr_last();
Handle<Object> errObj = Handle<Object>::Cast(Exception::Error(
String::New(err->message)));
errObj->Set(String::New("code"), Integer::New(err->klass));
return errObj;
}
static inline Handle<Value> ThrowGitError() {
return ThrowException(CreateGitError());
}
template<typename T>
static inline T* GetBaton(uv_work_t *req) {
return static_cast<T*>(req->data);
}
/**
Invokes provided callback with given parameters, handles catching user-land
exceptions and propagating them to top of Node's event loop
*/
static inline bool FireCallback(Handle<Function> callback, int argc,
Handle<Value> argv[]) {
TryCatch tryCatch;
callback->Call(Context::GetCurrent()->Global(), argc, argv);
if(tryCatch.HasCaught()) {
FatalException(tryCatch);
return false;
}
return true;
}
/**
Examines return of a libgit2 call. If it's in error state, grab error object
and hand it off to ref provided.
*/
static inline int LibCall(int result, const git_error **err) {
if(result != GIT_OK) {
*err = giterr_last();
return 0;
}
return 1;
}
static inline void AsyncLibCall(int result, Baton *baton) {
const git_error *err;
if(!LibCall(result, &err)) {
baton->setError(err);
}
}
static inline void ImmutableSet(Handle<Object> o, Handle<Value> k, Handle<Value> v) {
o->Set(k, v, (PropertyAttribute)(ReadOnly | DontDelete));
}
static inline string GitObjectTypeToString(git_otype type) {
switch(type) {
case GIT_OBJ_COMMIT: {
return string("commit");
}
case GIT_OBJ_TREE: {
return string("tree");
}
case GIT_OBJ_BLOB: {
return string("blob");
}
case GIT_OBJ_TAG: {
return string("tag");
}
default: {
assert(0);
}
}
}
} // namespace gitteh
namespace cvv8 {
template<>
struct NativeToJS<git_oid> {
Handle<Value> operator() (git_oid const *oid) const {
char oidStr[40];
git_oid_fmt(oidStr, oid);
return CastToJS(oidStr);
}
};
template<>
struct JSToNative<git_oid> {
typedef git_oid ResultType;
ResultType operator() (Handle<Value> const &h) const {
git_oid id;
memset(&id, 0, sizeof(git_oid));
string idStr = CastFromJS<string>(h);
git_oid_fromstr(&id, idStr.c_str());
return id;
}
};
}
#endif // GITTEH_H