-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.ts
More file actions
48 lines (43 loc) · 1.61 KB
/
Copy pathgit.ts
File metadata and controls
48 lines (43 loc) · 1.61 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
import { execFileSync } from 'child_process';
import { CommitInfo } from './types';
export function getCommits(baseTag: string, currentTag: string, repoPath: string = process.cwd()): CommitInfo[] {
const range = baseTag ? `${baseTag}..${currentTag}` : currentTag;
try {
const output = execFileSync(
'git',
['log', range, '--first-parent', '--pretty=format:%H||%P||%an||%ae||%aI||%s'],
{ encoding: 'utf8', cwd: repoPath },
);
if (!output.trim()) return [];
return output.trim().split('\n').map(line => {
const [sha, parents, name, email, dateStr, message] = line.split('||');
return {
sha,
parent_shas: parents ? parents.split(' ') : [],
author: { git_name: name, git_email: email },
date: new Date(dateStr),
message,
};
});
} catch (error) {
console.error(`Error executing git log: ${error}`);
throw error;
}
}
export function getCommitHistory(ref: string, repoPath: string = process.cwd()): string[] {
try {
const output = execFileSync('git', ['log', ref, '--first-parent', '--pretty=format:%H'], { encoding: 'utf8', cwd: repoPath });
return output.trim().split('\n').filter(line => line.length > 0);
} catch (error) {
console.error(`Error getting commit history for ${ref}: ${error}`);
throw error;
}
}
export function getInitialCommit(repoPath: string = process.cwd()): string {
try {
return execFileSync('git', ['rev-list', '--max-parents=0', 'HEAD'], { encoding: 'utf8', cwd: repoPath }).trim();
} catch (error) {
console.error(`Error getting initial commit: ${error}`);
throw error;
}
}