forked from nodegit/nodegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlfs.cc
More file actions
178 lines (148 loc) · 5.81 KB
/
lfs.cc
File metadata and controls
178 lines (148 loc) · 5.81 KB
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
#include <nan.h>
extern "C" {
#include <git2.h>
}
#include "../include/nodegit.h"
#include "../include/context.h"
#include "../include/lock_master.h"
#include "../include/functions/copy.h"
#include "../include/lfs.h"
#include "../include/run_command.h"
#include "../include/repository.h"
using namespace v8;
void GitLFS::InitializeComponent(v8::Local<v8::Object> target, nodegit::Context *nodegitContext) {
Nan::HandleScope scope;
v8::Local<Object> lfs = Nan::New<Object>();
Local<External> nodegitExternal = Nan::New<External>(nodegitContext);
Nan::SetMethod(lfs, "initialize", Initialize, nodegitExternal);
Nan::Set(target, Nan::New<String>("LFS").ToLocalChecked(), lfs);
nodegitContext->SaveToPersistent("LFS", lfs);
}
NAN_METHOD(GitLFS::Initialize) {
if (info.Length() == 0 || !info[0]->IsObject()) {
return Nan::ThrowError("Repository repo is required.");
}
if (info.Length() >= 3 && !info[1]->IsNull() && !info[1]->IsUndefined() && !info[1]->IsObject()) {
return Nan::ThrowError("Options must be an object, null, or undefined.");
}
if (!info[info.Length() - 1]->IsFunction()) {
return Nan::ThrowError("Callback is required and must be a Function.");
}
InitializeBaton* baton = new InitializeBaton();
baton->error_code = GIT_OK;
baton->error = NULL;
baton->repo = Nan::ObjectWrap::Unwrap<GitRepository>(Nan::To<v8::Object>(info[0]).ToLocalChecked())->GetValue();
std::unique_ptr<nodegit::LfsCmdOptsInitialize> lfsCmdOpts = std::make_unique<nodegit::LfsCmdOptsInitialize>();
if (info.Length() == 3 && info[1]->IsObject()) {
v8::Local<v8::Object> options = Nan::To<v8::Object>(info[1]).ToLocalChecked();
v8::Local<v8::Value> maybeBool = nodegit::safeGetField(options, "local");
if (!maybeBool.IsEmpty() && !maybeBool->IsUndefined() && !maybeBool->IsNull()) {
if (!maybeBool->IsBoolean()) {
return Nan::ThrowError("Must pass Boolean to local");
}
lfsCmdOpts->local = static_cast<bool>(Nan::To<bool>(maybeBool).FromJust());
}
}
baton->cmd = std::make_unique<nodegit::LfsCmd>(nodegit::LfsCmd::Type::kInitialize, std::move(lfsCmdOpts));
Nan::Callback *callback = new Nan::Callback(v8::Local<Function>::Cast(info[info.Length() - 1]));
InitializeWorker *worker = new InitializeWorker(baton, callback);
worker->Reference<GitRepository>("repo", info[0]);
nodegit::Context *nodegitContext = reinterpret_cast<nodegit::Context *>(info.Data().As<External>()->Value());
nodegitContext->QueueWorker(worker);
return;
}
nodegit::LockMaster GitLFS::InitializeWorker::AcquireLocks() {
nodegit::LockMaster lockMaster(
/*asyncAction: */true
,baton->repo
);
return lockMaster;
}
void GitLFS::InitializeWorker::Execute() {
git_error_clear();
int result = GIT_OK;
const char *repoPath = git_repository_workdir(baton->repo);
// TODO: execute command if repository is bare?
if (repoPath) {
baton->cmd->SetEnv(nodegit::Cmd::Env::kCWD, repoPath);
}
// TODO: execute command if '.git/lfs' folder already exists?
if (!nodegit::runcommand::exec(baton->cmd.get())) {
result = GIT_EUSER;
}
baton->error_code = result;
if (result != GIT_OK && git_error_last() != NULL) {
baton->error = git_error_dup(git_error_last());
}
}
void GitLFS::InitializeWorker::HandleErrorCallback() {
if (!GetIsCancelled()) {
v8::Local<v8::Object> err = Nan::To<v8::Object>(Nan::Error(ErrorMessage())).ToLocalChecked();
Nan::Set(err, Nan::New("errorFunction").ToLocalChecked(), Nan::New("LFS.initialize").ToLocalChecked());
v8::Local<v8::Value> argv[1] = {
err
};
callback->Call(1, argv, async_resource);
}
if (baton->error) {
if (baton->error->message) {
free((void *)baton->error->message);
}
free((void *)baton->error);
}
delete baton;
}
void GitLFS::InitializeWorker::HandleOKCallback() {
if (baton->error_code == GIT_OK) {
v8::Local<v8::Value> result = Nan::Undefined();
v8::Local<v8::Value> argv[2] = {
Nan::Null(),
result
};
callback->Call(2, argv, async_resource);
} else {
if (baton->error) {
v8::Local<v8::Object> err;
if (baton->error->message) {
err = Nan::To<v8::Object>(Nan::Error(baton->error->message)).ToLocalChecked();
} else {
err = Nan::To<v8::Object>(Nan::Error("Method initialize has thrown an error.")).ToLocalChecked();
}
Nan::Set(err, Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
Nan::Set(err, Nan::New("errorFunction").ToLocalChecked(), Nan::New("LFS.initialize").ToLocalChecked());
v8::Local<v8::Value> argv[1] = {
err
};
callback->Call(1, argv, async_resource);
if (baton->error->message) {
free((void *)baton->error->message);
}
free((void *)baton->error);
} else if (baton->error_code < 0) {
bool callbackFired = false;
if (!callbackErrorHandle.IsEmpty()) {
v8::Local<v8::Value> maybeError = Nan::New(callbackErrorHandle);
if (!maybeError->IsNull() && !maybeError->IsUndefined()) {
v8::Local<v8::Value> argv[1] = {
maybeError
};
callback->Call(1, argv, async_resource);
callbackFired = true;
}
}
if (!callbackFired) {
std::string errorMessage = std::string("Method initialize has thrown an error: ").append(baton->cmd->errorMsg);
v8::Local<v8::Object> err = Nan::To<v8::Object>(Nan::Error(errorMessage.c_str())).ToLocalChecked();
Nan::Set(err, Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
Nan::Set(err, Nan::New("errorFunction").ToLocalChecked(), Nan::New("LFS.initialize").ToLocalChecked());
v8::Local<v8::Value> argv[1] = {
err
};
callback->Call(1, argv, async_resource);
}
} else {
callback->Call(0, NULL, async_resource);
}
}
delete baton;
}