2015-09-05 13:11:10 -04:00
|
|
|
#!/bin/bash
|
|
|
|
|
2018-10-05 12:08:35 -04:00
|
|
|
set -eu
|
2015-09-06 10:30:05 -04:00
|
|
|
|
2020-09-23 00:12:35 -04:00
|
|
|
PULL_BASE="https://git.adamgoldsmith.name"
|
|
|
|
DOTFILES_PULL_BASE="${PULL_BASE}/dotfiles"
|
|
|
|
BOOTSTRAP_URL="${PULL_BASE}/adam/bootstrap.git"
|
|
|
|
|
|
|
|
function sync_urls() {
|
|
|
|
cd "$(dirname ${BASH_SOURCE[0]})"
|
|
|
|
git remote set-url origin "$BOOTSTRAP_URL"
|
|
|
|
git remote set-url --push --delete origin '.*' || true
|
|
|
|
|
|
|
|
for dir in */
|
|
|
|
do
|
|
|
|
git -C "$dir" remote set-url origin "$DOTFILES_PULL_BASE/${dir%/}.git"
|
|
|
|
git -C "$dir" remote set-url --push --delete origin '.*' || true
|
|
|
|
done
|
|
|
|
}
|
2018-10-05 12:08:35 -04:00
|
|
|
|
|
|
|
function do_get() {
|
|
|
|
target="$1"
|
|
|
|
if [ -d "$target" ]
|
2015-09-05 13:11:10 -04:00
|
|
|
then
|
2018-10-05 12:08:35 -04:00
|
|
|
echo "$target already exists, not getting"
|
2015-09-05 13:11:10 -04:00
|
|
|
else
|
2018-10-05 12:08:35 -04:00
|
|
|
echo "Getting $target"
|
2020-09-23 00:12:35 -04:00
|
|
|
git clone "$DOTFILES_PULL_BASE/$target.git" --recurse-submodules
|
2015-09-05 13:11:10 -04:00
|
|
|
fi
|
2018-10-05 12:08:35 -04:00
|
|
|
}
|
|
|
|
|
2019-01-15 21:46:37 -05:00
|
|
|
function do_pull() {
|
2020-04-26 01:47:14 -04:00
|
|
|
dir="$1"
|
2019-01-15 21:46:37 -05:00
|
|
|
if [ "$stashAndSync" = true ]
|
|
|
|
then
|
2019-01-15 22:06:11 -05:00
|
|
|
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)"
|
2023-04-15 21:47:21 -04:00
|
|
|
first_line="${git_out%%$'\n'*}"
|
|
|
|
if [[ "$quiet" != true || "$first_line" != "Already up to date." ]]
|
2020-04-26 01:47:14 -04:00
|
|
|
then
|
2023-04-15 21:47:21 -04:00
|
|
|
echo "$(tput setaf 4)}$(tput sgr0) $(tput bold)$name$(tput sgr0): $first_line"
|
|
|
|
fi
|
|
|
|
if [[ $first_line == Updating* ]]
|
|
|
|
then
|
|
|
|
git -C "${dir}" log --graph --color --format='%C(yellow)%h%C(reset) %s %C(cyan)(%cr)%C(reset)' ${first_line##Updating } \
|
|
|
|
| sed 's/^/ /'
|
2019-01-15 21:46:37 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2018-10-05 12:08:35 -04:00
|
|
|
function run_script() {
|
|
|
|
script="$1"; shift
|
|
|
|
targets="$@"
|
|
|
|
for target in $targets
|
|
|
|
do
|
|
|
|
if [ -e "$target/$script" ]
|
|
|
|
then
|
2023-04-15 21:50:16 -04:00
|
|
|
[[ "$quiet" == true ]] || echo "Running $target/$script"
|
2020-04-26 01:47:14 -04:00
|
|
|
"$target/$script" ${quiet:+-q}
|
2018-10-05 12:08:35 -04:00
|
|
|
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
|
2018-10-05 12:08:35 -04:00
|
|
|
stow=true
|
|
|
|
bootstrap=false
|
2019-01-15 21:46:37 -05:00
|
|
|
stashAndSync=false
|
2018-10-05 12:08:35 -04:00
|
|
|
while [ "$#" -gt 0 ]
|
|
|
|
do
|
|
|
|
case $1 in
|
|
|
|
-ns | --no-stow)
|
|
|
|
stow=false
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-bs | --bootstrap)
|
|
|
|
bootstrap=true
|
|
|
|
shift
|
|
|
|
;;
|
2019-01-15 21:46:37 -05:00
|
|
|
-s | --stash-and-sync)
|
|
|
|
stashAndSync=true
|
|
|
|
shift
|
|
|
|
;;
|
2020-04-26 01:47:14 -04:00
|
|
|
-q | --quiet)
|
|
|
|
quiet=true
|
|
|
|
shift
|
|
|
|
;;
|
2020-09-23 00:12:35 -04:00
|
|
|
--sync-urls)
|
|
|
|
sync_urls
|
|
|
|
exit
|
|
|
|
;;
|
2018-10-05 12:08:35 -04:00
|
|
|
--)
|
|
|
|
shift
|
|
|
|
break
|
|
|
|
;;
|
|
|
|
-?*)
|
|
|
|
echo "Usage: $0 [-ns|--no-stow] [-bs|bootstrap] [<targets>]"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
break
|
|
|
|
esac
|
2015-09-05 13:11:10 -04:00
|
|
|
done
|
2015-09-06 10:30:05 -04:00
|
|
|
|
2018-10-05 12:08:35 -04:00
|
|
|
if [ "$bootstrap" = true ]
|
|
|
|
then
|
2020-09-23 00:12:35 -04:00
|
|
|
git clone "$BOOTSTRAP_URL" .dotfiles
|
2018-10-05 12:08:35 -04:00
|
|
|
cd .dotfiles
|
2020-09-23 00:12:35 -04:00
|
|
|
git remote set-url origin "$BOOTSTRAP_URL"
|
2018-10-05 13:41:51 -04:00
|
|
|
else
|
|
|
|
cd "$(dirname ${BASH_SOURCE[0]})"
|
2018-10-05 12:08:35 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$#" -gt 0 ]
|
2015-09-06 10:30:05 -04:00
|
|
|
then
|
2018-10-05 13:57:34 -04:00
|
|
|
targets="$@"
|
|
|
|
for target in $targets
|
|
|
|
do
|
|
|
|
do_get "$target"
|
|
|
|
done
|
|
|
|
maybe_stow "$targets"
|
2018-10-05 12:08:35 -04:00
|
|
|
elif [ "$bootstrap" != true ]
|
|
|
|
then
|
2020-04-26 01:47:14 -04:00
|
|
|
do_pull_maybe_quiet .dotfiles .
|
2018-10-05 12:08:35 -04:00
|
|
|
|
2023-04-15 21:50:16 -04:00
|
|
|
targets="$(find * -maxdepth 0 -type d -execdir test -e "{}/.git" \; -print)"
|
|
|
|
|
|
|
|
for dir in $targets
|
2018-10-05 12:08:35 -04:00
|
|
|
do
|
2020-04-26 01:47:14 -04:00
|
|
|
do_pull_maybe_quiet "$dir"
|
2018-10-05 12:08:35 -04:00
|
|
|
done
|
|
|
|
|
2023-04-15 21:50:16 -04:00
|
|
|
maybe_stow $targets
|
2015-09-06 10:30:05 -04:00
|
|
|
fi
|