@@ -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
0 commit comments