Skip to content

Commit c01531e

Browse files
author
John Haley
committed
Add git_reset with test cases
1 parent 2343430 commit c01531e

File tree

3 files changed

+160
-1
lines changed

3 files changed

+160
-1
lines changed

generate/input/descriptor.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,6 +1416,17 @@
14161416
"reset": {
14171417
"functions": {
14181418
"git_reset": {
1419+
"args": {
1420+
"checkout_opts": {
1421+
"isOptional": true
1422+
},
1423+
"log_message": {
1424+
"isOptional": true
1425+
},
1426+
"signature": {
1427+
"isOptional": true
1428+
}
1429+
},
14191430
"isAsync": true,
14201431
"return": {
14211432
"isErrorCode": true

lib/reset.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@ Reset.default = function(repo, target, pathspecs) {
99
};
1010

1111
var reset = Reset.reset;
12-
Reset.reset = function(target, resetType, checkoutOpts, signature, logMessage) {
12+
Reset.reset = function( repo,
13+
target,
14+
resetType,
15+
checkoutOpts,
16+
signature,
17+
logMessage) {
1318
checkoutOpts = normalizeOptions(checkoutOpts, NodeGit.CheckoutOptions);
1419

1520
return reset.call(
1621
this,
22+
repo,
1723
target,
1824
resetType,
1925
checkoutOpts,

test/tests/reset.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var assert = require("assert");
22
var path = require("path");
33
var local = path.join.bind(path, __dirname);
4+
var promisify = require("promisify-node");
5+
var fse = promisify(require("fs-extra"));
46

57
describe("Reset", function() {
68
var Repository = require(local("../../lib/repository"));
@@ -72,6 +74,146 @@ describe("Reset", function() {
7274

7375
assert(resetContents != currentCommitContents);
7476
assert(resetContents == previousCommitContents);
77+
})
78+
.then(function() {
79+
return Reset.default(test.repo, test.currentCommit, filePath);
80+
})
81+
.then(function() {
82+
return test.repo.openIndex();
83+
})
84+
.then(function(index) {
85+
return index.writeTree();
86+
})
87+
.then(function(oid) {
88+
return test.repo.getTree(oid);
89+
})
90+
.then(function(tree) {
91+
return tree.getEntry(filePath);
92+
})
93+
.then(function(entry) {
94+
return entry.getBlob();
95+
})
96+
.then(function(blob) {
97+
var currentCommitContents = test.currentCommitBlob.toString();
98+
var previousCommitContents = test.previousCommitBlob.toString();
99+
var resetContents = blob.toString();
100+
101+
assert(resetContents == currentCommitContents);
102+
assert(resetContents != previousCommitContents);
103+
});
104+
});
105+
106+
it("can perform a soft reset", function() {
107+
var test = this;
108+
109+
return Reset.reset(test.repo, test.previousCommit, Reset.TYPE.SOFT)
110+
.then(function() {
111+
return test.repo.openIndex();
112+
})
113+
.then(function(index) {
114+
return index.writeTree();
115+
})
116+
.then(function(oid) {
117+
return test.repo.getTree(oid);
118+
})
119+
.then(function(tree) {
120+
return tree.getEntry(filePath);
121+
})
122+
.then(function(entry) {
123+
return entry.getBlob();
124+
})
125+
.then(function(blob) {
126+
var currentCommitContents = test.currentCommitBlob.toString();
127+
var previousCommitContents = test.previousCommitBlob.toString();
128+
var resetContents = blob.toString();
129+
130+
// With a soft reset all of the changes should be in the index
131+
// still so the index should still == what we had at the current
132+
// commit and not the one we reset to
133+
assert(resetContents == currentCommitContents);
134+
assert(resetContents != previousCommitContents);
135+
136+
return Reset.reset(test.repo, test.currentCommit, Reset.TYPE.HARD);
137+
});
138+
});
139+
140+
it("can perform a mixed reset", function() {
141+
var test = this;
142+
143+
return Reset.reset(test.repo, test.previousCommit, Reset.TYPE.MIXED)
144+
.then(function() {
145+
return test.repo.openIndex();
146+
})
147+
.then(function(index) {
148+
return index.writeTree();
149+
})
150+
.then(function(oid) {
151+
return test.repo.getTree(oid);
152+
})
153+
.then(function(tree) {
154+
return tree.getEntry(filePath);
155+
})
156+
.then(function(entry) {
157+
return entry.getBlob();
158+
})
159+
.then(function(blob) {
160+
var currentCommitContents = test.currentCommitBlob.toString();
161+
var previousCommitContents = test.previousCommitBlob.toString();
162+
var resetContents = blob.toString();
163+
164+
// With a mixed reset all of the changes should removed from the index
165+
// but still in the working directory. (i.e. unstaged)
166+
assert(resetContents != currentCommitContents);
167+
assert(resetContents == previousCommitContents);
168+
169+
return fse.readFile(path.join(test.repo.workdir(), filePath));
170+
})
171+
.then(function(fileContents) {
172+
var currentCommitContents = test.currentCommitBlob.toString();
173+
174+
assert(fileContents == currentCommitContents);
175+
176+
return Reset.reset(test.repo, test.currentCommit, Reset.TYPE.HARD);
177+
});
178+
});
179+
180+
it("can perform a hard reset", function() {
181+
var test = this;
182+
183+
return Reset.reset(test.repo, test.previousCommit, Reset.TYPE.HARD)
184+
.then(function() {
185+
return test.repo.openIndex();
186+
})
187+
.then(function(index) {
188+
return index.writeTree();
189+
})
190+
.then(function(oid) {
191+
return test.repo.getTree(oid);
192+
})
193+
.then(function(tree) {
194+
return tree.getEntry(filePath);
195+
})
196+
.then(function(entry) {
197+
return entry.getBlob();
198+
})
199+
.then(function(blob) {
200+
var currentCommitContents = test.currentCommitBlob.toString();
201+
var previousCommitContents = test.previousCommitBlob.toString();
202+
var resetContents = blob.toString();
203+
204+
// With a hard reset all of the changes should removed from the index
205+
// and also removed from the working directory
206+
assert(resetContents != currentCommitContents);
207+
assert(resetContents == previousCommitContents);
208+
209+
return fse.readFile(path.join(test.repo.workdir(), filePath));
210+
})
211+
.then(function(fileContents) {
212+
var previousCommitContents = test.previousCommitBlob.toString();
213+
214+
assert(fileContents == previousCommitContents);
215+
216+
return Reset.reset(test.repo, test.currentCommit, Reset.TYPE.HARD);
75217
});
76218
});
77219
});

0 commit comments

Comments
 (0)