chiark / gitweb /
wip list
[topbloke.git] / tb-list.pl
1 #!/usr/bin/perl
2 # usage: tb-list [<patch-spec>]
3 #  lists all patches matching <patch-spec> and other criteria
4
5 use warnings;
6 use strict;
7
8 use Getopt::Long;
9 use Topbloke;
10
11 Getopt::Long::Configure(qw(bundling));
12
13 our $deleted=0;
14 our $deleted_only=0;
15 our $current=0;
16 our $relatedto=0;
17 our $leaves=0;
18 our $sort='';
19
20 GetOptions("d|deleted!" => \$deleted,         # including deleted patches
21            "deleted-only!" => \$deleted_only, # only deleted patches
22            "r|related=s" => \$relatedto,     # only patches related to this one
23            "l|last|leaf|leaves" => \$leaves, # only leaf patches
24            "sort=s" => \$sort,
25     ) or die "bad options\n";
26
27 our $spec;
28
29 if (@ARGV==1) {
30     $spec = parse_patch_spec($ARGV[0]);
31 } elsif (!@ARGV) {
32 } else {
33     die "too many arguments\n";
34 }
35
36 our @sort = grep { /./ } split m/,/, $sort;
37 push @sort, 'topo' if !$spec;
38 push @sort, 'created';
39 my $toposort = 0;
40 foreach $sort (@sort) {
41     die "bad sort $sort\n" unless grep { $_ eq $sort } 
42         qw(fullname created nick topo);
43     $toposort=1 if $sort eq $toposort;
44 }
45
46 our %patches;
47
48 foreach_patch($relatedto || $leaves || !$spec ? { } : $spec, 
49               $deleted || $deleted_only, 
50               [0, !!$leaves, 0, $toposort || !!$relatedto],
51               sub { 
52                   my ($patch,$parsedname,@info) = @_;
53                   $patches{$patch}{Info} = \@info;
54                   $patches{$patch}{ParsedName} = $parsedname;
55               });
56
57 if ($leaves) {
58     foreach my $p (keys %patches) {
59         my $v = $patches{$p};
60         next if $v->{Info}[0]{Deleted};
61         foreach my $dep (keys %{ $v->{Info}[1] }) {
62             next unless exists $patches{$dep};
63             $patches{$dep}{NotLeaf} = 1;
64         }
65     }
66 }
67
68 if ($relatedto) {
69     foreach my $p (keys %patches) {
70         my $v = $patches{$p};
71         # mark Related=1 if any patch matching $relatedto includes us
72         foreach my $dep (keys %{ $v->{Info}[3] }) {
73             next unless exists $patches{$dep};
74             my $depv = $patches{$dep};
75             next unless patch_matches_spec($depv->{ParsedName}, $relatedto);
76             $v->{Related} = 1;
77             last;
78         }
79         if (patch_matches_spec($v->{ParsedName}, $relatedto)) {
80             # if we match $relatedto, mark all our inclusions as Related=1
81             foreach my $dep (keys %{ $v->{Info}[3] }) {
82                 next unless exists $patches{$dep};
83                 $patches{$dep}{Related} = 1;
84             }
85         }
86     }
87 }
88
89 our @output;
90
91 foreach my $p (keys %patches) {
92     my $v = $patches{$p};
93     next if !$deleted && $v->{Info}[0]{Deleted};
94     next if $deleted_only && !$v->{Info}[0]{Deleted};
95     next if $leaves && $v->{NotLeaf};
96     next if $relatedto && !$v->{Related};
97     push @output, $p;
98 }
99
100 sub sortsub () {
101     my $txt = "sub sort_cmp {\n    my \$r;\n";
102     debug("@sort");
103     my $def_vab;
104     $def_vab = sub {
105         foreach my $ab (qw(a b)) {
106             $txt .= "    my \$v$ab = \$patches{\$$ab};\n";
107         }
108         $def_vab = sub { };
109     };
110     my $by_r = sub {
111         $txt .= "    \$r = $_[0];\n    return \$r if \$r;\n";
112     };
113     my $by_parsed = sub {
114         $def_vab->();
115         $by_r->("\$va->{ParsedName}{$_[0]} cmp \$vb->{ParsedName}{$_[0]}");
116     };
117     my %done;
118     foreach my $sort (@sort) {
119         next if $done{$sort}++;
120         if ($sort eq 'fullname') {
121             $by_r->('$a cmp $b');
122         } elsif ($sort eq 'created') {
123             $by_parsed->('Date');
124         } elsif ($sort eq 'nick') {
125             $by_parsed->('Nick');
126         } elsif ($sort eq 'topo') {
127             $def_vab->();
128             foreach my $ix (qw(0 1)) {
129                 my $ab = (qw(a b))[$ix];
130                 my $ba = (qw(b a))[$ix];
131                 my $r = (qw(-1 1))[$ix];
132                 $txt .= "    return $r if \$v${ab}->{Info}[3]{\$$ba};\n";
133             }
134         } else {
135             die $sort;
136         }
137     }
138     $txt .= "    return 0;\n}\n";
139     debug("sortsub | $_") foreach split /\n/, $txt;
140     return $txt;
141 }
142 eval sortsub()." 1;" or die "$@ ?";
143
144 @output = sort sort_cmp @output;
145
146 use Data::Dumper;
147
148 foreach my $p (@output) {
149     my $v = $patches{$p};
150     print Dumper($p, $v);
151 }