<commit_before>import { git, envForAuthentication } from './core'
import { Repository } from '../../models/repository'
import { Branch, BranchType, Tip, BranchState } from '../../models/branch'
import { User } from '../../models/user'
import { getCurrentBranch } from './for-each-ref'

import { fatalError } from '../../lib/fatal-error'

/** Create a new branch from the given start point. */
export async function createBranch(repository: Repository, name: string, startPoint: string): Promise<void> {
  await git([ 'branch', name, startPoint ], repository.path, 'createBranch')
}

/** Rename the given branch to a new name. */
export async function renameBranch(repository: Repository, branch: Branch, newName: string): Promise<void> {
  await git([ 'branch', '-m', branch.nameWithoutRemote, newName ], repository.path, 'renameBranch')
}

/**
 * Delete the branch. If the branch has a remote branch, it too will be
 * deleted.
 */
export async function deleteBranch(repository: Repository, branch: Branch, user: User | null): Promise<true> {
  if (branch.type === BranchType.Local) {
    await git([ 'branch', '-D', branch.name ], repository.path, 'deleteBranch')
  }

  const remote = branch.remote

  // If the user is not authenticated, the push is going to fail
  // Let this propagate and leave it to the caller to handle
  if (remote) {
    await git([ 'push', remote, `:${branch.nameWithoutRemote}` ], repository.path, 'deleteBranch', { env: envForAuthentication(user) })
  }

  return true
}

/** Get the name of the current branch. */
export async function getTip(repository: Repository): Promise<Tip> {

  const revParse = await git([ 'rev-parse', 'HEAD' ], repository.path, 'getTip', { successExitCodes: new Set([ 0, 128 ]) })
  if (revParse.exitCode === 128) {
    // fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
    return {
      kind: BranchState.Unborn,
    }
  }

  // TODO: verify this is correct
  const currentSha = revParse.stdout.trim()

  const symbolicRef = await git([ 'symbolic-ref', 'HEAD' ], repository.path, 'getTip', { successExitCodes: new Set([ 0, 128 ]) })
  if (symbolicRef.exitCode === 128) {
    // fatal: ref HEAD is not a symbolic ref
    return {
      kind: BranchState.Detached,
      currentSha,
    }
  }

  const currentBranch = await getCurrentBranch(repository)
  if (!currentBranch) {
    fatalError(`getTip failed despite all the previous guard checks`)
    return { kind: BranchState.Unknown }
  }

  return {
    kind: BranchState.Valid,
    branch: currentBranch,
  }
}
<commit_msg>this totally works<commit_after>import { git, envForAuthentication } from './core'
import { Repository } from '../../models/repository'
import { Branch, BranchType, Tip, BranchState } from '../../models/branch'
import { User } from '../../models/user'
import { getCurrentBranch } from './for-each-ref'

import { fatalError } from '../../lib/fatal-error'

/** Create a new branch from the given start point. */
export async function createBranch(repository: Repository, name: string, startPoint: string): Promise<void> {
  await git([ 'branch', name, startPoint ], repository.path, 'createBranch')
}

/** Rename the given branch to a new name. */
export async function renameBranch(repository: Repository, branch: Branch, newName: string): Promise<void> {
  await git([ 'branch', '-m', branch.nameWithoutRemote, newName ], repository.path, 'renameBranch')
}

/**
 * Delete the branch. If the branch has a remote branch, it too will be
 * deleted.
 */
export async function deleteBranch(repository: Repository, branch: Branch, user: User | null): Promise<true> {
  if (branch.type === BranchType.Local) {
    await git([ 'branch', '-D', branch.name ], repository.path, 'deleteBranch')
  }

  const remote = branch.remote

  // If the user is not authenticated, the push is going to fail
  // Let this propagate and leave it to the caller to handle
  if (remote) {
    await git([ 'push', remote, `:${branch.nameWithoutRemote}` ], repository.path, 'deleteBranch', { env: envForAuthentication(user) })
  }

  return true
}

/** Get the name of the current branch. */
export async function getTip(repository: Repository): Promise<Tip> {

  const revParse = await git([ 'rev-parse', 'HEAD' ], repository.path, 'getTip', { successExitCodes: new Set([ 0, 128 ]) })
  if (revParse.exitCode === 128) {
    // fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
    return {
      kind: BranchState.Unborn,
    }
  }

  const currentSha = revParse.stdout.trim()

  const symbolicRef = await git([ 'symbolic-ref', 'HEAD' ], repository.path, 'getTip', { successExitCodes: new Set([ 0, 128 ]) })
  if (symbolicRef.exitCode === 128) {
    // fatal: ref HEAD is not a symbolic ref
    return {
      kind: BranchState.Detached,
      currentSha,
    }
  }

  const currentBranch = await getCurrentBranch(repository)
  if (!currentBranch) {
    fatalError(`getTip failed despite all the previous guard checks`)
    return { kind: BranchState.Unknown }
  }

  return {
    kind: BranchState.Valid,
    branch: currentBranch,
  }
}

{'fraction_non_alphanumeric': 0.13655010744285995, 'fraction_numerical': 0.005469818323891385, 'mean_word_length': 3.7276084949215145, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '207075', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1497, 'n_words': 513}