Skip to content

Commit f10a193

Browse files
Merge pull request #1916 from julianmesa-gitkraken/get-patches-from-index
Improve Diff.patches to allow an index array
2 parents 6e9a540 + 711bb65 commit f10a193

3 files changed

Lines changed: 77 additions & 24 deletions

File tree

generate/input/libgit2-supplement.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,10 @@
673673
"name": "diff",
674674
"type": "git_diff *"
675675
},
676+
{
677+
"name": "indexes",
678+
"type": "std::vector<int>"
679+
},
676680
{
677681
"name": "out",
678682
"type": "std::vector<PatchData*> *"

generate/templates/manual/patches/convenient_patches.cc

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ NAN_METHOD(GitPatch::ConvenientFromDiff) {
1313
baton->error = NULL;
1414

1515
baton->diff = Nan::ObjectWrap::Unwrap<GitDiff>(Nan::To<v8::Object>(info[0]).ToLocalChecked())->GetValue();
16+
17+
if (info[1]->IsArray()) {
18+
v8::Local<v8::Context> context = Nan::GetCurrentContext();
19+
const v8::Local<v8::Array> indexesArray = info[1].As<v8::Array>();
20+
const uint32_t numIndexes = indexesArray->Length();
21+
22+
for (uint32_t i = 0; i < numIndexes; ++i) {
23+
v8::Local<v8::Value> value = indexesArray->Get(context, i).ToLocalChecked();
24+
int idx = value.As<v8::Number>()->Value();
25+
baton->indexes.push_back(idx);
26+
}
27+
}
28+
1629
baton->out = new std::vector<PatchData *>;
1730
baton->out->reserve(git_diff_num_deltas(baton->diff));
1831

@@ -37,37 +50,73 @@ void GitPatch::ConvenientFromDiffWorker::Execute() {
3750

3851
std::vector<git_patch *> patchesToBeFreed;
3952

40-
for (std::size_t i = 0; i < git_diff_num_deltas(baton->diff); ++i) {
41-
git_patch *nextPatch;
42-
int result = git_patch_from_diff(&nextPatch, baton->diff, i);
53+
if (baton->indexes.size() > 0) {
54+
for (int idx : baton->indexes) {
55+
git_patch *nextPatch;
56+
int result = git_patch_from_diff(&nextPatch, baton->diff, idx);
57+
58+
if (result) {
59+
while (!patchesToBeFreed.empty())
60+
{
61+
git_patch_free(patchesToBeFreed.back());
62+
patchesToBeFreed.pop_back();
63+
}
64+
65+
while (!baton->out->empty()) {
66+
PatchDataFree(baton->out->back());
67+
baton->out->pop_back();
68+
}
69+
70+
baton->error_code = result;
71+
72+
if (git_error_last() != NULL) {
73+
baton->error = git_error_dup(git_error_last());
74+
}
4375

44-
if (result) {
45-
while (!patchesToBeFreed.empty())
46-
{
47-
git_patch_free(patchesToBeFreed.back());
48-
patchesToBeFreed.pop_back();
76+
delete baton->out;
77+
baton->out = NULL;
78+
79+
return;
4980
}
5081

51-
while (!baton->out->empty()) {
52-
PatchDataFree(baton->out->back());
53-
baton->out->pop_back();
82+
if (nextPatch != NULL) {
83+
baton->out->push_back(createFromRaw(nextPatch));
84+
patchesToBeFreed.push_back(nextPatch);
5485
}
86+
}
87+
} else {
88+
for (std::size_t i = 0; i < git_diff_num_deltas(baton->diff); ++i) {
89+
git_patch *nextPatch;
90+
int result = git_patch_from_diff(&nextPatch, baton->diff, i);
5591

56-
baton->error_code = result;
92+
if (result) {
93+
while (!patchesToBeFreed.empty())
94+
{
95+
git_patch_free(patchesToBeFreed.back());
96+
patchesToBeFreed.pop_back();
97+
}
5798

58-
if (git_error_last() != NULL) {
59-
baton->error = git_error_dup(git_error_last());
60-
}
99+
while (!baton->out->empty()) {
100+
PatchDataFree(baton->out->back());
101+
baton->out->pop_back();
102+
}
61103

62-
delete baton->out;
63-
baton->out = NULL;
104+
baton->error_code = result;
64105

65-
return;
66-
}
106+
if (git_error_last() != NULL) {
107+
baton->error = git_error_dup(git_error_last());
108+
}
67109

68-
if (nextPatch != NULL) {
69-
baton->out->push_back(createFromRaw(nextPatch));
70-
patchesToBeFreed.push_back(nextPatch);
110+
delete baton->out;
111+
baton->out = NULL;
112+
113+
return;
114+
}
115+
116+
if (nextPatch != NULL) {
117+
baton->out->push_back(createFromRaw(nextPatch));
118+
patchesToBeFreed.push_back(nextPatch);
119+
}
71120
}
72121
}
73122

lib/diff.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,6 @@ Diff.blobToBuffer= function(
6262
* @return {Array<ConvenientPatch>} a promise that resolves to an array of
6363
* ConvenientPatches
6464
*/
65-
Diff.prototype.patches = function() {
66-
return Patch.convenientFromDiff(this);
65+
Diff.prototype.patches = function(idxs) {
66+
return Patch.convenientFromDiff(this, idxs);
6767
};

0 commit comments

Comments
 (0)