Added client work scripts
[clearscm.git] / clients / LynuxWorks / bin / files4cr
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         files4cr
5 # Description:  This script will go through CVS looking for files that have
6 #               the passed in CR #.
7 # Author:       Andrew@DeFaria.com
8 # Created:      Fri Dec 17 12:18:21 PST 2004
9 # Language:     Perl
10 #
11 # (c) Copyright 2004, LynxWorks Inc., all rights reserved
12 #
13 ################################################################################
14 use warnings;
15 use strict;
16
17 # Options
18 my $verbose     = 0;
19 my $debug       = 0;
20 my $execute     = 0;
21 my $local       = "";
22
23 my $cr;
24
25 sub verbose {
26   my $msg = shift;
27
28   print "$msg\n" if $verbose;
29 } # verbose
30
31 sub debug {
32   my $msg = shift;
33
34   print "DEBUG: $msg\n" if $debug;
35 } # debug
36
37 sub Usage {
38   my $msg = shift;
39
40   print "ERROR: $msg\n\n" if defined $msg;
41
42   print "Usage: files4cr [-v] [-d] [-l] [-x] [-u] <cr>\n";
43   print "\nWhere:\n\n";
44   print "\t-v:\t\tTurn on verbose mode (Default: off)\n";
45   print "\t-d:\t\tTurn on debug mode (Default: off)\n";
46   print "\t-l:\t\tLocal directory only, no recursion\n";
47   print "\t-x:\t\tTurn on execute mode (Default: off)\n";
48   print "\t-u:\t\tDisplay usage\n";
49   print "\tcr\t\tCR number to search for\n";
50   exit 1;
51 } # Usage
52
53 sub GetFiles4 {
54   my $cr        = shift;
55   my $local     = shift;
56
57   # Perform a cvs log command and grep through the output
58   print "Gathering CVS info..." if $verbose;
59   my @output = grep {
60     /^Working file: /   or
61     /^revision /        or
62     /^date: /           or
63     /^\s*CR#/           or
64     /^\s*CR /
65   } `cvs -q log $local 2>/dev/null`;
66   verbose " done";
67
68   # Now process this array. Entries may look like:
69   #
70   # Working file: <filename>
71   # revision <revision>
72   # date:...
73   # revision <revision>
74   # date:...
75   # CR Number: <cr>
76   #
77   # It's quite possible that there are no CR numbers for a file. It's also
78   # possible that there is the same CR number for multiple revisions! For
79   # example:
80   #
81   # Working file: <filename>
82   # revision 10.2
83   # date:...
84   # CR Number: 1000
85   # revision 10.1
86   # date:...
87   # CR Number: 1000
88   #
89   # In this case we want to return the <filename> and 10.2.
90
91   my %files;
92   my $filename;
93   my $revision;
94
95   while ($_ = shift @output) {
96     chomp;
97     chop if /\r/;
98
99     if (/^Working file: (.*)/) {
100       $filename = $1;
101       debug "file: $filename";
102     } elsif (/^revision (.*)/) {
103       $revision = $1;
104       debug "revision: $revision";
105     } elsif (/^date:.*state: (.*);.*/) {
106       # Check to see if dead!
107       if ($1 eq "dead") {
108         # Indicate we're dead by setting $revision to blank.
109         debug "Dead file encountered $filename";
110         $revision= "";
111       } # if
112     } elsif (/^CR Number: (\d*)$/       or
113              /^CR# (\d*)$/              or
114              /^CR # (\d*)$/             or
115              /^\s*CR (\d*)/) {
116       debug "CR: $1";
117       if ($cr eq $1) {
118         $files{$filename} = $revision;
119         debug "Set $filename: $revision";
120
121         # Now skip to next file
122         do {
123           $_ = shift @output;
124         } while @output and !/^Working file: /;
125         unshift @output, $_;
126       } # if
127     } else {
128       verbose "Unknown line encountered: $_\n";
129     } # if
130   } # foreach
131
132   return %files;
133 } # GetFiles4
134
135 sub GetWorkingRev {
136   my $filename = shift;
137
138   my @output = grep { /Working revision:/ } `cvs status $filename`;
139
140   if (defined $output [0] and $output [0] =~ /Working revision:\s*(\S*)/) {
141     return $1;
142   } # if
143
144   return undef;
145 } # GetWorkingRev
146
147 # Get args
148 while ($ARGV [0]) {
149   if ($ARGV [0] eq "-d") {
150     $debug = 1;
151   } elsif ($ARGV [0] eq "-v") {
152     $verbose = 1;
153   } elsif ($ARGV [0] eq "-l") {
154     $local = $ARGV [0];
155   } elsif ($ARGV [0] eq "-x") {
156     $execute = 1;
157   } elsif ($ARGV [0] eq "-u") {
158     Usage;
159   } # if
160
161   $cr = $ARGV [0];
162
163   shift (@ARGV);
164 } # while
165
166 Usage "No CR specified to process" if !defined $cr;
167
168 my %files = GetFiles4 $cr, $local;
169
170 foreach (keys %files) {
171   if ($files{$_} eq "") {
172     print "$_: Is dead\n";
173     next;
174   } # if
175
176   my $working_revision  = GetWorkingRev $_;
177   my $up_to_date        = 0;
178
179   if (defined $working_revision and $working_revision eq $files{$_}) {
180     $up_to_date = 1;
181   } # if
182
183   if ($execute) {
184     print "cvs update -r$files{$_}  $_";
185
186     if (!$up_to_date) {
187       `cvs update -r$files{$_} $_`;
188       print " - Updated\n";
189     } else {
190       print " - Already up to date\n";
191     } # if
192   } else {
193     print "$_: $files{$_}";
194
195     if ($up_to_date) {
196       print " - Already up to date\n";
197     } else {
198       print " - Out of date\n";
199     } # if
200   } # if
201 } # foreach
202