Skip to content

Instantly share code, notes, and snippets.

@tksst
Last active June 10, 2023 02:53
Show Gist options
  • Save tksst/e1654186a613c186d1e80334e21484e5 to your computer and use it in GitHub Desktop.
Save tksst/e1654186a613c186d1e80334e21484e5 to your computer and use it in GitHub Desktop.
GitHub clone script
#!/bin/bash
set -e
set -u
DEFAULT_ORGANIZATION=tksst
function getconf(){
git config --local "$1"|| true
}
function normalizeRepo(){
# https://github.com/ で始まる場合は取り、末尾の.gitも取る
if [[ $1 =~ ^https://github.com/ ]]; then
local tmp="${1#https://github.com/}"
else
local tmp="$1"
fi
if [[ ! $tmp =~ / ]]; then
local tmp="$DEFAULT_ORGANIZATION/$tmp"
fi
echo "${tmp%.git}"
}
# devcontainer内からは実行しないようにする
if [[ -f /.dockerenv ]]; then
echo "We are seems to be in container" >&2
exit 4
fi
repo=$( normalizeRepo "$1" )
dir="${repo##*/}"
if [[ -d $dir ]]; then
echo "seems it is already cloned" >&2
exit 5
fi
mkdir "$dir"
cd "$dir"
# public or private ?
status=$( curl -s --head -w '%{http_code}\n' -o /dev/null "https://github.com/$repo" )
if [[ $status == '200' ]]; then
git clone --filter=blob:none --recursive "https://github.com/$repo.git" .
else
git clone --recursive "https://tksst@github.com/$repo.git" .
fi
git remote set-url --push origin "git@github.com:$repo.git"
ms_before=$( getconf merge.stat )
sa_before=$( getconf status.aheadbehind )
scalar register
if [[ $ms_before != "false" && $( getconf merge.stat ) == "false" ]]; then
git config --local --unset merge.stat
echo "restore default for merge.stat" >&2
fi
if [[ $sa_before != "false" && $( getconf status.aheadbehind ) == "false" ]]; then
git config --local --unset status.aheadbehind
echo "restore default for status.aheadbehind" >&2
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment