dot

packages and services management
Log | Files | Refs | README

commit 75af1d82e6730ebf7fc5ff78caf391ddd721af33
parent 6c6a8d3db233c33da93c3ab354ffc7a8240b0b9b
Author: josuah <mail@josuah.net>
Date:   Fri, 30 Dec 2016 00:26:21 +0100

Git.  Marvel.

Diffstat:
M.local/bin/build | 4++--
M.local/bin/version | 148+++++++++++++++++++++++++++++++++++--------------------------------------------
2 files changed, 68 insertions(+), 84 deletions(-)

diff --git a/.local/bin/build b/.local/bin/build @@ -43,14 +43,14 @@ view() error() { - printf '\033[1;31merror\033[0;1m %-12s\033[m %s\n' "$1" "$2" 1>&2 + printf '\033[31merror: \033[1m%-12s \033[m%s\n' "$1" "$2" 1>&2 exit 1 } info() { - printf '\033[1;36minfo\033[0;1m %-12s\033[m %s\n' "$1" "$2" 1>&2 + printf '\033[36minfo: \033[1m%-12s \033[m%s\n' "$1" "$2" 1>&2 } diff --git a/.local/bin/version b/.local/bin/version @@ -1,21 +1,28 @@ #!/bin/sh # dependencies: -# mkdir find sort xargs md5sum tail rm test mv comm sed uniq grep printf +# mkdir find sort xargs md5sum rm test mv comm sed printf tac 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" + fi +} + + usage() { - printf %s "\ -${0##*/} i[nit] create a new version directory in working directory -${0##*/} n[ew] create a new version entry -${0##*/} l[ist] display full history of revisions -${0##*/} d[diff] [R1 [R2]] diff changes between R1 R2 (or latests) revisions -${0##*/} m[erge] TREE merge current tree with foreign TREE -" + 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" m 'm[erge] TREE' 'merge current tree with foreign TREE' exit 1 } @@ -48,13 +55,12 @@ root() do [ "$PWD" = '/' ] && return 1 || cd .. done - pwd + pwd && return 0 } init() { - [ "$1" ] && die "existing .version dir in $1" mkdir -p .version exit 0 @@ -73,7 +79,7 @@ new() if [ -f .version/history ] && - [ "$hash" = "$(tail -n 1 .version/history)" ] + [ "$hash" = "$(sed -n '$p' .version/history)" ] then rm .version/tmp die 'already at latest version' @@ -84,25 +90,24 @@ new() } +# +# 1: 2: root of tree in which look for common revision +# merge() { - [ -d "$1/.version" ] || die "no .version dir in $PWD" - [ -d "$2/.version" ] || die "no .version dir in $2" - [ -f "$1/.version/history" ] || die "no revision in $PWD" - [ -f "$2/.version/history" ] || die "no revision in $2" - # find latest common revision - revision="$( - comm -12 "$1/.version/history" "$2/.version/history" | tail -1 - )" - [ "$revision" ] || die "no revision common to $1 and $2" - - # generate a diff for both trees - for root in "$1" "$2" + for revision in $(tac "$1/.version/history") do - printf '\n\n\n%s\n' "$root" - + common="$(grep "$revision" "$2/.version/history")" && break done + + [ "$common" ] || die "no revision common to $1 and $2" + + # generate a diff for both trees + printf '%s\n\n' "$1" + makediff "$common" '' "$1" + printf '\n\n%s\n\n' "$2" + makediff "$common" '' "$2" } @@ -112,87 +117,66 @@ merge() # makediff() { - printf '\n\n: %s %s\n\n' "$1" "$2" + set -- "${1:-$( + sed '1q;d' "$3/.version/history" + )}" "${2:-$( + sed '2q;d' "$3/.version/history" + )}" "$3" - # d: deleted: neither name nor hash present in the next revision - while read -r hash name - do - ! grep -F "$hash" "$3/.version/$2" > /dev/null && - ! grep -F "$name" "$3/.version/$2" > /dev/null && - printf 'd %s %s\n' "$hash" "$name" - done < "$3/.version/$1" + printf ': %s %s\n\n' "$1" "$2" - # c: created: neither name nor hash present in the previous - while read -r hash name - do - ! grep -F "$hash" "$3/.version/$1" > /dev/null && - ! grep -F "$name" "$3/.version/$1" > /dev/null && - printf 'c %s %s\n' "$hash" "$name" - done < "$3/.version/$2" -} + sort -o "$3/.version/tmp1" "$3/.version/$1" + sort -o "$3/.version/tmp2" "$3/.version/$2" + { + # =: neither name nor hash changed + comm -12 "$3/.version/tmp1" "$3/.version/tmp2" | sed 's/^/= /' -# -# transform create / delete into rename / edit -# -reducediff() -{ - true -} + # +: neither name 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 + comm -31 "$3/.version/tmp1" "$3/.version/tmp2" | sed 's/^/- /' + } | sort -t "$NL" -k 1.$((32 + 6)) -# -# 1: 2: revisions between which generate each diff -# 3: root of the tree for which make a diff -# -tracediff() -{ - [ -d "$3/.version" ] || die "no .version dir in $3" - revision="${1:-$( - tail -n 2 "$3/.version/history" | sed 1q - )}" - - # get all changes since "$1" - revisions="$(cat "$3/.version/history")$NL" - revisions="${revisions##*$revision$NL}" - - # generate a diff of every revision with next one - prev="$revision" - for next in $revisions - do - makediff "$prev" "$next" "$3" - prev="$next" - done + rm -f "$3/.version/tmp1" "$3/.version/tmp2" } main() { - root="$(root)" + case "$1" in ( i | init | '' | * ) true ;; + ( * ) 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" + esac + case "$1" in ( i | init ) - [ $# -ne 1 ] && usage + [ $# -ne 1 ] && usage i init "$root" ;; ( n | new ) - [ $# -ne 1 ] && usage + [ $# -ne 1 ] && usage n new "$root" ;; ( d | diff ) [ $# -gt 2 ] && [ ! -f ".version/$1" ] || [ $# -eq 3 ] && [ ! -f ".version/$2" ] || - [ $# -gt 3 ] && usage - tracediff "$2" "$3" "$root" + [ $# -gt 3 ] && usage d + makediff "$2" "$3" "$root" ;; ( l | list ) - [ $# -gt 1 ] && usage - [ -d "$root/.version" ] || die "no .version dir in $PWD" - [ -f "$root/.version/history" ] || die "no revision in $PWD" - cat "$root/.version/history" + [ $# -gt 1 ] && usage l + less "$root/.version/history" ;; ( m | merge ) - [ $# -ne 2 ] && usage - merge "$(root)" "$2" + [ $# -ne 2 ] && usage m + [ -d "$2/.version" ] || die "no .version dir in $2" + [ -f "$2/.version/history" ] || die "no revision in $2" + merge "$(root)" "$(cd "$2"; pwd)" ;; ( * | '' ) usage @@ -201,4 +185,4 @@ main() } -main "$@" | less -FR~ +main "$@"