5 use File::Glob ':glob';
8 # Use this script to determine the reviewers for a given commit, like so:
10 # ayourtch@ayourtch-lnx:~/vpp$ ./extras/scripts/vpp-review HEAD
11 # commit 7ea63c597f82412b68b5a565df10e13669b1decb
12 # Author: Andrew Yourtchenko <ayourtch@gmail.com>
13 # Date: Wed Jul 14 22:44:05 2021 +0200
15 # misc: experimental script to get the list of the reviewers for a commit
17 # accepts zero or one argument (the commit hash), and outputs
18 # the detected components, the component maintainers,
19 # and the final suggested reviewer list
21 # Change-Id: Ief671fe837c6201bb11fd05d02af881822b0bb33
23 # Signed-off-by: Andrew Yourtchenko <ayourtch@gmail.com>
25 # :000000 100755 000000000 635dde59f A extras/scripts/vpp-review
28 # Components of files in the commit:
29 # misc: extras/scripts/vpp-review
31 # Component misc maintainers:
32 # vpp-dev Mailing List <vpp-dev@fd.io>
35 # Final reviewer list:
36 # vpp-dev Mailing List <vpp-dev@fd.io>
37 # ayourtch@ayourtch-lnx:~/vpp$
41 # Read the maintainers file into a hash, indexed by short component name.
43 sub read_maintainers() {
44 open(F, "MAINTAINERS") || die("Could not open MAINTAINERS file");
50 my $exclude_paths = [];
51 my $feature_yaml = [];
57 # print ("LINE: $aLine\n");
58 if (/^([A-Z]):\s+(.*)$/) {
61 # print ("LETTER: $aLetter, VALUE: $aValue\n");
62 if ($aLetter eq "I") {
63 $short_name = $aValue;
65 elsif ($aLetter eq "M") {
66 push(@{$maintainers}, $aValue);
68 elsif ($aLetter eq "F") {
69 push(@{$paths}, $aValue);
71 elsif ($aLetter eq "E") {
72 push(@{$exclude_paths}, $aValue);
74 elsif ($aLetter eq "Y") {
75 push(@{$feature_yaml}, $aValue);
77 elsif ($aLetter eq "C") {
78 push(@{$comments}, $aValue);
80 print ("LETTER: $aLetter, VALUE: $aValue\n");
82 # FIXME: deal with all the other letters here
86 if ($short_name ne "") {
88 $comp->{'short_name'} = $short_name;
89 $comp->{'name'} = $long_name;
90 $comp->{'maintainers'} = $maintainers;
91 $comp->{'paths'} = $paths;
92 $comp->{'comments'} = $comments;
93 $comp->{'exclude_paths'} = $exclude_paths;
94 $comp->{'feature_yaml'} = $feature_yaml;
95 $out->{$short_name} = $comp;
96 # print("FEATURE: $short_name => $long_name\n");
107 } elsif (/^([^\s].*)$/) {
115 if ($short_name ne "") {
117 $comp->{'short_name'} = $short_name;
118 $comp->{'name'} = $long_name;
119 $comp->{'maintainers'} = $maintainers;
120 $comp->{'paths'} = $paths;
121 $comp->{'comments'} = $comments;
122 $comp->{'exclude_paths'} = $exclude_paths;
123 $comp->{'feature_yaml'} = $feature_yaml;
124 $out->{$short_name} = $comp;
140 my $pattern = "$root$p1";
142 if ($apath =~ /$pattern/) {
149 sub match_path_to_list {
150 my $path_list = $_[0];
153 foreach $p (@{$path_list}) {
154 if (match_my_path($p, $apath, $root)) {
161 sub match_path_to_comp_hash {
165 my $is_included = match_path_to_list($chash->{'paths'}, $apath, $root);
166 my $is_excluded = match_path_to_list($chash->{'exclude_paths'}, $apath, $root);
167 return ($is_included && !$is_excluded);
172 my $components = $_[0];
177 foreach $aCompName (keys %{$components}) {
178 my $chash = $components->{$aCompName};
179 if (match_path_to_comp_hash($chash, $apath, $root)) {
180 push(@{$out}, $aCompName);
185 if (scalar @{$out} > 1) {
186 # not very efficient way to filter out "misc" but oh well.
188 foreach $aval (@{$out}) {
189 if ($aval ne "misc") {
190 push(@{$out1}, $aval);
198 my $commit_id = $ARGV[0];
199 my $commit_log = `git log --raw -n 1 $commit_id`;
201 foreach my $aLine (split(/[\r\n]+/, $commit_log)) {
202 if ($aLine =~ /^:[0-7]+\s+[0-7]+\s+[0-9a-f]+\s+[0-9a-f]+\s+\S+\s+(\S+)$/) {
207 my $comp = read_maintainers();
208 my $matched_comps = {};
209 my $matched_reviewers = {};
211 print("$commit_log\n\n");
213 print("Components of files in the commit:\n");
215 foreach $aFile (@{$files}) {
216 my $matches = match_path($comp, $aFile, '');
217 my $matches_str = join(" ", @{$matches});
218 print (" $matches_str: $aFile\n");
220 foreach my $aComp (@{$matches}) {
221 $matched_comps->{$aComp} = 1;
225 foreach my $aKey (keys %{$matched_comps}) {
226 print("\nComponent $aKey maintainers:\n");
227 foreach my $aRV (@{$comp->{$aKey}->{'maintainers'}}) {
229 $matched_reviewers->{$aRV} = 1;
233 print("\n\nFinal reviewer list:\n");
235 foreach my $aRV (keys %{$matched_reviewers}) {
238 # print Dumper(\$comp);