diff options
author | EuAndreh <eu@euandre.org> | 2022-10-27 22:54:01 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2022-10-27 22:54:01 -0300 |
commit | be97a9ef4d4205bed7b9687129d32f1009a6da6c (patch) | |
tree | afd57d3fb75acaa58b942c737338b52a764fe8c3 | |
parent | bin/repos: Add explicit note on intentionally commented code (diff) | |
download | dotfiles-be97a9ef4d4205bed7b9687129d32f1009a6da6c.tar.gz dotfiles-be97a9ef4d4205bed7b9687129d32f1009a6da6c.tar.xz |
bin/vcs: Do not require TYPE, and guess it instead
-rwxr-xr-x | bin/vcs | 225 |
1 files changed, 188 insertions, 37 deletions
@@ -2,10 +2,162 @@ set -eu +TYPES=' +git +mercurial +bitkeeper +darcs +cvs +fossil +' + + +guess_type() { + if [ -e "$1"/.git ]; then + echo git + elif [ -e "$1"/.hg ]; then + echo mercurial + elif [ -e "$1"/.bk ]; then + echo bitkeeper + elif [ -e "$1"/_darcs ]; then + echo darcs + elif [ -e "$1"/CVS/ ]; then + echo cvs + elif [ -e "$1"/.fslckout ]; then + echo fossil + else + return 1 + fi +} + + + + +git_fetch() { + git fetch "$@" +} + +darcs_fetch() { + darcs fetch "$@" +} + +mercurial_fetch() { + hg pull "$@" +} + +fossil_fetch() { + fossil pull "$@" +} + +cvs_fetch() { + timeout 300 cvs update "$@" +} + +git_status() { + git status "$@" +} + +git_diff() { + git diff "$@" +} + +git_ps1() { + BRANCH_NAME="$(git rev-parse --abbrev-ref HEAD)" + OUT="$(git status --short --branch --porcelain)" + BRANCH_LINE="$(echo "$OUT" | head -n 1)" + DIFF_LINES="$(echo "$OUT" | tail -n +2)" + + IS_AHEAD=false + IS_BEHIND=false + if echo "$BRANCH_LINE" | grep -q 'ahead'; then + IS_AHEAD=true + fi + if echo "$BRANCH_LINE" | grep -q 'behind'; then + IS_BEHIND=true + fi + + LINE='' + + if [ "$IS_AHEAD" = true ] && [ "$IS_BEHIND" = true ]; then + LINE="^^^ $BRANCH_NAME vvv" + elif [ "$IS_AHEAD" = true ]; then + LINE="^ $BRANCH_NAME ^" + elif [ "$IS_BEHIND" = true ]; then + LINE="v $BRANCH_NAME v" + else + LINE="$BRANCH_NAME" + fi + + HAS_DIFF=false + HAS_UNTRACKED=false + if echo "$DIFF_LINES" | grep -q '^[A|D|M| ][M|D| ]'; then + HAS_DIFF=true + fi + if echo "$DIFF_LINES" | grep -q '^[?][?]'; then + HAS_UNTRACKED=true + fi + + if [ "$HAS_DIFF" = true ]; then + COLOR_FN=redb + LINE="{$LINE}" + elif [ "$IS_AHEAD" = true ] || [ "$IS_BEHIND" = true ]; then + COLOR_FN=bluei + LINE="[$LINE]" + elif [ "$HAS_UNTRACKED" = true ]; then + COLOR_FN=lightblue + LINE="{$LINE}" + else + COLOR_FN=green + LINE="($LINE)" + fi + + color -c "$COLOR_FN" "$LINE" + + BRANCH_COUNT="$(git branch --list | wc -l)" + if [ "$BRANCH_COUNT" -gt 1 ]; then + color -c lightblue "<$BRANCH_COUNT>" + fi + + STASH_COUNT="$(git stash list | wc -l)" + if [ "$STASH_COUNT" != 0 ]; then + color -c red "*$STASH_COUNT" + fi + + color -c blacki " - git/$(git rev-parse HEAD)" +} + +fossil_ps1() { + BRANCH_NAME="$(fossil branch current)" + + if [ -n "$(fossil extras)" ]; then + HAS_UNTRACKED=1 + fi + + BRANCH_MARKER="$BRANCH_NAME" + + if [ -n "${HAS_UNTRACKED:-}" ]; then + COLOR_FN=lightblue + LINE="($BRANCH_MARKER)" + else + COLOR_FN=green + LINE="($BRANCH_MARKER)" + fi + + color -c "$COLOR_FN" "$LINE" + + color -c blacki " - fossil/$(fossil info | awk '/^checkout:/ { print $2 }')" +} + +mercurial_ps1() { + BRANCH_NAME="$(hg branch)" +} + + usage() { cat <<-'EOF' Usage: - vcs TYPE ACTION + vcs [-C DIR] ACTION + vcs [-C DIR] -l|-t vcs -h EOF } @@ -15,16 +167,14 @@ help() { Options: + -C DIR change to DIR instead of PWD + -l list the supported VCS types + -t show the guesses VCS type -h, --help show this message - TYPE the type of the underlying VCS: - - git - - darcs - - mercurial - - fossil - - cvs ACTION the action to be performed on the repository: - fetch + - ps1 EOF } @@ -44,8 +194,23 @@ for flag in "$@"; do esac done -while getopts 'h' flag; do +while getopts 'C:lth' flag; do case "$flag" in + C) + cd "$OPTARG" + ;; + l) + + echo $TYPES | tr ' ' '\n' + exit + ;; + t) + guess_type "$PWD" || { + printf 'Could not guess the type of the repository.\n' >&2 + exit 2 + } + exit + ;; h) usage help @@ -58,39 +223,25 @@ while getopts 'h' flag; do done shift $((OPTIND - 1)) - -git_fetch() { - git fetch -} - -darcs_fetch() { - darcs fetch -} - -mercurial_fetch() { - hg pull -} - -fossil_fetch() { - fossil pull -} - -cvs_fetch() { - timeout 300 cvs update -} - - -VCS="${1:-}" -ACTION="${2:-}" - -eval "$(assert-arg "$VCS" 'VCS')" +ACTION="${1:-}" eval "$(assert-arg "$ACTION" 'ACTION')" +shift +if [ "${1:-}" = '--' ]; then + shift +fi + +TYPE="$(guess_type "$PWD")" +if [ -z "$TYPE" ]; then + printf 'Could not guess the type of the repository.\n' >&2 + exit 2 +fi -CMD="$VCS"_"$ACTION" +CMD="$TYPE"_"$ACTION" if ! command -v "$CMD" >/dev/null; then - printf 'Invalid VCS/action combination: %s %s.\n' "$VCS" "$ACTION" >&2 + printf 'Invalid ACTION: "%s".\n\n' "$ACTION" >&2 + usage >&2 exit 2 fi -"$CMD" +"$CMD" "$@" |