pr-check / src / git / getDiffObjectForCommit.ts
getDiffObjectForCommit.ts
Raw
import { parseGitCommitShowOutput } from '../parsers/parseGitShowOutput';
import { getDiffForCommit } from './getDiffForCommit';
import { CommitDiff } from '../interfaces/CommitDiff';

/**
 * Constructs an object that wraps the `git show` output for a commit.
 */
export async function getDiffObjectForCommit(commitHash: string): Promise<CommitDiff> {
  const diff = await getDiffForCommit(commitHash);
  exitIfDiffEmpty(diff);

  const diffObject = parseGitCommitShowOutput(diff);
  return diffObject;
}

function exitIfDiffEmpty(diff: string) {
  if (diff == "") {
    throw new Error('Diff empty. Exiting.');
  }
}