pr-check / src / git / doesBranchExist.ts
doesBranchExist.ts
Raw
import { getOutputFromRunningCommands } from "../commandline/getOutputFromRunningCommands";

/**
 * @description Checks if a branch with a given name exists, either locally or on the remote.
 * @returns {boolean} True if the branch exists, false otherwise.
 */
async function doesBranchExist(branchName: string): Promise<boolean> {
  const commandsToGetAllGitBranches = ["git", "branch", "-a"];
  
  let branchListString = await getOutputFromRunningCommands(commandsToGetAllGitBranches)
  // convert the branch list to an array and trim to remove whitespace
  let branchList = branchListString.split("\n")
  branchList = branchList.map((branch) => {
    return branch.trim()
  })

  return branchList.includes(branchName)
}

export { doesBranchExist }