mdisk: Use color, red for error messages and green for success
[sunny256-utils.git] / ga-rdiff
blob1d3a80dcf1a648d816635dc8d8ae9fdfc36c0ac6
1 #!/bin/sh
3 #==============================================================================
4 # ga-rdiff
5 # File ID: 6317a006-78d9-11ea-8dd3-4f45262dc9b5
7 # Author: Øyvind A. Holm <sunny@sunbase.org>
8 # License: GNU General Public License version 2 or later.
9 #==============================================================================
11 progname=ga-rdiff
12 VERSION=0.3.1
14 std_block_size=2048
16 opt_block_size=$std_block_size
17 opt_help=0
18 opt_old_file=''
19 opt_quiet=0
20 opt_verbose=0
21 opt_whole=0
22 while test -n "$1"; do
23 case "$1" in
24 -b|--block-size) opt_block_size="$2"; shift 2 ;;
25 -h|--help) opt_help=1; shift ;;
26 -o|--old-file) opt_old_file="$2"; shift 2 ;;
27 -q|--quiet) opt_quiet=$(($opt_quiet + 1)); shift ;;
28 -v|--verbose) opt_verbose=$(($opt_verbose + 1)); shift ;;
29 -w|--whole) opt_whole=1; shift ;;
30 --version) echo $progname $VERSION; exit 0 ;;
31 --) shift; break ;;
33 if printf '%s\n' "$1" | grep -q ^-; then
34 echo "$progname: $1: Unknown option" >&2
35 exit 1
36 else
37 break
39 break ;;
40 esac
41 done
42 opt_verbose=$(($opt_verbose - $opt_quiet))
44 if test "$opt_help" = "1"; then
45 test $opt_verbose -gt 0 && { echo; echo $progname $VERSION; }
46 cat <<END
48 Create a rdiff(1) file with differences between the previous and
49 current version of a file stored in git-annex.
51 Usage: $progname [options] ANNEX_FILE RDIFF_FILE
53 Options:
55 -b SIZE, --block-size SIZE
56 Use SIZE as block size with "rdiff signature". Smaller size may
57 create a smaller rdiff file, but takes longer time.
58 Default value: $std_block_size
59 -h, --help
60 Show this help.
61 -o FILE, --old-file FILE
62 Use FILE instead of previous version as base for the patch.
63 -q, --quiet
64 Be more quiet. Can be repeated to increase silence.
65 -v, --verbose
66 Increase level of verbosity. Can be repeated.
67 -w, --whole
68 Create rdiff patch for the whole file against /dev/null if no
69 previous version of the file was found.
70 --version
71 Print version information.
73 For example, to create rdiff files for all files in the current
74 directory tree:
76 \$ git annex find | while read f; do ga-rdiff "\$f" "\$f.rdiff"; done
78 Does not work with files renamed or moved in the newest revision.
80 END
81 exit 0
84 create_rdiff() {
85 local src="$1" dest="$2"
87 repotop="$(git rev-parse --show-toplevel)"
88 if [ -z "$repotop" ]; then
89 echo "$progname: Could not find top of Git repo" >&2
90 return 1
93 local currfile="$(realpath -e "$src")"
94 if [ -z "$currfile" ]; then
95 echo -n "$progname: $src: " >&2
96 echo "Could not find annex data for current revision" >&2
97 return 1
100 local prevfile="$opt_old_file"
101 if [ -z "$opt_old_file" ]; then
102 prevfile="$(git log -1 -p --color=never "$src" \
103 | grep ^- \
104 | grep -F .git/annex/objects/ \
105 | sed 's/^.*\.git/.git/')"
106 prevfile="$(cd "$repotop" && test -e "$prevfile" \
107 && realpath -e "$prevfile")"
108 if [ -z "$prevfile" ]; then
109 if [ "$opt_whole" = "1" ]; then
110 prevfile=/dev/null
111 else
112 echo -n "$progname: $src: Could not find " >&2
113 echo "annex data for previous revision" >&2
114 return 1
119 rdiff -b $opt_block_size signature "$prevfile" \
120 | rdiff delta - "$currfile" "$dest"
123 if [ -z "$2" ]; then
124 echo "$progname: Need src and dest command line arguments" >&2
125 exit 1
128 create_rdiff "$1" "$2"
130 # vim: set ts=8 sw=8 sts=8 noet fo+=w tw=79 fenc=UTF-8 :