Skip to content

Commit f91c501

Browse files
committed
Merge pull request nodegit#495 from nodegit/enable-stash
Enable `git_stash_foreach`
2 parents 873a80c + 3528b52 commit f91c501

5 files changed

Lines changed: 135 additions & 3 deletions

File tree

generate/input/callbacks.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@
424424
},
425425
{
426426
"name": "stash_id",
427-
"cType": "const int *"
427+
"cType": "const git_oid *"
428428
},
429429
{
430430
"name": "payload",
@@ -433,7 +433,7 @@
433433
],
434434
"return": {
435435
"type": "int",
436-
"noResults": 1,
436+
"noResults":0,
437437
"success": 0,
438438
"error": -1
439439
}

generate/input/descriptor.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,10 @@
15861586
"stash": {
15871587
"functions": {
15881588
"git_stash_foreach": {
1589-
"ignore": true
1589+
"isAsync": true,
1590+
"return": {
1591+
"isErrorCode": true
1592+
}
15901593
}
15911594
}
15921595
},

generate/input/libgit2-supplement.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,36 @@
141141
"type": "int"
142142
},
143143
"group": "reset"
144+
},
145+
"git_stash_save": {
146+
"type": "function",
147+
"file": "stash.h",
148+
"args": [
149+
{
150+
"name": "out",
151+
"type": "git_oid *"
152+
},
153+
{
154+
"name": "repo",
155+
"type": "git_repository *"
156+
},
157+
{
158+
"name": "stasher",
159+
"type": "const git_signature *"
160+
},
161+
{
162+
"name": "message",
163+
"type": "const char *"
164+
},
165+
{
166+
"name": "flags",
167+
"type": "unsigned int"
168+
}
169+
],
170+
"return": {
171+
"type": "int"
172+
},
173+
"group": "stash"
144174
}
145175
},
146176
"groups": [
@@ -470,6 +500,9 @@
470500
"groups": {
471501
"reset": [
472502
"git_reset"
503+
],
504+
"stash": [
505+
"git_stash_save"
473506
]
474507
}
475508
}

lib/stash.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var NodeGit = require("../");
2+
3+
var Stash = NodeGit.Stash;
4+
5+
// Override Stash.foreach to eliminate the need to pass null payload
6+
var foreach = Stash.foreach;
7+
Stash.foreach = function(repo, callback) {
8+
return foreach(repo, callback, null);
9+
};

test/tests/stash.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
var assert = require("assert");
2+
var path = require("path");
3+
var promisify = require("promisify-node");
4+
var Promise = require("nodegit-promise");
5+
var fse = promisify(require("fs-extra"));
6+
var local = path.join.bind(path, __dirname);
7+
8+
describe("Stash", function() {
9+
var NodeGit = require("../../");
10+
var Repository = NodeGit.Repository;
11+
var Stash = NodeGit.Stash;
12+
13+
var reposPath = local("../repos/workdir");
14+
15+
before(function() {
16+
var test = this;
17+
return Repository.open(reposPath)
18+
.then(function(repository) {
19+
test.repository = repository;
20+
});
21+
});
22+
23+
it("gets no stashes on clean working directory", function() {
24+
var stashes = [];
25+
var stashCb = function(index, message, oid) {
26+
stashes.push({index: index, message: message, oid: oid});
27+
};
28+
29+
return Stash.foreach(this.repository, stashCb)
30+
.then(function() {
31+
assert.equal(stashes.length, 0);
32+
});
33+
});
34+
35+
it("can save and drop a stash", function() {
36+
var fileName = "README.md";
37+
var fileContent = "Cha-cha-cha-chaaaaaangessssss";
38+
var repo = this.repository;
39+
var filePath = path.join(repo.workdir(), fileName);
40+
var oldContent;
41+
var stashes = [];
42+
var stashOid;
43+
var stashMessage = "stash test";
44+
45+
return fse.readFile(filePath)
46+
.then(function(content) {
47+
oldContent = content;
48+
return fse.writeFile(filePath, fileContent);
49+
})
50+
.then(function() {
51+
return Stash.save(repo, repo.defaultSignature(), stashMessage, 0);
52+
})
53+
.then(function(oid) {
54+
stashOid = oid;
55+
var stashCb = function(index, message, oid) {
56+
stashes.push({index: index, message: message, oid: oid});
57+
};
58+
59+
return Stash.foreach(repo, stashCb);
60+
})
61+
.then(function() {
62+
assert.equal(stashes.length, 1);
63+
assert.equal(stashes[0].index, 0);
64+
assert.equal(stashes[0].message, "On master: " + stashMessage);
65+
assert.equal(stashes[0].oid.toString(), stashOid.toString());
66+
67+
return Stash.drop(repo, 0);
68+
})
69+
.then(function () {
70+
stashes = [];
71+
var stashCb = function(index, message, oid) {
72+
stashes.push({index: index, message: message, oid: oid});
73+
};
74+
75+
return Stash.foreach(repo, stashCb);
76+
})
77+
.then(function() {
78+
assert.equal(stashes.length, 0);
79+
})
80+
.catch(function(e) {
81+
return fse.writeFile(filePath, oldContent)
82+
.then(function() {
83+
return Promise.reject(e);
84+
});
85+
});
86+
});
87+
});

0 commit comments

Comments
 (0)