<commit_before>#!/bin/sh
# Contains custom Git functions that can be run in the terminal.

# General {{{

# Determines whether the given directory is in a Git repository.
# If no repository directory is provided, defaults to the current dir.
# Return 0 if the directory is a Git repo. Return non-zero otherwise.
isgitrepo() {
  local repoDir="${1:-$(git rev-parse --show-toplevel)}"

  git -C "${repoDir}" rev-parse >>/dev/null 2>&1
}

# Retrieves the Git URL for the current repository.
remoteurl() {
  git remote -v | egrep '^origin.*push' | awk '{print $2}'
}

# Return the name of the default configured remote repository for the given Git
# repository. If no repository path is provided, the current working directory
# will be used.
defaultremote() {
  local gitRepo="${1:-$(git rev-parse --show-toplevel 2>/dev/null)}"
  local defaultRemote

  if ! isgitrepo "${gitRepo}"; then
    err "${BLUE}${gitRepo}${NC} is not a Git repository."
    return 1
  fi

  defaultRemote="$(git -C "${gitRepo}" remote 2>/dev/null)"

  if [ -z "${defaultRemote}" ]; then
    # If we can't intelligently determine the default remote name, assume the
    # standard remote name of "origin"
    defaultRemote="origin"
  fi

  echo "${defaultRemote}"
}

# Returns the default remote branch name for a given repository. If no
# repository is provided, defaults to the current directory.
defaultbranch() {
  local gitRepo="${1:-$(git rev-parse --show-toplevel 2>/dev/null)}"
  local defaultRemote="$(defaultremote "${gitRepo}")"

  if ! isgitrepo "${gitRepo}"; then
    err "${BLUE}${gitRepo}${NC} is not a Git repository."
    return 1
  fi

  git -C "${gitRepo}" remote show "${defaultRemote}" 2>/dev/null |
    sed -n '/HEAD branch/s/.*: //p'
}

# Retrieve the name of the currently checked-out Git ref.
currentref() {
  local gitRepo="${1:-$(git rev-parse --show-toplevel 2>/dev/null)}"

  git -C "${gitRepo}" rev-parse --abbrev-ref HEAD 2>/dev/null
}

# Opens the commit message for the current repo in the configured editor.
emsg() {
  edit "$(git rev-parse --show-toplevel)/.git/COMMIT_EDITMSG"
}
# }}}

# Branching/switching branches {{{

# Switch to a Git branch/commit/tag.
# Supports history, so we can return to the previously visited ref using
#   sw -1
# or even further with, for instance:
#   sw -4
sw() {
  local arg="${1}"
  local ref
  local histFile="${SW_HISTORY_FILE:-${HOME}/.sw_ref_hist}"
  # We want to write the current branch/commit to history after we switch.
  local currentRef="$(currentref)"

  if [ -z "${arg}" ]; then
    err "No ref provided to switch"
    return 1
  fi

  if echo "${arg}" | grep -q '^-[0-9]$'; then
    # Get nth line from history file if arg is of the form'-<n>' where n is an
    # integer.
    ref="$(sed "${arg#-}q;d" "${histFile}")"
  else
    # Otherwise, assume arg is the name of the ref we want to checkout.
    ref="${arg}"
  fi

  if ! verifyref "${ref}"; then
    err "${CYAN}${ref}${NC} is not a valid Git ref"
    return 2
  fi

  if ! __checkout_local_or_remote_branch "${ref}"; then
    err "Could not switch to ref: ${CYAN}${ref}${NC}"
    return 3
  fi

  if [ "${ref}" != "${currentRef}" ]; then
    __save_ref_to_sw_hist "${currentRef}"
  fi
}

# Intelligently determine whether a local copy of a remote branch exists. If it
# does not, we must first create a local copy and instruct the branch to track
# its remote counterpart. If the local copy already exists, we have only to
# switch to it.
__checkout_local_or_remote_branch() {
  local remoteBranch="${1}"
  local localBranch="$(basename "${remoteBranch}" 2>/dev/null)"
  local currentRef="$(currentref)"

  if [ -z "${remoteBranch}" ]; then
    err "No remote branch name provided"
    return 1
  elif [ "${localBranch}" = "${currentRef}" ]; then
    warn "HEAD is already set to ref: ${CYAN}${localBranch}${NC}"
    return 0
  fi

  if ! verifyref "${localBranch}"; then
    git checkout -t "${remoteBranch}"
  else
    git checkout "${localBranch}"
  fi
}

# Persist most recent ref passed to `sw' to the branch history file.
__save_ref_to_sw_hist() {
  local ref="${1}"
  local histFile="${SW_HISTORY_FILE:-${HOME}/.sw_ref_hist}"
  local histTempFile="${histFile}.tmp"
  local numRefsToSave="10"

  if [ -z "${ref}" ]; then
    err "No ref provided to save to sw history file"
    return 1
  fi

  if [ -f "${histFile}" ]; then
    log_debug "Writing sw ref to history file: ${GREEN}${histFile}${NC}"

    echo "${ref}" |
      cat - "${histFile}" |
      rmduplines |
      head -n "${numRefsToSave}" >"${histTempFile}"

    mv "${histTempFile}" "${histFile}"
  else
    log_debug "Creating new ref history file with entry: ${CYAN}${ref}${NC}"
    echo "${ref}" >"${histFile}"
  fi
}
<commit_msg>Slightly enhanced branch checkout function<commit_after>#!/bin/sh
# Contains custom Git functions that can be run in the terminal.

# General {{{

# Determines whether the given directory is in a Git repository.
# If no repository directory is provided, defaults to the current dir.
# Return 0 if the directory is a Git repo. Return non-zero otherwise.
isgitrepo() {
  local repoDir="${1:-$(git rev-parse --show-toplevel)}"

  git -C "${repoDir}" rev-parse >>/dev/null 2>&1
}

# Retrieves the Git URL for the current repository.
remoteurl() {
  git remote -v | egrep '^origin.*push' | awk '{print $2}'
}

# Return the name of the default configured remote repository for the given Git
# repository. If no repository path is provided, the current working directory
# will be used.
defaultremote() {
  local gitRepo="${1:-$(git rev-parse --show-toplevel 2>/dev/null)}"
  local defaultRemote

  if ! isgitrepo "${gitRepo}"; then
    err "${BLUE}${gitRepo}${NC} is not a Git repository."
    return 1
  fi

  defaultRemote="$(git -C "${gitRepo}" remote 2>/dev/null)"

  if [ -z "${defaultRemote}" ]; then
    # If we can't intelligently determine the default remote name, assume the
    # standard remote name of "origin"
    defaultRemote="origin"
  fi

  echo "${defaultRemote}"
}

# Returns the default remote branch name for a given repository. If no
# repository is provided, defaults to the current directory.
defaultbranch() {
  local gitRepo="${1:-$(git rev-parse --show-toplevel 2>/dev/null)}"
  local defaultRemote="$(defaultremote "${gitRepo}")"

  if ! isgitrepo "${gitRepo}"; then
    err "${BLUE}${gitRepo}${NC} is not a Git repository."
    return 1
  fi

  git -C "${gitRepo}" remote show "${defaultRemote}" 2>/dev/null |
    sed -n '/HEAD branch/s/.*: //p'
}

# Retrieve the name of the currently checked-out Git ref.
currentref() {
  local gitRepo="${1:-$(git rev-parse --show-toplevel 2>/dev/null)}"

  git -C "${gitRepo}" rev-parse --abbrev-ref HEAD 2>/dev/null
}

# Opens the commit message for the current repo in the configured editor.
emsg() {
  edit "$(git rev-parse --show-toplevel)/.git/COMMIT_EDITMSG"
}
# }}}

# Branching/switching branches {{{

# Switch to a Git branch/commit/tag.
# Supports history, so we can return to the previously visited ref using
#   sw -1
# or even further with, for instance:
#   sw -4
sw() {
  local arg="${1}"
  local ref
  local histFile="${SW_HISTORY_FILE:-${HOME}/.sw_ref_hist}"
  # We want to write the current branch/commit to history after we switch.
  local currentRef="$(currentref)"

  if [ -z "${arg}" ]; then
    err "No ref provided to switch"
    return 1
  fi

  if echo "${arg}" | grep -q '^-[0-9]$'; then
    # Get nth line from history file if arg is of the form'-<n>' where n is an
    # integer.
    ref="$(sed "${arg#-}q;d" "${histFile}")"
  else
    # Otherwise, assume arg is the name of the ref we want to checkout.
    ref="${arg}"
  fi

  if ! verifyref "${ref}"; then
    err "${CYAN}${ref}${NC} is not a valid Git ref"
    return 2
  fi

  if ! __checkout_local_or_remote_branch "${ref}"; then
    err "Could not switch to ref: ${CYAN}${ref}${NC}"
    return 3
  fi

  if [ "${ref}" != "${currentRef}" ]; then
    __save_ref_to_sw_hist "${currentRef}"
  fi
}

# Intelligently determine whether a local copy of a remote branch exists. If it
# does not, we must first create a local copy and instruct the branch to track
# its remote counterpart. If the local copy already exists, we have only to
# switch to it.
__checkout_local_or_remote_branch() {
  local remoteBranch="${1}"
  local localBranch="$(basename "${remoteBranch}" 2>/dev/null)"
  local currentRef="$(currentref)"
  local defaultRemote="$(defaultremote)"

  if [ -z "${remoteBranch}" ]; then
    err "No branch name provided"
    return 1
  elif [ "${localBranch}" = "${currentRef}" ]; then
    warn "HEAD is already set to ref: ${CYAN}${localBranch}${NC}"
    return 0
  fi

  if ! verifyref "${localBranch}"; then
    git checkout -t "${remoteBranch}"
  else
    git checkout "${localBranch}"
    git branch -u "${defaultRemote}/${localBranch}" "${localBranch}" 2>/dev/null
  fi
}

# Persist most recent ref passed to `sw' to the branch history file.
__save_ref_to_sw_hist() {
  local ref="${1}"
  local histFile="${SW_HISTORY_FILE:-${HOME}/.sw_ref_hist}"
  local histTempFile="${histFile}.tmp"
  local numRefsToSave="10"

  if [ -z "${ref}" ]; then
    err "No ref provided to save to sw history file"
    return 1
  fi

  if [ -f "${histFile}" ]; then
    log_debug "Writing sw ref to history file: ${GREEN}${histFile}${NC}"

    echo "${ref}" |
      cat - "${histFile}" |
      rmduplines |
      head -n "${numRefsToSave}" >"${histTempFile}"

    mv "${histTempFile}" "${histFile}"
  else
    log_debug "Creating new ref history file with entry: ${CYAN}${ref}${NC}"
    echo "${ref}" >"${histFile}"
  fi
}

{'fraction_non_alphanumeric': 0.14733542319749215, 'fraction_numerical': 0.006792058516196447, 'mean_word_length': 3.548954372623574, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 30, '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': '126009', 'n_tokens_mistral': 3123, 'n_tokens_neox': 3048, 'n_words': 1125}