simplfy and switch to git bare repo with home worktree style
This commit is contained in:
parent
8a60ae1e21
commit
293d8b3af3
1 changed files with 45 additions and 172 deletions
217
bin/dotfiles
217
bin/dotfiles
|
@ -1,204 +1,77 @@
|
||||||
#!/bin/bash -e
|
#!/bin/sh -e
|
||||||
# Installation:
|
DOTFILES_HOME="${DOTFILES_HOME:-$HOME}"
|
||||||
# curl https://raw.github.com/Klowner/dotfiles/main/bin/dotfiles | bash
|
DOTFILES_ROOT="${DOTFILES_ROOT:-$DOTFILES_HOME/.dotfiles}"
|
||||||
basedir="${DOTFILES_ROOT:-$HOME/.dotfiles}"
|
DOTFILES_REMOTE="${DOTFILES_REMOTE:-git@github.com:Klowner/dotfiles}"
|
||||||
bindir="${DOTFILES_BIN:-$HOME/bin}"
|
DOTFILES_WORK_TREE="${DOTFILES_WORK_TREE:-$HOME}"
|
||||||
repo="Klowner/dotfiles"
|
DOTFILES_ALIAS="/usr/bin/git --git-dir=${DOTFILES_ROOT} --work-tree=${DOTFILES_WORK_TREE}"
|
||||||
gitbase="git://github.com/${repo}.git"
|
DEPENDENCIES=(git)
|
||||||
gitbranch="${DOTFILES_BRANCH:-main}"
|
|
||||||
gitremote="git@github.com:${repo}"
|
|
||||||
tarball="http://github.com/${repo}/tarball/${gitbranch}"
|
|
||||||
|
|
||||||
function has() {
|
function has() {
|
||||||
return $(which $1 &> /dev/null)
|
return $(which $1 &> /dev/null)
|
||||||
}
|
}
|
||||||
|
|
||||||
function missing() {
|
|
||||||
! has $1
|
|
||||||
}
|
|
||||||
|
|
||||||
function stat() {
|
|
||||||
GIT_DIR=${basedir}/.git git diff --stat
|
|
||||||
}
|
|
||||||
|
|
||||||
function note() {
|
|
||||||
echo "[32;1m * [0m$*"
|
|
||||||
}
|
|
||||||
|
|
||||||
function warn() {
|
|
||||||
echo "[31;1m * [0m$*"
|
|
||||||
}
|
|
||||||
|
|
||||||
function die() {
|
function die() {
|
||||||
warn $*
|
echo $*
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
function destination() {
|
function info() {
|
||||||
path=$1
|
echo "DOTFILES_ROOT: ${DOTFILES_ROOT}"
|
||||||
path=${path/\_/.}
|
echo "DOTFILES_REMOTE: ${DOTFILES_REMOTE}"
|
||||||
path=${path//\_//}
|
echo "DOTFILES_WORK_TREE: ${DOTFILES_WORK_TREE}"
|
||||||
echo $path
|
echo "DOTFILES_ALIAS: ${DOTFILES_ALIAS}"
|
||||||
}
|
}
|
||||||
|
|
||||||
function dirname() {
|
function smart_clone() {
|
||||||
path=$1
|
if [ -d $DOTFILES_ROOT ]; then
|
||||||
path=${path%"${path##*[!/]}"}
|
EXISTING_REMOTE=$(git --git-dir=${DOTFILES_ROOT} remote get-url origin)
|
||||||
path=${path%/*}
|
if [ $EXISTING_REMOTE == $DOTFILES_REMOTE ]; then
|
||||||
echo $path
|
dotfiles fetch
|
||||||
}
|
|
||||||
|
|
||||||
function reverse_destination() {
|
|
||||||
path=$1
|
|
||||||
path=${path/\./_/}
|
|
||||||
echo $path
|
|
||||||
echo path $path
|
|
||||||
}
|
|
||||||
|
|
||||||
function link() {
|
|
||||||
src=$1
|
|
||||||
dst=$2
|
|
||||||
if [ -e $dst ]; then
|
|
||||||
if [ -L $dst ]; then
|
|
||||||
# Symlink already exists
|
|
||||||
return
|
|
||||||
else
|
else
|
||||||
# Rename existing files with ".old" extension
|
die "$DOTFILES_ROOT already exists, aborting."
|
||||||
warn "$dst already exists, renaming to $dst.old"
|
|
||||||
backup=$dst.old
|
|
||||||
if [ -e $backup ]; then
|
|
||||||
die "$backup already exists. Aborting."
|
|
||||||
fi
|
|
||||||
mv -v $dst $backup
|
|
||||||
fi
|
fi
|
||||||
|
else
|
||||||
|
git clone --bare $DOTFILES_REMOTE $DOTFILES_ROOT
|
||||||
fi
|
fi
|
||||||
# Update existing or create new symlink
|
|
||||||
ln -vsf $src $dst
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function install_via_tarball() {
|
function check_requirements() {
|
||||||
note "Downloading tarball..."
|
for DEP in ${DEPENDENCIES[@]}; do
|
||||||
mkdir -vp $basedir
|
has $DEP || die "${DEP} not found, please install and try again."
|
||||||
cd $basedir
|
done
|
||||||
tempfile=TEMP.tar.gz
|
|
||||||
if has curl; then
|
|
||||||
curl -L $tarball >$tempfile
|
|
||||||
elif has wget; then
|
|
||||||
wget -O $tempfile $tarball
|
|
||||||
else:
|
|
||||||
die "Can't download tarball."
|
|
||||||
fi
|
|
||||||
tar --strip-components 1 -xzvf $tempfile
|
|
||||||
rm -v $tempfile
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function install_via_git() {
|
function dotfiles() {
|
||||||
note "Cloning dotfiles git repository..."
|
eval $DOTFILES_ALIAS $*
|
||||||
git clone ${gitremote} ${basedir}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function bootstrap() {
|
function backup_existing() {
|
||||||
note "Bootstrapping..."
|
dotfiles checkout
|
||||||
if missing curl && missing wget; then
|
DST="${DOTFILES_ROOT}-backup"
|
||||||
die "missing: curl or wget"
|
SRC="${DOTFILES_WORK_TREE}"
|
||||||
fi
|
mkdir -p $DST && \
|
||||||
if missing git; then
|
dotfiles checkout 2>&1 | grep -E "\s+\." | awk {'print $1'} | \
|
||||||
die "missing: git"
|
xargs -d $'\n' sh -c "for arg do mkdir -p ${DST}/\${arg%/*} && mv -v ${SRC}/\$arg ${DST}/\$arg; done" _
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -e $basedir ]; then
|
|
||||||
# Try to determine if these dotfiles already installed
|
|
||||||
if [ -e "${basedir}/.git" ]; then
|
|
||||||
local current_remote=$(GIT_DIR=${basedir}/.git git remote -v | grep origin\.\*\(fetch\) | awk '{print $2}')
|
|
||||||
if [ $current_remote = $gitremote ]; then
|
|
||||||
die "Dotfiles already installed!"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
warn "$basedir exists, moving to ${basedir}.bak"
|
|
||||||
mv "${basedir}" "${basedir}.bak"
|
|
||||||
fi
|
|
||||||
install_via_tarball
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function install() {
|
function install() {
|
||||||
note "Symlinking configuration files..."
|
check_requirements
|
||||||
for path in _*; do
|
smart_clone
|
||||||
dst=$(destination $path)
|
dotfiles checkout || backup_existing && dotfiles checkout
|
||||||
dstdir=$(dirname $dst)
|
dotfiles config --local status.showUntrackedFiles no
|
||||||
echo "$path"
|
|
||||||
case $path in
|
|
||||||
.|..|.git)
|
|
||||||
continue
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
link $basedir/$path $HOME/$dst
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
|
|
||||||
note "Installing ~/bin script links... ${bindir}"
|
|
||||||
mkdir -v -p ${bindir}
|
|
||||||
if [ -d ${bindir} ]; then
|
|
||||||
for path in ${basedir}/bin/*; do
|
|
||||||
link $path ${bindir}/${path##*/}
|
|
||||||
done
|
|
||||||
fi
|
|
||||||
note "Done"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function git_migrate() {
|
case "${1:-help}" in
|
||||||
if missing git; then
|
info) info;;
|
||||||
return
|
|
||||||
fi
|
|
||||||
if [ ! -d "${basedir}/.git" ]; then
|
|
||||||
note "Migrating dotfiles to git repository..."
|
|
||||||
git clone $gitremote --no-checkout --branch ${gitbranch} "${basedir}/migrate"
|
|
||||||
git mv "${basedir}/migrate/.git" "${basedir}/.git"
|
|
||||||
rm -rf "${basedir}/migrate"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
function add() {
|
|
||||||
path=$1
|
|
||||||
dst="${path/"$HOME"/}" # remove leading $HOME from path
|
|
||||||
dst="${dst/\//}" # remove leading slash
|
|
||||||
dst="${dst//[.|\/]/_}" # replace slashes with underscores
|
|
||||||
|
|
||||||
note "Adding $path..."
|
|
||||||
cp -avr $path $basedir/$dst
|
|
||||||
mv $path $path.old
|
|
||||||
link $basedir/$dst $path
|
|
||||||
}
|
|
||||||
|
|
||||||
function info() {
|
|
||||||
echo "location: ${basedir}"
|
|
||||||
echo "commands: ${bindir}"
|
|
||||||
echo "git: ${gitremote}:${gitbranch}"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Hopefully detect if this script is being piped to bash
|
|
||||||
if [ "${0/*\//}" != "dotfiles" ]; then
|
|
||||||
bootstrap && \
|
|
||||||
install && \
|
|
||||||
exit
|
|
||||||
fi
|
|
||||||
|
|
||||||
git_migrate
|
|
||||||
|
|
||||||
cmd=${1:-help}
|
|
||||||
case $cmd in
|
|
||||||
install) install;;
|
install) install;;
|
||||||
add) add ${@:2};;
|
|
||||||
info) info;;
|
|
||||||
status) stat;;
|
|
||||||
help)
|
help)
|
||||||
echo "Commands:"
|
echo "dotfiles commands: "
|
||||||
echo " - install Install to $basedir"
|
echo " - install"
|
||||||
echo " - add <path>"
|
echo " - info"
|
||||||
echo " - info Print script variables"
|
echo "all other commands will be forwarded to git"
|
||||||
echo " - status Show changed items in .dotfiles"
|
|
||||||
;;
|
;;
|
||||||
|
*) eval $DOTFILES_ALIAS $*;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# vim:ts=2:sw=2:et:
|
# vim:ts=2:sw=2:et:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue