Skip to content

Commit e9ba21c

Browse files
committed
feat: add patch to convenient function
1 parent 6ddfe14 commit e9ba21c

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

generate/input/libgit2-supplement.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,19 @@
693693
"isErrorCode": true
694694
}
695695
},
696+
"git_patch_to_convenient": {
697+
"args": [],
698+
"type": "function",
699+
"isManual": true,
700+
"cFile": "generate/templates/manual/patches/to_convenient.cc",
701+
"isAsync": false,
702+
"isPrototypeMethod": true,
703+
"group": "patch",
704+
"return": {
705+
"type": "PatchData*",
706+
"isErrorCode": false
707+
}
708+
},
696709
"git_path_is_gitfile": {
697710
"type": "function",
698711
"file": "sys/path.h",
@@ -1221,7 +1234,8 @@
12211234
[
12221235
"patch",
12231236
[
1224-
"git_patch_convenient_from_diff"
1237+
"git_patch_convenient_from_diff",
1238+
"git_patch_to_convenient"
12251239
]
12261240
],
12271241
[
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
NAN_METHOD(GitPatch::ToConvenient) {
2+
Nan::EscapableHandleScope scope;
3+
4+
git_error_clear();
5+
6+
{ // lock master scope start
7+
nodegit::LockMaster lockMaster(
8+
/*asyncAction: */false,
9+
Nan::ObjectWrap::Unwrap<GitPatch>(info.This())->GetValue()
10+
);
11+
12+
const PatchData * result = createFromRaw(
13+
Nan::ObjectWrap::Unwrap<GitPatch>(info.This())->GetValue()
14+
);
15+
16+
// null checks on pointers
17+
if (!result) {
18+
return info.GetReturnValue().Set(scope.Escape(Nan::Undefined()));
19+
}
20+
21+
v8::Local<v8::Value> v8ConversionSlot;
22+
// start convert_to_v8 block
23+
if (result != NULL) {
24+
v8ConversionSlot = ConvenientPatch::New((void *) result);
25+
}
26+
else {
27+
v8ConversionSlot = Nan::Null();
28+
}
29+
// end convert_to_v8 block
30+
31+
return info.GetReturnValue().Set(scope.Escape(v8ConversionSlot));
32+
}
33+
}

test/tests/patch.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe("Patch", function() {
8484
});
8585

8686
it("can generate patch from blobs without 'old_blob'", function() {
87-
// Generates a patch for README.md from commit
87+
// Generates a patch for README.md from commit
8888
// fce88902e66c72b5b93e75bdb5ae717038b221f6 without
8989
// old_blob. Should show all lines as additions.
9090
const file = "README.md";
@@ -106,4 +106,26 @@ describe("Patch", function() {
106106
assert.strictEqual(patch.size(0, 0, 0), 0);
107107
});
108108
});
109+
110+
it("can create convenient patch from patch", function() {
111+
// Generates a patch for README.md from commit
112+
// fce88902e66c72b5b93e75bdb5ae717038b221f6
113+
const file = "README.md";
114+
115+
return NodeGit.Blob.lookup(
116+
this.repository,
117+
"b252f396b17661462372f78b7bcfc403b8731aaa"
118+
).then(blob => {
119+
return NodeGit.Blob.lookup(
120+
this.repository,
121+
"b8d014998072c3f9e4b7eba8486011e80d8de98a"
122+
).then(oldBlob => {
123+
return NodeGit.Patch.fromBlobs(oldBlob, file, blob, file)
124+
.then(patch => {
125+
var conv = patch.toConvenient();
126+
assert.equal(conv.size(), patch.numHunks());
127+
});
128+
});
129+
});
130+
});
109131
});

0 commit comments

Comments
 (0)