2 # usage: tb-list [<patch-spec>]
3 # lists all patches matching <patch-spec> and other criteria
11 #----- option parsing -----
13 Getopt::Long::Configure(qw(bundling));
22 GetOptions("d|deleted!" => \$deleted, # including deleted patches
23 "deleted-only!" => \$deleted_only, # only deleted patches
24 "r|related=s" => \$relatedto, # only patches related to this one
25 "l|last|leaf|leaves" => \$leaves, # only leaf patches
27 ) or die "bad options\n";
32 $spec = parse_patch_spec($ARGV[0]);
35 die "too many arguments\n";
38 our @sort = grep { /./ } split m/,/, $sort;
39 push @sort, 'topo' if !$spec;
40 push @sort, 'created';
42 foreach $sort (@sort) {
43 die "bad sort $sort\n" unless grep { $_ eq $sort }
44 qw(fullname created nick topo);
45 $toposort=1 if $sort eq $toposort;
48 $relatedto = $relatedto ? parse_patch_spec($relatedto) : undef;
50 #----- list patches -----
54 foreach_patch($relatedto || $leaves || !$spec ? { } : $spec,
55 $deleted || $deleted_only,
56 [0, !!$leaves, 0, $toposort || !!$relatedto],
58 my ($patch,$parsedname,@info) = @_;
59 $patches{$patch}{Info} = \@info;
60 $patches{$patch}{ParsedName} = $parsedname;
63 #----- selection -----
66 foreach my $p (keys %patches) {
68 next if $v->{Info}[0]{Deleted};
69 foreach my $dep (keys %{ $v->{Info}[1] }) {
70 next unless exists $patches{$dep};
71 $patches{$dep}{NotLeaf} = 1;
77 foreach my $p (keys %patches) {
79 # mark Related=1 if any patch matching $relatedto includes us
80 foreach my $dep (keys %{ $v->{Info}[3] }) {
81 next unless exists $patches{$dep};
82 my $depv = $patches{$dep};
83 next unless patch_matches_spec($depv->{ParsedName}, $relatedto);
87 if (patch_matches_spec($v->{ParsedName}, $relatedto)) {
88 # if we match $relatedto, mark all our inclusions as Related=1
89 foreach my $dep (keys %{ $v->{Info}[3] }) {
90 next unless exists $patches{$dep};
91 $patches{$dep}{Related} = 1;
93 # oh, and mark ourselves as Related=1 too!
101 foreach my $p (keys %patches) {
102 my $v = $patches{$p};
103 next if !$deleted && $v->{Info}[0]{Deleted};
104 next if $deleted_only && !$v->{Info}[0]{Deleted};
105 next if $leaves && $v->{NotLeaf};
106 next if $relatedto && !$v->{Related};
107 next if $spec && !patch_matches_spec($v->{ParsedName}, $spec);
114 my $txt = "sub sort_cmp {\n my \$r;\n";
118 foreach my $ab (qw(a b)) {
119 $txt .= " my \$v$ab = \$patches{\$$ab};\n";
124 $txt .= " \$r = $_[0];\n return \$r if \$r;\n";
126 my $by_parsed = sub {
128 $by_r->("\$va->{ParsedName}{$_[0]} cmp \$vb->{ParsedName}{$_[0]}");
131 foreach my $sort (@sort) {
132 next if $done{$sort}++;
133 if ($sort eq 'fullname') {
134 $by_r->('$a cmp $b');
135 } elsif ($sort eq 'created') {
136 $by_parsed->('Date');
137 } elsif ($sort eq 'nick') {
138 $by_parsed->('Nick');
139 } elsif ($sort eq 'topo') {
141 foreach my $ix (qw(0 1)) {
142 my $ab = (qw(a b))[$ix];
143 my $ba = (qw(b a))[$ix];
144 my $r = (qw(-1 1))[$ix];
145 $txt .= " return $r if \$v${ab}->{Info}[3]{\$$ba};\n";
151 $txt .= " return 0;\n}\n";
152 debug("sortsub | $_") foreach split /\n/, $txt;
155 eval sortsub()." 1;" or die "$@ ?";
157 @output = sort sort_cmp @output;
159 #----- printing -----
161 my $current_branch = current_branch();
162 my $current_patch = $current_branch->{Kind} eq 'tip'
163 ? $current_branch->{Fullname} : '';
165 foreach my $p (@output) {
166 my $v = $patches{$p};
167 my $pa = $v->{ParsedName};
168 my ($msgkind, $msg) = git_get_object("$pa->{Ref}:.topbloke/msg");
169 die "$p $msgkind ?" unless $msgkind eq 'blob';
171 $msg =~ m/^Subject:\s*(?:\[[^][]*\]\s*)?(.*\S)\s*$/mi
172 ? $1 : "[no subject]";
173 printf("%1s%1s %s\@%s/%s/%-20s %s\n",
174 $p eq $current_patch ? '>' : '',
175 $v->{Info}[0]{Deleted} ? 'D' : '',
176 $pa->{Email}, $pa->{Domain}, $pa->{Date}, $pa->{Nick},