set -e # handle base args for arg in "$@"; do case "$arg" in -v|--verbose) VERBOSE=1 ;; -d|--dry-run) DRY_RUN=1 ;; esac done [ -n "$VERBOSE" ] && VERBOSE_ARG=--verbose run() { [ -n "$DRY_RUN" ] && echo "DRY RUN:" $@ || $@; } # define base functions clonemissing() { url="$1" target="$2" flags="$3" #branch="$3" # return if already existing if [ -d "${target}/.git" ]; then return fi # clone url to target echo "cloning $url to $target" run mkdir -p $VERBOSE_ARG "$target" run git clone $VERBOSE_ARG $flags "$url" "$target" } pull() { target="$1" flags="$2" cd "$target" # check if local changes exist before running pull if ! (git diff --quiet && git diff --cached --quiet); then echo "$target: local changes exist. won't attempt to pull" return fi # pull changes echo "$target: pulling latest changes" run git pull $VERBOSE_ARG $flags } remoteadd() { target="$1" remote="$2" url="$3" cd "$target" # check if remote exists remoteurl="$(git $VERBOSE_ARG remote get-url "$remote" 2>/dev/null)" if [ $? = 0 ]; then # verify remote url if [ "$remoteurl" = "$url" ]; then return fi echo "$target: the url of remote $remote ($remoteurl) does not match the expected value ($url)" return fi # add remote echo "$target: adding remote $remote ($remoteurl)" run git remote $VERBOSE_ARG add "$remote" "$url" } stowrepo() { dir="$1" target="$2" packages="$3" cd "$dir" echo "$target: stowing packages ($packages) to $target" run stow $VERBOSE_ARG --restow --target="$target" $packages } # define repo specific sync funcs