English
Deutsch
  

Code


About Matthias Nott
Business Objects
Picture Galleries
Code
Technical
Download





Locations of visitors to this page

This is a very simple perl script that adds entries to the .cvsignore file. I find it useful, as before adding a project to CVS using Eclipse, I walk through my directory structure and on the fly add things to .cvsignore.

  1. #!/usr/bin/perl -w
  2. ############################################
  3. #
  4. # Add files to the .cvsignore file.
  5. #
  6. ############################################
  7. # The file .cvsignore  is created if not yet
  8. # there. The entries are sorted. If an entry
  9. # is already there, it is not added again.
  10. #
  11. # You can use stdin like
  12. #
  13. # ls -1 *.class | cvsignore
  14. #
  15. # or you can pass command line parameters
  16. #
  17. # cvsignore *.class
  18. #
  19. # If the parameter is "-d", the entry is de-
  20. # leted;
  21. #
  22. # cvsignore -d *.class
  23. #
  24. # The  -d  can be passed at any place in the
  25. # command line:
  26. #
  27. # cvsignore *.java -d *.class
  28. #
  29. # Once you passed -d,  the rest of  the line
  30. # will be deleted until the next -d.
  31. #
  32. # -d cannot be used together with stdin.
  33. #
  34. ############################################
  35. # (C) 2006 MN Soft Industry Software
  36. #############################################
  37.  
  38. use strict;
  39.  
  40. my @cvsignore;
  41. my %cvskeys;
  42. my $op = 1;    # -1 to delete
  43.  
  44. if(-f ".cvsignore") {
  45.   open(CVS, ".cvsignore") || die "Cannot open: $!\n";
  46.   while(<CVS>) {
  47.     chomp;
  48.     $cvskeys{$_} = "";
  49.   }
  50.   close(CVS);
  51. }
  52.  
  53. if(scalar(@ARGV)>0) {
  54.   for (@ARGV) {
  55.     if(uc($_) eq "-D") {
  56.       $op = -$op;
  57.       next;
  58.     }
  59.     if($op == 1) {
  60.       addKey($_);
  61.     } else {
  62.       deleteKey($_);
  63.     }
  64.   }
  65. } else {
  66.   while(<>) {
  67.     addKey($_);
  68.   }
  69. }
  70.  
  71. open(CVS, ">.cvsignore") || die "Cannot open: $!\n";
  72. for my $key (sort keys %cvskeys) {
  73.   print CVS "$key\n";
  74. }
  75. close(CVS);
  76.  
  77. #
  78. # Add an entry to .cvsignore
  79. #
  80. sub addKey {
  81.   chomp;
  82.   if (/^(.*)\/$/) {
  83.     $_ = $1;
  84.   }
  85.   if (exists $cvskeys{$_}) {
  86.     print ".cvsignore: contained already: $_\n";
  87.   } else {
  88.     print ".cvsignore: added a new entry: $_\n";
  89.     $cvskeys{$_} = "";
  90.   }
  91. }
  92.  
  93. #
  94. # Delete an entry from .cvsignore
  95. #
  96. sub deleteKey {
  97.   chomp;
  98.   if (/^(.*)\/$/) {
  99.     $_ = $1;
  100.   }
  101.   if (exists $cvskeys{$_}) {
  102.     print ".cvsignore: deleted the entry: $_\n";
  103.     delete $cvskeys{$_};
  104.   }
  105. }

Sample call (more detailed examples are in the comments above):

  1. linux:/pgm/java/CPEclipseCP # ls -la
  2. total 38
  3. drwxr-xr-x   6 mnott users   328 Oct  8  2004 ./
  4. drwxr-xr-x  29 mnott users  1856 Jan 11 15:35 ../
  5. -rw-r--r--   1 mnott users   495 Jan 18  2005 .classpath
  6. -rw-r--r--   1 mnott users   370 Oct  8  2004 .project
  7. -rwx------   1 mnott users 11560 Oct  8  2004 LICENSE.txt*
  8. drwxr-xr-x   3 mnott users    72 Jun 29  2005 bin/
  9. -rw-r--r--   1 mnott users  6686 Jan 18  2005 build.xml
  10. drwxr-xr-x   3 mnott users    80 Oct  8  2004 distribution/
  11. drwx------   3 mnott users   352 Oct  8  2004 helpers/
  12. -rwxr-xr-x   1 mnott users   417 Oct  8  2004 make.sh*
  13. -rw-r--r--   1 mnott users  1457 Oct  8  2004 overview.html
  14. drwxr-xr-x   3 mnott users    72 Oct  8  2004 src/
  15. linux:/pgm/java/CPEclipseCP # cvsignore LICENSE.txt bin/ distribution/ helpers/
  16. .cvsignore: added a new entry: LICENSE.txt
  17. .cvsignore: added a new entry: bin
  18. .cvsignore: added a new entry: distribution
  19. .cvsignore: added a new entry: helpers
  20. linux:/pgm/java/CPEclipseCP # cat .cvsignore
  21. LICENSE.txt
  22. bin
  23. distribution
  24. helpers
  25. linux:/pgm/java/CPEclipseCP # cvsignore LICENSE.txt bin/ distribution/
  26. .cvsignore: contained already: LICENSE.txt
  27. .cvsignore: contained already: bin
  28. .cvsignore: contained already: distribution
  29. linux:/pgm/java/CPEclipseCP
  30.  
 


12.01.2006, 13:04 Copyright © 2005 MN Soft Industry Software, 108, route de la Fin, CH-1874 Champéry
Tel.: +41 797 844554; Fax: +41 860 797 844554, Responsible: Matthias Nott., mn(at)mnsoft.org
Top of Page