Skip to content

Commit

Permalink
feat: Enhance GetGitDiffCommand class in git diff command file
Browse files Browse the repository at this point in the history
The GetGitDiffCommand class has been updated to improve its functionality. Now, an instance of the SimpleGit class is created during the construction phase and used later for obtaining git diffs. Furthermore, a new private method isBlank has been added to check if the diff result is empty. This leads to cleaner and more maintainable code.
  • Loading branch information
jgoedde committed Jul 19, 2024
1 parent 5f936b9 commit d203de3
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/commands/get-git-diff/GetGitDiffCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ export type GitDiffError = Error;

export class GetGitDiffCommand implements Command<GitDiffResult> {
private readonly gitDiffOptions = ['--staged'];
private readonly git: SimpleGit;

public constructor(private readonly repositoryPath: string) {}
public constructor(private readonly repositoryPath: string) {
this.git = simpleGit(this.repositoryPath).clean(CleanOptions.FORCE);
}

public async execute(): Promise<Result<GitDiffResult, GitDiffError>> {
eventBroker.emit('git-diff-started-event', this.gitDiffOptions);

const git: SimpleGit = simpleGit(this.repositoryPath).clean(CleanOptions.FORCE);
try {
const now = Date.now();
const diff = await git.diff(this.gitDiffOptions);
const diff = await this.git.diff(this.gitDiffOptions);
const duration = Date.now() - now;

if (!z.string().min(1).safeParse(diff).success) {
if (this.isBlank(diff)) {
eventBroker.emit('git-diff-empty-event');

return Err(new Error('The git diff is empty.'));
Expand All @@ -38,4 +40,8 @@ export class GetGitDiffCommand implements Command<GitDiffResult> {
return error;
}
}

private isBlank(str: string): boolean {
return !z.string().min(1).safeParse(str).success;
}
}

0 comments on commit d203de3

Please sign in to comment.