-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.js
48 lines (39 loc) · 1.39 KB
/
git.js
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
const fs = require('node:fs')
const path = require('node:path')
function findClosestGitFolder(directory) {
let currentDir = directory;
let parentDir = path.dirname(currentDir);
do {
const gitFolderPath = path.join(currentDir, '.git');
if (fs.existsSync(gitFolderPath)) {
return gitFolderPath;
}
currentDir = parentDir;
parentDir = path.dirname(currentDir);
// currentDir and parentDir are equal when root folder of system is reached
} while (currentDir !== parentDir)
return null;
}
const currentFolder = path.resolve('.')
const gitFolder = findClosestGitFolder(currentFolder);
if (gitFolder) {
console.debug(`eslint-plugin-todohub: git-folder found at: ${gitFolder}`);
} else {
console.debug(`eslint-plugin-todohub: Not in a git (sub)-folder: ${currentFolder}: Rule "current-feature-branch-issues" disabled.`);
}
module.exports = {
getCurrentBranchInfo: () => {
let issueNumber;
let isFeatureBranch;
if (gitFolder) {
try {
const gitHEAD = fs.readFileSync(path.join(gitFolder, 'HEAD'), 'utf8');
issueNumber = gitHEAD && Number.parseInt(gitHEAD.split('/').pop()?.split('-')[0]);
isFeatureBranch = gitHEAD !== undefined && !Number.isNaN(issueNumber);
} catch (err) {
console.error('eslint-plugin-todohub: Cannot read HEAD from .git folder.');
}
}
return { issueNumber, isFeatureBranch };
}
}