dot

packages and services management
Log | Files | Refs | README

commit 3abfc04a257dc5a83fbcffa7c51d837b39e50ab4
parent 9d77f83506aa41223d6c2ce1de643a9ebc252b66
Author: josuah <mail@josuah.net>
Date:   Sat, 31 Dec 2016 13:05:01 +0100

Simplified "new()"

Diffstat:
M.local/bin/version | 45++++++++++++++++++++-------------------------
1 file changed, 20 insertions(+), 25 deletions(-)

diff --git a/.local/bin/version b/.local/bin/version @@ -9,6 +9,12 @@ NL=' ' +die() +{ + printf "\033[1;31m%s\033[m\n" "$1"; exit 1 +} + + opt() { [ -z "${1#$2}" ] && if [ "$1" ] @@ -29,11 +35,6 @@ usage() } -die() -{ - printf '\033[31m%s: \033[1m%s\033[m\n' "${0##*/}" "$*" - exit 1 -} # Generate exclude rules for find(1) for each arguments @@ -50,7 +51,7 @@ init() { pwd="$PWD/" while pwd="${pwd%/*}" && [ "$pwd" ] - do [ -d "$pwd/.version" ] && die "already in a version tree" + do [ -d "$pwd/.version" ] && die 'already in a version tree' done mkdir -p .version/obj .version/rev @@ -59,24 +60,18 @@ init() new() { - printf 'generating a new revision\n' - find . -type f $(exclude .git .version) -print0 | sort -z | xargs -0 md5sum > .version/tmp hash="$(md5sum .version/tmp)" || die 'could not generate hash' - hash="${hash%% *}" + hash="${hash%% *}" old="$(sed '$qd' .version/history)" + printf '1 %s\n2 %s\n' "$old" "$hash" mv -f .version/tmp ".version/rev/$hash" - [ -f .version/history ] && - [ "$hash" = "$(sed '$qd' .version/history)" ] && - die 'already at latest version' - + [ -f .version/history ] && [ "$hash" = "$old" ] || printf '%s\n' "$hash" >> .version/history - printf 'saving content in .version/obj\n' - while read -r hash path do [ ! -f ".version/obj/$hash" ] && cp -f "$path" ".version/obj/$hash" done < ".version/rev/$hash" @@ -89,7 +84,7 @@ new() import() { - printf 'importing objects from "%s" to "%s"\n' "$1" "$PWD" + :: "importing objects from \"$1\" to \"$PWD\"" find "$1/.version/obj" -type f | while IFS='' read -r path do [ -f "${path#$1/}" ] || cp "$path" "${path#$1/}" done @@ -98,10 +93,10 @@ import() for revision in $(tac "$1/.version/history") do common="$(grep "$revision" "$2/.version/history")" && break done - [ "$common" ] || die "no revision common to $1 and $2" + [ "$common" ] || die "no revision common to \"$1\" and \"$2\"" # TODO: start editor to edit diff, so that user can choose what to apply - makediff "$common" + difftree "$common" } @@ -117,14 +112,14 @@ merge() # 1: old revision to diff # 2: new revision to diff -makediff() +difftree() { IFS="$NL" [ "$1" ] || set -- $(sed '$!{xd} ${xpx}' ".version/history") [ $# -ne 2 ] && die 'can not find the two revisions to diff' unset IFS - printf ': %s -> %s\n\n' "$1" "$2" + printf '1 %s\n2 %s\n\n' "$1" "$2" sort -o .version/tmp1 ".version/rev/$1" sort -o .version/tmp2 ".version/rev/$2" @@ -141,11 +136,11 @@ makediff() main() { case "$1" in ( n | new | d | diff | l | list | m | merge ) - [ -d .version ] || die "no .version dir in $PWD" + [ -d .version ] || die "no .version dir in \"$PWD\"" esac case "$1" in ( d | diff | l | list | m | merge ) - [ -f ".version/history" ] || die "no revision in $PWD" + [ -f ".version/history" ] || die "no revision in \"$PWD\"" esac case "$1" in @@ -161,7 +156,7 @@ main() [ $# -gt 1 ] && [ ! -f ".version/rev/$2" ] && usage d [ $# -gt 1 ] && [ ! -f ".version/rev/$3" ] && usage d [ $# -gt 3 ] && usage d - makediff "$2" "$3" + difftree "$2" "$3" ;; ( l | list ) [ $# -gt 2 ] && usage l @@ -171,8 +166,8 @@ main() ;; ( m | merge ) [ $# -lt 2 ] && [ $# -gt 3 ] && usage m - [ -d "$2/.version" ] || die "no .version dir in $2" - [ -f "$2/.version/history" ] || die "no revision in $2" + [ -d "$2/.version" ] || die "no .version dir in \"$2\"" + [ -f "$2/.version/history" ] || die "no revision in \"$2\"" merge "$(cd "$1" || exit 1; pwd)" "$(cd "$2" || exit 1; pwd)" ;; ( * | '' )