dot

packages and services management
Log | Files | Refs | README

commit b64691e33d9ccb4970ee3af07aeb815b1a7e5943
parent 2e0ab5e66ed7a8f8421ac77a96ce47c5131f1326
Author: josuah <mail@josuah.net>
Date:   Fri, 30 Dec 2016 16:56:27 +0100

Fixed diff selection

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

diff --git a/.local/bin/version b/.local/bin/version @@ -1,7 +1,8 @@ -#!/bin/sh +# Simple version control system to store large datasets using hashs # dependencies: -# mkdir find sort xargs md5sum rm test mv comm sed printf rev +# mkdir find sort xargs md5sum rm test mv comm sed printf tac + TAB=' ' NL=' ' @@ -10,7 +11,7 @@ TAB=' ' NL=' opt() { [ -z "${1#$2}" ] && if [ "$1" ] - then printf '%s %s %s\n' "${0##*/}" "$3" "$4" + then printf '%s %s %s\n' "${0##*/}" "$3" "$4" else printf '%s %-20s %s\n' "${0##*/}" "$3" "$4" fi } @@ -21,7 +22,7 @@ usage() 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] [REV]' 'display full history of revisions or REV' - opt "$1" d 'd[diff] [R1] [R2]' 'diff revisions R1 and R2 (or latests)' + opt "$1" d 'd[diff] [OLD NEW]' 'diff revisions OLD and NEW (or latests)' opt "$1" m 'm[erge] TREE' 'merge current tree with foreign TREE' exit 1 } @@ -67,15 +68,19 @@ init() } +# +# 1: root in which generate the new version +# new() { printf 'generating a new revision\n' + cd "$1" || die 'could not go to root of tree' find . -type f $(exclude .git .version) -print0 | sort -z | xargs -0 md5sum > "$1/.version/tmp" - hash="$(md5sum "$1/.version/tmp")" - hash="${hash%% *}" + hash="$(md5sum "$1/.version/tmp")" || die 'could not generate md5sum' + hash="${hash%% *}" if [ -f "$1/.version/history" ] && @@ -106,7 +111,7 @@ new() merge() { # find latest common revision - for revision in $(rev "$1/.version/history") + for revision in $(tac "$1/.version/history") do common="$(grep "$revision" "$2/.version/history")" && break done @@ -122,18 +127,17 @@ merge() # -# 1: 2: revisions for the diff +# 1: old revision to diff +# 2: new revision to diff # 3: root of the tree for which make a diff # makediff() { - set -- "${1:-$( - sed '1q;d' "$3/.version/history" - )}" "${2:-$( - sed '2q;d' "$3/.version/history" - )}" "$3" + IFS="$NL" + [ "$1" ] || set -- $(sed '$!{xd} ${xpx}' "$3/.version/history") "$3" + [ $# -ne 3 ] && die 'can not find the two revisions to diff' - printf ': %s %s\n\n' "$1" "$2" + printf ': %s -> %s\n\n' "$1" "$2" sort -o "$3/.version/tmp1" "$3/.version/rev/$1" sort -o "$3/.version/tmp2" "$3/.version/rev/$2" @@ -142,11 +146,11 @@ makediff() # =: neither path nor hash changed comm -12 "$3/.version/tmp1" "$3/.version/tmp2" | sed 's/^/= /' - # +: neither path nor hash present in the next revision - comm -32 "$3/.version/tmp1" "$3/.version/tmp2" | sed 's/^/+ /' + # -: not present in the next revision + comm -32 "$3/.version/tmp1" "$3/.version/tmp2" | sed 's/^/- /' - # -: neither path nor hash present in the previous revision - comm -31 "$3/.version/tmp1" "$3/.version/tmp2" | sed 's/^/- /' + # +: not present in the previous revision + comm -31 "$3/.version/tmp1" "$3/.version/tmp2" | sed 's/^/+ /' } | sort -t "$NL" -k 1.$((32 + 6)) rm -f "$3/.version/tmp1" "$3/.version/tmp2" @@ -173,8 +177,8 @@ main() new "$root" ;; ( d | diff ) - [ $# -gt 1 ] && [ ! -f ".version/rev/$1" ] || - [ $# -gt 2 ] && [ ! -f ".version/rev/$2" ] || + [ $# -gt 1 ] && [ ! -f "$root/.version/rev/$2" ] && usage d + [ $# -gt 1 ] && [ ! -f "$root/.version/rev/$3" ] && usage d [ $# -gt 3 ] && usage d makediff "$2" "$3" "$root" ;;