diff --git a/changelog.md b/changelog.md new file mode 100644 index 0000000..2d1e04f --- /dev/null +++ b/changelog.md @@ -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 +``` + diff --git a/package.json b/package.json index 62cb7c9..e99a77c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 00965d5..d4e9828 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -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 diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index bd432a1..c6535d4 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -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.' } diff --git a/ts/smartgit.classes.gitrepo.ts b/ts/smartgit.classes.gitrepo.ts index c95f70c..45820ff 100644 --- a/ts/smartgit.classes.gitrepo.ts +++ b/ts/smartgit.classes.gitrepo.ts @@ -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 { + 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; } } diff --git a/ts/smartgit.plugins.ts b/ts/smartgit.plugins.ts index e46a5db..b312dcf 100644 --- a/ts/smartgit.plugins.ts +++ b/ts/smartgit.plugins.ts @@ -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';