fix(GitRepo): add diff function
This commit is contained in:
@ -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.'
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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 };
|
||||
|
Reference in New Issue
Block a user