bootstrap/dup

137 lines
2.7 KiB
Plaintext
Raw Normal View History

#!/bin/bash
set -eu
PULL_BASE="https://adamgoldsmith.name/cgit"
PUSH_BASE="ag:/srv/git"
function do_get() {
target="$1"
if [ -d "$target" ]
then
echo "$target already exists, not getting"
else
echo "Getting $target"
git clone "$PULL_BASE/dotfiles/$target.git" --recurse-submodules
cd $target
git remote set-url origin "$PULL_BASE/dotfiles/$target.git"
git remote set-url --push origin "$PUSH_BASE/dotfiles/$target.git"
cd ..
fi
}
function do_pull() {
2020-04-26 01:47:14 -04:00
dir="$1"
if [ "$stashAndSync" = true ]
then
local do_stash
do_stash=$(git diff)
2020-04-26 01:47:14 -04:00
[ -n "$do_stash" ] && git -C "$dir" stash
git pull -C "$dir" --rebase
git push -C "$dir"
[ -n "$do_stash" ] && git -C "$dir" stash pop || true
2019-01-16 00:14:55 -05:00
else
2020-04-26 01:47:14 -04:00
git -C "$dir" pull
fi
}
function do_pull_maybe_quiet() {
name="$1"
dir="${2:-$name}"
git_out="$(do_pull $dir)"
if [[ "$quiet" != true || "$git_out" != "Already up to date." ]]
then
echo "Updating $name"
echo "$git_out"
fi
}
function run_script() {
script="$1"; shift
targets="$@"
for target in $targets
do
if [ -e "$target/$script" ]
then
2020-04-26 01:47:14 -04:00
$quiet || echo "Running $target$script"
"$target/$script" ${quiet:+-q}
fi
done
}
function maybe_stow() {
targets="$@"
if [ "$stow" = true ]
then
run_script PRESTOW "$targets"
stow -t"$HOME" --ignore="^PRESTOW$" --ignore="^POSTSTOW$" $targets
run_script POSTSTOW "$targets"
fi
}
2020-04-26 01:47:14 -04:00
quiet=false
stow=true
bootstrap=false
stashAndSync=false
while [ "$#" -gt 0 ]
do
case $1 in
-ns | --no-stow)
stow=false
shift
;;
-bs | --bootstrap)
bootstrap=true
shift
;;
-s | --stash-and-sync)
stashAndSync=true
shift
;;
2020-04-26 01:47:14 -04:00
-q | --quiet)
quiet=true
shift
;;
--)
shift
break
;;
-?*)
echo "Usage: $0 [-ns|--no-stow] [-bs|bootstrap] [<targets>]"
exit 1
;;
*)
break
esac
done
if [ "$bootstrap" = true ]
then
git clone "$PULL_BASE/bootstrap.git" .dotfiles
cd .dotfiles
git remote set-url origin "$PULL_BASE/bootstrap.git"
2019-01-15 21:51:53 -05:00
git remote set-url --push origin "$PUSH_BASE/bootstrap.git"
else
cd "$(dirname ${BASH_SOURCE[0]})"
fi
if [ "$#" -gt 0 ]
then
2018-10-05 13:57:34 -04:00
targets="$@"
for target in $targets
do
do_get "$target"
done
maybe_stow "$targets"
elif [ "$bootstrap" != true ]
then
2020-04-26 01:47:14 -04:00
do_pull_maybe_quiet .dotfiles .
for dir in */
do
2020-04-26 01:47:14 -04:00
do_pull_maybe_quiet "$dir"
done
maybe_stow "*/"
fi