Skip to content

Commit a84916a

Browse files
author
Yulong
authored
add worktree.js for worktree.prune and Worktree.add (nodegit#29)
1 parent 6570afb commit a84916a

5 files changed

Lines changed: 53 additions & 230 deletions

File tree

‎generate/input/descriptor.json‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4222,6 +4222,12 @@
42224222
}
42234223
}
42244224
},
4225+
"worktree_prune_options": {
4226+
"hasConstructor": true
4227+
},
4228+
"worktree_add_options": {
4229+
"hasConstructor": true
4230+
},
42254231
"writestream": {
42264232
"cType": "git_writestream",
42274233
"needsForwardDeclaration": false,

‎generate/input/libgit2-supplement.json‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,6 @@
107107
"isPrototypeMethod": false,
108108
"group": "clone"
109109
},
110-
"git_worktree_add": {
111-
"isManual": true,
112-
"cFile": "generate/templates/manual/worktree/worktree_add.cc",
113-
"isAsync": true,
114-
"isPrototypeMethod": false,
115-
"group": "worktree"
116-
},
117110
"git_commit_extract_signature": {
118111
"args": [
119112
{

‎generate/templates/manual/worktree/worktree_add.cc‎

Lines changed: 0 additions & 213 deletions
This file was deleted.

‎lib/worktree.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var NodeGit = require("../");
2+
var shallowClone = NodeGit.Utils.shallowClone;
3+
var normalizeOptions = NodeGit.Utils.normalizeOptions;
4+
5+
var Worktree = NodeGit.Worktree;
6+
var _prune = Worktree.prototype.prune;
7+
var _add = Worktree.add;
8+
9+
Worktree.add = function(repo, name, dir, options) {
10+
if (options) {
11+
options = shallowClone(options);
12+
} else {
13+
options = { lock:0, version: 1 };
14+
}
15+
options = normalizeOptions(options, NodeGit.WorktreeAddOptions);
16+
return _add.call(this, repo, name, dir, options);
17+
};
18+
19+
Worktree.prototype.prune = function(options) {
20+
if (options) {
21+
options = shallowClone(options);
22+
}
23+
options = normalizeOptions(options, NodeGit.WorktreePruneOptions);
24+
return _prune.call(this, options);
25+
};
26+

‎test/tests/worktree.js‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var path = require("path");
22
var assert = require("assert");
33
var local = path.join.bind(path, __dirname);
44

5-
describe("Worktree", function() {
5+
describe("Worktree", function () {
66
var NodeGit = require("../../");
77
var Repository = NodeGit.Repository;
88
var Worktree = NodeGit.Worktree;
@@ -14,34 +14,45 @@ describe("Worktree", function() {
1414
// Set a reasonable timeout here now that our repository has grown.
1515
this.timeout(30000);
1616

17-
before(function() {
17+
before(function () {
1818
var test = this;
1919
var url = "https://github.com/nodegit/test.git";
2020
var opts = {
21-
fetchOpts: {
22-
callbacks: {
23-
certificateCheck: () => 0
21+
fetchOpts: {
22+
callbacks: {
23+
certificateCheck: () => 0
2424
}
2525
}
2626
};
2727

28-
return Clone(url, clonePath, opts).then(function(repo) {
28+
return Clone(url, clonePath, opts).then(function (repo) {
2929
assert.ok(repo instanceof Repository);
3030
test.repository = repo;
3131
});
3232
});
3333

34-
it("can create worktree", function() {
34+
it("can create worktree", function () {
3535
return Worktree.add(this.repository, "workspace", worktreePath, {})
36-
.then(function(wt) {
36+
.then(function (wt) {
3737
assert.ok(wt instanceof Worktree);
3838
});
3939
});
4040

41-
it("can open a worktree repository", function() {
42-
return Repository.open(worktreePath).then(function(repo) {
41+
it("can open a worktree repository", function () {
42+
return Repository.open(worktreePath).then(function (repo) {
4343
assert.ok(repo instanceof Repository);
4444
assert.ok(repo.isWorktree());
4545
});
4646
});
47+
48+
it("can prune worktree", function () {
49+
const repository = this.repository;
50+
return Worktree.lookup(repository, "workspace")
51+
.then(function (wt) {
52+
assert.ok(wt instanceof Worktree);
53+
wt.prune({flags: 1});
54+
return assert.rejects(Worktree.lookup(repository, "workspace"));
55+
});
56+
});
57+
4758
});

0 commit comments

Comments
 (0)