pulsar/native/v8_extensions/git.mm

126 lines
3.3 KiB
Plaintext
Raw Normal View History

2012-10-25 04:06:45 +04:00
#import "git.h"
#import "include/git2.h"
#import <Cocoa/Cocoa.h>
namespace v8_extensions {
2012-10-26 01:10:42 +04:00
2012-10-25 21:09:42 +04:00
class GitRepository : public CefBase {
2012-10-26 03:41:09 +04:00
private:
bool exists;
git_repository *repo;
public:
2012-11-02 21:42:05 +04:00
GitRepository(const char *pathInRepo) {
exists = git_repository_open_ext(&repo, pathInRepo, 0, NULL) == GIT_OK;
2012-10-26 03:41:09 +04:00
}
~GitRepository() {
if (repo)
git_repository_free(repo);
}
CefRefPtr<CefV8Value> GetPath() {
if (exists)
return CefV8Value::CreateString(git_repository_path(repo));
else
return CefV8Value::CreateNull();
}
CefRefPtr<CefV8Value> GetHead() {
if (!exists)
return CefV8Value::CreateNull();
git_reference *head;
if (git_repository_head(&head, repo) == GIT_OK) {
if (git_repository_head_detached(repo) == 1) {
const git_oid* sha = git_reference_oid(head);
if (sha) {
char oid[GIT_OID_HEXSZ + 1];
git_oid_tostr(oid, GIT_OID_HEXSZ + 1, sha);
return CefV8Value::CreateString(oid);
2012-10-25 22:06:23 +04:00
}
2012-10-26 03:41:09 +04:00
}
return CefV8Value::CreateString(git_reference_name(head));
2012-11-02 21:42:05 +04:00
}
return CefV8Value::CreateNull();
2012-10-26 03:41:09 +04:00
}
2012-10-26 01:10:42 +04:00
CefRefPtr<CefV8Value> IsIgnored(const char *path) {
2012-11-03 00:24:15 +04:00
if (!exists) {
return CefV8Value::CreateBool(false);
2012-11-03 00:24:15 +04:00
}
2012-10-27 03:39:18 +04:00
int ignored;
2012-11-03 00:24:15 +04:00
if (git_ignore_path_is_ignored(&ignored, repo, path) == GIT_OK) {
2012-10-27 03:39:18 +04:00
return CefV8Value::CreateBool(ignored == 1);
2012-11-03 00:24:15 +04:00
}
else {
return CefV8Value::CreateBool(false);
2012-11-03 00:24:15 +04:00
}
}
CefRefPtr<CefV8Value> GetStatus(const char *path) {
if (!exists) {
return CefV8Value::CreateInt(-1);
}
unsigned int status;
if (git_status_file(&status, repo, path) == GIT_OK) {
return CefV8Value::CreateInt(status);
}
else {
return CefV8Value::CreateInt(-1);
}
}
2012-10-25 21:09:42 +04:00
IMPLEMENT_REFCOUNTING(GitRepository);
};
2012-10-25 04:06:45 +04:00
Git::Git() : CefV8Handler() {
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"v8_extensions/git.js"];
NSString *extensionCode = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
CefRegisterExtension("v8/git", [extensionCode UTF8String], this);
}
bool Git::Execute(const CefString& name,
2012-10-27 04:08:12 +04:00
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception) {
2012-10-25 21:09:42 +04:00
if (name == "getRepository") {
2012-10-26 01:36:08 +04:00
CefRefPtr<CefBase> userData = new GitRepository(arguments[0]->GetStringValue().ToString().c_str());
2012-10-25 21:09:42 +04:00
retval = CefV8Value::CreateObject(NULL);
retval->SetUserData(userData);
return true;
}
2012-10-26 01:10:42 +04:00
2012-10-25 21:09:42 +04:00
if (name == "getHead") {
GitRepository *userData = (GitRepository *)object->GetUserData().get();
retval = userData->GetHead();
2012-10-25 04:53:33 +04:00
return true;
}
2012-10-26 02:20:12 +04:00
if (name == "getPath") {
GitRepository *userData = (GitRepository *)object->GetUserData().get();
retval = userData->GetPath();
return true;
}
if (name == "isIgnored") {
GitRepository *userData = (GitRepository *)object->GetUserData().get();
retval = userData->IsIgnored(arguments[0]->GetStringValue().ToString().c_str());
return true;
}
2012-11-03 00:24:15 +04:00
if (name == "getStatus") {
GitRepository *userData = (GitRepository *)object->GetUserData().get();
retval = userData->GetStatus(arguments[0]->GetStringValue().ToString().c_str());
return true;
}
2012-10-25 04:53:33 +04:00
return false;
2012-10-25 04:06:45 +04:00
}
}