svn-clean 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #! /usr/bin/perl
  2. #
  3. # This script recursively (beginning with the current directory)
  4. # wipes out everything not registered in SVN.
  5. #
  6. # rewritten in perl by Oswald Buddenhagen <ossi@kde.org>
  7. # based on bash version by Thiago Macieira <thiago@kde.org>
  8. # inspired by cvs-clean, written by Oswald Buddenhagen <ossi@kde.org>
  9. # inspired by the "old" cvs-clean target from Makefile.common
  10. #
  11. # This file is free software in terms of the BSD licence. That means
  12. # that you can do anything with it except removing this license or
  13. # the above copyright notice. There is NO WARRANTY of any kind.
  14. #
  15. # Warning:
  16. # This script processes the output from the SVN executable
  17. # Do not run it along with colorsvn
  18. use File::Path;
  19. my $version = "svn-clean v1.0";
  20. my $heading = $version.": cleans up the Subversion working directory\n";
  21. my $usage = $heading.
  22. "svn-clean [-h] [-n] [-q] [-i|-f] [dirname]\n\n".
  23. "Where:\n".
  24. " -h shows this help screen\n".
  25. " -n dry-run: doesn't actually erase the files, just show their names\n".
  26. " -i interactive: ask for confirmation before erasing the files\n".
  27. " -f force: doesn't ask for confirmation before erasing\n".
  28. " -q quiet: doesn't show output\n";
  29. my $dry_run = 0;
  30. my $force = 0;
  31. my $quiet = 0;
  32. sub check_confirm()
  33. {
  34. return if ($force);
  35. open(TTY, "+< /dev/tty") or die "cannot open /dev/tty";
  36. print TTY "This will erase files and directories that aren't in Subversion\n".
  37. "Are you sure you want to continue? (y/n) ";
  38. if (<TTY> =~ /^[Yy]/) {
  39. $force = 1;
  40. close TTY;
  41. return;
  42. }
  43. # user cancelled
  44. exit 0;
  45. }
  46. # Parse arguments
  47. my $rest = 0;
  48. my @files = ();
  49. foreach my $arg (@ARGV) {
  50. if ($rest) {
  51. push @files, $arg;
  52. } else {
  53. if ($arg eq '-h' || $arg eq '--help') {
  54. print $usage;
  55. exit (0);
  56. } elsif ($arg eq '-n' || $arg eq '--dry-run') {
  57. $dry_run = 1;
  58. $force = 1;
  59. } elsif ($arg eq '-f' || $arg eq '--force') {
  60. $force = 1;
  61. } elsif ($arg eq '-i' || $arg eq '--interactive') {
  62. $force = 0;
  63. } elsif ($arg eq '-q' || $arg eq '--quiet') {
  64. $quiet = 1;
  65. } elsif ($arg eq '--') {
  66. $rest = 1;
  67. } elsif ($arg =~ /^-/) {
  68. print STDERR "svn-clean: unknown argument '".$arg."'\n\n".$usage;
  69. exit (1);
  70. } else {
  71. push @files, $arg;
  72. }
  73. }
  74. }
  75. if (!@files) {
  76. push @files, '.';
  77. }
  78. # Unset TERM just so that no colours are output
  79. # in case $SVN points to colorsvn
  80. delete $ENV{'TERM'};
  81. #print($heading."\n") unless $quiet;
  82. foreach my $dir (@files) {
  83. open SVN, "svn status --no-ignore \"".$dir."\"|";
  84. while (<SVN>) {
  85. /^[I?] +(.*)$/ or next;
  86. my $file = $1;
  87. check_confirm();
  88. lstat $file;
  89. if (-d _) {
  90. print("D ".$file."\n") unless $quiet;
  91. rmtree($file, 0, 0) unless $dry_run;
  92. } else {
  93. print("F ".$file."\n") unless $quiet;
  94. unlink($file) unless $dry_run;
  95. }
  96. }
  97. close SVN;
  98. }