summaryrefslogtreecommitdiff
path: root/bin/cvslog
blob: 1330db622f8f9852774308b86646a1c97484b767 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
    & eval 'exec perl -S $0 $argv:q'
    if 0;

# $Id$
#
# Wraps cvs log, and substitutes messages of form "ChangeLogTag"
# with their corresponding ChangeLog entries.
#
# Authors:  Luther J. Baker and David L. Levine

####
#### global
####
use strict;
my $dir_sep = $^O eq 'MSWin32'  ?  '\\'  :  '/';
my $cvs_log_options = '';
my @changelogs = ();
my %changelog_hash = ();


####
#### main (I do this for reading clarity)
####
{
  ####
  #### Save any command line options (beginning with -), to pass to cvs log.
  ####
  while ($#ARGV >= $[  &&  $ARGV[0] =~ /^-/) {
    $cvs_log_options .= $cvs_log_options  ?  " " . shift  :  shift;
  }

  ####
  #### Build up the array of ChangeLog files to search,
  ####
  &find_changelogs($ARGV[0]);

  ####
  #### Build the hash table of key=tags value=entry
  ####
  &build_changelog_hash();


  ####
  #### Print the cvs log for each filename argument.
  #### Inserting expanded entries after ChangeLog tags
  ####
  foreach my $arg (@ARGV) {
    &print_log ($arg);
  }
}


####
#### Function surrounding cvs log
####
sub print_log () {
  my $file = shift;

  open (CVSLOG, "cvs log $cvs_log_options $file |")  ||
    die "$0: unable to open cvs log\n";

  while (<CVSLOG>) {

    if (/ChangeLog(Tag)?: *(.*)/i  ||
        /ChangeLog( *Entry)?: *(.*)/i) {

      chomp;
      print "$_:\n";

      # An array reference HAS to be defined, the following will NOT work
      # print "$changelog_hash{$2})" || "ChangeLogTag NOT FOUND!!!!\n";

      if (defined $changelog_hash{$2}) {
        print "@{$changelog_hash{$2}}";
      } else {
        print "\n\tChangeLogTag \"$2\" NOT FOUND!!!!\n\n";
      }

    } else {
      print;
    }
  }

  close CVSLOG;
}


####
#### Build the hash
####
sub build_changelog_hash () {
  my $key = 0;
  my @entry = ();

  foreach my $changelog_file (@changelogs) {

    open (CHANGELOG, $changelog_file)  ||
      die "$0: unable to open '$changelog_file'\n";

    while (<CHANGELOG>) {
      if (/^\w/) {
        if ($key) {
          if (defined $changelog_hash{$key}) {
            #### Deal with multiple identical ChangeLogTags.
            push @{$changelog_hash{$key}}, @entry;
          } else {
            $changelog_hash{$key} = [ @entry ];
          }
        }
        @entry = ();
        chomp;
        $key = $_;
      }
      else {
        push @entry, $_;
      }
    }

    close CHANGELOG;
  }
}


####
#### Find the ChangeLog(s) associated with the file.
####
sub find_changelogs () {
  my $file = shift;

  if ($#changelogs >= 0) {
    @changelogs;
  } else {
    my $pwd = &basename ($file)  ||  '.';

    #### The [C] ensures that the glob will actually look for the file.
    while (! (@changelogs =
              glob ("$pwd/[C]hangeLog " .
                    "$pwd/[C]hangeLog-97 " .   #### ACE_wrappers/TAO
                    "$pwd/[C]hangeLog-97b " .  #### ACE_wrappers
                    "$pwd/ChangeLog-9[89]* " .
                    "$pwd/ChangeLog-0*"))) {
      if ($pwd !~ m%^${dir_sep}%) {
        #### We're starting with a relative path.  Get the
        #### absolute path.
        chomp ($pwd = `pwd`);
        $pwd .= "/$file";
      }

      $pwd = &basename ($pwd);

      if ($pwd eq '') {
        warn "$0: ChangeLog NOT FOUND for '$file'!!!!\n";
        return ();
      }
    }
  }
}


####
#### Return directory component of a filename, without trailing $dir_sep.
#### Return '' if there is no directory component.
####
sub basename () {
  my $filename = shift;

  $filename =~ s%[${dir_sep}][^${dir_sep}]+$%%  ?  $filename  :  '';
}