1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- #!/usr/bin/env bash
- source "${BASH_SOURCE%/*}/common.sh"
- ensure_pwd
- ROOT_FOLDER="${PWD}"
- # Get a static hash based on the repo url
- function get_hash() {
- url="${1}"
- echo -n "${url}" | sha256sum | awk '{print $1}'
- }
- if [ -z ${1+x} ]; then
- command=""
- else
- command="$1"
- fi
- # Clone a repo
- if [[ "$command" = "clone" ]]; then
- repo="$2"
- repo_hash=$(get_hash "${repo}")
- write_log "Cloning ${repo} to ${ROOT_FOLDER}/repos/${repo_hash}"
- repo_dir="${ROOT_FOLDER}/repos/${repo_hash}"
- if [ -d "${repo_dir}" ]; then
- write_log "Repo already exists"
- exit 0
- fi
- write_log "Cloning ${repo} to ${repo_dir}"
- if ! git clone "${repo}" "${repo_dir}"; then
- write_log "Failed to clone repo"
- exit 1
- fi
- write_log "Done"
- exit 0
- fi
- # Update a repo
- if [[ "$command" = "update" ]]; then
- repo="$2"
- repo_hash=$(get_hash "${repo}")
- repo_dir="${ROOT_FOLDER}/repos/${repo_hash}"
- if [ ! -d "${repo_dir}" ]; then
- write_log "Repo does not exist"
- exit 1
- fi
- write_log "Updating ${repo} in ${repo_hash}"
- cd "${repo_dir}" || exit
- if ! git pull origin master; then
- write_log "Failed to update repo"
- exit 1
- fi
- cd "${ROOT_FOLDER}" || exit
- write_log "Done"
- exit 0
- fi
- if [[ "$command" = "get_hash" ]]; then
- repo="$2"
- get_hash "${repo}"
- exit
- fi
|