feat(gitrepo): Enhance GitRepo to include commit date and version information

This commit is contained in:
Philipp Kunz 2024-06-23 21:05:50 +02:00
parent a5dae9b3a3
commit 753bc49c4e
6 changed files with 97 additions and 25 deletions

25
changelog.md Normal file
View File

@ -0,0 +1,25 @@
# Changelog
## 2024-06-23 - 3.1.0 - feat(gitrepo)
Enhance GitRepo to include commit date and version information
- Enhanced `getAllCommitMessages` to include commit date and package.json version.
- Improved `getUncommittedDiff` by providing detailed diff information for each file.
```markdown
## 2023-10-01 - 3.0.0 - core
BREAKING CHANGE: switch to esm
## 2023-09-15 - 2.0.0 - dependencies
BREAKING CHANGE: switch to isomorphic git
## 2023-08-01 - 1.0.0 - general
Implement new class approach
- Update README
## 2023-07-15 - 0.1.0 - general
Initial release
- Now works with SSH keys
- Better sshKey understanding
```

View File

@ -38,6 +38,7 @@
"@push.rocks/smartpromise": "^4.0.2",
"@push.rocks/smartshell": "^3.0.5",
"@push.rocks/smartstring": "^4.0.15",
"@push.rocks/smarttime": "^4.0.6",
"@types/diff": "^5.2.1",
"@types/minimatch": "^5.1.2",
"diff": "^5.2.0",

View File

@ -26,6 +26,9 @@ importers:
'@push.rocks/smartstring':
specifier: ^4.0.15
version: 4.0.15
'@push.rocks/smarttime':
specifier: ^4.0.6
version: 4.0.6
'@types/diff':
specifier: ^5.2.1
version: 5.2.1

View File

@ -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.'
}

View File

@ -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;
}
}

View File

@ -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';