fix(GitRepo): add diff function

This commit is contained in:
Philipp Kunz 2024-06-22 02:44:19 +02:00
parent a2485560b3
commit e8c3d3c44f
6 changed files with 5100 additions and 3735 deletions

View File

@ -33,19 +33,21 @@
"homepage": "https://code.foss.global/push.rocks/smartgit",
"dependencies": {
"@push.rocks/smartenv": "^5.0.12",
"@push.rocks/smartfile": "^11.0.0",
"@push.rocks/smartpath": "^5.0.5",
"@push.rocks/smartfile": "^11.0.20",
"@push.rocks/smartpath": "^5.0.18",
"@push.rocks/smartpromise": "^4.0.2",
"@push.rocks/smartshell": "^3.0.3",
"@push.rocks/smartstring": "^4.0.9",
"@push.rocks/smartshell": "^3.0.5",
"@push.rocks/smartstring": "^4.0.15",
"@types/diff": "^5.2.1",
"@types/minimatch": "^5.1.2",
"isomorphic-git": "^1.25.0"
"diff": "^5.2.0",
"isomorphic-git": "^1.25.10"
},
"devDependencies": {
"@git.zone/tsbuild": "^2.1.66",
"@git.zone/tsbuild": "^2.1.80",
"@git.zone/tsrun": "^1.2.44",
"@git.zone/tstest": "^1.0.77",
"@push.rocks/tapbundle": "^5.0.15"
"@git.zone/tstest": "^1.0.90",
"@push.rocks/tapbundle": "^5.0.23"
},
"private": false,
"files": [

File diff suppressed because it is too large Load Diff

View File

@ -2,12 +2,12 @@ import { tap, expect } from '@push.rocks/tapbundle';
import * as smartgit from '../ts/index.js';
import * as smartpath from '@push.rocks/smartpath';
const __dirname = smartpath.get.dirnameFromImportMetaUrl(import.meta.url);
import * as path from 'path';
let testSmartgitInstance: smartgit.Smartgit;
const testRepoDir = path.join(__dirname, '../.nogit/testrepo');
const testRepoDirSmartfile = path.join(__dirname, '../.nogit/pushrocks_smartfile');
const packageDir = path.join(smartpath.get.dirnameFromImportMetaUrl(import.meta.url), '../');
const testRepoDir = path.join(packageDir, './.nogit/testrepo');
const testRepoDirSmartfile = path.join(packageDir, './.nogit/pushrocks_smartfile');
tap.test('should create a valid smartgit instance', async () => {
testSmartgitInstance = new smartgit.Smartgit();
@ -26,4 +26,12 @@ tap.test('should clone a repo', async () => {
);
});
tap.test('should open a repo', async () => {
const gitRepo = await testSmartgitInstance.createRepoByOpen(packageDir);
const diff = await gitRepo.getUncommittedDiff([
'pnpm-lock.yaml',
]);
console.log(diff);
});
await tap.start();

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@push.rocks/smartgit',
version: '3.0.2',
description: 'smart wrapper for nodegit'
version: '3.0.3',
description: 'A smart wrapper for nodegit that simplifies Git operations in Node.js.'
}

View File

@ -1,5 +1,4 @@
import * as plugins from './smartgit.plugins.js';
import { Smartgit } from './smartgit.classes.smartgit.js';
/**
@ -68,7 +67,7 @@ export class GitRepo {
}
/**
* ensures the existance of a remote within a repository
* ensures the existence of a remote within a repository
* @param remoteNameArg
* @param remoteUrlArg
*/
@ -110,4 +109,67 @@ export class GitRepo {
remote: remoteName,
});
}
/**
* Get the diff of the current uncommitted changes while excluding specified files
*/
public async getUncommittedDiff(excludeFiles: string[] = []): Promise<string[]> {
const statusMatrix = await plugins.isomorphicGit.statusMatrix({
fs: this.smartgitRef.envDeps.fs,
dir: this.repoDir,
});
const diffs: string[] = [];
for (const row of statusMatrix) {
const [filepath, head, workdir] = row;
if (excludeFiles.includes(filepath)) {
continue; // Skip excluded files
}
let headContent = '';
let workdirContent = '';
// 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({
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');
}
// Handle added files
if (head === 0 && workdir !== 0) {
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({
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);
diffs.push(diff);
}
}
return diffs;
}
}

View File

@ -12,6 +12,7 @@ import * as smartstring from '@push.rocks/smartstring';
export { smartenv, smartfile, smartpath, smartpromise, smartstring };
// third party
import * as diff from 'diff';
import isomorphicGit from 'isomorphic-git';
export { isomorphicGit };
export { diff, isomorphicGit };