Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 192 lines (161 sloc) 3.33 KB
#!/usr/bin/env bash
set -euo pipefail
clone_command() {
if ! echo "${2}" | grep -Eq 'https:\/\/x-access-token:\w+@github\.com\/\w+\/\w+\.git'; then
echo "Bad upstream: ${2}" 1>&2
return 1
fi
local org name repo
org="$(echo "${2}" | cut -d '/' -f 4)"
name="$(echo "${2}" | cut -d '/' -f 5 | cut -d '.' -f 1)"
repo="${org}/${name}"
if [ -z "${repo}" ]; then
echo "Bad repo: ${2}" 1>&2
return 1
elif [ ! -d "${FIXTURE_DIR}/${repo}" ]; then
echo "No ${repo} fixture for ${2}" 1>&2
return 1
else
mkdir -p "${3}/.git"
echo "${repo}" > "${3}/.git/repo"
return 0
fi
}
remote_command() {
if [ $# -ne 3 ]; then
echo "Cannot stub git remote: $*" 1>&2
return 1
elif [ ! -d .git ]; then
echo "No git repo in ${PWD}" 1>&2
return 1
elif [ "${1:-}" = 'set-url' ] && [ "${2:-}" = 'origin' ]; then
if ! echo "${3}" | grep -Eq 'https:\/\/x-access-token:\w+@github\.com\/\w+\/\w+\.git'; then
echo "Bad upstream: ${3}" 1>&2
return 1
fi
local org name repo
org="$(echo "${3}" | cut -d '/' -f 4)"
name="$(echo "${3}" | cut -d '/' -f 5 | cut -d '.' -f 1)"
repo="${org}/${name}"
if [ -z "${repo}" ]; then
echo "Bad repo: ${3}" 1>&2
return 1
elif [ ! -d "${FIXTURE_DIR}/${repo}" ]; then
echo "No ${repo} fixture for ${3}" 1>&2
return 1
else
echo "${repo}" > .git/repo
return 0
fi
else
echo "Cannot stub git remote: $*" 1>&2
return 1
fi
}
fetch_command() {
shift 2
if [ $# -ne 2 ]; then
echo "Cannot stub fetch: $*" 1>&2
return 1
else
echo "${2}" > .git/branch
return 0
fi
}
checkout_command() {
if [ "${1}" != '-f' ]; then
echo "Unexpected git checkout option: ${1}" 1>&2
return 1
else
local repo branch fixture
repo="$(cat .git/repo)"
branch="$(cat .git/branch)"
fixture="${FIXTURE_DIR}/${repo}/${branch}"
if [ -z "${repo:-}" ] || [ -z "${branch:-}" ]; then
echo "No repo/branch stub configured" 1>&2
return 1
elif [ ! -d "${fixture}" ]; then
echo "No git fixture ${fixture}" 1>&2
return 1
else
rsync -avh --delete "${fixture}/" .
mkdir .git
echo "${repo}" > .git/repo
echo "${branch}" > .git/branch
fi
fi
}
pull_command() {
if [ ! -f .git/branch ] || [ ! -f .git/repo ]; then
echo "No git branch in ${PWD}" 1>&2
return 1
elif [ $# -ne 2 ]; then
echo "Cannot stub pull $*" 1>&2
return 1
elif [ "${1}" != 'origin' ]; then
echo "must pull from origin" 1>&2
return 1
else
return 0
fi
}
clean_command() {
local OPTIND
if [ "${1}" != '-fdx' ]; then
echo "Unexpected git clean option: ${1}" 1>&2
return 1
else
repo="$(cat .git/repo)"
branch="$(cat .git/branch)"
fixture="${FIXTURE_DIR}/${repo}/${branch}"
rsync -avh --delete "${fixture}/" .
mkdir .git
echo "${repo}" > .git/repo
echo "${branch}" > .git/branch
return 0
fi
}
ls_files_command() {
if [ ! -d .git ]; then
echo "No git repo found" 1>&2
return 1
else
find . -type f -print | grep -vF '.git' | sed 's/^\.\///'
fi
}
main() {
case "${1:-}" in
checkout)
shift
checkout_command "$@"
;;
clean)
shift
clean_command "$@"
;;
clone)
shift
clone_command "$@"
;;
fetch)
shift
fetch_command "$@"
;;
ls-files)
shift
ls_files_command "$@"
;;
pull)
shift
pull_command "$@"
;;
remote)
shift
remote_command "$@"
;;
*)
echo "Cannot stub git command: $*" 1>&2
return 1
esac
}
main "$@"
You can’t perform that action at this time.