1
0
mirror of https://gitcode.com/github-mirrors/react-native-update-cli.git synced 2025-09-17 18:06:10 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

add commit info

This commit is contained in:
sunnylqm
2025-03-15 18:50:39 +08:00
parent d974be6706
commit 8bd19bc0f7
4 changed files with 65 additions and 10 deletions

50
src/utils/git.ts Normal file
View File

@@ -0,0 +1,50 @@
import git from 'isomorphic-git';
import fs from 'node:fs';
import path from 'node:path';
export interface CommitInfo {
hash: string;
message: string;
author: string;
date: number;
origin: string;
}
function findGitRoot(dir = process.cwd()) {
const gitRoot = fs.readdirSync(dir).find((dir) => dir === '.git');
if (gitRoot) {
// console.log({ gitRoot });
return path.join(dir, gitRoot);
}
const parentDir = path.dirname(dir);
if (parentDir === dir) {
return null;
}
return findGitRoot(parentDir);
}
const gitRoot = findGitRoot();
export async function getCommitInfo(): Promise<CommitInfo | undefined> {
if (!gitRoot) {
return;
}
try {
const remotes = await git.listRemotes({ fs, gitdir: gitRoot });
const origin =
remotes.find((remote) => remote.remote === 'origin') || remotes[0];
const { commit, oid } = (
await git.log({ fs, gitdir: gitRoot, depth: 1 })
)[0];
return {
hash: oid,
message: commit.message,
author: commit.author.name || commit.committer.name,
date: commit.committer.timestamp,
origin: origin.url,
};
} catch (error) {
console.error(error);
return;
}
}