Skip to content

Commit 7fa34f7

Browse files
committed
Added test for commit amend
-Added convenience method to commit.amend -Made some amend arguments optional
1 parent 37db75e commit 7fa34f7

File tree

4 files changed

+179
-25
lines changed

4 files changed

+179
-25
lines changed

generate/input/descriptor.json

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -325,25 +325,27 @@
325325
},
326326
"commit": {
327327
"functions": {
328-
"git_commit_ammend": {
328+
"git_commit_amend": {
329329
"args": {
330+
"author": {
331+
"isOptional": true
332+
},
333+
"committer": {
334+
"isOptional": true
335+
},
330336
"id": {
331337
"isReturn": true
332338
},
333339
"message_encoding": {
334340
"isOptional": true
335341
},
336-
"parents": {
337-
"cType": "const git_commit **",
338-
"cppClassName": "Array",
339-
"jsClassName": "Array",
340-
"arrayElementCppClassName": "GitCommit"
342+
"message": {
343+
"isOptional": true
341344
},
342-
"update_ref": {
345+
"tree": {
343346
"isOptional": true
344347
}
345-
},
346-
"isAsync": true
348+
}
347349
},
348350
"git_commit_create": {
349351
"args": {

lib/commit.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,48 @@ Commit.prototype.getDiff = function(callback) {
199199
}, callback);
200200
};
201201

202+
/**
203+
* Amend a commit
204+
* @async
205+
* @param {String} update_ref
206+
* @param {Signature} author
207+
* @param {Signature} committer
208+
* @param {String} message_encoding
209+
* @param {String} message
210+
* @param {Tree|Oid} tree
211+
* @param {Oid} callback
212+
*/
213+
var amend = Commit.prototype.amend;
214+
Commit.prototype.amend = function (
215+
updateRef, author, committer, message_encoding, message, tree, callback) {
216+
var repo = this.repo;
217+
var treeObject = tree;
218+
var _this = this;
219+
220+
if (tree instanceof NodeGit.Oid){
221+
return repo.getTree(tree).then(function(result){
222+
treeObject = result;
223+
return amend.call(_this,
224+
updateRef,
225+
author,
226+
committer,
227+
message_encoding,
228+
message,
229+
treeObject
230+
);
231+
});
232+
} else {
233+
return new Promise(amend.call(_this,
234+
updateRef,
235+
author,
236+
committer,
237+
message_encoding,
238+
message,
239+
treeObject
240+
));
241+
}
242+
};
243+
202244
/**
203245
* The sha of this commit
204246
* @return {String}

lib/repository.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -413,22 +413,6 @@ Repository.prototype.getHeadCommit = function(callback) {
413413
return repo.getCommit(head, callback);
414414
});
415415
};
416-
/**
417-
* Create a commit
418-
*
419-
* @async
420-
* @param {Commit} commit
421-
* @param {String} updateRef
422-
* @param {Signature} author
423-
* @param {Signature} committer
424-
* @param {String} message
425-
* @param {Tree|Oid|String} Tree
426-
* @return {Oid} The oid of the commit
427-
*/
428-
Repository.prototype.amendCommit = function (
429-
commit, updateRef, author, committer, message, tree, callback) {
430-
431-
};
432416
/**
433417
* Create a commit
434418
*

test/tests/commit.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,132 @@ describe("Commit", function() {
137137
});
138138

139139

140+
it.only("can amend commit", function(){
141+
var commitToAmendId = "315e77328ef596f3bc065d8ac6dd2c72c09de8a5";
142+
var amendedCommitId = "98835fb6903436c298a603f263ed698b03106c05";
143+
var fileName = "newfile.txt";
144+
var fileContent = "hello world";
145+
var newFileName = "newerfile.txt";
146+
var newFileContent = "goodbye world";
147+
var messageEncoding = "US-ASCII";
148+
var message = "KYLE + KEN = BFF INDEFINITELY";
149+
150+
var repo;
151+
var index;
152+
var treeOid;
153+
var parent;
154+
var author;
155+
var committer;
156+
157+
return NodeGit.Repository.open(reposPath)
158+
.then(function(repoResult) {
159+
repo = repoResult;
160+
return fse.writeFile(path.join(repo.workdir(), fileName), fileContent);
161+
})
162+
.then(function() {
163+
return repo.openIndex();
164+
})
165+
.then(function(indexResult) {
166+
index = indexResult;
167+
return index.read(1);
168+
})
169+
.then(function() {
170+
return index.addByPath(fileName);
171+
})
172+
.then(function() {
173+
return index.write();
174+
})
175+
.then(function() {
176+
return index.writeTree();
177+
})
178+
.then(function(oidResult) {
179+
console.log("Commit to amend tree id: " + oidResult);
180+
treeOid = oidResult;
181+
return NodeGit.Reference.nameToId(repo, "HEAD");
182+
})
183+
.then(function(head) {
184+
return repo.getCommit(head);
185+
})
186+
.then(function(parentResult) {
187+
parent = parentResult;
188+
return Promise.all([
189+
NodeGit.Signature.create("Foo Bar", "foo@bar.com", 123456789, 60),
190+
NodeGit.Signature.create("Foo A Bar", "foo@bar.com", 987654321, 90)
191+
]);
192+
})
193+
.then(function(signatures) {
194+
var author = signatures[0];
195+
var committer = signatures[1];
196+
197+
return repo.createCommit(
198+
"HEAD",
199+
author,
200+
committer,
201+
"message",
202+
treeOid,
203+
[parent]);
204+
})
205+
.then(function() {
206+
return fse.writeFile(
207+
path.join(repo.workdir(), newFileName),
208+
newFileContent
209+
);
210+
})
211+
.then(function() {
212+
return repo.openIndex();
213+
})
214+
.then(function(indexResult) {
215+
index = indexResult;
216+
return index.read(1);
217+
})
218+
.then(function() {
219+
return index.addByPath(newFileName);
220+
})
221+
.then(function() {
222+
return index.write();
223+
})
224+
.then(function() {
225+
return index.writeTree();
226+
})
227+
.then(function(resultOid){
228+
treeOid = resultOid;
229+
console.log("New Tree oid before amend: " + treeOid);
230+
return Promise.all([
231+
repo.getCommit(commitToAmendId),
232+
NodeGit.Signature.create(
233+
"New Foo Bar",
234+
"fizz@buzz.com",
235+
246802468,
236+
12
237+
),
238+
NodeGit.Signature.create(
239+
"New Foo A Bar",
240+
"fizz@buzz.com",
241+
4807891730,
242+
32
243+
),
244+
repo.getTree(resultOid)
245+
]);
246+
})
247+
.then(function(amendInfo){
248+
var commit = amendInfo[0];
249+
author = amendInfo[1];
250+
committer = amendInfo[2];
251+
//var tree = amendInfo[3];
252+
253+
return commit.amend(
254+
"HEAD",
255+
author,
256+
committer,
257+
messageEncoding,
258+
message,
259+
treeOid
260+
);
261+
})
262+
.then(function(newCommitId){
263+
assert.equal(newCommitId, amendedCommitId);
264+
});
265+
});
140266

141267
it("has an owner", function() {
142268
var owner = this.commit.owner();

0 commit comments

Comments
 (0)