Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 71 additions & 37 deletions lib/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ Commit.prototype.amend = function (
* @param {String} messageEncoding
* @param {String} message
* @param {Tree|Oid} tree
* @param {String} signatureField
* @param {Function} onSignature
* @param {Function} onSignature Callback to be called with string to be signed
* @return {Oid}
*/
Commit.prototype.amendWithSignature = function(
Expand All @@ -71,13 +70,12 @@ Commit.prototype.amendWithSignature = function(
messageEncoding,
message,
tree,
signatureField,
onSignature
) {
var repo = this.repo;
var parentOids = this.parents();
var _this = this;
var promises = [];
let repo = this.repo;
let parentOids = this.parents();
let _this = this;
let promises = [];

if (tree instanceof NodeGit.Oid) {
promises.push(repo.getTree(tree));
Expand All @@ -89,22 +87,27 @@ Commit.prototype.amendWithSignature = function(
promises.push(repo.getCommit(parentOid));
});

var treeObject;
var parents;
var commitContent;
var commitOid;
var commit;

var createCommitPromise = Promise.all(promises)
let treeObject;
let parents;
let commitContent;
let commit;
let skippedSigning;
let resolvedAuthor;
let resolvedCommitter;
let resolvedMessageEncoding;
let resolvedMessage;
let resolvedTree;

let createCommitPromise = Promise.all(promises)
.then(function(results) {
treeObject = fp.head(results);
parents = fp.tail(results);
return _this.getTree();
})
.then(function(commitTreeResult) {
var commitTree = commitTreeResult;
let commitTree = commitTreeResult;

var truthyArgs = fp.omitBy(
let truthyArgs = fp.omitBy(
fp.isNil,
{
author,
Expand All @@ -115,15 +118,15 @@ Commit.prototype.amendWithSignature = function(
}
);

var commitFields = {
let commitFields = {
author: _this.author(),
committer: _this.committer(),
messageEncoding: _this.messageEncoding(),
message: _this.message(),
tree: commitTree
};

var {
({
author: resolvedAuthor,
committer: resolvedCommitter,
messageEncoding: resolvedMessageEncoding,
Expand All @@ -132,7 +135,7 @@ Commit.prototype.amendWithSignature = function(
} = fp.assign(
commitFields,
truthyArgs
);
));

return Commit.createBuffer(
repo,
Expand All @@ -152,30 +155,61 @@ Commit.prototype.amendWithSignature = function(
}
return onSignature(commitContent);
})
.then(function(signature) {
return Commit.createWithSignature(
repo,
commitContent,
signature,
signatureField
);
.then(function({ code, field, signedData }) {
switch (code) {
case NodeGit.Error.CODE.OK:
return Commit.createWithSignature(
repo,
commitContent,
signedData,
field
);
case NodeGit.Error.CODE.PASSTHROUGH:
skippedSigning = true;
return Commit.create(
repo,
updateRef,
resolvedAuthor,
resolvedCommitter,
resolvedMessageEncoding,
resolvedMessage,
resolvedTree,
parents.length,
parents
);
default: {
const error = new Error(
`Commit.amendWithSignature threw with error code ${code}`
);
error.errno = code;
throw error;
}
}
});

if (!updateRef) {
return createCommitPromise;
}

return createCommitPromise.then(function(commitOidResult) {
commitOid = commitOidResult;
return repo.getCommit(commitOid);
}).then(function(commitResult) {
commit = commitResult;
return repo.getReference(updateRef);
}).then(function(ref) {
return ref.setTarget(commitOid, `commit (amend): ${commit.summary()}`);
}).then(function() {
return commitOid;
});
return createCommitPromise
.then(function(commitOid) {
if (skippedSigning) {
return commitOid;
}

return repo.getCommit(commitOid)
.then(function(commitResult) {
commit = commitResult;
return repo.getReference(updateRef);
}).then(function(ref) {
return ref.setTarget(
commitOid,
`commit (amend): ${commit.summary()}`
);
}).then(function() {
return commitOid;
});
});
};

/**
Expand Down
25 changes: 23 additions & 2 deletions lib/rebase.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var _abort = Rebase.prototype.abort;
var _commit = Rebase.prototype.commit;

function defaultRebaseOptions(options, checkoutStrategy) {
var checkoutOptions;
var mergeOptions;
let checkoutOptions;
let mergeOptions;

if (options) {
options = shallowClone(options);
Expand All @@ -19,6 +19,27 @@ function defaultRebaseOptions(options, checkoutStrategy) {
delete options.checkoutOptions;
delete options.mergeOptions;

if (options.signingCb) {
let signingCb = options.signingCb;
options.signingCb = function (
signatureBuf,
signatureFieldBuf,
commitContent
) {
return Promise.resolve(signingCb(commitContent))
.then(function({ code, field, signedData }) {
if (code === NodeGit.Error.CODE.OK) {
signatureBuf.setString(signedData);
if (field) {
signatureFieldBuf.setString(field);
}
}

return code;
});
};
}

options = normalizeOptions(options, NodeGit.RebaseOptions);
} else {
options = normalizeOptions({}, NodeGit.RebaseOptions);
Expand Down
95 changes: 62 additions & 33 deletions lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -658,25 +658,24 @@ Repository.prototype.createCommitBuffer = function(
* @param {String} message
* @param {Tree|Oid|String} Tree
* @param {Array} parents
* @param {String} signature_field typically "gpgsig"
* @param {Function} onSignature Callback to be called with string to be signed
* @return {Oid} The oid of the commit
*/
Repository.prototype.createCommitWithSignature = function(
updateRef,
author,
committer,
message,
tree,
parents,
signature_field,
onSignature) {
updateRef,
author,
committer,
message,
tree,
parents,
onSignature
) {

var repo = this;
var promises = [];
var commit_content;
var commit_oid;
var commitContent;
var commit;
var skippedSigning;

parents = parents || [];

Expand Down Expand Up @@ -707,35 +706,65 @@ Repository.prototype.createCommitWithSignature = function(
parents.length,
parents
);
}).then(function(commit_contentResult) {
commit_content = commit_contentResult;
if (!commit_content.endsWith("\n")) {
commit_content += "\n";
}).then(function(commitContentResult) {
commitContent = commitContentResult;
if (!commitContent.endsWith("\n")) {
commitContent += "\n";
}
return onSignature(commitContent);
}).then(function({ code, field, signedData }) {
switch (code) {
case NodeGit.Error.CODE.OK:
return Commit.createWithSignature(
repo,
commitContent,
signedData,
field
);
case NodeGit.Error.CODE.PASSTHROUGH:
skippedSigning = true;
return Commit.create(
repo,
updateRef,
author,
committer,
null /* use default message encoding */,
message,
tree,
parents.length,
parents
);
default: {
const error = new Error(
"Repository.prototype.createCommitWithSignature " +
`threw with error code ${code}`
);
error.errno = code;
throw error;
}
}
return onSignature(commit_content);
}).then(function(signature) {
return Commit.createWithSignature(
repo,
commit_content,
signature,
signature_field);
});

if (!updateRef) {
return createCommitPromise;
}

return createCommitPromise.then(function(commit_oidResult) {
commit_oid = commit_oidResult;
return repo.getCommit(commit_oid);
}).then(function(commitResult) {
commit = commitResult;
return repo.getReference(updateRef);
}).then(function(ref) {
return ref.setTarget(commit_oid, getReflogMessageForCommit(commit));
}).then(function() {
return commit_oid;
});
return createCommitPromise
.then(function(commitOid) {
if (skippedSigning) {
return commitOid;
}

return repo.getCommit(commitOid)
.then(function(commitResult) {
commit = commitResult;
return repo.getReference(updateRef);
}).then(function(ref) {
return ref.setTarget(commitOid, getReflogMessageForCommit(commit));
}).then(function() {
return commitOid;
});
});
};

/**
Expand Down
30 changes: 25 additions & 5 deletions lib/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,30 @@ Tag.createWithSignature = function(
tagBuffer = tagBufferResult;
return signingCallback(tagBuffer);
})
.then((tagSignature) => {
const normalizedEnding = tagSignature.endsWith("\n") ? "" : "\n";
const signedTagString = tagBuffer + tagSignature + normalizedEnding;
return Tag.createFromBuffer(repo, signedTagString, force);
.then(({ code, signedData }) => {
switch (code) {
case NodeGit.Error.CODE.OK: {
const normalizedEnding = signedData.endsWith("\n") ? "" : "\n";
const signedTagString = tagBuffer + signedData + normalizedEnding;
return Tag.createFromBuffer(repo, signedTagString, force);
}
case NodeGit.Error.CODE.PASSTHROUGH:
return Tag.create(
repo,
tagName,
target,
tagger,
message,
force
);
default: {
const error = new Error(
`Tag.createWithSignature threw with error code ${code}`
);
error.errno = code;
throw error;
}
}
});
};

Expand Down Expand Up @@ -110,6 +130,6 @@ Tag.prototype.extractSignature = function(signatureType = "gpgsig") {
}
}

return null;
throw new Error("this tag is not signed");
});
};
Loading