Skip to content

Commit debfe06

Browse files
[ci-app] remove git-repo-info dependency
* copy index.js from repository and adjust * add file to eslintignore
1 parent 5817d11 commit debfe06

File tree

5 files changed

+359
-3
lines changed

5 files changed

+359
-3
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ node_modules
66
protobuf
77
versions
88
acmeair-nodejs
9+
packages/dd-trace/src/plugins/util/git-repo-info.js

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
"@types/node": "^10.12.18",
5555
"axios": "^0.21.1",
5656
"form-data": "^3.0.0",
57-
"git-repo-info": "^2.1.1",
5857
"hdr-histogram-js": "^2.0.1",
5958
"koalas": "^1.0.2",
6059
"limiter": "^1.1.4",
Lines changed: 356 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,356 @@
1+
// From https://github.com/rwjblue/git-repo-info/blob/d3ab418ef8b392eabbe911a37871708b15201b70/index.js
2+
3+
'use strict';
4+
5+
var fs = require('fs');
6+
var path = require('path');
7+
var zlib = require('zlib');
8+
9+
var GIT_DIR = '.git';
10+
11+
function changeGitDir(newDirName) {
12+
GIT_DIR = newDirName;
13+
}
14+
15+
function findRepoHandleLinkedWorktree(gitPath) {
16+
var stat = fs.statSync(gitPath);
17+
var root = path.dirname(path.resolve(gitPath));
18+
if (stat.isDirectory()) {
19+
return {
20+
// for the base (non-linked) dir, there is no distinction between where we
21+
// find the HEAD file and where we find the rest of .git
22+
worktreeGitDir: gitPath,
23+
commonGitDir: gitPath,
24+
root: root,
25+
};
26+
} else {
27+
// We have a file that tells us where to find the worktree git dir. Once we
28+
// look there we'll know how to find the common git dir, depending on
29+
// whether it's a linked worktree git dir, or a submodule dir
30+
31+
var linkedGitDir = fs.readFileSync(gitPath).toString();
32+
var absolutePath=path.resolve(path.dirname(gitPath));
33+
var worktreeGitDirUnresolved = /gitdir: (.*)/.exec(linkedGitDir)[1];
34+
var worktreeGitDir = path.resolve(absolutePath,worktreeGitDirUnresolved);
35+
var commonDirPath = path.join(worktreeGitDir, 'commondir');
36+
if (fs.existsSync(commonDirPath)) {
37+
// this directory contains a `commondir` file; we're in a linked worktree
38+
39+
var commonDirRelative = fs.readFileSync(commonDirPath).toString().replace(/\r?\n$/, '');
40+
var commonDir = path.resolve(path.join(worktreeGitDir, commonDirRelative));
41+
42+
return {
43+
worktreeGitDir: worktreeGitDir,
44+
commonGitDir: commonDir,
45+
root: path.dirname(commonDir),
46+
};
47+
} else {
48+
// there is no `commondir` file; we're in a submodule
49+
return {
50+
worktreeGitDir: worktreeGitDir,
51+
commonGitDir: worktreeGitDir,
52+
root: root,
53+
};
54+
}
55+
}
56+
}
57+
58+
function findRepo(startingPath) {
59+
var gitPath, lastPath;
60+
var currentPath = startingPath;
61+
62+
if (!currentPath) { currentPath = process.cwd(); }
63+
64+
do {
65+
gitPath = path.join(currentPath, GIT_DIR);
66+
67+
if (fs.existsSync(gitPath)) {
68+
return findRepoHandleLinkedWorktree(gitPath);
69+
}
70+
71+
lastPath = currentPath;
72+
currentPath = path.resolve(currentPath, '..');
73+
} while (lastPath !== currentPath);
74+
75+
return null;
76+
}
77+
78+
function findPackedTags(gitPath, refPath) {
79+
return getPackedRefsForType(gitPath, refPath, 'tag');
80+
}
81+
82+
function findPackedCommit(gitPath, refPath) {
83+
return getPackedRefsForType(gitPath, refPath, 'commit')[0];
84+
}
85+
86+
function getPackedRefsForType(gitPath, refPath, type) {
87+
var packedRefsFile = getPackedRefsFile(gitPath);
88+
if (packedRefsFile) {
89+
return getLinesForRefPath(packedRefsFile, type, refPath).map(function(shaLine) {
90+
return getShaBasedOnType(type, shaLine);
91+
});
92+
}
93+
return [];
94+
}
95+
96+
function getPackedRefsFile(gitPath) {
97+
var packedRefsFilePath = path.join(gitPath, 'packed-refs');
98+
return fs.existsSync(packedRefsFilePath) ? fs.readFileSync(packedRefsFilePath, { encoding: 'utf8' }) : false;
99+
}
100+
101+
function getLinesForRefPath(packedRefsFile, type, refPath) {
102+
return packedRefsFile.split(/\r?\n/).reduce(function(acc, line, idx, arr) {
103+
var targetLine = line.indexOf('^') > -1 ? arr[idx-1] : line;
104+
return doesLineMatchRefPath(type, line, refPath) ? acc.concat(targetLine) : acc;
105+
}, []);
106+
}
107+
108+
function doesLineMatchRefPath(type, line, refPath) {
109+
var refPrefix = type === 'tag' ? 'refs/tags' : 'refs/heads';
110+
return (line.indexOf(refPrefix) > -1 || line.indexOf('^') > -1) && line.indexOf(refPath) > -1;
111+
}
112+
113+
function getShaBasedOnType(type, shaLine) {
114+
var shaResult = '';
115+
if (type === 'tag') {
116+
shaResult = shaLine.split('tags/')[1];
117+
} else if (type === 'commit') {
118+
shaResult = shaLine.split(' ')[0];
119+
}
120+
121+
return shaResult;
122+
}
123+
124+
function commitForTag(gitPath, tag) {
125+
var tagPath = path.join(gitPath, 'refs', 'tags', tag);
126+
var taggedObject = fs.readFileSync(tagPath, { encoding: 'utf8' }).trim();
127+
var objectPath = path.join(gitPath, 'objects', taggedObject.slice(0, 2), taggedObject.slice(2));
128+
129+
if (!zlib.inflateSync || !fs.existsSync(objectPath)) {
130+
// we cannot support annotated tags on node v0.10 because
131+
// zlib does not allow sync access
132+
return taggedObject;
133+
}
134+
135+
var objectContents = zlib.inflateSync(fs.readFileSync(objectPath)).toString();
136+
137+
// 'tag 172\u0000object c1ee41c325d54f410b133e0018c7a6b1316f6cda\ntype commit\ntag awesome-tag\ntagger Robert Jackson
138+
// <[email protected]> 1429100021 -0400\n\nI am making an annotated tag.\n'
139+
if (objectContents.slice(0,3) === 'tag') {
140+
var sections = objectContents.split(/\0|\r?\n/);
141+
var sha = sections[1].slice(7);
142+
143+
return sha;
144+
} else {
145+
// this will return the tag for lightweight tags
146+
return taggedObject;
147+
}
148+
}
149+
150+
function findTag(gitPath, sha) {
151+
var tags = findPackedTags(gitPath, sha)
152+
.concat(findUnpackedTags(gitPath, sha));
153+
tags.sort();
154+
return tags.length ? tags[0] : false;
155+
}
156+
157+
var LAST_TAG_CACHE = {};
158+
159+
function findLastTagCached(gitPath, sha) {
160+
if(!LAST_TAG_CACHE[gitPath]) {
161+
LAST_TAG_CACHE[gitPath] = {};
162+
}
163+
164+
if(!LAST_TAG_CACHE[gitPath][sha]) {
165+
LAST_TAG_CACHE[gitPath][sha] = findLastTag(gitPath, sha);
166+
}
167+
168+
return LAST_TAG_CACHE[gitPath][sha];
169+
}
170+
171+
function findLastTag(gitPath, sha) {
172+
var queue = [{ sha: sha, depth: 0 }];
173+
var seenCommits = new Set();
174+
while (queue.length) {
175+
var element = queue.shift();
176+
if (seenCommits.has(element.sha)) {
177+
continue;
178+
}
179+
seenCommits.add(element.sha);
180+
var tag = findTag(gitPath, element.sha);
181+
if (tag) {
182+
return {
183+
tag: tag,
184+
commitsSinceLastTag: element.depth
185+
};
186+
}
187+
var commitData = getCommitData(gitPath, sha);
188+
if (commitData && commitData.parents) {
189+
for (var i = 0; i < commitData.parents.length; i++) {
190+
queue.push({ sha: commitData.parents[i], depth: element.depth + 1 });
191+
}
192+
}
193+
}
194+
return { tag: null, commitsSinceLastTag: Infinity };
195+
}
196+
197+
function findUnpackedTags(gitPath, sha) {
198+
var unpackedTags = [];
199+
var tags = findLooseRefsForType(gitPath, 'tags');
200+
for (var i = 0, l = tags.length; i < l; i++) {
201+
var commitAtTag = commitForTag(gitPath, tags[i]);
202+
if (commitAtTag === sha) {
203+
unpackedTags.push(tags[i]);
204+
}
205+
}
206+
return unpackedTags;
207+
}
208+
209+
function findLooseRefsForType(gitPath, type) {
210+
var refsPath = path.join(gitPath, 'refs', type);
211+
return fs.existsSync(refsPath) ? fs.readdirSync(refsPath) : [];
212+
}
213+
214+
module.exports = function(gitPath) {
215+
var gitPathInfo = findRepo(gitPath);
216+
217+
var result = {
218+
sha: null,
219+
abbreviatedSha: null,
220+
branch: null,
221+
tag: null,
222+
committer: null,
223+
committerDate: null,
224+
author: null,
225+
authorDate: null,
226+
commitMessage: null,
227+
root: null,
228+
commonGitDir: null,
229+
worktreeGitDir: null,
230+
lastTag: null,
231+
commitsSinceLastTag: 0,
232+
};
233+
234+
if (!gitPathInfo) { return result; }
235+
236+
try {
237+
result.root = gitPathInfo.root;
238+
result.commonGitDir = gitPathInfo.commonGitDir;
239+
result.worktreeGitDir = gitPathInfo.worktreeGitDir;
240+
241+
var headFilePath = path.join(gitPathInfo.worktreeGitDir, 'HEAD');
242+
243+
if (fs.existsSync(headFilePath)) {
244+
var headFile = fs.readFileSync(headFilePath, {encoding: 'utf8'});
245+
var branchName = headFile.split('/').slice(2).join('/').trim();
246+
if (!branchName) {
247+
branchName = headFile.split('/').slice(-1)[0].trim();
248+
}
249+
var refPath = headFile.split(' ')[1];
250+
251+
// Find branch and SHA
252+
if (refPath) {
253+
refPath = refPath.trim();
254+
var branchPath = path.join(gitPathInfo.commonGitDir, refPath);
255+
256+
result.branch = branchName;
257+
if (fs.existsSync(branchPath)) {
258+
result.sha = fs.readFileSync(branchPath, { encoding: 'utf8' }).trim();
259+
} else {
260+
result.sha = findPackedCommit(gitPathInfo.commonGitDir, refPath);
261+
}
262+
} else {
263+
result.sha = branchName;
264+
}
265+
266+
result.abbreviatedSha = result.sha.slice(0,10);
267+
268+
// Find commit data
269+
var commitData = getCommitData(gitPathInfo.commonGitDir, result.sha);
270+
if (commitData) {
271+
result = Object.keys(commitData).reduce(function(r, key) {
272+
result[key] = commitData[key];
273+
return result;
274+
}, result);
275+
}
276+
277+
// Find tag
278+
var tag = findTag(gitPathInfo.commonGitDir, result.sha);
279+
if (tag) {
280+
result.tag = tag;
281+
}
282+
283+
var lastTagInfo = findLastTagCached(gitPathInfo.commonGitDir, result.sha);
284+
result.lastTag = lastTagInfo.tag;
285+
result.commitsSinceLastTag = lastTagInfo.commitsSinceLastTag;
286+
}
287+
} catch (e) {
288+
if (!module.exports._suppressErrors) {
289+
throw e; // helps with testing and scenarios where we do not expect errors
290+
} else {
291+
// eat the error
292+
}
293+
}
294+
295+
return result;
296+
};
297+
298+
module.exports._suppressErrors = true;
299+
module.exports._findRepo = findRepo;
300+
module.exports._changeGitDir = changeGitDir;
301+
302+
function getCommitData(gitPath, sha) {
303+
var objectPath = path.join(gitPath, 'objects', sha.slice(0, 2), sha.slice(2));
304+
305+
if (zlib.inflateSync && fs.existsSync(objectPath)) {
306+
var objectContents = zlib.inflateSync(fs.readFileSync(objectPath)).toString();
307+
308+
return objectContents.split(/\0|\r?\n/)
309+
.filter(function(item) {
310+
return !!item;
311+
})
312+
.reduce(function(data, section) {
313+
var part = section.slice(0, section.indexOf(' ')).trim();
314+
315+
switch(part) {
316+
case 'commit':
317+
case 'tag':
318+
case 'object':
319+
case 'type':
320+
case 'tree':
321+
//ignore these for now
322+
break;
323+
case 'author':
324+
case 'committer':
325+
var parts = section.match(/^(?:author|committer)\s(.+)\s(\d+\s(?:\+|\-)\d{4})$/);
326+
327+
if (parts) {
328+
data[part] = parts[1];
329+
data[part + 'Date'] = parseDate(parts[2]);
330+
}
331+
break;
332+
case 'parent':
333+
if (!data.parents) {
334+
data.parents = [];
335+
}
336+
data.parents.push(section.split(' ')[1]);
337+
break;
338+
default:
339+
// Added compatibility with multiline commit message
340+
// https://github.com/rwjblue/git-repo-info/pull/60
341+
if (!data.commitMessage) {
342+
data.commitMessage = section;
343+
} else {
344+
data.commitMessage = `${data.commitMessage}\n${section}`;
345+
}
346+
}
347+
348+
return data;
349+
}, {});
350+
}
351+
}
352+
353+
function parseDate(d) {
354+
var epoch = d.split(' ')[0];
355+
return new Date(epoch * 1000).toISOString();
356+
}

packages/dd-trace/src/plugins/util/git.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const coalesce = require('koalas')
2-
const getRepoInfo = require('git-repo-info')
2+
const getRepoInfo = require('./git-repo-info')
33

44
const { sanitizedExec } = require('./exec')
55

packages/dd-trace/test/plugins/util/git.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const {
3030
'./exec': {
3131
'sanitizedExec': sanitizedExecStub
3232
},
33-
'git-repo-info': gitRepoInfoStub
33+
'./git-repo-info': gitRepoInfoStub
3434
}
3535
)
3636

0 commit comments

Comments
 (0)