feat(gitrepo): Enhance GitRepo to include commit date and version information
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
* autocreated commitinfo by @push.rocks/commitinfo
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@push.rocks/smartgit',
|
||||
version: '3.0.4',
|
||||
version: '3.1.0',
|
||||
description: 'A smart wrapper for nodegit that simplifies Git operations in Node.js.'
|
||||
}
|
||||
|
@ -131,41 +131,56 @@ export class GitRepo {
|
||||
|
||||
// Handle modified files
|
||||
if (head !== 0 && workdir !== 0 && head !== workdir) {
|
||||
headContent = await plugins.isomorphicGit.readBlob({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
dir: this.repoDir,
|
||||
oid: await plugins.isomorphicGit.resolveRef({
|
||||
headContent = await plugins.isomorphicGit
|
||||
.readBlob({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
dir: this.repoDir,
|
||||
ref: 'HEAD',
|
||||
}),
|
||||
filepath,
|
||||
}).then(result => new TextDecoder().decode(result.blob));
|
||||
oid: await plugins.isomorphicGit.resolveRef({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
dir: this.repoDir,
|
||||
ref: 'HEAD',
|
||||
}),
|
||||
filepath,
|
||||
})
|
||||
.then((result) => new TextDecoder().decode(result.blob));
|
||||
|
||||
workdirContent = await this.smartgitRef.envDeps.fs.promises.readFile(plugins.path.join(this.repoDir, filepath), 'utf8');
|
||||
workdirContent = await this.smartgitRef.envDeps.fs.promises.readFile(
|
||||
plugins.path.join(this.repoDir, filepath),
|
||||
'utf8'
|
||||
);
|
||||
}
|
||||
|
||||
// Handle added files
|
||||
if (head === 0 && workdir !== 0) {
|
||||
workdirContent = await this.smartgitRef.envDeps.fs.promises.readFile(plugins.path.join(this.repoDir, filepath), 'utf8');
|
||||
workdirContent = await this.smartgitRef.envDeps.fs.promises.readFile(
|
||||
plugins.path.join(this.repoDir, filepath),
|
||||
'utf8'
|
||||
);
|
||||
}
|
||||
|
||||
// Handle deleted files
|
||||
if (head !== 0 && workdir === 0) {
|
||||
headContent = await plugins.isomorphicGit.readBlob({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
dir: this.repoDir,
|
||||
oid: await plugins.isomorphicGit.resolveRef({
|
||||
headContent = await plugins.isomorphicGit
|
||||
.readBlob({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
dir: this.repoDir,
|
||||
ref: 'HEAD',
|
||||
}),
|
||||
filepath,
|
||||
}).then(result => new TextDecoder().decode(result.blob));
|
||||
oid: await plugins.isomorphicGit.resolveRef({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
dir: this.repoDir,
|
||||
ref: 'HEAD',
|
||||
}),
|
||||
filepath,
|
||||
})
|
||||
.then((result) => new TextDecoder().decode(result.blob));
|
||||
}
|
||||
|
||||
if (headContent || workdirContent) {
|
||||
const diff = plugins.diff.createTwoFilesPatch(filepath, filepath, headContent, workdirContent);
|
||||
const diff = plugins.diff.createTwoFilesPatch(
|
||||
filepath,
|
||||
filepath,
|
||||
headContent,
|
||||
workdirContent
|
||||
);
|
||||
diffs.push(diff);
|
||||
}
|
||||
}
|
||||
@ -174,13 +189,40 @@ export class GitRepo {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all commit messages
|
||||
* Get all commit messages with their dates and package.json version at each commit
|
||||
*/
|
||||
public async getAllCommitMessages(): Promise<string[]> {
|
||||
public async getAllCommitMessages(): Promise<
|
||||
{ date: string; version: string; message: string }[]
|
||||
> {
|
||||
const commits = await plugins.isomorphicGit.log({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
dir: this.repoDir,
|
||||
});
|
||||
return commits.map(commit => commit.commit.message);
|
||||
|
||||
const results = [];
|
||||
|
||||
for (const commit of commits) {
|
||||
let version = 'unknown';
|
||||
try {
|
||||
const packageJsonBlob = await plugins.isomorphicGit.readBlob({
|
||||
fs: this.smartgitRef.envDeps.fs,
|
||||
dir: this.repoDir,
|
||||
oid: commit.oid,
|
||||
filepath: 'package.json',
|
||||
});
|
||||
const packageJson = JSON.parse(new TextDecoder().decode(packageJsonBlob.blob));
|
||||
version = packageJson.version;
|
||||
} catch (error) {
|
||||
// If package.json does not exist or any error occurs, leave version as 'unknown'
|
||||
}
|
||||
|
||||
results.push({
|
||||
date: new plugins.smarttime.ExtendedDate(commit.commit.committer.timestamp * 1000).exportToHyphedSortableDate(),
|
||||
version: version,
|
||||
message: commit.commit.message,
|
||||
});
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
@ -8,8 +8,9 @@ import * as smartfile from '@push.rocks/smartfile';
|
||||
import * as smartpath from '@push.rocks/smartpath';
|
||||
import * as smartpromise from '@push.rocks/smartpromise';
|
||||
import * as smartstring from '@push.rocks/smartstring';
|
||||
import * as smarttime from '@push.rocks/smarttime';
|
||||
|
||||
export { smartenv, smartfile, smartpath, smartpromise, smartstring };
|
||||
export { smartenv, smartfile, smartpath, smartpromise, smartstring, smarttime };
|
||||
|
||||
// third party
|
||||
import * as diff from 'diff';
|
||||
|
Reference in New Issue
Block a user