2 # usage: tb-list [<patch-spec>]
3 # lists all patches matching <patch-spec> and other criteria
5 xxx needs updating for new metadata and new theory
13 #----- option parsing -----
15 Getopt::Long::Configure(qw(bundling));
24 GetOptions("d|deleted!" => \$deleted, # including deleted patches
25 "deleted-only!" => \$deleted_only, # only deleted patches
26 "r|related=s" => sub { # only patches related to that
27 $relatedto = parse_patch_spec($_[1]);
29 "c|related-current" => sub { # only patches related to current
30 my $current_branch = current_branch();
31 die "-c only permitted when on a topbloke branch\n"
32 unless $current_branch->{Kind} =~ m/^(?:tip|base)$/;
33 $relatedto = $current_branch;
35 "l|last|leaf|leaves" => \$leaves, # only leaf patches
37 ) or die "bad options\n";
42 $spec = parse_patch_spec($ARGV[0]);
45 die "too many arguments\n";
48 our @sort = grep { /./ } split m/,/, $sort;
49 push @sort, 'topo' if !$spec;
50 push @sort, 'created';
52 foreach $sort (@sort) {
53 die "bad sort $sort\n" unless grep { $_ eq $sort }
54 qw(fullname created nick topo);
55 $toposort=1 if $sort eq $toposort;
58 #----- list patches -----
62 foreach_patch($relatedto || $leaves || !$spec ? { } : $spec,
63 xxx new api for foreach_patch
64 $deleted || $deleted_only,
65 [0, !!$leaves, 0, $toposort || !!$relatedto],
67 my ($patch,$parsedname,@info) = @_;
68 $patches{$patch}{Info} = \@info;
69 $patches{$patch}{ParsedName} = $parsedname;
72 #----- selection -----
76 foreach my $p (keys %patches) {
79 next if $v->{Info}[0]{Deleted};
80 foreach my $dep (keys %{ $v->{Info}[1] }) {
81 debug("leaves $p $dep");
82 next unless exists $patches{$dep};
83 $patches{$dep}{NotLeaf} = 1;
89 foreach my $p (keys %patches) {
91 # mark Related=1 if any patch matching $relatedto includes us
92 foreach my $dep (keys %{ $v->{Info}[3] }) {
93 next unless exists $patches{$dep};
94 my $depv = $patches{$dep};
95 next unless patch_matches_spec($depv->{ParsedName}, $relatedto);
99 if (patch_matches_spec($v->{ParsedName}, $relatedto)) {
100 # if we match $relatedto, mark all our inclusions as Related=1
101 foreach my $dep (keys %{ $v->{Info}[3] }) {
102 next unless exists $patches{$dep};
103 $patches{$dep}{Related} = 1;
105 # oh, and mark ourselves as Related=1 too!
113 foreach my $p (keys %patches) {
114 my $v = $patches{$p};
115 next if !$deleted && $v->{Info}[0]{Deleted};
116 next if $deleted_only && !$v->{Info}[0]{Deleted};
117 next if $leaves && $v->{NotLeaf};
118 next if $relatedto && !$v->{Related};
119 next if $spec && !patch_matches_spec($v->{ParsedName}, $spec);
126 my $txt = "sub sort_cmp {\n my \$r;\n";
130 foreach my $ab (qw(a b)) {
131 $txt .= " my \$v$ab = \$patches{\$$ab};\n";
136 $txt .= " \$r = $_[0];\n return \$r if \$r;\n";
138 my $by_parsed = sub {
140 $by_r->("\$va->{ParsedName}{$_[0]} cmp \$vb->{ParsedName}{$_[0]}");
143 foreach my $sort (@sort) {
144 next if $done{$sort}++;
145 if ($sort eq 'fullname') {
146 $by_r->('$a cmp $b');
147 } elsif ($sort eq 'created') {
148 $by_parsed->('Date');
149 } elsif ($sort eq 'nick') {
150 $by_parsed->('Nick');
151 } elsif ($sort eq 'topo') {
153 foreach my $ix (qw(0 1)) {
154 my $ab = (qw(a b))[$ix];
155 my $ba = (qw(b a))[$ix];
156 my $r = (qw(1 -1))[$ix];
157 $txt .= " return $r if \$v${ab}->{Info}[3]{\$$ba};\n";
163 $txt .= " return 0;\n}\n";
164 debug("sortsub | $_") foreach split /\n/, $txt;
167 eval sortsub()." 1;" or die "$@ ?";
169 @output = sort sort_cmp @output;
171 #----- printing -----
173 my $current_branch = current_branch();
174 my $current_patch = '';
176 if ($current_branch->{Kind} eq 'tip') {
177 $current_patch = $current_branch->{Fullname};
179 } elsif ($current_branch->{Kind} eq 'base') {
180 $current_patch = $current_branch->{Fullname};
184 foreach my $p (@output) {
185 my $v = $patches{$p};
186 my $pa = $v->{ParsedName};
187 my ($msgkind, $msg) = git_get_object("$pa->{Ref}:.topbloke/msg");
188 die "$p $msgkind ?" unless $msgkind eq 'blob';
190 $msg =~ m/^Subject:\s*(?:\[[^][]*\]\s*)?(.*\S)\s*$/mi
191 ? $1 : "[no subject]";
192 printf("%1s%1s %s\@%s/%s/%-20s %s\n",
193 $p eq $current_patch ? $ifcurrent : '',
194 $v->{Info}[0]{Deleted} ? 'D' : '',
195 $pa->{Email}, $pa->{Domain}, $pa->{Date}, $pa->{Nick},