import util from 'util';
import fs from 'fs';
import child_process from 'child_process';

const exec = util.promisify(child_process.exec);
util.promisify(child_process.execFile);
util.promisify(child_process.spawn);
const stat = util.promisify(fs.stat);
const readFile = util.promisify(fs.readFile);

const EDIT_MSG_FILE = ".git/COMMIT_EDITMSG";
async function getCommitMsg() {
  try {
    const gitLog = await exec("git log -1 --pretty=%B");
    return gitLog.stdout;
  } catch {
  }
  if (await stat(EDIT_MSG_FILE))
    return (await readFile(EDIT_MSG_FILE)).toString();
  throw new Error("not found");
}

export { getCommitMsg };
