Skip to content

Commit 97ff2cd

Browse files
authored
Merge pull request #1214 from rcjsuen/optional-reset
Make `Reset.fromAnnotated` the same as `Reset.reset`
2 parents 58c9a68 + 55d9c8e commit 97ff2cd

File tree

3 files changed

+125
-38
lines changed

3 files changed

+125
-38
lines changed

generate/input/descriptor.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,6 +2109,17 @@
21092109
"return": {
21102110
"isErrorCode": true
21112111
}
2112+
},
2113+
"git_reset_from_annotated": {
2114+
"args": {
2115+
"checkout_opts": {
2116+
"isOptional": true
2117+
}
2118+
},
2119+
"isAsync": true,
2120+
"return": {
2121+
"isErrorCode": true
2122+
}
21122123
}
21132124
}
21142125
},

lib/reset.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var normalizeOptions = NodeGit.Utils.normalizeOptions;
44
var Reset = NodeGit.Reset;
55
var _default = Reset.default;
66
var _reset = Reset.reset;
7+
var _fromAnnotated = Reset.fromAnnotated;
78

89
/**
910
* Look up a refs's commit.
@@ -50,3 +51,25 @@ Reset.reset = function(repo, target, resetType, opts) {
5051

5152
return _reset.call(this, repo, target, resetType, opts);
5253
};
54+
55+
/**
56+
* Sets the current head to the specified commit oid and optionally
57+
* resets the index and working tree to match.
58+
*
59+
* This behaves like reset but takes an annotated commit, which lets
60+
* you specify which extended sha syntax string was specified by a
61+
* user, allowing for more exact reflog messages.
62+
*
63+
* See the documentation for reset.
64+
*
65+
* @async
66+
* @param {Repository} repo
67+
* @param {AnnotatedCommit} target
68+
* @param {Number} resetType
69+
* @param {CheckoutOptions} opts
70+
*/
71+
Reset.fromAnnotated = function(repo, target, resetType, opts) {
72+
opts = normalizeOptions(opts, NodeGit.CheckoutOptions);
73+
74+
return _fromAnnotated.call(this, repo, target, resetType, opts);
75+
};

test/tests/reset.js

Lines changed: 91 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe("Reset", function() {
88
var NodeGit = require("../../");
99
var Repository = NodeGit.Repository;
1010
var Reset = NodeGit.Reset;
11+
var AnnotatedCommit = NodeGit.AnnotatedCommit;
1112

1213
var reposPath = local("../repos/workdir");
1314
var currentCommitOid = "32789a79e71fbc9e04d3eff7425e1771eb595150";
@@ -104,33 +105,65 @@ describe("Reset", function() {
104105
});
105106
});
106107

107-
it("can perform a soft reset", function() {
108-
var test = this;
109-
110-
return Reset.reset(test.repo, test.previousCommit, Reset.TYPE.SOFT)
108+
function resetFrom(repo, commit, resetType, annotated) {
109+
var promise = null;
110+
if (annotated) {
111+
promise = AnnotatedCommit.lookup(repo, commit.id())
112+
.then(function(annotatedCommit) {
113+
return Reset.fromAnnotated(repo, annotatedCommit, resetType);
114+
});
115+
} else {
116+
promise = Reset.reset(repo, commit, resetType);
117+
}
118+
return promise
111119
.then(function() {
112-
return test.repo.refreshIndex();
120+
return repo.refreshIndex();
113121
})
114122
.then(function(index) {
115123
return index.writeTree();
116124
})
117125
.then(function(oid) {
118-
return test.repo.getTree(oid);
126+
return repo.getTree(oid);
119127
})
120128
.then(function(tree) {
121129
return tree.getEntry(filePath);
122130
})
123131
.then(function(entry) {
124132
return entry.getBlob();
125-
})
133+
});
134+
}
135+
136+
it("can perform a soft reset", function() {
137+
var test = this;
138+
139+
return resetFrom(test.repo, test.previousCommit, Reset.TYPE.SOFT, false)
140+
.then(function(blob) {
141+
var currentCommitContents = test.currentCommitBlob.toString();
142+
var previousCommitContents = test.previousCommitBlob.toString();
143+
var resetContents = blob.toString();
144+
145+
// With a soft reset all of the changes should be in the index
146+
// still so the index should still == what we had at the current
147+
// commit and not the one nwe reset to
148+
assert(resetContents == currentCommitContents);
149+
assert(resetContents != previousCommitContents);
150+
151+
return Reset(test.repo, test.currentCommit, Reset.TYPE.HARD);
152+
});
153+
});
154+
155+
it("can perform an annotated soft reset", function() {
156+
var test = this;
157+
158+
return resetFrom(test.repo, test.previousCommit, Reset.TYPE.SOFT, true)
126159
.then(function(blob) {
127160
var currentCommitContents = test.currentCommitBlob.toString();
128161
var previousCommitContents = test.previousCommitBlob.toString();
129162
var resetContents = blob.toString();
130163

131164
// With a soft reset all of the changes should be in the index
132165
// still so the index should still == what we had at the current
133-
// commit and not the one we reset to
166+
// commit and not the one nwe reset to
134167
assert(resetContents == currentCommitContents);
135168
assert(resetContents != previousCommitContents);
136169

@@ -141,22 +174,32 @@ describe("Reset", function() {
141174
it("can perform a mixed reset", function() {
142175
var test = this;
143176

144-
return Reset.reset(test.repo, test.previousCommit, Reset.TYPE.MIXED)
145-
.then(function() {
146-
return test.repo.refreshIndex();
147-
})
148-
.then(function(index) {
149-
return index.writeTree();
150-
})
151-
.then(function(oid) {
152-
return test.repo.getTree(oid);
153-
})
154-
.then(function(tree) {
155-
return tree.getEntry(filePath);
156-
})
157-
.then(function(entry) {
158-
return entry.getBlob();
177+
return resetFrom(test.repo, test.previousCommit, Reset.TYPE.MIXED, false)
178+
.then(function(blob) {
179+
var currentCommitContents = test.currentCommitBlob.toString();
180+
var previousCommitContents = test.previousCommitBlob.toString();
181+
var resetContents = blob.toString();
182+
183+
// With a mixed reset all of the changes should removed from the index
184+
// but still in the working directory. (i.e. unstaged)
185+
assert(resetContents != currentCommitContents);
186+
assert(resetContents == previousCommitContents);
187+
188+
return fse.readFile(path.join(test.repo.workdir(), filePath));
159189
})
190+
.then(function(fileContents) {
191+
var currentCommitContents = test.currentCommitBlob.toString();
192+
193+
assert(fileContents == currentCommitContents);
194+
195+
return Reset.reset(test.repo, test.currentCommit, Reset.TYPE.HARD);
196+
});
197+
});
198+
199+
it("can perform an annotated mixed reset", function() {
200+
var test = this;
201+
202+
return resetFrom(test.repo, test.previousCommit, Reset.TYPE.MIXED, true)
160203
.then(function(blob) {
161204
var currentCommitContents = test.currentCommitBlob.toString();
162205
var previousCommitContents = test.previousCommitBlob.toString();
@@ -181,22 +224,32 @@ describe("Reset", function() {
181224
it("can perform a hard reset", function() {
182225
var test = this;
183226

184-
return Reset.reset(test.repo, test.previousCommit, Reset.TYPE.HARD)
185-
.then(function() {
186-
return test.repo.refreshIndex();
187-
})
188-
.then(function(index) {
189-
return index.writeTree();
190-
})
191-
.then(function(oid) {
192-
return test.repo.getTree(oid);
193-
})
194-
.then(function(tree) {
195-
return tree.getEntry(filePath);
196-
})
197-
.then(function(entry) {
198-
return entry.getBlob();
227+
return resetFrom(test.repo, test.previousCommit, Reset.TYPE.HARD, false)
228+
.then(function(blob) {
229+
var currentCommitContents = test.currentCommitBlob.toString();
230+
var previousCommitContents = test.previousCommitBlob.toString();
231+
var resetContents = blob.toString();
232+
233+
// With a hard reset all of the changes should removed from the index
234+
// and also removed from the working directory
235+
assert(resetContents != currentCommitContents);
236+
assert(resetContents == previousCommitContents);
237+
238+
return fse.readFile(path.join(test.repo.workdir(), filePath));
199239
})
240+
.then(function(fileContents) {
241+
var previousCommitContents = test.previousCommitBlob.toString();
242+
243+
assert(fileContents == previousCommitContents);
244+
245+
return Reset.reset(test.repo, test.currentCommit, Reset.TYPE.HARD);
246+
});
247+
});
248+
249+
it("can perform an annotated hard reset", function() {
250+
var test = this;
251+
252+
return resetFrom(test.repo, test.previousCommit, Reset.TYPE.HARD, true)
200253
.then(function(blob) {
201254
var currentCommitContents = test.currentCommitBlob.toString();
202255
var previousCommitContents = test.previousCommitBlob.toString();

0 commit comments

Comments
 (0)