From 0a3ab6820fd38e9e058cc75203de1d0de4574513 Mon Sep 17 00:00:00 2001 From: Julian Mesa Date: Wed, 9 Nov 2022 14:30:55 +0100 Subject: [PATCH] Fix commit_create_cb The definition was wrong parents should be an array of Commits not Oids. Also we are copying the Oid in the callback so we are not returning the Oid so we must not set selfFreeing to false. --- generate/input/callbacks.json | 2 +- generate/input/descriptor.json | 4 +- .../partials/configurable_callbacks.cc | 4 +- lib/repository.js | 90 +++++++++---------- 4 files changed, 48 insertions(+), 52 deletions(-) diff --git a/generate/input/callbacks.json b/generate/input/callbacks.json index aa0842995..7ef651349 100644 --- a/generate/input/callbacks.json +++ b/generate/input/callbacks.json @@ -168,7 +168,7 @@ }, { "name": "parents", - "cType": "const git_oid * []" + "cType": "const git_commit * []" }, { "name": "payload", diff --git a/generate/input/descriptor.json b/generate/input/descriptor.json index bf0efcd0a..dc60f33af 100644 --- a/generate/input/descriptor.json +++ b/generate/input/descriptor.json @@ -3142,10 +3142,10 @@ }, { "name": "parents", - "cType": "const git_oid **", + "cType": "const git_commit **", "cppClassName": "Array", "jsClassName": "Array", - "arrayElementCppClassName": "GitOid", + "arrayElementCppClassName": "GitCommit", "arrayLengthArgumentName": "parent_count" }, { diff --git a/generate/templates/partials/configurable_callbacks.cc b/generate/templates/partials/configurable_callbacks.cc index 79eef554b..528e9a059 100644 --- a/generate/templates/partials/configurable_callbacks.cc +++ b/generate/templates/partials/configurable_callbacks.cc @@ -145,11 +145,11 @@ else if (!result->IsNull() && !result->IsUndefined()) { {% if _return.isOutParam %} {{ _return.cppClassName }}* wrapper = Nan::ObjectWrap::Unwrap<{{ _return.cppClassName }}>(Nan::To(result).ToLocalChecked()); - wrapper->selfFreeing = false; {% if _return.cppClassName == "GitOid" %} git_oid_cpy(baton->{{ _return.name }}, wrapper->GetValue()); {% else %} + wrapper->selfFreeing = false; *baton->{{ _return.name }} = wrapper->GetValue(); {% endif %} baton->result = {{ field.return.success }}; @@ -185,11 +185,11 @@ else if (!result->IsNull() && !result->IsUndefined()) { {% if _return.isOutParam %} {{ _return.cppClassName }}* wrapper = Nan::ObjectWrap::Unwrap<{{ _return.cppClassName }}>(Nan::To(result).ToLocalChecked()); - wrapper->selfFreeing = false; {% if _return.cppClassName == "GitOid" %} git_oid_cpy(baton->{{ _return.name }}, wrapper->GetValue()); {% else %} + wrapper->selfFreeing = false; *baton->{{ _return.name }} = wrapper->GetValue(); {% endif %} baton->result = {{ field.return.success }}; diff --git a/lib/repository.js b/lib/repository.js index 00ce320c1..118f1648c 100644 --- a/lib/repository.js +++ b/lib/repository.js @@ -623,11 +623,11 @@ Repository.prototype.createCommitBuffer = function( * @param {Signature} committer * @param {String} message * @param {Tree|Oid|String} Tree - * @param {Array} parents + * @param {Array of Commits} parents * @param {Function} onSignature Callback to be called with string to be signed * @return {Oid} The oid of the commit */ -Repository.prototype.createCommitWithSignature = function( +Repository.prototype.createCommitWithSignature = async function( updateRef, author, committer, @@ -636,32 +636,33 @@ Repository.prototype.createCommitWithSignature = function( parents, onSignature ) { + let repo = this; + let newParents = []; + let skippedSigning = false; - var repo = this; - var promises = []; - var commitContent; - var skippedSigning; - - parents = parents || []; - - promises.push(repo.getTree(tree)); + if (!(tree instanceof Tree)) { + tree = await repo.getTree(tree); + } - parents.forEach(function(parent) { - promises.push(repo.getCommit(parent)); + let showDeprecationWarning = false; + parents.forEach(async (parent) => { + if (parent instanceof Commit) { + newParents.push(parent); + } else { + newParents.push(await repo.getCommit(parent)); + showDeprecationWarning = true; + } }); - const createCommitPromise = Promise.all(promises).then(function(results) { - tree = results[0]; - - // Get the normalized values for our input into the function - var parentsLength = parents.length; - parents = []; + if (showDeprecationWarning) { + console.warn("DeprecationWarning: Parents in Repository.createCommitWithSignature " + + "should be an array of Commits instead an array of Oid|String"); + } - for (var i = 0; i < parentsLength; i++) { - parents.push(results[i + 1]); - } + parents = newParents; - return Commit.createBuffer( + const createCommitPromise = async () => { + let commitContent = await Commit.createBuffer( repo, author, committer, @@ -671,13 +672,12 @@ Repository.prototype.createCommitWithSignature = function( parents.length, parents ); - }).then(function(commitContentResult) { - commitContent = commitContentResult; if (!commitContent.endsWith("\n")) { commitContent += "\n"; } - return onSignature(commitContent); - }).then(function({ code, field, signedData }) { + + const { code, field, signedData } = await onSignature(commitContent); + switch (code) { case NodeGit.Error.CODE.OK: return Commit.createWithSignature( @@ -708,32 +708,28 @@ Repository.prototype.createCommitWithSignature = function( throw error; } } - }); + }; if (!updateRef) { - return createCommitPromise; + return createCommitPromise(); } - return createCommitPromise - .then(function(commitOid) { - if (skippedSigning) { - return commitOid; - } + const commitOid = await createCommitPromise(); + if (skippedSigning) { + return commitOid; + } - return repo.getCommit(commitOid) - .then(function(commitResult) { - return Reference.updateTerminal( - repo, - updateRef, - commitOid, - getReflogMessageForCommit(commitResult), - committer - ); - }) - .then(function() { - return commitOid; - }); - }); + const commitResult = await repo.getCommit(commitOid); + + await Reference.updateTerminal( + repo, + updateRef, + commitOid, + getReflogMessageForCommit(commitResult), + committer + ); + + return commitOid; }; /**