Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add repository getSubmodules in C++
  • Loading branch information
implausible committed Apr 18, 2019
commit ac46386ab0c6e56607ccfbba95e40549169604b2
23 changes: 23 additions & 0 deletions generate/input/libgit2-supplement.json
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,28 @@
"isErrorCode": true
}
},
"git_repository_get_submodules": {
"args": [
{
"name": "out",
"type": "std::vector<git_submodule *> *"
},
{
"name": "repo",
"type": "git_repository *"
}
],
"type": "function",
"isManual": true,
"cFile": "generate/templates/manual/repository/get_submodules.cc",
"isAsync": true,
"isPrototypeMethod": true,
"group": "repository",
"return": {
"type": "int",
"isErrorCode": true
}
},
"git_repository_get_remotes": {
"args": [
{
Expand Down Expand Up @@ -578,6 +600,7 @@
"repository",
[
"git_repository_get_references",
"git_repository_get_submodules",
"git_repository_get_remotes"
]
],
Expand Down
115 changes: 115 additions & 0 deletions generate/templates/manual/repository/get_submodules.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
NAN_METHOD(GitRepository::GetSubmodules)
{
if (info.Length() == 0 || !info[0]->IsFunction()) {
return Nan::ThrowError("Callback is required and must be a Function.");
}

GetSubmodulesBaton* baton = new GetSubmodulesBaton;

baton->error_code = GIT_OK;
baton->error = NULL;
baton->out = new std::vector<git_submodule *>;
baton->repo = Nan::ObjectWrap::Unwrap<GitRepository>(info.This())->GetValue();

Nan::Callback *callback = new Nan::Callback(Local<Function>::Cast(info[0]));
GetSubmodulesWorker *worker = new GetSubmodulesWorker(baton, callback);
worker->SaveToPersistent("repo", info.This());
Nan::AsyncQueueWorker(worker);
return;
}

struct submodule_foreach_payload {
git_repository *repo;
std::vector<git_submodule *> *out;
};

int foreachSubmoduleCB(git_submodule *submodule, const char *name, void *void_payload) {
submodule_foreach_payload *payload = (submodule_foreach_payload *)void_payload;
git_submodule *out;

int result = git_submodule_lookup(&out, payload->repo, name);
if (result == GIT_OK) {
payload->out->push_back(out);
}

return result;
}

void GitRepository::GetSubmodulesWorker::Execute()
{
giterr_clear();

LockMaster lockMaster(true, baton->repo);

submodule_foreach_payload payload { baton->repo, baton->out };
baton->error_code = git_submodule_foreach(baton->repo, foreachSubmoduleCB, (void *)&payload);

if (baton->error_code != GIT_OK) {
if (giterr_last() != NULL) {
baton->error = git_error_dup(giterr_last());
}

while (baton->out->size()) {
git_submodule_free(baton->out->back());
baton->out->pop_back();
}
delete baton->out;
baton->out = NULL;
}
}

void GitRepository::GetSubmodulesWorker::HandleOKCallback()
{
if (baton->out != NULL)
{
unsigned int size = baton->out->size();
Local<Array> result = Nan::New<Array>(size);
for (unsigned int i = 0; i < size; i++) {
git_submodule *submodule = baton->out->at(i);
Nan::Set(
result,
Nan::New<Number>(i),
GitSubmodule::New(
submodule,
true,
GitRepository::New(git_submodule_owner(submodule), true)->ToObject()
)
);
}

delete baton->out;

Local<v8::Value> argv[2] = {
Nan::Null(),
result
};
callback->Call(2, argv, async_resource);
}
else if (baton->error)
{
Local<v8::Value> argv[1] = {
Nan::Error(baton->error->message)
};
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)
{
Local<v8::Object> err = Nan::Error("Repository getSubmodules has thrown an error.")->ToObject();
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(), Nan::New("Repository.getSubmodules").ToLocalChecked());
Local<v8::Value> argv[1] = {
err
};
callback->Call(1, argv, async_resource);
}
else
{
callback->Call(0, NULL, async_resource);
}
}