Skip to content

git

A library of git related functions.

lb_git_available

Return 0 if the git binary is available.

Parameters:
Name Type Description Default
None - - -
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/git"

function check_git(){
  if lb_git_available; then
    printf "git is available\n"
  else
    printf "git is not available\n"
  fi
}

function check_in_repo(){
  if lb_in_git_repo; then
    # shellcheck disable=SC2016
    printf '$(pwd) is in a repo\n'
  else
    # shellcheck disable=SC2016
    printf '$(pwd) is not in a repo\n'
  fi
}

function main() {
  check_git
  check_in_repo

  s=$(lb_repo_root)
  printf "repo_root: %s\n" "${s}"
}

main "${@}"
Show source code in init
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

function lb_git_available(){
  # shellcheck disable=SC2016
  command -v git &>/dev/null || lb_fail 'Git binary not found in ${PATH}'
}

function lb_in_git_repo(){
    lb_git_available
    git status >/dev/null || lb_fail "$(pwd) is not within a git repo."
}

# Get the top level of a git repo
function lb_repo_root(){
    lb_in_git_repo
    git rev-parse --show-toplevel
}

lb_in_git_repo

Return 0 if the $(pwd) is in a repo.

Parameters:
Name Type Description Default
None - - -
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/git"

function check_git(){
  if lb_git_available; then
    printf "git is available\n"
  else
    printf "git is not available\n"
  fi
}

function check_in_repo(){
  if lb_in_git_repo; then
    # shellcheck disable=SC2016
    printf '$(pwd) is in a repo\n'
  else
    # shellcheck disable=SC2016
    printf '$(pwd) is not in a repo\n'
  fi
}

function main() {
  check_git
  check_in_repo

  s=$(lb_repo_root)
  printf "repo_root: %s\n" "${s}"
}

main "${@}"
Show source code in init
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

function lb_git_available(){
  # shellcheck disable=SC2016
  command -v git &>/dev/null || lb_fail 'Git binary not found in ${PATH}'
}

function lb_in_git_repo(){
    lb_git_available
    git status >/dev/null || lb_fail "$(pwd) is not within a git repo."
}

# Get the top level of a git repo
function lb_repo_root(){
    lb_in_git_repo
    git rev-parse --show-toplevel
}

lb_repo_root

Return the path of the repo root directory.

Parameters:
Name Type Description Default
None - - -
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/git"

function check_git(){
  if lb_git_available; then
    printf "git is available\n"
  else
    printf "git is not available\n"
  fi
}

function check_in_repo(){
  if lb_in_git_repo; then
    # shellcheck disable=SC2016
    printf '$(pwd) is in a repo\n'
  else
    # shellcheck disable=SC2016
    printf '$(pwd) is not in a repo\n'
  fi
}

function main() {
  check_git
  check_in_repo

  s=$(lb_repo_root)
  printf "repo_root: %s\n" "${s}"
}

main "${@}"
Show source code in init
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

function lb_git_available(){
  # shellcheck disable=SC2016
  command -v git &>/dev/null || lb_fail 'Git binary not found in ${PATH}'
}

function lb_in_git_repo(){
    lb_git_available
    git status >/dev/null || lb_fail "$(pwd) is not within a git repo."
}

# Get the top level of a git repo
function lb_repo_root(){
    lb_in_git_repo
    git rev-parse --show-toplevel
}