Commit 12919bf6 authored by Carson Howard's avatar Carson Howard

Exposed filter_list; Fixed discard lines to use filters

parent 4611ca75
......@@ -1008,19 +1008,76 @@
"hasConstructor": true,
"functions": {
"git_filter_list_apply_to_blob": {
"ignore": true
"async": true,
"return": {
"isErrorCode": true
},
"args": {
"out": {
"isReturn": true,
"shouldAlloc": true
},
"filters": {
"isSelf": true
}
}
},
"git_filter_list_apply_to_data": {
"ignore": true
"async": true,
"return": {
"isErrorCode": true
},
"args": {
"out": {
"isReturn": true,
"shouldAlloc": true
},
"filters": {
"isSelf": true
}
}
},
"git_filter_list_apply_to_file": {
"ignore": true
"async": true,
"return": {
"isErrorCode": true
},
"args": {
"out": {
"isReturn": true,
"shouldAlloc": true
},
"filters": {
"isSelf": true
}
}
},
"git_filter_list_free": {
"ignore": true
"async": true,
"args": {
"filters": {
"isSelf": true
}
}
},
"git_filter_list_load": {
"ignore": true
"async": true,
"return": {
"isErrorCode": true
},
"args": {
"filters": {
"isReturn": true,
"cType": "git_filter_list **"
},
"repo": {
"cType": "git_repository *"
},
"blob": {
"isOptional": true,
"cType": "git_blob *"
}
}
},
"git_filter_list_push": {
"ignore": true
......@@ -1769,6 +1826,9 @@
}
},
"pathspec": {
"dependencies": [
"../include/str_array_converter.h"
],
"functions": {
"git_pathspec_match_list_free": {
"ignore": true
......@@ -2001,6 +2061,7 @@
},
"remote": {
"dependencies": [
"../include/str_array_converter.h",
"../include/remote_head.h"
],
"cType": "git_remote",
......@@ -2282,6 +2343,9 @@
}
},
"reset": {
"dependencies": [
"../include/str_array_converter.h"
],
"functions": {
"git_reset": {
"args": {
......@@ -2615,6 +2679,9 @@
}
},
"tag": {
"dependencies": [
"../include/str_array_converter.h"
],
"selfFreeing": true,
"functions": {
"git_tag_foreach": {
......
......@@ -273,6 +273,16 @@
"git_annotated_commit_lookup"
]
],
[
"filter_list",
[
"git_filter_list_apply_to_blob",
"git_filter_list_apply_to_data",
"git_filter_list_apply_to_file",
"git_filter_list_free",
"git_filter_list_load"
]
],
[
"filter_source",
[
......@@ -772,13 +782,6 @@
},
"filter": {
"functions": [
"git_filter_list_apply_to_blob",
"git_filter_list_apply_to_data",
"git_filter_list_apply_to_file",
"git_filter_list_contains",
"git_filter_list_free",
"git_filter_list_length",
"git_filter_list_load",
"git_filter_list_new",
"git_filter_list_push",
"git_filter_list_stream_blob",
......
......@@ -4,13 +4,13 @@
#include <v8.h>
#include "nan.h"
#include "git2/strarray.h"
#include "git2/buffer.h"
using namespace v8;
class StrArrayConverter {
class GitBufConverter {
public:
static git_strarray *Convert (v8::Local<v8::Value> val);
static git_buf *Convert(v8::Local<v8::Value> val);
};
#endif
#include <nan.h>
#include <node.h>
#include <string>
#include <cstring>
#include "../include/git_buf_converter.h"
#include "git2/buffer.h"
......@@ -9,18 +8,21 @@
using namespace v8;
using namespace node;
git_buf *StrArrayConverter::Convert(Local<v8::Value> val) {
git_buf *GitBufConverter::Convert(Local<v8::Value> val) {
if (val->IsString() || val->IsStringObject()) {
string v8String = ConvertString(val->ToString());
v8::String::Utf8Value param1(val->ToString());
std::string v8String = std::string(*param1);
const size_t size = sizeof(git_buf);
uint8_t* memory = reinterpret_cast<uint8_t *>(malloc(size));
git_buf *result = reinterpret_cast<git_buf *>(memory);
size_t stringLength = v8String.length() - 1;
size_t stringLength = v8String.length();
memory = reinterpret_cast<uint8_t *>(malloc(stringLength));
memcpy(memory, v8String.c_str(), stringLength);
result->asize = stringLength;
result->size = stringLength;
result->ptr = reinterpret_cast<char *>(memory);
return result;
......
......@@ -22,6 +22,7 @@
"src/convenient_patch.cc",
"src/convenient_hunk.cc",
"src/filter_registry.cc",
"src/git_buf_converter.cc",
"src/str_array_converter.cc",
"src/thread_pool.cc",
{% each %}
......
......@@ -8,6 +8,8 @@ var Commit = NodeGit.Commit;
var normalizeOptions = NodeGit.Utils.normalizeOptions;
var shallowClone = NodeGit.Utils.shallowClone;
var path = require("path");
var Filter = NodeGit.Filter;
var FilterList = NodeGit.FilterList;
var Reference = NodeGit.Reference;
var Remote = NodeGit.Remote;
var Repository = NodeGit.Repository;
......@@ -37,7 +39,6 @@ function applySelectedLinesToTarget
var oldIndex = 0;
var linesPromises = [];
// split the original file into lines
var oldLines = originalContent.toString().split("\n");
// if no selected lines were sent, return the original content
......@@ -121,6 +122,7 @@ function applySelectedLinesToTarget
}
}
}
return newContent;
});
}
......@@ -729,15 +731,28 @@ Repository.prototype.discardLines =
var fullFilePath = path.join(repo.workdir(), filePath);
var index;
var originalContent;
var filterList;
return repo.refreshIndex()
.then(function(indexResult) {
index = indexResult;
return FilterList.load(repo, null, filePath, Filter.MODE.CLEAN, Filter.FLAG.DEFAULT);
})
.then(function(_filterList) {
filterList = _filterList;
if (filterList) {
return filterList.applyToFile(repo, filePath);
}
return fse.readFile(fullFilePath, "utf8");
})
.then(function(content) {
originalContent = content;
if (filterList) {
filterList.free();
filterList = null;
}
return getPathHunks(repo, index, filePath, false, additionalDiffOptions);
})
......@@ -747,7 +762,23 @@ Repository.prototype.discardLines =
);
})
.then(function(newContent) {
return fse.writeFile(fullFilePath, newContent);
return FilterList.load(repo, null, filePath, Filter.MODE.SMUDGE, Filter.FLAG.DEFAULT)
.then(function(_filterList) {
filterList = _filterList;
if (filterList) {
return filterList.applyToData(new String(newContent));
}
return newContent;
});
})
.then(function(filteredContent) {
if (filterList) {
filterList.free();
filterList = null;
}
return fse.writeFile(fullFilePath, filteredContent);
});
};
......
......@@ -12,6 +12,7 @@ describe("Filter", function() {
var Registry = NodeGit.FilterRegistry;
var Checkout = NodeGit.Checkout;
var Repository = NodeGit.Repository;
var FilterList = NodeGit.FilterList;
var reposPath = local("../repos/workdir");
var packageJsonPath = path.join(reposPath, "package.json");
......@@ -926,4 +927,154 @@ describe("Filter", function() {
});
});
});
describe("Manually Apply", function() {
before(function() {
var test = this;
return fse.readFile(readmePath, "utf8")
.then((function(content) {
test.originalReadmeContent = content;
}));
});
afterEach(function() {
this.timeout(15000);
return fse.writeFile(readmePath, this.originalReadmeContent);
});
var message = "some new fancy filter";
var length = message.length;
var tempBuffer = new Buffer(message, "utf-8");
it("test test test", function() {
assert.equal(true, true);
});
it("applies the filters for a path on demand", function() {
var test = this;
var list;
return Registry.register(filterName, {
apply: function(to, from, source) {
return to.set(tempBuffer, length)
.then(function() {
return NodeGit.Error.CODE.OK;
});
},
check: function(src, attr) {
return NodeGit.Error.CODE.OK;
}
}, 0)
.then(function(result) {
assert.strictEqual(result, 0);
})
.then(function() {
var readmeContent = fse.readFileSync(
readmePath,
"utf-8"
);
assert.notStrictEqual(readmeContent, message);
fse.writeFileSync(readmePath, "whoa", "utf8");
return FilterList.load(test.repository, null, "README.md", NodeGit.Filter.MODE.CLEAN, NodeGit.Filter.FLAG.DEFAULT);
})
.then(function(_list) {
list = _list;
return list.applyToFile(test.repository, "README.md");
})
.then(function(content) {
assert.strictEqual(content, message);
list.free();
});
});
it("applies the filters to a buffer on demand", function() {
var test = this;
var list;
return Registry.register(filterName, {
apply: function(to, from, source) {
return to.set(tempBuffer, length)
.then(function() {
return NodeGit.Error.CODE.OK;
});
},
check: function(src, attr) {
return NodeGit.Error.CODE.OK;
}
}, 0)
.then(function(result) {
assert.strictEqual(result, 0);
})
.then(function() {
var readmeContent = fse.readFileSync(
readmePath,
"utf-8"
);
assert.notStrictEqual(readmeContent, message);
fse.writeFileSync(readmePath, "whoa", "utf8");
return FilterList.load(test.repository, null, "README.md", NodeGit.Filter.MODE.CLEAN, NodeGit.Filter.FLAG.DEFAULT);
})
.then(function(_list) {
list = _list;
return list.applyToData(new String("garbo garbo garbo garbo"));
})
.then(function(content) {
assert.strictEqual(content, message);
list.free();
});
});
it("applies the filters to a blob on demand", function() {
var test = this;
var list;
return Registry.register(filterName, {
apply: function(to, from, source) {
return to.set(tempBuffer, length)
.then(function() {
return NodeGit.Error.CODE.OK;
});
},
check: function(src, attr) {
return NodeGit.Error.CODE.OK;
}
}, 0)
.then(function(result) {
assert.strictEqual(result, 0);
})
.then(function() {
var readmeContent = fse.readFileSync(
readmePath,
"utf-8"
);
assert.notStrictEqual(readmeContent, message);
fse.writeFileSync(readmePath, "whoa", "utf8");
return FilterList.load(test.repository, null, "README.md", NodeGit.Filter.MODE.CLEAN, NodeGit.Filter.FLAG.DEFAULT);
})
.then(function(_list) {
list = _list;
return test.repository.getHeadCommit();
})
.then(function(commit) {
return commit.getTree();
})
.then(function(tree) {
return tree.entryByPath("README.md");
})
.then(function(entry) {
return test.repository.getBlob(entry.id());
})
.then(function(blob) {
return list.applyToBlob(blob);
})
.then(function(content) {
assert.strictEqual(content, message);
list.free();
});
});
});
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment