dot

packages and services management
Log | Files | Refs | README

commit 2e0ab5e66ed7a8f8421ac77a96ce47c5131f1326
parent 75af1d82e6730ebf7fc5ff78caf391ddd721af33
Author: josuah <mail@josuah.net>
Date:   Fri, 30 Dec 2016 01:37:14 +0100

Working initial version of 'version' script

Diffstat:
M.local/bin/version | 80+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
M.profile | 2+-
2 files changed, 47 insertions(+), 35 deletions(-)

diff --git a/.local/bin/version b/.local/bin/version @@ -1,7 +1,7 @@ #!/bin/sh # dependencies: -# mkdir find sort xargs md5sum rm test mv comm sed printf tac +# mkdir find sort xargs md5sum rm test mv comm sed printf rev TAB=' ' NL=' ' @@ -10,18 +10,18 @@ TAB=' ' NL=' opt() { [ -z "${1#$2}" ] && if [ "$1" ] - then printf '%s %s %s\n' "${0##*/}" "$3" "$4" - else printf '%s %-16s %s\n' "${0##*/}" "$3" "$4" + then printf '%s %s %s\n' "${0##*/}" "$3" "$4" + else printf '%s %-20s %s\n' "${0##*/}" "$3" "$4" fi } usage() { - opt "$1" i 'i[nit]' 'new ".version" directory in working directory' + opt "$1" i 'i[nit]' 'new "./.version/" directory in working directory' opt "$1" n 'n[ew]' 'new version entry saving all tree content' - opt "$1" l 'l[ist]' 'display full history of revisions' - opt "$1" d 'd[diff] [1] [2]' 'diff revisions 1 and 2 (or latests)' + opt "$1" l 'l[ist] [REV]' 'display full history of revisions or REV' + opt "$1" d 'd[diff] [R1] [R2]' 'diff revisions R1 and R2 (or latests)' opt "$1" m 'm[erge] TREE' 'merge current tree with foreign TREE' exit 1 } @@ -47,7 +47,7 @@ exclude() # -# Find the root ".version" dir if any or return error. +# Find the root "./.version/" dir if any or return error. # root() { @@ -61,32 +61,42 @@ root() init() { - [ "$1" ] && die "existing .version dir in $1" - mkdir -p .version + [ "$1" ] && die "existing ./.version/ dir in $1" + mkdir -p .version/obj .version/rev exit 0 } new() { - cd "$1" && [ -d .version ] || die "no .version dir in $PWD" + printf 'generating a new revision\n' find . -type f $(exclude .git .version) -print0 | sort -z | - xargs -0 md5sum > .version/tmp + xargs -0 md5sum > "$1/.version/tmp" - hash="$(md5sum .version/tmp)" + hash="$(md5sum "$1/.version/tmp")" hash="${hash%% *}" if - [ -f .version/history ] && - [ "$hash" = "$(sed -n '$p' .version/history)" ] + [ -f "$1/.version/history" ] && + [ "$hash" = "$(sed -n '$p' "$1/.version/history")" ] then - rm .version/tmp + rm "$1/.version/tmp" die 'already at latest version' fi - printf '%s\n' "$hash" >> .version/history - mv .version/tmp ".version/$hash" + printf '%s\n' "$hash" >> "$1/.version/history" + mv "$1/.version/tmp" "$1/.version/rev/$hash" + + printf 'saving content in ./.version/obj/\n' + + while read -r hash path + do + if [ ! -f "$1/.version/obj/$hash" ] + then + cp -f "$1/$path" "$1/.version/obj/$hash" + fi + done < "$1/.version/rev/$hash" } @@ -96,7 +106,7 @@ new() merge() { # find latest common revision - for revision in $(tac "$1/.version/history") + for revision in $(rev "$1/.version/history") do common="$(grep "$revision" "$2/.version/history")" && break done @@ -125,17 +135,17 @@ makediff() printf ': %s %s\n\n' "$1" "$2" - sort -o "$3/.version/tmp1" "$3/.version/$1" - sort -o "$3/.version/tmp2" "$3/.version/$2" + sort -o "$3/.version/tmp1" "$3/.version/rev/$1" + sort -o "$3/.version/tmp2" "$3/.version/rev/$2" { - # =: neither name nor hash changed + # =: neither path nor hash changed comm -12 "$3/.version/tmp1" "$3/.version/tmp2" | sed 's/^/= /' - # +: neither name nor hash present in the next revision + # +: neither path nor hash present in the next revision comm -32 "$3/.version/tmp1" "$3/.version/tmp2" | sed 's/^/+ /' - # -: neither name nor hash present in the previous revision + # -: neither path nor hash present in the previous revision comm -31 "$3/.version/tmp1" "$3/.version/tmp2" | sed 's/^/- /' } | sort -t "$NL" -k 1.$((32 + 6)) @@ -145,36 +155,38 @@ makediff() main() { - case "$1" in ( i | init | '' | * ) true ;; - ( * ) root="$(root)" || die "no .version dir in $PWD" ;; + case "$1" in ( n | new | d | diff | l | list | m | merge ) + root="$(root)" || die "no ./.version/ dir in $PWD" esac - case "$1" in ( i | init | d | diff | l | list | m | merge | * ) true ;; - ( * ) [ -f "$root/.version/history" ] || die "$no revision in $root" + case "$1" in ( d | diff | l | list | m | merge ) + [ -f "$root/.version/history" ] || die "no revision in $root" esac case "$1" in ( i | init ) [ $# -ne 1 ] && usage i - init "$root" + init "$(root)" ;; - ( n | new ) + ( n | new ) [ $# -ne 1 ] && usage n new "$root" ;; ( d | diff ) - [ $# -gt 2 ] && [ ! -f ".version/$1" ] || - [ $# -eq 3 ] && [ ! -f ".version/$2" ] || + [ $# -gt 1 ] && [ ! -f ".version/rev/$1" ] || + [ $# -gt 2 ] && [ ! -f ".version/rev/$2" ] || [ $# -gt 3 ] && usage d makediff "$2" "$3" "$root" ;; ( l | list ) - [ $# -gt 1 ] && usage l - less "$root/.version/history" + [ $# -gt 2 ] && usage l + [ $# -eq 1 ] && less -F "$root/.version/history" + [ $# -eq 2 ] && [ -f "$root/.version/rev/$2" ] && + less -F "$root/.version/rev/$2" ;; ( m | merge ) [ $# -ne 2 ] && usage m - [ -d "$2/.version" ] || die "no .version dir in $2" + [ -d "$2/.version" ] || die "no ./.version/ dir in $2" [ -f "$2/.version/history" ] || die "no revision in $2" merge "$(root)" "$(cd "$2"; pwd)" ;; diff --git a/.profile b/.profile @@ -165,7 +165,7 @@ main() { if [ "$TERM" = 'linux' ] then - setfont ~/.local/share/consolefonts/tamsyn-16n.psf + setfont ~/.local/share/consolefonts/terminus-16n.psf fi [ "$ABDUCO" ] || TERM='screen' ABDUCO='dvtm' abduco -A dvtm dvtm-status