Debian Codegraph @

bin/del

raw
1 #!/usr/bin/perl -w
2
3 # vv : visual versioning
4 # zz : generic shell clip board
5 # ezz : clip board editor
6 # del : (un)delete
7 #
8 # http://fex.belwue.de/fstools/vv.html
9 # http://fex.belwue.de/fstools/zz.html
10 # http://fex.belwue.de/fstools/del.html
11 #
12 # by Ulli Horlacher <[email protected]>
13 #
14 # Perl Artistic Licence
15
16 # vv is a script to handle file versions:
17 # list, view, recover, diff, purge, migrate, save, delete
18 #
19 # vv is an extension to emacs idea of backup~ files
20 #
21 # vv does version rotating, the newest version is always #1
22 #
23 # Versions are stored in local subdirectory .versions/
24 #
25 # You can integrate vv into your editor, so file versions will be automatically
26 # created!
27 #
28 # To integrate vv into vim, add to $HOME/.vimrc:
29 #
30 # autocmd BufWritePre * execute '! vv -qs ' . shellescape(@%)
31 # autocmd BufWritePost * execute '! vv -qs ' . shellescape(@%)
32 #
33 # To integrate vv into emacs, add to $HOME/.emacs:
34 #
35 # (add-hook 'before-save-hook (lambda () (shell-command (
36 # concat "vv -qs " (shell-quote-argument (buffer-file-name))))))
37 # (add-hook 'after-save-hook (lambda () (shell-command (
38 # concat "vv -qs " (shell-quote-argument (buffer-file-name))))))
39 # (setq make-backup-files nil)
40 #
41 # To integrate vv into jed, see http://fex.belwue.de/sw/share/jedlib/vv.sl
42 #
43 # To use vv with ANY editor, first set:
44 #
45 # export EDITOR=your_favourite_editor
46 # alias ve='vv -e'
47 #
48 # and then edit your file with:
49 #
50 # ve file
51 #
52 # $HOME/.vvrc is the config file for vv and will be created and filled with
53 # default values, if it does not exist.
54 #
55 # vv contains also del, zz and ezz. To install them, call: perl vv -I
56
57 # del needs GNU cp and find
58
59 # 2013-04-15 initial version
60 # 2013-04-16 added options -m and -v
61 # 2013-04-18 added option -s
62 # 2013-04-22 realfilename() fixes symlink problematics
63 # 2013-04-22 use rsync instead of cp
64 # 2013-04-23 added option -I
65 # 2013-04-23 renamed from jedv to vv
66 # 2013-04-24 added options -e -0
67 # 2013-05-09 added option -R
68 # 2013-05-22 modified option -d to double argument
69 # 2013-05-22 added vvrc with $exclude and @diff
70 # 2013-07-05 fixed bug potential endless loop in rotate()
71 # 2014-04-16 added change-file-test for opt_s (needs .versions/$file)
72 # 2014-04-18 added option -b : save backup
73 # 2014-05-02 fixed bug wrong file ownership when using as root
74 # 2014-06-18 options -r -d -v : parameter is optional, default is 1
75 # 2014-06-18 fixed (stupid!) bug option -s does only sometimes saving
76 # 2014-06-20 options -d -v : argument is optional, default is last file
77 # 2014-07-22 fixed bug no (new) backup version 0 on option -r
78 # 2014-11-14 added option -D : delete last saved version
79 # 2014-11-14 make .versions/ mode 777 if parent directory is world writable
80 # 2015-03-19 allow write access by root even if file and .versions/ have different owners
81 # 2015-03-20 better error formating for jed
82 # 2015-06-02 added option -r . : restore backup
83 # 2016-03-07 added options -M -L : 10 or 100 versions
84 # 2016-03-08 renamed option -I to -H
85 # 2016-05-02 call rsync -A to preserve ACLs
86 # 2016-06-07 option -v : use PAGER=cat if STDOUT is not a tty
87 # 2016-06-08 added zz, ezz and installer vvzz
88 # 2016-07-06 avoid empty $ZZ versioning
89 # 2016-09-12 added vv option -q : quiet mode
90 # 2016-11-29 show bytes up to 9999999, then M or G
91 # 2016-11-29 added vv option -P : delete all versions
92 # 2016-11-29 support for rsync without -A option
93 # 2016-11-30 fixed bug vv option -r breaks hard links
94 # 2016-11-30 added version number/range argument to vv option -D
95 # 2016-12-03 installer creates symbolic links, no more hard links
96 # 2016-12-03 handle backup as regular version: vv option -b acts like -s
97 # 2016-12-04 added del
98 # 2016-12-04 added vv option -I : installation
99 # 2016-12-04 added vv option -N : set number of versions
100 # 2016-12-04 removed vv options -M -L
101 # 2016-12-05 added vv option -U : update
102 # 2016-12-06 fixed bug cannot handle files with meta characters
103 # 2016-12-07 added vv option -V : show programm version and history
104 # 2016-12-08 better (more secure) directory handling for del
105 # 2016-12-08 added del option -f : force deletion of directories
106 # 2016-12-08 added del option -D : real delete
107 # 2016-12-08 modified del option -p : purge named file(s)
108 # 2016-12-08 modified del option -P : purge all file(s)
109 # 2016-12-08 modified vv option -v : verbose
110 # 2016-12-14 recognize identical files on vv option -r
111 # 2016-12-15 modified option -V : show history only with -v
112 # 2017-02-08 added vv option -f : force = ignore exclude lists
113 # 2017-02-23 modified vv option -r : number is optional
114 # 2017-02-24 ask user for missing version number on vv -r or vv -d
115 # 2017-10-26 added option -D .
116 # 2018-06-08 added ezz options -1 ... -9
117 # 2018-06-10 reverse list if there are more than 20 versions and STDOUT is a tty
118 # 2018-08-28 fixed bug cannot edit new file with vv -e
119 # 2018-11-30 ezz STDIN is /dev/tty for $EDITOR
120 # 2018-12-17 del DIRECTORY does not remove existing .del/DIRECTORY, but appends
121 # 2018-12-18 added vv option -M : move
122 # 2018-12-19 vv: new .versions/ gets same permissions and ownership as .
123 # 2018-12-19 vv: new versions gets same permissions and ownership as file
124 # 2019-02-14 vv: added $excludedir to vvrc
125 # 2019-07-25 vv: added command execution
126 # 2019-08-01 vv: new .versions directory gets owner and group from parent directory
127 # 2019-08-01 vv: added optional configuration variable $versionsdir_perms
128 # 2019-08-04 vv: added .versions read/write check ==> better error message
129 # 2019-08-21 vv: added $versions to vvrc
130 # 2019-08-29 vv: added btrfs clone-copy support
131 # 2019-08-29 vv: speedup (less md5sum checks)
132 # 2019-12-09 vv: option -l shows modifiction date of last version
133
134 use Getopt::Std;
135 use File::Basename;
136 use Term::ReadLine;
137 use Digest::MD5 'md5_hex';
138 use Cwd qw'abs_path getcwd';
139
140 $prg = basename($0);
141 $ZZ = $ENV{ZZ} || "$ENV{HOME}/.zz";
142
143 $version = 0;
144 unless ($version) {
145 my @d = localtime((stat $0)[9]);
146 $version = sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3])
147 }
148
149 &install if $prg eq 'vvzz' or "@ARGV" eq '-I';
150 &update if "@ARGV" eq '-U';
151 &zz if $prg eq 'zz';
152 &ezz if $prg eq 'ezz';
153 &del if $prg eq 'del';
154
155 $prg eq 'vv' or die "$0: must be named vv\n";
156
157 # vv
158 $usage = <<EOD;
159
160 usage:
161
162 # list available versions
163 vv [-l] [FILE]
164
165 # save new version (-v : verbose, -f : force)
166 vv -s [-v] [-f] FILE
167
168 # recover version (-v : verbose)
169 vv -r [-v] [VERSION_NUMBER] FILE [NEW_FILE]
170
171 # show diff
172 vv -d [VERSION_NUMBER[:VERSION_NUMBER]] [FILE]
173
174 # delete versions (-v : verbose, from-to)
175 vv -D [-v] [VERSION_NUMBER[-VERSION_NUMBER]] FILE
176
177 # delete all versions (-v : verbose, -R : recursive)
178 vv -D [-v] [-R] .
179
180 # view version
181 vv -v VERSION_NUMBER FILE
182
183 # edit file with \$EDITOR (with versioning)
184 vv -e file
185
186 # set or show number of versions (1-99, use file "." to set or show default)
187 vv -N [NUMBER] FILE
188
189 # migrate backup~ files to version files (-v : verbose, -R : recursive)
190 vv -m [-v] [-R]
191
192 # purge orphaned versions (-v : verbose)
193 vv -p [-v]
194
195 # move file with versions to new directory
196 vv -M [-v] FILE DIRECTORY
197
198 # execute shell command on file and versions
199 vv SHELL_COMMAND FILE~
200
201 # show vv version (-v : history)
202 vv -V [-v]
203
204 examples:
205 vv -s project.pl # save new version
206 vv -e project.pl # edit with versioning
207 vv project.pl # list versions
208 vv -d 2 project.pl # show difference to version 2
209 vv -r project.pl # recover (with prompting)
210 vv -r 2 project.pl project_2.pl # recover version 2 as project_2.pl
211 vv -N 20 project.pl # max 20 versions for project.pl
212 vv wc project.pl~ # execute wc on project.pl and its versions
213
214 see also:
215 del -h
216 zz -h
217 EOD
218
219 $fmt = " %-50s # %s\n";
220 $short_usage = "usage:\n";
221 foreach (split "\n",$usage) {
222 last if /examples/;
223 next if /vv -V|history/;
224 if (/# (\w+.*(diff|number|all))/ or /# (\w+)/) {
225 $comment = $1;
226 } elsif (/(vv .+)/) {
227 $short_usage .= sprintf($fmt,$1,$comment);
228 }
229 }
230 $short_usage .= sprintf($fmt,"vv -H","help");
231
232 if ("@ARGV" !~ /^-/) {
233 if (scalar(@ARGV) > 1) {
234 if ("@ARGV " =~ /.~ /) {
235 &vv_command;
236 exit;
237 } elsif (-f $ARGV[-1] and not -l $ARGV[-1]) {
238 $ARGV[-1] .= '~';
239 &vv_command;
240 exit;
241 } else {
242 die $short_usage;
243 }
244 } else {
245 $noopt = $opt_l = 1;
246 }
247 }
248
249 $opt_h = $opt_p = $opt_m = $opt_s = $opt_0 = $opt_e = $opt_H = $opt_b = 0;
250 $opt_f = $opt_q = $opt_R = $opt_P = $opt_U = $opt_V = $opt_M = 0;
251 $opt_r = $opt_d = $opt_D = $opt_v = $opt_N = '';
252 ${'opt_+'} = 0;
253 getopts('hHls0bepPfqmMRDdvV+rN') or die $short_usage;
254 $opt_s ||= $opt_b;
255
256 if ($opt_h) {
257 print "vv: visual versioning\n";
258 if ($opt_v) {
259 print $usage;
260 } else {
261 print $short_usage;
262 }
263 exit;
264 }
265
266 if ($opt_H) {
267 open $0,$0 or die "vv: $0 - $!\n";
268 while (<$0>) {
269 $help = $_ if /^# vv is/;
270 if ($help) {
271 last if /^\s*$/ or /^#\s*\d\d\d\d-\d\d-\d\d/;
272 print;
273 }
274 }
275 print "\nvv: visual versioning ($version)\n";
276 print $usage;
277 exit;
278 }
279
280 if ($opt_V) {
281 warn "$prg: version $version\n";
282 if ($opt_v) {
283 open $0,$0 or die "$prg: $0 - $!\n";
284 while (<$0>) {
285 print if s/^#\s*(\d\d\d\d-\d\d-\d\d)/$1/;
286 }
287 }
288 exit;
289 }
290
291 if ($opt_U) { &update }
292
293 $vvrc = $ENV{HOME} . '/.vvrc';
294 unless (-e $vvrc) {
295 open $vvrc,'>',$vvrc or die "vv: cannot write $vvrc - $!\n";
296 print {$vvrc} q{
297 # default number of versions
298 $versions = 9;
299
300 # exclude files (regexp)
301 $exclude = q(
302 ~$
303 \.tmp$
304 ^mutt-.+-\d+
305 ^#.*#$
306 );
307
308 # exclude directories (regexp)
309 $excludedir = q(
310 /\.ssh/
311 /\.versions/
312 );
313
314 # diff command
315 @diff = qw'diff -u';
316 };
317 close $vvrc;
318
319 # default: use permissions of parent directory for .versions
320 # $versionsdir_perms = 0700;
321 }
322
323 $versions = 9;
324
325 require $vvrc;
326
327 $_ = `rsync -h`;
328 if (/rsync/) {
329 @rsync = qw'rsync -a';
330 push @rsync,'-A' if /-A.*ACL/;
331 } else {
332 die "vv: no rsync found\n";
333 }
334
335 if ($< == 0 and scalar(@ARGV) and my @s = stat($ARGV[-1])) {
336 local ($(,$>,$)) = (0,@s[4,5]);
337 }
338
339 if ($opt_d) {
340 &check_ARGV;
341 if (@ARGV and $ARGV[0] =~ /^\d\d?(:\d\d?)?$/) {
342 $opt_d = shift;
343 } else {
344 $opt_d = 1;
345 if (@ARGV) {
346 my $a = $ARGV[0];
347 if (-f $a) {
348 my $v = sdf(abs_path($a),'.versions');
349 if (-f "$v~01~" and -f "$v~02~" and same_file($a,"$v~01~")) {
350 $opt_d = 2;
351 }
352 }
353 vars($a);
354 &vv_list;
355 $term = new Term::ReadLine $0;
356 $term->ornaments(0) unless $ENV{PERL_RL};
357 $opt_d = $term->readline('show diff to version number: ',$opt_d);
358 exit if $opt_d =~ /^\s*$/;
359 }
360 }
361 option_usage('-d') if scalar(@ARGV) != 1;
362 $a = shift @ARGV;
363 }
364
365 elsif ($opt_D) {
366 if (@ARGV and $ARGV[0] =~ /^\d\d?(-(\d?\d?))?$/) {
367 $opt_D = shift(@ARGV)||0;
368 } else {
369 $opt_D = '-';
370 }
371 option_usage('-D') if scalar(@ARGV) != 1;
372 $a = shift @ARGV;
373 if ($a eq '.') {
374 if ($opt_R) {
375 vsystem(qw'find . -name .versions -exec rm -rf {} +');
376 } else {
377 vsystem(qw'rm -rf .versions');
378 }
379 exit $?;
380 }
381 }
382
383 elsif ($0 eq 've' or $opt_e) {
384 $a = pop @ARGV or option_usage('-e');
385 $opt_e = 1;
386 }
387
388 elsif ($opt_s) {
389 option_usage('-s') if scalar(@ARGV) != 1;
390 $a = shift @ARGV;
391 }
392
393 elsif ($opt_r) {
394 my $nargs = scalar(@ARGV);
395 option_usage('-r') if $nargs == 0 or $nargs > 3;
396 if ($nargs == 1) {
397 $opt_r = '-';
398 } else {
399 $opt_r = shift @ARGV;
400 option_usage('-r') if $opt_r !~ /^\d\d?$/;
401 }
402 $a = shift @ARGV;
403 }
404
405 elsif ($opt_N) {
406 $opt_N = shift @ARGV || '.';
407 $a = shift @ARGV;
408 }
409
410 elsif ($opt_l or $opt_s or $opt_P or $opt_0) {
411 $a = shift @ARGV;
412 }
413
414 elsif ($opt_M) {
415 if (scalar(@ARGV) != 2) {
416 die "usage: $prg file directory\n";
417 }
418
419 my ($file,$dir) = @ARGV;
420
421 if (-l $file or not -f $file) {
422 die "$prg: no such file '$file'\n";
423 }
424 unless (-d $dir) {
425 die "$prg: no such directory '$dir'\n";
426 }
427 if (abs_path(dirname($file)) eq abs_path($dir)) {
428 die "$prg: destination directory '$dir' is same as source\n";
429 }
430 unless (-w $dir) {
431 die "$prg: no write access for directory '$dir'\n";
432 }
433 print "'$file' -> '$dir/'\n" if $opt_v;
434 if (system('mv',$file,$dir)==0) {
435 my $vfile = $file;
436 $vfile =~ s:(.+)/(.+):$1/.versions/$2: or $vfile = ".versions/$vfile";
437 my $vdir = "$dir/.versions";
438 checkvdir($vdir,'w');
439 if (mkdir $vdir and my @s = stat("$vdir/..")) {
440 chmod $versionsdir_perms||$s[2],$vdir;
441 chown $s[4],$s[5],$vdir;
442 }
443 if (my @versions = fmatch(quotemeta($vfile).'(~\d\d?~)?')) {
444 system('mv',@versions,$vdir)
445 }
446 }
447 exit $?;
448 }
449
450 elsif ($opt_m or $opt_p or $opt_N) {
451 }
452
453 elsif ($opt_v) {
454 if (@ARGV and $ARGV[0] =~ /^\d\d?$/) { $opt_v = shift }
455 else { $opt_v = 1 }
456 # &check_ARGV;
457 option_usage('-v20~') if scalar(@ARGV) != 1;
458 $a = shift @ARGV;
459 }
460
461 $file = '*';
462 $vfile = '';
463 $vdir = '.versions';
464
465 if ($a) {
466
467 vars($a);
468
469 # change eugid if root and version directory belongs user
470 my @s = stat($vdir);
471 if (${">"} == 0 and (not @s or $s[4])) {
472 if (my @s = stat($a)) {
473 ${")"} = $s[5];
474 ${">"} = $s[4];
475 }
476 }
477
478 # rename old one-digit versions
479 if ($vfile and (-f "$vfile~0~" or -f "$vfile~1~")) {
480 for (my $i=0; $i<10; $i++) {
481 my $vfile1 = "$vfile~$i~";
482 my $vfile2 = "$vfile~0$i~";
483 if (-f $vfile1) {
484 rename $vfile1,$vfile2 or warn "vv: $vfile1 -> $vfile2 - $!\n";
485 }
486 }
487 }
488
489 if (not (length($opt_r) or $opt_N or $opt_v)) {
490 if (not lstat $file) {
491 if (-s $vfile) {
492 warn "vv: '$a' does not exist any more\n";
493 print "found '$vfile' - recover it? ";
494 $_ = <STDIN>;
495 copy($vfile,$file) if /^y/i;
496 exit 0;
497 }
498 if (-e $vfile1) {
499 warn "vv: '$a' does not exist any more\n";
500 warn "found old:\n";
501 &vv_list;
502 $a = quote($a);
503 warn "to recover latest version, use:\nvv -r $a\n";
504 exit 1;
505 }
506 if (-e sdf($file,'.del')) {
507 warn "vv: '$a' does not exist any more, but you can recover it with:\n";
508 $a = quote($a);
509 warn "del -u $a\n";
510 exit 1;
511 }
512 die "vv: '$a' does not exist\n" unless $opt_e;
513 }
514 if (not $opt_e and (-l $file or not -f $file)) {
515 die "vv: '$a' is not a regular file\n";
516 }
517 }
518 }
519
520 checkvdir($vdir);
521 $n = norm("$vdir/.versions/n");
522 if ($nn = readlink $n and $nn =~ /^\d\d?$/) {
523 $nn--;
524 unlink $n;
525 symlink $nn,norm("$vdir/.versions/.versions");
526 }
527
528 if ($opt_N) {
529 if (not $bfile) {
530 if ($opt_N eq '.') {
531 my $n = readlink('.versions/.versions/.versions')||9;
532 print "files in this directory have default $n versions\n";
533 } elsif (-f $opt_N) {
534 my $file = $opt_N;
535 my $vv = dirname($file).'/.versions/.versions';
536 my $n = readlink($vv.'/'.basename($file))||
537 readlink($vv.'/.versions')||
538 9;
539 print "'$file' has up to $n versions\n";
540 } else {
541 option_usage('-N');
542 }
543 exit;
544 }
545 option_usage('-N') if not $bfile or @ARGV;
546 option_usage('-N') if $opt_N !~ /^\d\d?$/;
547 checkvdir($vdir,'w');
548 if (mkdir $vdir and my @s = stat("$vdir/..")) {
549 chmod $versionsdir_perms||$s[2],$vdir;
550 chown $s[4],$s[5],$vdir;
551 }
552 if (not -d $vdir) { die "vv: cannot mkdir $vdir - $!\n" }
553 my $vvv = norm($vdir.'/.versions');
554 if (mkdir $vvv and my @s = stat("$vvv/..")) {
555 chmod $versionsdir_perms||$s[2],$vvv;
556 chown $s[4],$s[5],$vvv;
557 }
558 if (not -d $vvv) { die "vv: cannot mkdir $vvv - $!\n" }
559 if ($bfile eq '.') {
560 $vvv .= "/.versions";
561 unlink $vvv;
562 symlink $opt_N,$vvv or die "vv: cannot create $vvv - $!\n";
563 } else {
564 $vvv .= "/$bfile";
565 unlink $vvv;
566 symlink $opt_N,$vvv or die "vv: cannot create $vvv - $!\n";
567 print "'$file' has up to $opt_N versions\n" if $opt_v;
568 for (my $i = $opt_N+1; $i <= 99; $i++) {
569 unlink sprintf("$vfile~%02d~",$i);
570 }
571 }
572 exit;
573 }
574
575 if ($opt_e) {
576 option_usage('-e') if not $a or @ARGV;
577 $editor = $ENV{EDITOR} or die "vv: environment variable EDITOR not set\n";
578 system(qw'vv -qs',$file) if -f $file; # save current version
579 system($editor,@ARGV,$file);
580 exit $? if $?;
581 unlink $ofile; # delete new file~ created by editor
582 system(qw'vv -0',$file); # delete last version if no change
583 system(qw'vv -qs',$file); # save new version
584 exit;
585 }
586
587 if ($opt_p) {
588 checkvdir($vdir,'w');
589 opendir $vdir,$vdir or die "vv: no $vdir\n";
590 while (defined($vfile = readdir($vdir))) {
591 next unless -f norm("$vdir/$vfile");
592 $bfile = $vfile;
593 $bfile =~ s/~\d\d?~$//;
594 if (not -f $bfile or -l $bfile) {
595 unlink norm("$vdir/$vfile");
596 $purge{$bfile}++;
597 }
598 }
599 if (@purge = keys %purge) {
600 foreach $p (@purge) {
601 printf "%2d %s purged\n",$purge{$p},$p;
602 }
603 }
604 exit;
605 }
606
607 if ($opt_m) {
608 option_usage('-m') if @ARGV;
609 migrate('.');
610 exit;
611 }
612
613 if (length($opt_r)) {
614 push @rsync,'--inplace';
615 if ($opt_r eq '.') {
616 # legacy backup file
617 if (not -f $vfile) { die "vv: no backup '$vfile'\n" }
618 if (lstat $file) { die "vv: '$file' does exist\n" }
619 copy($vfile,$file);
620 } else {
621 if ($opt_r eq '-') {
622 $opt_r = 1;
623 $md5f = md5f($file);
624 foreach my $v ("$vfile~1~","$vfile~01~") {
625 if (-f $v and $md5f eq md5f($v)) {
626 $opt_r = 2;
627 last;
628 }
629 }
630 &vv_list;
631 exit 1 unless -f $vfile1;
632 $term = new Term::ReadLine $0;
633 $term->ornaments(0) unless $ENV{PERL_RL};
634 $opt_r = $term->readline('recover version number: ',$opt_r);
635 exit if $opt_r =~ /^\s*$/;
636 $opt_v = 1;
637 }
638 if ($opt_r =~ /^\d$/ and -f "$vfile~0$opt_r~") {
639 $vfile .= "~0$opt_r~"
640 } else {
641 $vfile .= "~$opt_r~"
642 }
643 if (not -f $vfile) { die "vv: '$vfile' does not exist'\n" }
644 if (defined($nfile = shift @ARGV)) {
645 if (-f $nfile and not -l $nfile) {
646 if (same_file($vfile,$nfile)) {
647 warn "vv: '$file' and '$nfile' are identical\n";
648 exit;
649 }
650 if ($opt_v) { system qw'vv -vs',$nfile }
651 else { system qw'vv -s',$nfile }
652 } elsif (lstat $nfile) {
653 die "vv: '$nfile' does already exist and is not a regular file\n";
654 }
655 copy($vfile,$nfile);
656 } else {
657 $md5f ||= md5f($file);
658 if (md5f($vfile) eq $md5f) {
659 warn "vv: '$file' and '$vfile' are identical\n";
660 } else {
661 if ($vfile ne $vfile0 and mtime($file) > mtime($vfile0)) {
662 copy($file,$vfile0);
663 }
664 copy($vfile,$file);
665 }
666 }
667 }
668 exit;
669 }
670
671 if (length($opt_d)) {
672 option_usage('-d') if @ARGV;
673 if (not $bfile) { die "vv: no such file '$bfile'\n" }
674 @diff = qw'diff -u' unless @diff;
675 if ($opt_d =~ /^(\d\d?):(\d\d?)$/) {
676 if (-f "$vdir/$bfile~0$1~" and -f "$vdir/$bfile~0$2~") {
677 exec @diff,"$vdir/$bfile~0$2~","$vdir/$bfile~0$1~"
678 } else {
679 exec @diff,"$vdir/$bfile~$2~","$vdir/$bfile~$1~"
680 }
681 } else {
682 if (-f "$vdir/$bfile~0$opt_d~") {
683 exec @diff,"$vdir/$bfile~0$opt_d~",$file;
684 } else {
685 exec @diff,"$vdir/$bfile~$opt_d~",$file;
686 }
687 }
688 exit $!;
689 }
690
691 if ($opt_s) {
692 option_usage('-s') if not $bfile or @ARGV;
693 unless ($opt_f) {
694 if ($exclude) {
695 $exclude =~ s/^\s+//;
696 $exclude =~ s/\s+$//;
697 $exclude =~ s/\s*\n\s*/|/g;
698 if ($bfile =~ /$exclude/) {
699 warn "\r\nvv: ignoring '$bfile'\n" unless $opt_q;
700 exit;
701 }
702 }
703 if ($excludedir) {
704 $excludedir =~ s/^\s+//;
705 $excludedir =~ s/\s+$//;
706 $excludedir =~ s/\s*\n\s*/|/g;
707 my $afile = abs_path($file);
708 if ($afile =~ /$excludedir/) {
709 warn "\r\nvv: ignoring '$afile'\n" unless $opt_q;
710 exit;
711 }
712 }
713 }
714 unless (-d $vdir) {
715 mkdir $vdir or die "vv: cannot mkdir $vdir - $!\n";
716 if (my @s = stat("$vdir/..")) {
717 chmod $versionsdir_perms||$s[2],$vdir;
718 chown $s[4],$s[5],$vdir;
719 }
720 }
721
722 # migrate old file~ to versions
723 if (-f $ofile and not -l $ofile and -r $ofile) {
724 rotate($vfile);
725 rename($ofile,$vfile1);
726 }
727
728 # turn old backup file into regular version
729 if (-f $vfile) {
730 rotate($vfile);
731 rename($vfile,$vfile1);
732 }
733
734 # rotate if file has changed
735 if (-f $vfile1 and not same_file($vfile1,$file)) {
736 rotate($vfile);
737 }
738
739 copy($file,$vfile1);
740 exit;
741 }
742
743 # special post rotating from -e : delete last version if no change
744 if ($opt_0) {
745 my @sb = stat $file or die "vv: '$file' - $!\n";
746 if (-f $vfile1) {
747 while (my @sv = stat $vfile1) {
748 # no version change?
749 if ($sb[7] == $sv[7] and $sb[9] == $sv[9]) {
750 rotate_back($vfile);
751 } else {
752 last;
753 }
754 }
755 }
756 exit;
757 }
758
759 # delete named version, roll back
760 if (length($opt_D)) {
761 option_usage('-D') if @ARGV or not $file;
762 stat $file or die "vv: '$file' - $!\n";
763 # version 0 is special! ==> backup from recovery operation
764 if ($opt_D =~ /^0\b/) {
765 if (-f $vfile0) {
766 if (unlink $vfile0) {
767 printf "delete %s\n",quote($vfile0) if $opt_v;
768 } else {
769 die "vv: cannot delete '$vfile0' - $!\n";
770 }
771 } else {
772 die "vv: no version 0 for '$file'\n";
773 }
774 $opt_D =~ s/^0-/1-/;
775 }
776 if ($opt_D eq '-') {
777 # 0 version?
778 if (-f $vfile0) {
779 if (unlink $vfile0) {
780 printf "delete %s\n",quote($vfile0) if $opt_v;
781 exit;
782 } else {
783 die "vv: cannot delete '$vfile0' - $!\n";
784 }
785 } else {
786 if (-f $vfile1) {
787 printf "delete %s\n",quote($vfile1) if $opt_v;
788 rotate_back($vfile)
789 } else {
790 die "vv: no versions for '$file'\n"
791 }
792 }
793 } else {
794 if ($opt_D =~ /^(\d\d?)$/) {
795 $opt_D = int($1);
796 if (-f $vfile1) {
797 $dfile = sprintf "$vfile~%02d~",$opt_D;
798 if (not -f $dfile) { die "vv: no version $opt_D for '$file'\n" }
799 printf "delete %s\n",quote($dfile) if $opt_v;
800 rotate_back($vfile,$opt_D);
801 } else {
802 die "vv: no versions found\n";
803 }
804 } elsif ($opt_D =~ /^(\d\d?)-(\d\d?)?$/) {
805 $ds = $1;
806 $de = $2;
807 if (-f $vfile1) {
808 $dfile = sprintf "$vfile~%02d~",$ds;
809 if (not -f $dfile) { die "vv: no version $ds for '$file'\n" }
810 if ($de) {
811 for($n = $ds; $n <= $de and -f $dfile; $n++) {
812 printf "delete %s\n",quote(sprintf "$vfile~%02d~",$n) if $opt_v;
813 rotate_back($vfile,$ds);
814 }
815 } else {
816 for($n = $ds; $n <= 99; $n++) {
817 $dfile = sprintf("$vfile~%02d~",$n);
818 if (unlink $dfile) {
819 printf "delete %s\n",quote($dfile) if $opt_v;
820 }
821 }
822 }
823 } else {
824 die "vv: no versions found\n";
825 }
826 }
827 }
828 exit;
829 }
830
831 # delete all versions (legacy option)
832 if ($opt_P) {
833 unless ($bfile) { die $short_usage }
834 if (not($vfile0 or $vfile1)) { die "vv: '$file' has no versions\n" }
835 unlink fmatch(quotemeta($vfile).'(~\d\d?~)?');
836 exit;
837 }
838
839 if ($opt_v and $bfile and not $opt_l) {
840 if (-f "$vfile~0$opt_v~") { $vfile .= "~0$opt_v~" }
841 else { $vfile .= "~$opt_v~" }
842 if (-f $vfile) {
843 if (-t STDOUT) {
844 if (($ENV{EDITOR}||$0) =~ /jed/) {
845 $ENV{JEDINIT} = "SAVE_STATE=0";
846 exec 'jed','-most',$vfile;
847 # exec 'jed',$vfile,qw'-tmp -f set_readonly(1)';
848 } elsif ($ENV{PAGER}) {
849 exec $ENV{PAGER},$vfile;
850 } else {
851 exec 'view',$vfile;
852 }
853 } else {
854 exec 'cat',$vfile;
855 }
856 } else {
857 die "vv: no '$vfile'\n";
858 }
859 exit;
860 }
861
862 # default!
863 if (scalar(@ARGV)) { die $short_usage }
864 &vv_list;
865 exit;
866
867
868 sub vv_list {
869 my (@stat,%v,$ct);
870
871 checkvdir($vdir);
872 if (opendir $vdir,$vdir) {
873 while (defined(my $vfile = readdir($vdir))) {
874 if (-f "$vdir/$vfile") {
875 if ($bfile) {
876 if ($vfile =~ /^\Q$bfile\E~(\d\d?)~$/) {
877 push @{$v{$file}},$1;
878 }
879 } else {
880 if ($vfile =~ /^(.+)~(\d\d?)~$/) {
881 push @{$v{$1}},$2;
882 }
883 }
884 }
885 }
886 closedir $vdir;
887 `stty -a` =~ /columns (\d+)/;
888 $tw = ($1||80)-40;
889 $ct = '';
890 foreach my $file (sort keys %v) {
891 # if (not $bfile and not -f $file or -l $file) {
892 # warn "vv: orphaned $file\n";
893 # next;
894 # }
895 my @v = sort @{$v{$file}};
896 if ($bfile) {
897 @stat = stat $file; # or die "vv: $file - $!\n";
898 print "version bytes date time";
899 if (${'opt_+'}) {
900 print " content";
901 $ct = content($file);
902 $ct =~ s/(.{$tw}).+/$1*/;
903 }
904 print "\n";
905 if (length($v[0]) == 1) { $lf = "%s %15s %s %s\n" }
906 else { $lf = "%2s %15s %s %s\n" }
907 if (scalar(@v) > 21 and -t STDOUT) {
908 foreach my $v (reverse @v) {
909 my $vfile = "$vdir/$bfile~$v~";
910 @stat = stat $vfile or next;
911 if (${'opt_+'}) {
912 $ct = content($vfile);
913 $ct =~ s/(.{$tw}).+/$1*/;
914 }
915 printf $lf,int($v),size($stat[7]),isodate($stat[9]),$ct;
916 }
917 @stat = stat $file;
918 printf $lf,'.',size($stat[7]),isodate($stat[9]),$ct if @stat;
919 } else {
920 printf $lf,'.',size($stat[7]),isodate($stat[9]),$ct if @stat;
921 foreach my $v (@v) {
922 my $vfile = "$vdir/$bfile~$v~";
923 @stat = stat $vfile or next;
924 if (${'opt_+'}) {
925 $ct = content($vfile);
926 $ct =~ s/(.{$tw}).+/$1*/;
927 }
928 printf $lf,int($v),size($stat[7]),isodate($stat[9]),$ct;
929 }
930 }
931 } else {
932 if (my $n = scalar(@v)) {
933 $n-- if $v[0] == 0; # do not count zero version
934 my $orphaned = -f $file?'':' (orphaned)';
935 if ($noopt) {
936 printf "%2d %s%s\n",$n,quote($file),$orphaned;
937 } else {
938 my $mtime = mtime(".versions/$file~01~")||
939 mtime(".versions/$file~1~");
940 printf "%s %s %s%s\n",isodate($mtime),$n,quote($file),$orphaned;
941 }
942 }
943 }
944 }
945 }
946 warn "vv: no versions found, usage: vv -h, help: vv -H\n" unless %v;
947 }
948
949
950 sub vv_command {
951 my @cmd = ();
952 foreach my $a (@ARGV) {
953 if ($a =~ /(.+)~$/) {
954 my $file = $1;
955 my $v = dirname($file).'/.versions/'.basename($file);
956 $v =~ s/^\.\///;
957 my @v = ();
958 push @v,$file if -f $file and not -l $file;
959 push @v,glob(shellquote($v).'~[0-9]~');
960 push @v,glob(shellquote($v).'~[0-9][0-9]~');
961 if (@v) {
962 push @cmd,@v;
963 } else {
964 die "$prg: $file does not exist and has no versions\n";
965 }
966 } else {
967 push @cmd,$a;
968 }
969 }
970 warn "\$ @cmd\n";
971 system @cmd;
972 }
973
974
975 sub shellquote {
976 local $_ = shift;
977 s/([^\w\@\/!^%:_.,=+-])/\\$1/g;
978 return $_;
979 }
980
981 sub size {
982 my $s = shift;
983 # if ($s > 9999999999) { $s = int($s/2**30).'G' }
984 # elsif ($s > 9999999) { $s = int($s/2**20).'M' }
985 # elsif ($s > 9999) { $s = int($s/2**10).'k' }
986 # else { $s = d3($s) }
987 $s = d3($s);
988 return $s;
989 }
990
991
992 sub d3 {
993 local $_ = shift;
994 while (s/(\d)(\d\d\d\b)/$1,$2/) {};
995 return $_;
996 }
997
998
999 sub content {
1000 my $file = shift;
1001 my $qfile = quotemeta($file);
1002 my $ct;
1003 local $_;
1004
1005 chomp ($ct = `file $qfile`);
1006 $ct =~ s/.*?: //;
1007 $ct =~ s/,.*//;
1008
1009 if ($ct =~ /text/ and open $file,$file) {
1010 read $file,$_,1024;
1011 close $file;
1012 s/[\x00-\x20]+/ /g;
1013 s/^ */\"/;
1014 s/ *$/\"/;
1015 $ct = $_;
1016 }
1017
1018 return $ct;
1019 }
1020
1021
1022 sub isodate {
1023 my @d = localtime shift;
1024 return sprintf('%d-%02d-%02d %02d:%02d:%02d',
1025 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
1026 }
1027
1028
1029 sub rotate {
1030 my $vf = shift; # version base file
1031 my $vf0 = "$vf~00~";
1032 my $vf1 = "$vf~01~";
1033 my $vv = dirname($vf).'/.versions'; # config .versions/.versions
1034 my $n = readlink($vv.'/'.basename($vf))||
1035 readlink($vv.'/.versions')||
1036 $versions;
1037
1038 unlink $vf0 if same_file($vf0,$vf1);
1039
1040 for (my $i = $n-1; $i >= 0; $i--) {
1041 my $vfi = sprintf("%s~%02d~",$vf,$i);
1042 my $vfn = sprintf("%s~%02d~",$vf,$i+1);
1043 if (-e $vfi) {
1044 rename $vfi,$vfn or die "vv: '$vfi' -> '$vfn' : $!\n";
1045 }
1046 }
1047
1048 for (my $i = $n+1; $i <= 99; $i++) {
1049 unlink sprintf("%s~%02d~",$vf,$i);
1050 }
1051
1052 if ($n > 1) {
1053 # was there a version 0?
1054 if (-e $vf1) {
1055 my $bf = $vf;
1056 $bf =~ s:/\.versions/:/:;
1057 my @sb = stat $bf;
1058 my @sv = stat $vf1;
1059 # same version? (size and mtime)
1060 if (@sb and @sv and $sb[7] == $sv[7] and $sb[9] == $sv[9]) {
1061 # same version
1062 unlink $vf1;
1063 } else {
1064 # new version
1065 rotate($vf);
1066 }
1067 }
1068 }
1069 }
1070
1071
1072 sub copy {
1073 my $from = rrp(shift);
1074 my $to = rrp(shift);
1075 my @copy = @rsync;
1076 my $qfrom = quote($from);
1077
1078 unless (-e $from) {
1079 die "vv: '$from' does not exist\n";
1080 }
1081 if (-l $from or not -f $from) {
1082 die "vv: '$from' is not a regular file\n";
1083 }
1084 if (-l $to or -e $to and not -f $to) {
1085 die "vv: '$to' is not a regular file\n";
1086 }
1087
1088 if (`stat -f -c %T $qfrom 2>/dev/null` =~ /^btrfs/) {
1089 @copy = qw'cp -ax --reflink';
1090 }
1091
1092 if (open $to,'>>',$to) {
1093 close $to;
1094 if (system(@copy,$from,$to) == 0) {
1095 if ($ENV{VIMRUNTIME}) {
1096 print "\n";
1097 } else {
1098 print "$from -> $to\n" if $opt_v;
1099 }
1100 } else {
1101 exit $?;
1102 }
1103 } else {
1104 die "\r\nvv: cannot write '$to' - $!\n";
1105 }
1106 }
1107
1108
1109 sub realfilename {
1110 my $file = shift;
1111
1112 return $file unless -e $file;
1113
1114 if (-l $file) {
1115 my $link = readlink($file);
1116 if ($link !~ /^\// and $file =~ m:(.*/).:) {
1117 $link = $1 . $link;
1118 }
1119 return realfilename($link);
1120 } else {
1121 return $file;
1122 }
1123 }
1124
1125
1126 sub migrate {
1127 my $dir = shift;
1128 my $vdir = norm("$dir/.versions");
1129 my $dfile;
1130
1131 unless (opendir $dir,$dir) {
1132 warn "vv: cannot read directory $dir - $!\n";
1133 return
1134 }
1135 while (defined($file = readdir($dir))) {
1136 $dfile = "$dir/$file";
1137 next if -l $dfile or $file eq '.' or $file eq '..';
1138 if (-d $dfile and $opt_R and $file ne '.versions') {
1139 migrate($dfile);
1140 } elsif (-f $dfile and $file =~ /~$/) {
1141 if (-d $vdir) {
1142 for ($i = 8; $i > 0; $i--) {
1143 $n = $i+1;
1144 rename "$vdir/$file$i~","$vdir/$file$n~";
1145 }
1146 } else {
1147 mkdir $vdir or die "vv: cannot mkdir $vdir - $!\n";
1148 if (my @s = stat("$vdir/..")) {
1149 chmod $versionsdir_perms||$s[2],$vdir;
1150 chown $s[4],$s[5],$vdir;
1151 }
1152 }
1153 $nfile = sprintf("%s/%s1~",$vdir,$file);
1154 rename $dfile,$nfile or die "vv: cannot move '$dfile' to '$nfile' - $!\n";
1155 print "$dfile -> $nfile\n" if $opt_v;
1156 }
1157 }
1158 closedir $dir;
1159 }
1160
1161
1162 sub mtime {
1163 my @s = stat shift;
1164 return @s ? $s[9] : 0;
1165 }
1166
1167
1168 sub md5f {
1169 my $file = shift;
1170 my $md5 = Digest::MD5->new;
1171 my $MB = 2**20;
1172 local $_;
1173
1174 if (open $file,$file) {
1175 while (read($file,$_,$MB)) {
1176 $md5->add($_);
1177 }
1178 close $file;
1179 }
1180
1181 return $md5->hexdigest;
1182 }
1183
1184
1185 sub same_file {
1186 my $file1 = shift;
1187 my $file2 = shift;
1188
1189 return unless -f $file1 and -f $file2;
1190 return 0 if -s $file1 != -s $file2;
1191 return 1 if mtime($file1) == mtime($file2);
1192 return 1 if md5f($file1) eq md5f($file2);
1193 return 0;
1194 }
1195
1196
1197 # if ARGV is empty use last saved file as default file argument
1198 sub check_ARGV {
1199 local $_;
1200 local *V;
1201
1202 if (not @ARGV) {
1203 if (-d '.versions' and open V,'ls -at .versions|') {
1204 while (<V>) {
1205 chomp;
1206 if (-f ".versions/$_") {
1207 close V;
1208 s/~\d+~$//;
1209 warn "$prg: using $_\n";
1210 @ARGV = ($_);
1211 return;
1212 }
1213 }
1214 }
1215 }
1216
1217 }
1218
1219
1220 sub rotate_back {
1221 my $vfile = shift;
1222 my $start = shift||1;
1223
1224 for (my $i = $start; $i <= 98; $i++) {
1225 my $vfi = sprintf("%s~%02d~",$vfile,$i);
1226 my $vfn = sprintf("%s~%02d~",$vfile,$i+1);
1227 if (-f $vfn) {
1228 rename $vfn,$vfi;
1229 } else {
1230 unlink $vfi if $i == $start;
1231 last;
1232 }
1233 }
1234 }
1235
1236
1237 sub pathsearch {
1238 my $prg = shift;
1239
1240 foreach my $dir (split(':',$ENV{PATH})) {
1241 return "$dir/$prg" if -x "$dir/$prg";
1242 }
1243 }
1244
1245
1246 # subdirectory file
1247 sub sdf {
1248 my $file = shift;
1249 my $dir = shift;
1250 return rrp(sprintf("%s/%s/%s",dirname($file),$dir,basename($file)));
1251 }
1252
1253
1254 sub option_usage {
1255 my $o = shift;
1256 if ($usage =~ /(#.+)\n\s+(vv $o.*)/) {
1257 die "$1\n".
1258 "usage: $2\n".
1259 "help: vv -H\n";
1260 } else {
1261 die $short_usage;
1262 }
1263 }
1264
1265
1266 # zz is the generic clip board program
1267 #
1268 # to use zz with vim, write to your .vimrc:
1269 #
1270 # noremap <silent> zz> :w !zz<CR><CR>
1271 # noremap <silent> zz< :r !zz --<CR>
1272 sub zz {
1273 my $bs = 2**16;
1274 my $wm = '>';
1275 my ($file,$tee,$x);
1276
1277 if ("@ARGV" =~ /^(-h|--help)$/) {
1278 print <<'EOD';
1279 zz is the generic clip board program. It can hold any data, ASCII or binary.
1280 The clip board itself is $ZZ (default: $HOME/.zz) and has up to 9 versions.
1281 See also the clip board editor "ezz".
1282 Limitation: zz does not work across accounts or hosts! Use xx instead.
1283
1284 Options and modes are:
1285
1286 "zz" show content of $ZZ
1287 "zz file(s)" copy file(s) content into $ZZ
1288 "zz -" write STDIN (keyboard, mouse buffer) to $ZZ
1289 "zz +" add STDIN (keyboard, mouse buffer) to $ZZ
1290 "... | zz" write STDIN from pipe to $ZZ
1291 "... | zz +" add STDIN from pipe to $ZZ
1292 "... | zz -" write STDIN from pipe to $ZZ and STDOUT
1293 "zz | ..." write $ZZ to pipe
1294 "... | zz | ..." save pipe data to $ZZ (like tee)
1295 "zz --" write $ZZ to STDOUT
1296 "zz -v" show clip board versions (history)
1297 "zz -1" write $ZZ version 1 to STDOUT
1298 "zz -9" write $ZZ version 9 to STDOUT
1299
1300 Examples:
1301
1302 zz *.txt
1303 ls -l | zz
1304 zz | wc -l
1305 (within vi) :w !zz
1306 (within vi) :r !zz
1307 (within mutt) |zz
1308 EOD
1309 exit;
1310 }
1311
1312 if ("@ARGV" eq '-V') {
1313 warn "$prg: version $version\n";
1314 warn "see: vv -vV\n";
1315 exit;
1316 }
1317
1318 if ("@ARGV" eq '-v') {
1319 exec qw'vv -+l',$ZZ;
1320 }
1321
1322 if ("@ARGV" =~ /^-(\d)$/) {
1323 exec "vv -v $1 '$ZZ' | cat";
1324 }
1325
1326 # read mode
1327 if (-t STDIN and not @ARGV or "@ARGV" eq '--') {
1328 exec 'cat',$ZZ;
1329 }
1330
1331 # write mode
1332 system "vv -s '$ZZ' >/dev/null 2>&1" if -s $ZZ;
1333
1334 if (@ARGV and $ARGV[0] eq '+') {
1335 shift @ARGV;
1336 $wm = '>>';
1337 }
1338
1339 if ("@ARGV" eq '-') {
1340 @ARGV = ();
1341 $tee = 1 unless -t STDIN;
1342 }
1343
1344 unless (@ARGV or -t STDIN or -t STDOUT) {
1345 $tee = $bs = 1;
1346 }
1347
1348 open $ZZ,$wm,$ZZ or die "zz: cannot write $ZZ - $!\n";
1349
1350 if (@ARGV) {
1351 while ($file = shift @ARGV) {
1352 if (-f $file) {
1353 if (open $file,$file) {
1354 while (read($file,$x,$bs)) {
1355 my $s = syswrite $ZZ,$x;
1356 defined($s) or die "zz: cannot write to $ZZ - $!\n";
1357 }
1358 close $file;
1359 } else {
1360 warn "zz: cannot read '$file' - $!\n";
1361 }
1362 } elsif (-e $file) {
1363 warn "zz: '$file' is not a regular file\n";
1364 } else {
1365 warn "zz: '$file' does not exist\n";
1366 }
1367 }
1368 close $ZZ;
1369 $ZZ1 = $ZZ.'~1~';
1370 $ZZ1 =~ s:(.*)/(.*):$1/.versions/$2:;
1371 if (-e $ZZ and not -s $ZZ and -s $ZZ1 ) {
1372 system @rsync,$ZZ1,$ZZ;
1373 }
1374 } else {
1375 while (read(STDIN,$x,$bs)) {
1376 syswrite $ZZ,$x;
1377 syswrite STDOUT,$x if $tee;
1378 }
1379 }
1380
1381 exit;
1382 }
1383
1384
1385 sub ezz {
1386 my $bs = 2**16;
1387 my $wm = '>';
1388 my $editor = $ENV{EDITOR} || 'vi';
1389 my ($out,$file,$x);
1390
1391 $ENV{JEDINIT} = "SAVE_STATE=0";
1392
1393 if ("@ARGV" =~ /^(-h|--help)$/) {
1394 print <<'EOD';
1395 ezz is the edit helper for the zz clip board program.
1396 The clip board itself is $ZZ (default: $HOME/.zz) and has up to 9 versions.
1397
1398 Options and modes are:
1399
1400 "ezz" edit $ZZ with $EDITOR
1401 "ezz -1" edit $ZZ version 1 (see: zz -v)
1402 "ezz -9" edit $ZZ version 9 (see: zz -v)
1403 "... | ezz" write STDIN from pipe to $ZZ and call $EDITOR
1404 "... | ezz +" add STDIN from pipe to $ZZ and call $EDITOR
1405 "ezz 'perl commands'" execute perl commands on $ZZ
1406 "ezz - 'perl commands'" execute perl commands on $ZZ and show result
1407 "ezz filter [args]" run filter [with args] on $ZZ
1408 "ezz - filter [args]" run filter [with args] on $ZZ and show result
1409
1410 Examples:
1411
1412 ls -l | ezz
1413 ezz 's/ /_/g'
1414 ezz head -3
1415 ezz - head -3
1416 EOD
1417 exit;
1418 }
1419
1420 system "vv -s '$ZZ' >/dev/null 2>&1" if -s $ZZ;
1421
1422 unless (-t STDIN) {
1423 if ("@ARGV" eq '+') {
1424 @ARGV = ();
1425 $wm = '>>';
1426 }
1427 open $ZZ,$wm,$ZZ or die "zz: cannot write $ZZ - $!\n";
1428 syswrite $ZZ,$x while read(STDIN,$x,$bs);
1429 close $ZZ;
1430 }
1431
1432 if (-t STDIN and "@ARGV" =~ /^-([1-9])$/) {
1433 my $v = $1;
1434 $ZZ =~ s:(.+)/(.+):$1/.versions/$2~0$v~:;
1435 if (-f $ZZ) {
1436 close STDIN;
1437 open STDIN,'/dev/tty';
1438 exec $editor,$ZZ;
1439 } else {
1440 die "$0: no zz version $v\n";
1441 }
1442 }
1443
1444 if (@ARGV) {
1445 $out = shift @ARGV if $ARGV[0] eq '-';
1446 $cmd = shift @ARGV or exec 'cat',$ZZ;
1447 rename $ZZ,"$ZZ~" or die "zz: cannot move $ZZ to $ZZ~ - $!\n";
1448 $cmd = quotemeta $cmd;
1449 @ARGV = map { quotemeta } @ARGV;
1450 if (pathsearch($cmd)) {
1451 system "$cmd @ARGV <'$ZZ~'>'$ZZ'";
1452 } else {
1453 system "perl -pe $cmd @ARGV <'$ZZ~'>'$ZZ'";
1454 }
1455 if ($? == 0) { unlink "$ZZ~" }
1456 else { rename "$ZZ~",$ZZ }
1457 exec 'cat',$ZZ if $out;
1458 } else {
1459 close STDIN;
1460 open STDIN,'/dev/tty';
1461 exec $editor,$ZZ;
1462 }
1463 exit;
1464 }
1465
1466
1467 sub del {
1468 my $usage = <<EOD;
1469 del: (un)delete file(s) (with versions)
1470 usage: del [-v] [-f] file(s)
1471 del [-v] -u file(s)
1472 del [-v] -p file(s)
1473 del [-v] -P [-d days] [-r] [directory]
1474 del [-v] -D file(s)
1475 del [-v] -l
1476 options: -v verbose mode
1477 -f force delete
1478 -u undelete file(s)
1479 -p purge deleted file(s)
1480 -P purge all deleted files [older than -d days] [-r recursive]
1481 -D real delete file(s) and versions (NO WAY BACK!)
1482 -l list deleted files
1483 examples: del *.tmp # delete all *.tmp files
1484 del -u project.pl # undelete project.pl
1485 del -vprd 2 # verbose purge deleted files older than 2 days
1486 see also: vv -h
1487 EOD
1488
1489 $error = 0;
1490
1491 $opt_h = $opt_v = $opt_V = 0;
1492 $opt_u = $opt_p = $opt_P = $opt_D = $opt_r = $opt_l = $opt_d = $opt_f = 0;
1493 getopts('hvVufpPDlrd:') or die $usage;
1494
1495 if ($opt_h) {
1496 print $usage;
1497 exit 0;
1498 }
1499
1500 if ($opt_V) {
1501 warn "$prg: version $version\n";
1502 warn "see: vv -vV\n";
1503 exit;
1504 }
1505
1506
1507 if ($opt_u) {
1508 die $usage unless @ARGV;
1509 foreach $file (@ARGV) {
1510 $dfile = sdf($file,'.del');
1511 unless (lstat $dfile) {
1512 warn "del: '$dfile' does not exist\n";
1513 $error++;
1514 next;
1515 }
1516 if (lstat $file) {
1517 warn "del: '$file' does already exist\n";
1518 $error++;
1519 next;
1520 }
1521 if (move($dfile,dirname($file))) {
1522 print "'$dfile' -> '$file'\n" if $opt_v;
1523 } else {
1524 warn "del: moving '$dfile' to '$file' failed\n";
1525 $error++;
1526 }
1527 }
1528 }
1529
1530 elsif ($opt_p) {
1531 die $usage if $opt_d or $opt_r or not @ARGV;
1532 $error = 0;
1533 foreach $file (@ARGV) {
1534 $dfile = ".del/$file";
1535 if (unlink $dfile) {
1536 printf "purge %s\n",quote($dfile) if $opt_v;
1537 } else {
1538 warn "del: '$dfile' dies not exist\n";
1539 $error++;
1540 }
1541 }
1542 exit $error;;
1543 }
1544
1545 elsif ($opt_P) {
1546 die $usage if $opt_d !~ /^(\d+)$/;
1547 $dir = shift @ARGV || '.';
1548 die $usage if @ARGV;
1549 die "del: $dir is not a directory\n" if -l $dir or not -d $dir;
1550 if ($opt_r) {
1551 $find = "find $dir -type d -name .del 2>/dev/null";
1552 open $find,"$find|" or die "del: cannot run find - $!\n";
1553 while (<$find>) {
1554 chomp;
1555 purge($_);
1556 }
1557 } else {
1558 purge("$dir/.del");
1559 }
1560 rmdir("$dir/.del");
1561 exit;
1562 }
1563
1564 elsif ($opt_D) {
1565 unless (@ARGV) { die $usage }
1566 $error = 0;
1567 foreach $p (@ARGV) {
1568 $p =~ s:/+$::;
1569 @x = ();
1570 push @x,$p if lstat $p;
1571 $d = sdf($p,'.del');
1572 push @x,$d if lstat $d;
1573 push @x,fmatch(quotemeta(sdf($p,'.versions')).'~\d\d?~'),
1574 fmatch(quotemeta(sdf($p,'.del/.versions')).'~\d\d?~');
1575 if (@x) {
1576 if ($opt_v) {
1577 foreach my $x (@x) {
1578 printf "purge %s\n",quote($x);
1579 }
1580 }
1581 system qw'rm -rf',@x;
1582 } else {
1583 warn "del: cannot purge '$p': not found\n";
1584 $error++;
1585 }
1586 }
1587 exit $error;;
1588 }
1589
1590 elsif ($opt_l) {
1591 $dir = $ARGV[0] || '.';
1592 $deldir = "$dir/.del";
1593 chdir $deldir or die "del: $deldir - $!\n";
1594 opendir $deldir,'.' or die "del: cannot open $deldir - $!\n";
1595 @files = grep { not /^(\.\.?|\.del|\.versions)$/ } readdir($deldir);
1596 die "del: no deleted files found\n" unless @files;
1597 foreach $file (sort { lc $a cmp lc $b } @files) {
1598 printf "%s %15s ",mdtime($file),d3size($file) if $opt_v;
1599 print quote($file);
1600 print '/' if -d $file;
1601 print ' -> '.quote(readlink $file) if $opt_v and -l $file;
1602 print "\n";
1603 }
1604 exit;
1605 }
1606
1607 else {
1608 die $usage unless @ARGV;
1609
1610 foreach $file (@ARGV) {
1611 if ($file =~ /(.+)\/+$/ and not -d $1 and not -l $1 and -e $1) {
1612 warn "del: '$1' is a file, not a directory\n";
1613 $error++;
1614 next;
1615 }
1616 if (not -l $file and -e $file) {
1617 if (-d $file) {
1618 $dir = abs_path($file);
1619 if (not $opt_f and $dir !~ m:^/tmp/: and
1620 ($dir =~ m:^/[^/]*$: or $dir =~ m:^/[^/]*/[^/]*$:)
1621 ) {
1622 warn "del: cannot delete '$dir/', too dangerous\n";
1623 $error++;
1624 next;
1625 }
1626 if (not $file =~ s/\/+$// and not $opt_f) {
1627 warn "del: '$file' is a directory, use '$file/' to delete it\n";
1628 $error++;
1629 next;
1630 }
1631 }
1632 }
1633 unless (lstat $file) {
1634 warn "del: '$file' does not exist\n";
1635 $error++;
1636 next;
1637 }
1638 $dir = dirname($file);
1639 $deldir = norm("$dir/.del");
1640 unless (-d $deldir) {
1641 unless (mkdir($deldir)) {
1642 warn "del: cannot mkdir $deldir - $!\n";
1643 $error++;
1644 next;
1645 }
1646 if (my @s = stat($dir)) {
1647 chmod $s[2],$deldir;
1648 chown $s[4],$s[5],$deldir;
1649 }
1650 }
1651 if (cpal($file,$deldir)) {
1652 if (-l $file or not -d $file) {
1653 unlink $file;
1654 my $vfile = "$dir/.versions/".basename($file);
1655 if (my @versions = fmatch(quotemeta($vfile).'(~\d\d?~)?')) {
1656 my $vdir = norm($deldir.'/.versions');
1657 mkdir $vdir;
1658 system 'mv',@versions,$vdir if -d $vdir;
1659 }
1660 } else {
1661 system qw'rm -rf',$file;
1662 }
1663 } else {
1664 warn "del: moving '$file' to '$deldir/' failed";
1665 $error++;
1666 }
1667 }
1668 }
1669
1670 exit $error ? 1 : 0;
1671
1672 }
1673
1674 sub cpal {
1675 my $f = shift;
1676 my $ddir = shift;
1677 my @cp = qw'cp -laf';
1678 push @cp,'-v' if $opt_v;
1679
1680 $f =~ s:/+$::;
1681 unless (lstat $f) {
1682 warn "$0: cannot access '$a'\n";
1683 return 0;
1684 }
1685 unless (lstat $ddir) {
1686 warn "$0: cannot access '$ddir'\n";
1687 return 0;
1688 }
1689 if (-l $ddir or not -d $ddir) {
1690 warn "$0: '$ddir' is not a directory\n";
1691 return 0;
1692 }
1693
1694 my @files = ();
1695 if (open my $find,'-|',qw'find',$f,'-print0') {
1696 local $_;
1697 local $/ = "\0";
1698 while (<$find>) {
1699 chomp;
1700 push @files,$_;
1701 }
1702 close $find;
1703 }
1704 my $dir = dirname($f).'/';
1705 $dir = '' if $dir eq './';
1706 foreach my $s (@files) {
1707 my $d = $s;
1708 $d =~ s:^\Q$dir:$ddir/:;
1709 if ((-l $s or not -d $s) and (-d $d and not -l $d)) {
1710 system qw'rm -rf',$d;
1711 } elsif ((-d $s and not -l $s) and (-l $d or not -d $d)) {
1712 unlink $d;
1713 }
1714 }
1715
1716 my @cmd = (@cp,$f,$ddir);
1717 # print "\$ @cmd\n" if $opt_v;
1718 system @cmd;
1719 if ($?) {
1720 return 0;
1721 # $error = (($? >> 8) || 1);
1722 } else {
1723 return 1;
1724 }
1725 }
1726
1727
1728 # move file and versions
1729 sub move {
1730 my $file = rrp(shift);
1731 my $ddir = rrp(shift);
1732 my $fdir = dirname($file);
1733 my $dfile = basename($file);
1734 my $ddf;
1735
1736 unless (lstat $file) {
1737 warn "del: '$file' does not exist\n";
1738 $error++;
1739 return;
1740 }
1741
1742 unless (-d $ddir) {
1743 die "del: destionation '$ddir' is not a directory (internal error)";
1744 }
1745
1746 if ($dfile =~ m:^/*$: or $dfile eq '.' or $dfile eq '..') {
1747 die "del: \$dfile == '$dfile' ?!"
1748 }
1749
1750 $ddf = norm("$ddir/$dfile");
1751 if (lstat $ddf) {
1752 if ($ddf =~ m:/\.del/:) {
1753 # not used any more, but cpal()
1754 unlink $ddf or system qw'rm -rf',$ddf;
1755 if (lstat $ddf) {
1756 unlink $ddf;
1757 $error++;
1758 return;
1759 }
1760 } else {
1761 warn "del: '$ddf' does already exist\n";
1762 $error++;
1763 return;
1764 }
1765 }
1766 if (rename $file,$ddf) {
1767 my $vfile = "$fdir/.versions/$dfile";
1768 if (my @versions = fmatch(quotemeta($vfile).'(~\d\d?~)?')) {
1769 my $vdir = norm($ddir.'/.versions');
1770 mkdir $vdir;
1771 if (-d $vdir) {
1772 system 'mv',@versions,$vdir;
1773 }
1774 }
1775 # printf "%s -> %s\n",quote(rrp($file)),quote(rrp($ddf)) if $opt_v;
1776 @s = lstat($ddf);
1777 utime($s[8],$s[9],$ddf); # set CTIME
1778 return $ddf;
1779 } else {
1780 $error++;
1781 }
1782 }
1783
1784
1785 # relative path without leading ./
1786 sub rrp {
1787 local $_ = shift;
1788 s:^\./:: if m:/.+/:;
1789 s://+:/:g;
1790 return $_;
1791 }
1792
1793
1794 sub norm {
1795 local $_ = shift;
1796 s://+:/:g;
1797 s:^\./(.):$1:;
1798 return $_;
1799 }
1800
1801
1802 sub mdtime {
1803 my @d = localtime((lstat shift)[9]);
1804 return sprintf('%d-%02d-%02d %02d:%02d:%02d',
1805 $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]);
1806 }
1807
1808
1809 sub purge {
1810 my $ddir = shift;
1811 my $after = $opt_d * 60*60*24;
1812 my $now = time();
1813 my $cwd = getcwd();
1814 local $_;
1815
1816 die "del: purge $ddir ?!\n" if $ddir !~ /\.del$/;
1817 die "del: $ddir is a symlink\n" if -l $ddir;
1818 if ($< + $opt_d == 0) {
1819 opendir $ddir,$ddir or die "del: cannot open $ddir - $!\n";
1820 my @f = ();
1821 while (defined(my $f = readdir $ddir)) {
1822 push @f,"$ddir/$f" if $f ne '.' and $f ne '..';
1823 }
1824 if ($opt_v and @f) {
1825 printf "purge %s\n",quote($_) foreach (sort @f);
1826 }
1827 system qw'rm -rf',$ddir;
1828 if (lstat $ddir) {
1829 warn "del: cannot purge '$ddir'\n";
1830 $error++;
1831 } else {
1832 printf "purge %s\n",quote($ddir) if $opt_v;
1833 }
1834 return;
1835 }
1836 chdir($ddir) or die "del: cannot chdir($ddir) - $\n";
1837 if (opendir my $dir,'.') {
1838 foreach my $file (sort(readdir($dir))) {
1839 next if $file =~ /^\.\.?$/;
1840 my @stat = lstat $file;
1841 unless (@stat) {
1842 warn "del: cannot purge '$ddir/$file' - $!\n";
1843 $error++;
1844 next;
1845 }
1846 if ($now > $stat[10]+$after) {
1847 local ($(,$>,$)) = (0,@stat[4,5]) if $< == 0;
1848 unlink $file or system qw'rm -rf',$file;
1849 if (lstat $file) {
1850 unlink $file;
1851 warn "del: cannot purge '$ddir/$file' - $!\n";
1852 $error++;
1853 } else {
1854 unlink fmatch("$ddir/.versions/".quotemeta($file).'~\d\d?~');
1855 printf "purge %s\n",quote("$ddir/$file") if $opt_v;
1856 }
1857 }
1858 }
1859 } else {
1860 warn "del: cannot open $ddir - $!\n";
1861 $error++;
1862 }
1863 chdir($cwd);
1864 }
1865
1866
1867 sub quote {
1868 local $_ = shift;
1869 my $mc = '\'\[\]\\\\ `"$?&<>$*()|{};';
1870
1871 unless (defined $_) {
1872 die "@_";
1873 @x = caller;
1874 die "@x";
1875 }
1876 if (s/[\000-\037\200-\237\241-\250]/?/g or /\'/) {
1877 s/([$mc])/\\$1/g;
1878 s/^~/\\~/;
1879 } elsif (/[$mc]/) {
1880 $_ = "'$_'";
1881 }
1882 return $_;
1883 }
1884
1885
1886 sub checkvdir {
1887 my $vdir = shift;
1888 my $mode = shift||'';
1889
1890 if (-d $vdir) {
1891 if (not -x $vdir or not -r $vdir) {
1892 die "$prg: cannot read $vdir\n";
1893 }
1894 if ($mode eq 'w' and not -w $vdir) {
1895 die "$prg: cannot write to $vdir\n";
1896 }
1897 }
1898 }
1899
1900
1901 # file regexp match
1902 sub fmatch {
1903 my $m = shift;
1904 my $dir = dirname($m);
1905 my $fre = basename($m);
1906 my @match = ();
1907
1908 $dir =~ s/\\//g;
1909
1910 if (opendir $dir,$dir) {
1911 while (defined(my $file = readdir($dir))) {
1912 next if $file eq '.' or $file eq '..';
1913 if ($file =~ /^$fre$/) {
1914 if ($dir eq '.') {
1915 push @match,$file;
1916 } else {
1917 push @match,"$dir/$file";
1918 }
1919 }
1920 }
1921 closedir $dir;
1922 }
1923
1924 return sort(@match);
1925 }
1926
1927
1928 sub d3size {
1929 $file = shift;
1930 if (-f $file and not -l $file) {
1931 return d3(-s $file);
1932 } else {
1933 return '-';
1934 }
1935 }
1936
1937
1938 sub update {
1939 $0 = abs_path($0);
1940 die "vv: cannot write $0\n" unless -w $0;
1941 my $url = 'http://fex.belwue.de/fstools/bin/vv';
1942 warn "vv: downloading $url\n";
1943 my $new = `wget -qO- $url`;
1944 if ($new !~ /visual versioning/) { die "vv: bad update\n" }
1945 if ($new =~ /version = (\d+)/ and $1 le $version) {
1946 warn "vv: version $version is uptodate\n";
1947 exit;
1948 }
1949 system qw'vv -s',$0;
1950 exit $? if $?;
1951 open $0,'>',$0 or die "vv: cannot write $0 - $!\n";
1952 print {$0} $new;
1953 close $0;
1954 exit;
1955 }
1956
1957
1958 sub vars {
1959 my $a = shift;
1960
1961 $file = realfilename($a);
1962 $ofile = "$file~";
1963 $bfile = basename($file);
1964 $dir = dirname($file);
1965 $vdir = norm("$dir/.versions");
1966 $vfile = "$vdir/$bfile";
1967 $vfile0 = "$vfile~00~";
1968 $vfile1 = "$vfile~01~";
1969 }
1970
1971
1972 sub vsystem {
1973 print "\$ @_\n" if $opt_v;
1974 return system @_;
1975 }
1976
1977
1978 sub install {
1979 my ($dir);
1980 local $| = 1;
1981
1982 $0 = abs_path($0);
1983
1984 print "Installation directory [.]: ";
1985 $dir = <STDIN>||'';
1986 chomp $dir;
1987 $dir =~ s:/+$::;
1988 $dir ||= '.';
1989 if ($dir eq '.') {
1990 if ($0 =~ /vvzz$/) {
1991 unlink qw'zz ezz del vv';
1992 rename $0,'vv' or die "$0: cannot create vv - $!\n";
1993 }
1994 symlink 'vv','zz' or die "$0: cannot create zz - $!\n";
1995 symlink 'vv','ezz' or die "$0: cannot create ezz - $!\n";
1996 symlink 'vv','del' or die "$0: cannot create del - $!\n";
1997 } else {
1998 if (not -e $dir) { die "$0: $dir does not exist\n" }
1999 if (not -d $dir) { die "$0: $dir is not a directory\n" }
2000 if (not -w $dir) { die "$0: $dir is not writable\n" }
2001 chdir $dir or die "$0: cannot cd $dir - $!\n";
2002 unlink qw'zz ezz del vv';
2003 system qw'rsync -a',$0,'vv';
2004 exit $? if $?;
2005 symlink 'vv','zz' or die "$0: cannot create $dir/zz - $!\n";
2006 symlink 'vv','ezz' or die "$0: cannot create $dir/ezz - $!\n";
2007 symlink 'vv','del' or die "$0: cannot create $dir/del - $!\n";
2008 }
2009 print "Installation completed. See:\n";
2010 print "$dir/vv -h\n";
2011 print "$dir/zz -h\n";
2012 print "$dir/ezz -h\n";
2013 print "$dir/del -h\n";
2014 exit;
2015 }