2016-10-03 09:42:42 -04:00
|
|
|
#!/usr/bin/env zsh
|
2018-03-26 05:38:37 -04:00
|
|
|
# <a href="https://dots.00dani.me/README">WHAT IS THIS</a>
|
2016-10-03 09:42:42 -04:00
|
|
|
|
2016-10-22 06:54:25 -04:00
|
|
|
'bootstrap'() {
|
2016-10-03 09:42:42 -04:00
|
|
|
echo 'Bootstrapping your dotfiles...' >&2
|
2016-10-03 17:49:44 -04:00
|
|
|
mkdir -p $DOTFILES
|
|
|
|
touch $DOTFILES/.stow
|
2016-12-05 06:07:30 -05:00
|
|
|
if (( ! $+commands[stow] )); then
|
2016-10-03 09:42:42 -04:00
|
|
|
echo 'GNU Stow is not installed, fetching it...' >&2
|
2016-10-03 17:49:44 -04:00
|
|
|
clone-one stow || return $?
|
2016-10-07 08:20:22 -04:00
|
|
|
STOW=$DOTFILES/stow/local/bin/stow
|
2016-10-03 09:42:42 -04:00
|
|
|
fi
|
|
|
|
|
2016-10-04 09:01:54 -04:00
|
|
|
clone dots git vim zsh || return $?
|
2016-10-03 09:42:42 -04:00
|
|
|
link
|
|
|
|
}
|
|
|
|
|
2016-10-22 06:54:25 -04:00
|
|
|
'clone'() {
|
2016-10-03 17:49:44 -04:00
|
|
|
echo "Requested packages: $argv" >&2
|
2016-10-03 09:42:42 -04:00
|
|
|
for package in $argv; do
|
2016-10-03 17:33:22 -04:00
|
|
|
clone-one $package || return $?
|
2016-10-03 09:42:42 -04:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-10-22 06:54:25 -04:00
|
|
|
'clone-one'() {
|
2016-10-03 09:42:42 -04:00
|
|
|
local url=$1
|
|
|
|
local package=${${url##*/}%.git}
|
2020-06-17 23:37:53 -04:00
|
|
|
# Simple package names are fetched from the configured source prefix.
|
|
|
|
[[ $url != */* ]] && url=$DOTS_SOURCE_PREFIX/$package
|
2020-06-17 23:33:12 -04:00
|
|
|
# user/repo packages are fetched from that user's GitHub repos.
|
2016-10-03 09:42:42 -04:00
|
|
|
[[ $url != *:* ]] && url=https://github.com/$url
|
2016-10-03 17:49:44 -04:00
|
|
|
if [[ -d $DOTFILES/$package ]]; then
|
2016-10-03 09:42:42 -04:00
|
|
|
echo "Looks like you already have $package cloned." >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
echo "Retrieving $package from $url now..." >&2
|
2016-10-03 17:49:44 -04:00
|
|
|
git clone $url $DOTFILES/$package
|
2016-10-03 09:42:42 -04:00
|
|
|
}
|
|
|
|
|
2016-10-22 06:54:25 -04:00
|
|
|
'link'() {
|
2016-10-07 09:43:17 -04:00
|
|
|
local -a packages
|
|
|
|
packages=($argv)
|
2016-10-03 09:42:42 -04:00
|
|
|
if (( $#packages == 0 )); then
|
|
|
|
packages=( $DOTFILES/*(N:t) )
|
2016-10-03 17:49:44 -04:00
|
|
|
if (( $#packages == 0 )); then
|
|
|
|
echo "No installed packages! Do you want to clone some first?" >&2
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
echo "Linking all packages ($packages) into $HOME now..." >&2
|
|
|
|
else
|
|
|
|
echo "Linking $packages into $HOME now..." >&2
|
2016-10-03 09:42:42 -04:00
|
|
|
fi
|
2016-10-03 21:31:49 -04:00
|
|
|
|
2016-10-07 08:19:02 -04:00
|
|
|
for nofold in $DOTFILES/${^packages}/.stow-no-folding(N); process-stow-no-folding $nofold
|
2016-10-03 21:31:49 -04:00
|
|
|
|
2016-10-07 08:19:02 -04:00
|
|
|
$STOW -d $DOTFILES -t ~ $packages
|
2016-10-03 21:31:49 -04:00
|
|
|
}
|
|
|
|
|
2016-10-22 06:54:25 -04:00
|
|
|
'process-stow-no-folding'() {
|
2016-10-04 07:31:17 -04:00
|
|
|
zmodload zsh/mapfile
|
2016-10-03 21:31:49 -04:00
|
|
|
for file in ${(f)mapfile[$1]}; do
|
|
|
|
file=~/$file
|
|
|
|
[[ -e $file ]] && continue
|
|
|
|
mkdir -p ${file:h}
|
|
|
|
touch $file
|
|
|
|
done
|
2016-10-03 09:42:42 -04:00
|
|
|
}
|
|
|
|
|
2016-10-22 06:54:25 -04:00
|
|
|
'fetch'() {
|
2016-10-22 06:44:44 -04:00
|
|
|
local -a packages
|
|
|
|
packages=($argv)
|
|
|
|
(( $#packages == 0 )) && packages=( $DOTFILES/*(N:t) )
|
|
|
|
for p in $packages; do
|
|
|
|
cd $DOTFILES/$p
|
|
|
|
git fetch -p
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2016-10-29 19:17:24 -04:00
|
|
|
'pull'() {
|
|
|
|
local -a packages
|
|
|
|
packages=($argv)
|
|
|
|
(( $#packages == 0 )) && packages=( $DOTFILES/*(N:t) )
|
|
|
|
for p in $packages; do
|
|
|
|
cd $DOTFILES/$p
|
|
|
|
git pull
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-22 06:54:25 -04:00
|
|
|
'status'() {
|
2016-10-07 09:43:17 -04:00
|
|
|
local -a packages
|
|
|
|
packages=($argv)
|
2016-10-03 09:42:42 -04:00
|
|
|
(( $#packages == 0 )) && packages=( $DOTFILES/*(N:t) )
|
2016-10-17 05:43:45 -04:00
|
|
|
local length=${#${(O@)packages//?/X}[1]} git_status remote_status
|
2016-10-03 09:42:42 -04:00
|
|
|
for p in $packages; do
|
2016-10-17 05:43:45 -04:00
|
|
|
cd $DOTFILES/$p
|
|
|
|
# Always show the repo's name at the beginning of the line.
|
|
|
|
printf %${length}s' ' $p
|
|
|
|
|
|
|
|
# Check if the package is actually a repo. It might not be if it's brand-new.
|
|
|
|
git_status="$(git status --porcelain --ignore-submodules -unormal 2>/dev/null)"
|
2016-10-11 05:07:22 -04:00
|
|
|
if (( $? != 0 )); then
|
2016-10-17 05:43:45 -04:00
|
|
|
printf '%9s' '' # space across to the last column
|
|
|
|
print -P %F{247}not a git repository%f
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Indicate the working tree status.
|
|
|
|
if [[ -z $git_status ]]; then
|
|
|
|
print -Pn %F{green}clean%f' '
|
|
|
|
else
|
|
|
|
print -Pn %F{red}unclean%f
|
|
|
|
fi
|
|
|
|
printf '%2s' ''
|
|
|
|
|
|
|
|
# If the repository has an upstream remote configured, indicate the number
|
|
|
|
# of commits out of sync we are from the remote.
|
|
|
|
if git rev-parse --abbrev-ref @{u} &>/dev/null; then
|
|
|
|
remote_status="$(git rev-list --left-right --count HEAD...@{u} 2>/dev/null)"
|
|
|
|
remote_status=(${(ps:\t:)remote_status})
|
|
|
|
print -Pn %F{cyan}
|
|
|
|
(( $remote_status[1] > 0 )) && print -n $remote_status[1]⇡
|
|
|
|
(( $remote_status[2] > 0 )) && print -n $remote_status[2]⇣
|
|
|
|
print -P %f
|
2016-10-03 09:42:42 -04:00
|
|
|
else
|
2016-10-17 05:43:45 -04:00
|
|
|
print -P %F{247}no remote%f
|
2016-10-03 09:42:42 -04:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2020-06-17 23:34:41 -04:00
|
|
|
'main'() {
|
2020-06-17 23:37:53 -04:00
|
|
|
: ${DOTFILES:=~/dotfiles} ${STOW:=stow} ${DOTS_SOURCE_PREFIX:=https://git.00dani.me/dot}
|
2016-10-03 09:42:42 -04:00
|
|
|
|
2020-06-17 23:34:41 -04:00
|
|
|
comm=$1
|
|
|
|
if (( $# == 0 )); then
|
|
|
|
if [[ -d $DOTFILES ]]; then
|
|
|
|
comm=status
|
|
|
|
else
|
|
|
|
comm=bootstrap
|
|
|
|
fi
|
2016-10-03 09:42:42 -04:00
|
|
|
fi
|
2020-06-17 23:34:41 -04:00
|
|
|
|
|
|
|
case $comm in
|
|
|
|
bootstrap) bootstrap ;;
|
|
|
|
clone) clone ${argv[2,-1]} ;;
|
|
|
|
fetch) fetch ${argv[2,-1]} ;;
|
|
|
|
link) link ${argv[2,-1]} ;;
|
|
|
|
pull) pull ${argv[2,-1]} ;;
|
|
|
|
st|status) status ${argv[2,-1]} ;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
2020-12-14 17:27:28 -05:00
|
|
|
{'main' "$@"}
|