Skip to content

Commit e3d82d0

Browse files
committed
Merge pull request nodegit#374 from nodegit/ol-status
Status and StatusList
2 parents 814de3c + c396aa5 commit e3d82d0

24 files changed

Lines changed: 1303 additions & 52 deletions

examples/status.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var nodegit = require("../"),
2+
path = require("path");
3+
4+
// This code shows working directory changes similar to git status
5+
6+
nodegit.Repository.open(path.resolve(__dirname, "../.git"))
7+
.then(function(repo) {
8+
repo.getStatus().then(function(statuses) {
9+
function statusToText(status) {
10+
var words = [];
11+
if (status.isNew()) { words.push("NEW"); }
12+
if (status.isModified()) { words.push("MODIFIED"); }
13+
if (status.isTypechange()) { words.push("TYPECHANGE"); }
14+
if (status.isRenamed()) { words.push("RENAMED"); }
15+
if (status.isIgnored()) { words.push("IGNORED"); }
16+
17+
return words.join(" ");
18+
}
19+
20+
statuses.forEach(function(file) {
21+
console.log(file.path() + " " + statusToText(file));
22+
});
23+
});
24+
});
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
{%each args as cbFunction %}
2+
{%if cbFunction.isCallbackFunction %}
3+
4+
{{ cbFunction.return.type }} {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_cppCallback (
5+
{% each cbFunction.args|argsInfo as arg %}
6+
{{ arg.cType }} {{ arg.name}}{% if not arg.lastArg %},{% endif %}
7+
{% endeach %}
8+
) {
9+
{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = new {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton();
10+
11+
{% each cbFunction.args|argsInfo as arg %}
12+
baton->{{ arg.name }} = {{ arg.name }};
13+
{% endeach %}
14+
15+
baton->req.data = baton;
16+
baton->done = false;
17+
18+
uv_queue_work(uv_default_loop(), &baton->req, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncAfter);
19+
20+
while(!baton->done) {
21+
this_thread::sleep_for(chrono::milliseconds(1));
22+
}
23+
24+
{% each cbFunction|returnsInfo true false as _return %}
25+
*{{ _return.name }} = *baton->{{ _return.name }};
26+
{% endeach %}
27+
28+
return baton->result;
29+
}
30+
31+
void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork(uv_work_t* req) {
32+
// We aren't doing any work on a seperate thread, just need to
33+
// access the main node thread in the async after method.
34+
// However, this worker method is still needed
35+
}
36+
37+
void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncAfter(uv_work_t* req, int status) {
38+
NanScope();
39+
40+
{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = static_cast<{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton*>(req->data);
41+
42+
NanCallback* callback = (NanCallback *)baton->payload;
43+
44+
Local<Value> argv[{{ cbFunction.args|jsArgsCount }}] = {
45+
{% each cbFunction.args|argsInfo as arg %}
46+
{% if arg.name == "payload" %}
47+
{%-- payload is always the last arg --%}
48+
// payload is null because we can use closure scope in javascript
49+
NanUndefined()
50+
{% elsif arg.isJsArg %}
51+
{% if arg.isEnum %}
52+
NanNew((int)baton->{{ arg.name }}),
53+
{% elsif arg.isLibgitType %}
54+
NanNew({{ arg.cppClassName }}::New(&baton->{{ arg.name }}, false)),
55+
{% elsif arg.cType == "size_t" %}
56+
// HACK: NAN should really have an overload for NanNew to support size_t
57+
NanNew((unsigned int)baton->{{ arg.name }}),
58+
{% else %}
59+
NanNew(baton->{{ arg.name }}),
60+
{% endif %}
61+
{% endif %}
62+
{% endeach %}
63+
};
64+
65+
TryCatch tryCatch;
66+
Handle<Value> result = callback->Call({{ cbFunction.args|jsArgsCount }}, argv);
67+
68+
if (result->IsObject() && result->ToObject()->Has(NanNew("then"))) {
69+
Handle<Value> thenProp = result->ToObject()->Get(NanNew("then"));
70+
71+
if (thenProp->IsFunction()) {
72+
// we can be reasonbly certain that the result is a promise
73+
Local<Object> promise = result->ToObject();
74+
75+
NanAssignPersistent(baton->promise, promise);
76+
77+
uv_queue_work(uv_default_loop(), &baton->req, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromisePolling);
78+
return;
79+
}
80+
}
81+
82+
{{ cbFunction.return.type }} resultStatus;
83+
84+
{% each cbFunction|returnsInfo true false as _return %}
85+
if (result.IsEmpty() || result->IsNativeError()) {
86+
baton->result = {{ cbFunction.return.error }};
87+
}
88+
else if (!result->IsNull() && !result->IsUndefined()) {
89+
{{ _return.cppClassName }}* wrapper = ObjectWrap::Unwrap<{{ _return.cppClassName }}>(result->ToObject());
90+
wrapper->selfFreeing = false;
91+
92+
baton->{{ _return.name }} = wrapper->GetRefValue();
93+
baton->result = {{ cbFunction.return.success }};
94+
}
95+
else {
96+
baton->result = {{ cbFunction.return.noResults }};
97+
}
98+
{% endeach %}
99+
baton->done = true;
100+
}
101+
102+
void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromisePolling(uv_work_t* req, int status) {
103+
NanScope();
104+
105+
{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = static_cast<{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton*>(req->data);
106+
Local<Object> promise = NanNew<Object>(baton->promise);
107+
NanCallback* isPendingFn = new NanCallback(promise->Get(NanNew("isPending")).As<Function>());
108+
Local<Value> argv[1]; // MSBUILD won't assign an array of length 0
109+
Local<Boolean> isPending = isPendingFn->Call(0, argv)->ToBoolean();
110+
111+
if (isPending->Value()) {
112+
uv_queue_work(uv_default_loop(), &baton->req, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromisePolling);
113+
return;
114+
}
115+
116+
NanCallback* isFulfilledFn = new NanCallback(promise->Get(NanNew("isFulfilled")).As<Function>());
117+
Local<Boolean> isFulfilled = isFulfilledFn->Call(0, argv)->ToBoolean();
118+
119+
if (isFulfilled->Value()) {
120+
NanCallback* resultFn = new NanCallback(promise->Get(NanNew("value")).As<Function>());
121+
Handle<Value> result = resultFn->Call(0, argv);
122+
{{ cbFunction.return.type }} resultStatus;
123+
124+
{% each cbFunction|returnsInfo true false as _return %}
125+
if (result.IsEmpty() || result->IsNativeError()) {
126+
baton->result = {{ cbFunction.return.error }};
127+
}
128+
else if (!result->IsNull() && !result->IsUndefined()) {
129+
{{ _return.cppClassName }}* wrapper = ObjectWrap::Unwrap<{{ _return.cppClassName }}>(result->ToObject());
130+
wrapper->selfFreeing = false;
131+
132+
baton->{{ _return.name }} = wrapper->GetRefValue();
133+
baton->result = {{ cbFunction.return.success }};
134+
}
135+
else {
136+
baton->result = {{ cbFunction.return.noResults }};
137+
}
138+
{% endeach %}
139+
baton->done = true;
140+
}
141+
else {
142+
// promise was rejected
143+
baton->result = {{ cbFunction.return.error }};
144+
baton->done = false;
145+
}
146+
}
147+
{%endif%}
148+
{%endeach%}

0 commit comments

Comments
 (0)