From e8f8141ba85911b412b165bcadb4755848405103 Mon Sep 17 00:00:00 2001 From: Remy Suen Date: Tue, 28 Nov 2017 13:14:56 +0900 Subject: [PATCH] Generate options if unspecified in Revert.revert Due to the way the C++ binding layer gets the promise callback passed to it, if there are no options specified in Revert.revert, the promise callback ends up becoming the third parameter to the function and the expected fourth parameter will not be satisfied which will lead to an exception. We should generate a default options for Revert.revert if nothing is provided by the callee to make the API easier and simpler to use. Signed-off-by: Remy Suen --- lib/revert.js | 39 +++++++++++++++++++++++++++++++++++++++ test/tests/revert.js | 9 ++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lib/revert.js b/lib/revert.js index aee3f0baf..2dddb2cee 100644 --- a/lib/revert.js +++ b/lib/revert.js @@ -1,8 +1,10 @@ var NodeGit = require("../"); +var shallowClone = NodeGit.Utils.shallowClone; var normalizeOptions = NodeGit.Utils.normalizeOptions; var Revert = NodeGit.Revert; var _commit = Revert.commit; +var _revert = Revert.revert; /** * Reverts the given commit against the given "our" commit, producing an index @@ -44,3 +46,40 @@ Revert.commit = function( return result; }, callback); }; + +/** + * Reverts the given commit, producing changes in the index and + * working directory. + * + * @async + * @param {Repository} repo the repository to perform the revert in + * @param {Commit} commit the commit to revert + * @param {RevertOptions} revert_options the revert options + * (or null for defaults) + */ +Revert.revert = function(repo, commit, revertOpts) { + var mergeOpts; + var checkoutOpts; + + if (revertOpts) { + revertOpts = shallowClone(revertOpts); + mergeOpts = revertOpts.mergeOpts; + checkoutOpts = revertOpts.checkoutOpts; + delete revertOpts.mergeOpts; + delete revertOpts.checkoutOpts; + } + + revertOpts = normalizeOptions(revertOpts, NodeGit.RevertOptions); + + if (revertOpts) { + revertOpts.mergeOpts = + normalizeOptions(mergeOpts, NodeGit.MergeOptions); + } + + if (checkoutOpts) { + revertOpts.checkoutOpts = + normalizeOptions(checkoutOpts, NodeGit.CheckoutOptions); + } + + return _revert.call(this, repo, commit, revertOpts); +}; diff --git a/test/tests/revert.js b/test/tests/revert.js index a16d8c126..8733c870f 100644 --- a/test/tests/revert.js +++ b/test/tests/revert.js @@ -61,7 +61,14 @@ describe("Revert", function() { }); }); - it("RevertOptions is optional", function() { + it("RevertOptions is optional (unspecified)", function() { + return Revert.revert(test.repository, test.firstCommit) + .catch(function(error) { + throw error; + }); + }); + + it("RevertOptions is optional (null)", function() { return Revert.revert(test.repository, test.firstCommit, null) .catch(function(error) { throw error;