WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 38 additions & 0 deletions generate/templates/manual/include/lfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

#include <nan.h>
#include "context.h"
#include "lock_master.h"
#include "lfs_cmd.h"

extern "C" {
#include <git2.h>
}

class GitLFS : public Nan::ObjectWrap {
public:
Expand All @@ -12,6 +18,38 @@ class GitLFS : public Nan::ObjectWrap {
GitLFS& operator=(GitLFS &&other) = delete;

static void InitializeComponent(v8::Local<v8::Object> target, nodegit::Context *nodegitContext);

private:
struct InitializeBaton {
int error_code;
const git_error *error;
git_repository *repo;
std::unique_ptr<nodegit::LfsCmd> cmd;
};
class InitializeWorker : public nodegit::AsyncWorker {
public:
InitializeWorker(
InitializeBaton *_baton,
Nan::Callback *callback
) : nodegit::AsyncWorker(callback, "nodegit:AsyncWorker:LFS:Initialize")
, baton(_baton) {};
InitializeWorker(const InitializeWorker &) = delete;
InitializeWorker(InitializeWorker &&) = delete;
InitializeWorker &operator=(const InitializeWorker &) = delete;
InitializeWorker &operator=(InitializeWorker &&) = delete;
~InitializeWorker() {};
void Execute();
void HandleErrorCallback();
void HandleOKCallback();
nodegit::LockMaster AcquireLocks();

private:
InitializeBaton *baton {nullptr};
};

static NAN_METHOD(Initialize);


};

#endif // GITLFS_H
18 changes: 17 additions & 1 deletion generate/templates/manual/include/lfs_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ struct LfsCmdOpts {
virtual std::string BuildArgs() const = 0;
};

/**
* \struct LfsCmdOptsInitialize
*/
struct LfsCmdOptsInitialize final : public LfsCmdOpts {
LfsCmdOptsInitialize() = default;
~LfsCmdOptsInitialize() = default;
LfsCmdOptsInitialize(const LfsCmdOptsInitialize &other) = delete;
LfsCmdOptsInitialize(LfsCmdOptsInitialize &&other) = delete;
LfsCmdOptsInitialize& operator=(const LfsCmdOptsInitialize &other) = delete;
LfsCmdOptsInitialize& operator=(LfsCmdOptsInitialize &&other) = delete;

std::string BuildArgs() const;

bool local {false};
};

/**
* \class LfsCmd
* An LFS command to execute.
Expand All @@ -30,7 +46,7 @@ class LfsCmd : public Cmd {
static const char* kStrLfsCmd;

// Enumeration describing the type of LFS command
enum class Type {kNone};
enum class Type {kNone, kInitialize};

LfsCmd(Type lfsCmdType, std::unique_ptr<LfsCmdOpts> &&opts);
LfsCmd() = delete;
Expand Down
163 changes: 163 additions & 0 deletions generate/templates/manual/src/lfs.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#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;

Expand All @@ -10,6 +19,160 @@ void GitLFS::InitializeComponent(v8::Local<v8::Object> target, nodegit::Context

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;
}
19 changes: 18 additions & 1 deletion generate/templates/manual/src/lfs_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@
const char* nodegit::LfsCmd::kStrLfsCmd = "git lfs";

/**
* LfsCmd::LfsCmd
* nodegit::LfsCmdOptsInitialize::BuildArgs
*
* \return string with arguments for this LFS command.
*/
std::string nodegit::LfsCmdOptsInitialize::BuildArgs() const {
std::string strArgs {};

if (local) {
strArgs.append("--local");
}
return strArgs;
}

/**
* nodegit::LfsCmd::LfsCmd
*
* Constructor from type and options.
*/
Expand All @@ -20,6 +34,9 @@ std::string nodegit::LfsCmd::Command() const {
std::string strCmd {nodegit::LfsCmd::kStrLfsCmd};

switch (m_lfsCmdType) {
case Type::kInitialize:
strCmd.append(" install");
break;
default:
break;
}
Expand Down
4 changes: 4 additions & 0 deletions generate/templates/templates/nodegit.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ _FilterRegistry.register = promisify(_FilterRegistry_register);
var _FilterRegistry_unregister = _FilterRegistry.unregister;
_FilterRegistry.unregister = promisify(_FilterRegistry_unregister);

var _LFS = rawApi.LFS;
var _LFS_initialize = _LFS.initialize;
_LFS.initialize = promisify(_LFS_initialize);

/* jshint ignore:end */

// Set the exports prototype to the raw API.
Expand Down
46 changes: 46 additions & 0 deletions test/tests/lfs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var assert = require("assert");
var RepoUtils = require("../utils/repository_setup");
var path = require("path");
var local = path.join.bind(path, __dirname);
const fse = require("fs-extra");

describe("LFS", function() {
var NodeGit = require("../../");

var LFS = NodeGit.LFS;

var test;
var fileName = "foobar.js";
var repoPath = local("../repos/lfsRepo");

beforeEach(function() {
test = this;

return RepoUtils.createRepository(repoPath)
.then(function(repository) {
test.repository = repository;

return RepoUtils.commitFileToRepo(
repository,
fileName,
"line1\nline2\nline3"
);
});
});

// TODO: need to test against bare repositories?
it("can initialize LFS repository", function(done) {
LFS.initialize(test.repository, { local: true })
.then(function() {
let gitLfsDirPath = path.join(test.repository.path(), "/lfs");
fse.stat(gitLfsDirPath, function(err, stat) {
if (!err && stat && stat.isDirectory()) {
done();
}
else {
assert.fail("LFS repository initialization failed!");
}
});
});
});
});