mtcute/scripts/git-utils.js

77 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-08-12 09:19:45 +03:00
import { execSync } from 'child_process'
function getLatestTag() {
try {
2024-08-12 09:19:45 +03:00
const res = execSync('git describe --abbrev=0 --tags', { encoding: 'utf8', stdio: 'pipe' }).trim()
return res
} catch (e) {
if (e.stderr.match(/^fatal: (No names found|No tags can describe)/i)) {
// no tags found, let's just return the first commit
2024-08-12 09:19:45 +03:00
return execSync('git rev-list --max-parents=0 HEAD', { encoding: 'utf8' }).trim()
}
throw e
}
}
function findChangedFilesSince(tag, until = 'HEAD') {
2024-08-12 09:19:45 +03:00
return execSync(`git diff --name-only ${tag} ${until}`, { encoding: 'utf8', stdio: 'pipe' }).trim().split('\n')
}
function getCommitsSince(tag, until = 'HEAD') {
const delim = `---${Math.random().toString(36).slice(2)}---`
2024-08-12 09:19:45 +03:00
const lines = execSync(`git log --pretty="format:%H %s%n%b%n${delim}" ${tag}..${until}`, { encoding: 'utf8', stdio: 'pipe' })
.trim()
.split('\n')
const items = []
let current = null
for (const line of lines) {
if (line === delim) {
if (current) items.push(current)
current = null
} else if (current) {
if (current.description) current.description += '\n'
current.description += line
} else {
const [hash, ...msg] = line.split(' ')
current = { hash, msg: msg.join(' '), description: '' }
}
}
if (current) items.push(current)
return items.reverse()
}
function getCurrentCommit() {
2024-08-12 09:19:45 +03:00
return execSync('git rev-parse HEAD', { encoding: 'utf8', stdio: 'pipe' }).trim()
}
function getCurrentBranch() {
2024-08-12 09:19:45 +03:00
return execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf8', stdio: 'pipe' }).trim()
}
function parseConventionalCommit(msg) {
const match = msg.match(/^(\w+)(?:\(([^)]+)\))?(!?): (.+)$/)
if (!match) return null
const [, type, scope, breaking, subject] = match
return { type, scope, breaking: Boolean(breaking), subject }
}
2024-08-12 09:19:45 +03:00
export {
findChangedFilesSince,
getCommitsSince,
getCurrentBranch,
2024-08-12 09:19:45 +03:00
getCurrentCommit,
getLatestTag,
parseConventionalCommit,
}