Skip to content

Commit a381c0b

Browse files
Merge pull request nodegit#1945 from julianmesa-gitkraken/fix-tag-issues
Fix tag createWithSignature function
2 parents e2ed24d + bb33c59 commit a381c0b

File tree

2 files changed

+119
-63
lines changed

2 files changed

+119
-63
lines changed

lib/tag.js

+40-34
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
var util = require("util");
12
var NodeGit = require("../");
23
var LookupWrapper = NodeGit.Utils.lookupWrapper;
34
var Tag = NodeGit.Tag;
@@ -49,58 +50,63 @@ Tag.createBuffer = function(repo, tagName, target, tagger, message) {
4950
});
5051
};
5152

53+
const deprecatedCreateWithSignatureHelper = util.deprecate(function(repo, oidTarget) {
54+
return repo.getCommit(oidTarget);
55+
}, "Tag.createWithSignature target should be a Git Object, not Oid");
56+
5257
/**
5358
* @async
5459
* @param {Repository} repo
5560
* @param {String} tagName
56-
* @param {Oid} target
61+
* @param {Object} target
5762
* @param {Signature} tagger
5863
* @param {String} message
5964
* @param {Number} force
6065
* @param {Function} signingCallback Takes a string and returns a string
6166
* representing the signed message
6267
* @return {Oid}
6368
*/
64-
Tag.createWithSignature = function(
69+
Tag.createWithSignature = async (
6570
repo,
6671
tagName,
6772
target,
6873
tagger,
6974
message,
7075
force,
7176
signingCallback
72-
) {
73-
let tagBuffer;
74-
return Tag.createBuffer(repo, tagName, target, tagger, message)
75-
.then((tagBufferResult) => {
76-
tagBuffer = tagBufferResult;
77-
return signingCallback(tagBuffer);
78-
})
79-
.then(({ code, signedData }) => {
80-
switch (code) {
81-
case NodeGit.Error.CODE.OK: {
82-
const normalizedEnding = signedData.endsWith("\n") ? "" : "\n";
83-
const signedTagString = tagBuffer + signedData + normalizedEnding;
84-
return Tag.createFromBuffer(repo, signedTagString, force);
85-
}
86-
case NodeGit.Error.CODE.PASSTHROUGH:
87-
return Tag.create(
88-
repo,
89-
tagName,
90-
target,
91-
tagger,
92-
message,
93-
force
94-
);
95-
default: {
96-
const error = new Error(
97-
`Tag.createWithSignature threw with error code ${code}`
98-
);
99-
error.errno = code;
100-
throw error;
101-
}
102-
}
103-
});
77+
) => {
78+
let targetOid;
79+
if (!target.id) {
80+
targetOid = await deprecatedCreateWithSignatureHelper(repo, target);
81+
} else {
82+
targetOid = target;
83+
}
84+
85+
const tagBuffer = await Tag.createBuffer(repo, tagName, targetOid.id(), tagger, message);
86+
const { code, signedData } = await signingCallback(tagBuffer);
87+
switch (code) {
88+
case NodeGit.Error.CODE.OK: {
89+
const normalizedEnding = signedData.endsWith("\n") ? "" : "\n";
90+
const signedTagString = tagBuffer + signedData + normalizedEnding;
91+
return Tag.createFromBuffer(repo, signedTagString, force);
92+
}
93+
case NodeGit.Error.CODE.PASSTHROUGH:
94+
return Tag.create(
95+
repo,
96+
tagName,
97+
targetOid,
98+
tagger,
99+
message,
100+
force
101+
);
102+
default: {
103+
const error = new Error(
104+
`Tag.createWithSignature threw with error code ${code}`
105+
);
106+
error.errno = code;
107+
throw error;
108+
}
109+
}
104110
};
105111

106112
/**

test/tests/tag.js

+79-29
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ describe("Tag", function() {
322322
it(
323323
"can create a tag with a signature and extract the signature",
324324
function() {
325-
const targetOid = Oid.fromString(commitPointedTo);
326-
const otherTargetOid = Oid.fromString(commitPointedTo2);
325+
var targetCommit;
326+
var otherTargetCommit;
327327
const name = "created-signed-tag-annotationCreate";
328328
const repository = this.repository;
329329
const signature = Signature.create(
@@ -359,14 +359,19 @@ describe("Tag", function() {
359359
let oid;
360360
let object;
361361

362-
return repository.odb()
363-
.then((odbResult) => {
362+
return repository.getCommit(commitPointedTo).then((commit) => {
363+
targetCommit = commit;
364+
return repository.getCommit(commitPointedTo2);
365+
}).then((commit) => {
366+
otherTargetCommit = commit;
367+
return repository.odb();
368+
}).then((odbResult) => {
364369
odb = odbResult;
365370

366371
return Tag.createWithSignature(
367372
repository,
368373
name,
369-
targetOid,
374+
targetCommit,
370375
signature,
371376
message,
372377
1,
@@ -409,7 +414,7 @@ describe("Tag", function() {
409414
return Tag.createWithSignature(
410415
repository,
411416
name,
412-
targetOid,
417+
targetCommit,
413418
signature,
414419
message,
415420
1,
@@ -421,7 +426,7 @@ describe("Tag", function() {
421426
return Tag.createWithSignature(
422427
repository,
423428
name,
424-
otherTargetOid,
429+
otherTargetCommit,
425430
signature,
426431
message,
427432
0,
@@ -442,8 +447,8 @@ describe("Tag", function() {
442447
);
443448

444449
it("can optionally skip the signing process", function() {
445-
const targetOid = Oid.fromString(commitPointedTo);
446-
const otherTargetOid = Oid.fromString(commitPointedTo2);
450+
var targetCommit;
451+
var otherTargetCommit;
447452
const name = "created-signed-tag-annotationCreate";
448453
const repository = this.repository;
449454
const signature = Signature.create(
@@ -461,14 +466,19 @@ describe("Tag", function() {
461466
let oid;
462467
let object;
463468

464-
return repository.odb()
465-
.then((odbResult) => {
469+
return repository.getCommit(commitPointedTo).then((commit) => {
470+
targetCommit = commit;
471+
return repository.getCommit(commitPointedTo2);
472+
}).then((commit) => {
473+
otherTargetCommit = commit;
474+
return repository.odb();
475+
}).then((odbResult) => {
466476
odb = odbResult;
467477

468478
return Tag.createWithSignature(
469479
repository,
470480
name,
471-
targetOid,
481+
targetCommit,
472482
signature,
473483
message,
474484
1,
@@ -514,7 +524,7 @@ describe("Tag", function() {
514524
return Tag.createWithSignature(
515525
repository,
516526
name,
517-
targetOid,
527+
targetCommit,
518528
signature,
519529
message,
520530
1,
@@ -526,7 +536,7 @@ describe("Tag", function() {
526536
return Tag.createWithSignature(
527537
repository,
528538
name,
529-
otherTargetOid,
539+
otherTargetCommit,
530540
signature,
531541
message,
532542
0,
@@ -544,7 +554,7 @@ describe("Tag", function() {
544554
});
545555

546556
it("will throw if signing callback returns an error code", function() {
547-
const targetOid = Oid.fromString(commitPointedTo);
557+
var targetCommit;
548558
const name = "created-signed-tag-annotationCreate";
549559
const repository = this.repository;
550560
const signature = Signature.create(
@@ -559,16 +569,18 @@ describe("Tag", function() {
559569
});
560570

561571

562-
return Tag.createWithSignature(
563-
repository,
564-
name,
565-
targetOid,
566-
signature,
567-
message,
568-
1,
569-
signingCallback
570-
)
571-
.then(function() {
572+
return repository.getCommit(commitPointedTo).then((commit) => {
573+
targetCommit = commit;
574+
return Tag.createWithSignature(
575+
repository,
576+
name,
577+
targetCommit,
578+
signature,
579+
message,
580+
1,
581+
signingCallback
582+
);
583+
}).then(function() {
572584
assert.fail("Should not have been able to create tag");
573585
}, function(error) {
574586
if (error && error.errno === NodeGit.Error.CODE.ERROR) {
@@ -579,16 +591,54 @@ describe("Tag", function() {
579591
});
580592
});
581593

594+
it("will show a deprecation warning if createWithSignature use oid instead object", function() {
595+
var targetCommit;
596+
const name = "created-signed-tag-annotationCreate";
597+
const repository = this.repository;
598+
const signature = Signature.create(
599+
"Shaggy Rogers",
600+
601+
987654321,
602+
90
603+
);
604+
const message = "I'm a teapot";
605+
const signingCallback = () => ({
606+
code: NodeGit.Error.CODE.ERROR
607+
});
608+
609+
610+
return repository.getCommit(commitPointedTo).then((commit) => {
611+
targetCommit = commit;
612+
return Tag.createWithSignature(
613+
repository,
614+
name,
615+
targetCommit.id(),
616+
signature,
617+
message,
618+
1,
619+
signingCallback
620+
);
621+
}).then(function() {
622+
assert.fail("Should not have been able to create tag");
623+
}, function(error) {
624+
if (error && error.errno === NodeGit.Error.CODE.ERROR) {
625+
return;
626+
}
627+
throw error;
628+
});
629+
});
582630

583631
it("can create a new signed tag with Tag.annotationCreate", function() {
584-
var oid = Oid.fromString(commitPointedTo);
632+
var targetCommit;
585633
var name = "created-signed-tag-annotationCreate";
586634
var repository = this.repository;
587635
var signature = null;
588636
var odb = null;
589637

590-
return Signature.default(repository)
591-
.then(function(signatureResult) {
638+
return repository.getCommit(commitPointedTo).then((commit) => {
639+
targetCommit = commit;
640+
return Signature.default(repository);
641+
}).then(function(signatureResult) {
592642
signature = signatureResult;
593643
return repository.odb();
594644
})
@@ -597,7 +647,7 @@ describe("Tag", function() {
597647
})
598648
.then(function() {
599649
return Tag.annotationCreate(
600-
repository, name, oid, signature, tagMessage);
650+
repository, name, targetCommit, signature, tagMessage);
601651
})
602652
.then(function(oid) {
603653
return odb.read(oid);

0 commit comments

Comments
 (0)