Skip to content

Commit 945b09d

Browse files
committed
Convert git_status_file to an async method
1 parent 113493a commit 945b09d

File tree

3 files changed

+57
-20
lines changed

3 files changed

+57
-20
lines changed

generate/input/descriptor.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,6 +2241,7 @@
22412241
"isAsync": false
22422242
},
22432243
"git_status_file": {
2244+
"isAsync": true,
22442245
"args": {
22452246
"status_flags": {
22462247
"isReturn": true

generate/templates/partials/async_function.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ NAN_METHOD({{ cppClassName }}::{{ cppFunctionName }}) {
1010

1111
baton->error_code = GIT_OK;
1212
baton->error = NULL;
13+
{%if cppClassName == "GitStatus" %}
14+
{%if cppFunctionName == "File" %}
15+
baton->status_flags = (unsigned int *)malloc(sizeof(unsigned int));
16+
{%endif%}
17+
{%endif%}
1318

1419
{%each args|argsInfo as arg %}
1520
{%if arg.globalPayload %}
@@ -260,6 +265,12 @@ void {{ cppClassName }}::{{ cppFunctionName }}Worker::HandleOKCallback() {
260265
{%endeach%}
261266
}
262267

268+
{%if cppClassName == "GitStatus" %}
269+
{%if cppFunctionName == "File" %}
270+
free((void *)baton->status_flags);
271+
{%endif%}
272+
{%endif%}
273+
263274
{%each args|argsInfo as arg %}
264275
{%if arg.isCppClassStringOrArray %}
265276
{%if arg.freeFunctionName %}

lib/repository.js

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,15 @@ function getPathHunks(repo, index, filePath, isStaged, additionalDiffOptions) {
150150
});
151151
})
152152
.then(function(diff) {
153-
if (!(NodeGit.Status.file(repo, filePath) &
154-
NodeGit.Status.STATUS.WT_MODIFIED) &&
155-
!(NodeGit.Status.file(repo, filePath) &
156-
NodeGit.Status.STATUS.INDEX_MODIFIED)) {
157-
return Promise.reject
158-
("Selected staging is only available on modified files.");
159-
}
160-
161-
return diff.patches();
153+
return NodeGit.Status.file(repo, filePath)
154+
.then(function(status) {
155+
if (!(status & NodeGit.Status.STATUS.WT_MODIFIED) &&
156+
!(status & NodeGit.Status.STATUS.INDEX_MODIFIED)) {
157+
return Promise.reject
158+
("Selected staging is only available on modified files.");
159+
}
160+
return diff.patches();
161+
});
162162
})
163163
.then(function(patches) {
164164
var pathPatch = patches.filter(function(patch) {
@@ -1636,17 +1636,42 @@ Repository.prototype.stageFilemode =
16361636
})
16371637
.then(function(diff) {
16381638
var origLength = filePaths.length;
1639-
filePaths = filePaths.filter(function(p) {
1640-
return (
1641-
(NodeGit.Status.file(repo, p) & NodeGit.Status.STATUS.WT_MODIFIED) ||
1642-
(NodeGit.Status.file(repo, p) & NodeGit.Status.STATUS.INDEX_MODIFIED)
1643-
);
1644-
});
1645-
if (filePaths.length === 0 && origLength > 0) {
1646-
return Promise.reject
1647-
("Selected staging is only available on modified files.");
1648-
}
1649-
return diff.patches();
1639+
var fileFilterPromises = fp.map(function(p) {
1640+
return NodeGit.Status.file(repo, p)
1641+
.then(function(status) {
1642+
return {
1643+
path: p,
1644+
filter: (
1645+
(status & NodeGit.Status.STATUS.WT_MODIFIED) ||
1646+
(status & NodeGit.Status.STATUS.INDEX_MODIFIED)
1647+
)
1648+
};
1649+
});
1650+
}, filePaths);
1651+
1652+
return Promise.all(fileFilterPromises)
1653+
.then(function(results) {
1654+
filePaths = fp.flow([
1655+
fp.filter(function(filterResult) {
1656+
return filterResult.filter;
1657+
}),
1658+
fp.map(function(filterResult) {
1659+
return filterResult.path;
1660+
})
1661+
])(results);
1662+
1663+
if (filePaths.length === 0 && origLength > 0) {
1664+
return Promise.reject
1665+
("Selected staging is only available on modified files.");
1666+
}
1667+
return diff.patches();
1668+
});
1669+
// filePaths = filePaths.filter(function(p) {
1670+
// return (
1671+
// (NodeGit.Status.file(repo, p) & NodeGit.Status.STATUS.WT_MODIFIED) ||
1672+
// (NodeGit.Status.file(repo, p) & NodeGit.Status.STATUS.INDEX_MODIFIED)
1673+
// );
1674+
// });
16501675
})
16511676
.then(function(patches) {
16521677
var pathPatches = patches.filter(function(patch) {

0 commit comments

Comments
 (0)