forked from nodegit/nodegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtag.js
More file actions
135 lines (125 loc) · 3.5 KB
/
Copy pathtag.js
File metadata and controls
135 lines (125 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var NodeGit = require("../");
var LookupWrapper = NodeGit.Utils.lookupWrapper;
var Tag = NodeGit.Tag;
const signatureRegexesBySignatureType = {
gpgsig: [
/-----BEGIN PGP SIGNATURE-----[\s\S]+?-----END PGP SIGNATURE-----/gm,
/-----BEGIN PGP MESSAGE-----[\s\S]+?-----END PGP MESSAGE-----/gm,
],
x509: [
/-----BEGIN SIGNED MESSAGE-----[\s\S]+?-----END SIGNED MESSAGE-----/gm,
]
};
/**
* Retrieves the tag pointed to by the oid
* @async
* @param {Repository} repo The repo that the tag lives in
* @param {String|Oid|Tag} id The tag to lookup
* @return {Tag}
*/
Tag.lookup = LookupWrapper(Tag);
/**
* @async
* @param {Repository} repo
* @param {String} tagName
* @param {Oid} target
* @param {Signature} tagger
* @return {String}
*/
Tag.createBuffer = function(repo, tagName, target, tagger, message) {
return NodeGit.Object.lookup(repo, target, NodeGit.Object.TYPE.ANY)
.then((object) => {
if (!NodeGit.Object.typeisloose(object.type())) {
throw new Error("Object must be a loose type");
}
const id = object.id().toString();
const objectType = NodeGit.Object.type2String(object.type());
const lines = [
`object ${id}`,
`type ${objectType}`,
`tag ${tagName}`,
`tagger ${tagger.toString(true)}\n`,
`${message}${message.endsWith("\n") ? "" : "\n"}`
];
return lines.join("\n");
});
};
/**
* @async
* @param {Repository} repo
* @param {String} tagName
* @param {Oid} target
* @param {Signature} tagger
* @param {String} message
* @param {Number} force
* @param {Function} signingCallback Takes a string and returns a string
* representing the signed message
* @return {Oid}
*/
Tag.createWithSignature = function(
repo,
tagName,
target,
tagger,
message,
force,
signingCallback
) {
let tagBuffer;
return Tag.createBuffer(repo, tagName, target, tagger, message)
.then((tagBufferResult) => {
tagBuffer = tagBufferResult;
return signingCallback(tagBuffer);
})
.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;
}
}
});
};
/**
* Retrieves the signature of an annotated tag
* @async
* @param {String} signatureType
* @return {String|null}
*/
Tag.prototype.extractSignature = function(signatureType = "gpgsig") {
const id = this.id();
const repo = this.repo;
const signatureRegexes = signatureRegexesBySignatureType[signatureType];
if (!signatureRegexes) {
throw new Error("Unsupported signature type");
}
return repo.odb().then((odb) => {
return odb.read(id);
}).then((odbObject) => {
const odbData = odbObject.toString();
for (const regex of signatureRegexes) {
const matchResult = odbData.match(regex);
if (matchResult !== null) {
return matchResult[0];
}
}
throw new Error("this tag is not signed");
});
};