Skip to content

Commit 12919bf

Browse files
author
Carson Howard
committed
Exposed filter_list; Fixed discard lines to use filters
1 parent 4611ca7 commit 12919bf

7 files changed

Lines changed: 279 additions & 24 deletions

File tree

generate/input/descriptor.json

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@
258258
"args": {
259259
"out": {
260260
"isReturn": true,
261-
"cppClassName": "GitBuf",
261+
"cppClassName": "GitBuf",
262262
"jsClassName": "Buffer",
263263
"cType": "git_buf *",
264264
"shouldAlloc": true
@@ -1008,19 +1008,76 @@
10081008
"hasConstructor": true,
10091009
"functions": {
10101010
"git_filter_list_apply_to_blob": {
1011-
"ignore": true
1011+
"async": true,
1012+
"return": {
1013+
"isErrorCode": true
1014+
},
1015+
"args": {
1016+
"out": {
1017+
"isReturn": true,
1018+
"shouldAlloc": true
1019+
},
1020+
"filters": {
1021+
"isSelf": true
1022+
}
1023+
}
10121024
},
10131025
"git_filter_list_apply_to_data": {
1014-
"ignore": true
1026+
"async": true,
1027+
"return": {
1028+
"isErrorCode": true
1029+
},
1030+
"args": {
1031+
"out": {
1032+
"isReturn": true,
1033+
"shouldAlloc": true
1034+
},
1035+
"filters": {
1036+
"isSelf": true
1037+
}
1038+
}
10151039
},
10161040
"git_filter_list_apply_to_file": {
1017-
"ignore": true
1041+
"async": true,
1042+
"return": {
1043+
"isErrorCode": true
1044+
},
1045+
"args": {
1046+
"out": {
1047+
"isReturn": true,
1048+
"shouldAlloc": true
1049+
},
1050+
"filters": {
1051+
"isSelf": true
1052+
}
1053+
}
10181054
},
10191055
"git_filter_list_free": {
1020-
"ignore": true
1056+
"async": true,
1057+
"args": {
1058+
"filters": {
1059+
"isSelf": true
1060+
}
1061+
}
10211062
},
10221063
"git_filter_list_load": {
1023-
"ignore": true
1064+
"async": true,
1065+
"return": {
1066+
"isErrorCode": true
1067+
},
1068+
"args": {
1069+
"filters": {
1070+
"isReturn": true,
1071+
"cType": "git_filter_list **"
1072+
},
1073+
"repo": {
1074+
"cType": "git_repository *"
1075+
},
1076+
"blob": {
1077+
"isOptional": true,
1078+
"cType": "git_blob *"
1079+
}
1080+
}
10241081
},
10251082
"git_filter_list_push": {
10261083
"ignore": true
@@ -1769,6 +1826,9 @@
17691826
}
17701827
},
17711828
"pathspec": {
1829+
"dependencies": [
1830+
"../include/str_array_converter.h"
1831+
],
17721832
"functions": {
17731833
"git_pathspec_match_list_free": {
17741834
"ignore": true
@@ -2001,6 +2061,7 @@
20012061
},
20022062
"remote": {
20032063
"dependencies": [
2064+
"../include/str_array_converter.h",
20042065
"../include/remote_head.h"
20052066
],
20062067
"cType": "git_remote",
@@ -2282,6 +2343,9 @@
22822343
}
22832344
},
22842345
"reset": {
2346+
"dependencies": [
2347+
"../include/str_array_converter.h"
2348+
],
22852349
"functions": {
22862350
"git_reset": {
22872351
"args": {
@@ -2615,6 +2679,9 @@
26152679
}
26162680
},
26172681
"tag": {
2682+
"dependencies": [
2683+
"../include/str_array_converter.h"
2684+
],
26182685
"selfFreeing": true,
26192686
"functions": {
26202687
"git_tag_foreach": {

generate/input/libgit2-supplement.json

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@
273273
"git_annotated_commit_lookup"
274274
]
275275
],
276+
[
277+
"filter_list",
278+
[
279+
"git_filter_list_apply_to_blob",
280+
"git_filter_list_apply_to_data",
281+
"git_filter_list_apply_to_file",
282+
"git_filter_list_free",
283+
"git_filter_list_load"
284+
]
285+
],
276286
[
277287
"filter_source",
278288
[
@@ -772,13 +782,6 @@
772782
},
773783
"filter": {
774784
"functions": [
775-
"git_filter_list_apply_to_blob",
776-
"git_filter_list_apply_to_data",
777-
"git_filter_list_apply_to_file",
778-
"git_filter_list_contains",
779-
"git_filter_list_free",
780-
"git_filter_list_length",
781-
"git_filter_list_load",
782785
"git_filter_list_new",
783786
"git_filter_list_push",
784787
"git_filter_list_stream_blob",

generate/templates/manual/include/git_buf_converter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
#include <v8.h>
55

66
#include "nan.h"
7-
#include "git2/strarray.h"
7+
#include "git2/buffer.h"
88

99
using namespace v8;
1010

11-
class StrArrayConverter {
11+
class GitBufConverter {
1212
public:
13-
static git_strarray *Convert (v8::Local<v8::Value> val);
13+
static git_buf *Convert(v8::Local<v8::Value> val);
1414
};
1515

1616
#endif
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
#include <nan.h>
22
#include <node.h>
33
#include <string>
4-
#include <cstring>
54

65
#include "../include/git_buf_converter.h"
76
#include "git2/buffer.h"
87

98
using namespace v8;
109
using namespace node;
1110

12-
git_buf *StrArrayConverter::Convert(Local<v8::Value> val) {
11+
git_buf *GitBufConverter::Convert(Local<v8::Value> val) {
1312
if (val->IsString() || val->IsStringObject()) {
14-
string v8String = ConvertString(val->ToString());
13+
v8::String::Utf8Value param1(val->ToString());
14+
std::string v8String = std::string(*param1);
15+
1516
const size_t size = sizeof(git_buf);
1617
uint8_t* memory = reinterpret_cast<uint8_t *>(malloc(size));
1718
git_buf *result = reinterpret_cast<git_buf *>(memory);
18-
size_t stringLength = v8String.length() - 1;
19+
size_t stringLength = v8String.length();
1920

2021
memory = reinterpret_cast<uint8_t *>(malloc(stringLength));
2122

2223
memcpy(memory, v8String.c_str(), stringLength);
2324

25+
result->asize = stringLength;
2426
result->size = stringLength;
2527
result->ptr = reinterpret_cast<char *>(memory);
2628
return result;
2729
} else {
2830
return NULL;
2931
}
30-
}
32+
}

generate/templates/templates/binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"src/convenient_patch.cc",
2323
"src/convenient_hunk.cc",
2424
"src/filter_registry.cc",
25+
"src/git_buf_converter.cc",
2526
"src/str_array_converter.cc",
2627
"src/thread_pool.cc",
2728
{% each %}

lib/repository.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var Commit = NodeGit.Commit;
88
var normalizeOptions = NodeGit.Utils.normalizeOptions;
99
var shallowClone = NodeGit.Utils.shallowClone;
1010
var path = require("path");
11+
var Filter = NodeGit.Filter;
12+
var FilterList = NodeGit.FilterList;
1113
var Reference = NodeGit.Reference;
1214
var Remote = NodeGit.Remote;
1315
var Repository = NodeGit.Repository;
@@ -37,7 +39,6 @@ function applySelectedLinesToTarget
3739
var oldIndex = 0;
3840
var linesPromises = [];
3941

40-
// split the original file into lines
4142
var oldLines = originalContent.toString().split("\n");
4243

4344
// if no selected lines were sent, return the original content
@@ -121,6 +122,7 @@ function applySelectedLinesToTarget
121122
}
122123
}
123124
}
125+
124126
return newContent;
125127
});
126128
}
@@ -155,7 +157,7 @@ function getPathHunks(repo, index, filePath, isStaged, additionalDiffOptions) {
155157
if (!(status & NodeGit.Status.STATUS.WT_MODIFIED) &&
156158
!(status & NodeGit.Status.STATUS.INDEX_MODIFIED)) {
157159
return Promise.reject
158-
("Selected staging is only available on modified files.");
160+
("Selected staging is only available on modified files.");
159161
}
160162
return diff.patches();
161163
});
@@ -729,15 +731,28 @@ Repository.prototype.discardLines =
729731
var fullFilePath = path.join(repo.workdir(), filePath);
730732
var index;
731733
var originalContent;
734+
var filterList;
732735

733736
return repo.refreshIndex()
734737
.then(function(indexResult) {
735738
index = indexResult;
739+
return FilterList.load(repo, null, filePath, Filter.MODE.CLEAN, Filter.FLAG.DEFAULT);
740+
})
741+
.then(function(_filterList) {
742+
filterList = _filterList;
743+
744+
if (filterList) {
745+
return filterList.applyToFile(repo, filePath);
746+
}
736747

737748
return fse.readFile(fullFilePath, "utf8");
738749
})
739750
.then(function(content) {
740751
originalContent = content;
752+
if (filterList) {
753+
filterList.free();
754+
filterList = null;
755+
}
741756

742757
return getPathHunks(repo, index, filePath, false, additionalDiffOptions);
743758
})
@@ -747,7 +762,23 @@ Repository.prototype.discardLines =
747762
);
748763
})
749764
.then(function(newContent) {
750-
return fse.writeFile(fullFilePath, newContent);
765+
return FilterList.load(repo, null, filePath, Filter.MODE.SMUDGE, Filter.FLAG.DEFAULT)
766+
.then(function(_filterList) {
767+
filterList = _filterList;
768+
if (filterList) {
769+
return filterList.applyToData(new String(newContent));
770+
}
771+
772+
return newContent;
773+
});
774+
})
775+
.then(function(filteredContent) {
776+
if (filterList) {
777+
filterList.free();
778+
filterList = null;
779+
}
780+
781+
return fse.writeFile(fullFilePath, filteredContent);
751782
});
752783
};
753784

0 commit comments

Comments
 (0)