+sub add_values ($$$) {
+ my ($smsg, $bytes, $num) = @_;
+
+ my $ts = $smsg->ts;
+ my $doc = $smsg->{doc};
+ add_val($doc, &PublicInbox::Search::TS, $ts);
+
+ defined($num) and add_val($doc, &PublicInbox::Search::NUM, $num);
+
+ defined($bytes) and add_val($doc, &PublicInbox::Search::BYTES, $bytes);
+
+ add_val($doc, &PublicInbox::Search::LINES,
+ $smsg->{mime}->body_raw =~ tr!\n!\n!);
+
+ my $yyyymmdd = strftime('%Y%m%d', gmtime($ts));
+ add_val($doc, PublicInbox::Search::YYYYMMDD, $yyyymmdd);
+}
+
+sub index_users ($$) {
+ my ($tg, $smsg) = @_;
+
+ my $from = $smsg->from;
+ my $to = $smsg->to;
+ my $cc = $smsg->cc;
+
+ $tg->index_text($from, 1, 'A'); # A - author
+ $tg->increase_termpos;
+ $tg->index_text($to, 1, 'XTO') if $to ne '';
+ $tg->increase_termpos;
+ $tg->index_text($cc, 1, 'XCC') if $cc ne '';
+ $tg->increase_termpos;
+}
+
+sub index_text_inc ($$$) {
+ my ($tg, $text, $pfx) = @_;
+ $tg->index_text($text, 1, $pfx);
+ $tg->increase_termpos;
+}
+
+sub index_old_diff_fn {
+ my ($tg, $seen, $fa, $fb) = @_;
+
+ # no renames or space support for traditional diffs,
+ # find the number of leading common paths to strip:
+ my @fa = split('/', $fa);
+ my @fb = split('/', $fb);
+ while (scalar(@fa) && scalar(@fb)) {
+ $fa = join('/', @fa);
+ $fb = join('/', @fb);
+ if ($fa eq $fb) {
+ index_text_inc($tg, $fa,'XDFN') unless $seen->{$fa}++;
+ return 1;
+ }
+ shift @fa;
+ shift @fb;
+ }
+ 0;
+}
+
+sub index_diff ($$$) {
+ my ($tg, $lines, $doc) = @_;
+ my %seen;
+ my $in_diff;
+ foreach (@$lines) {
+ if ($in_diff && s/^ //) { # diff context
+ index_text_inc($tg, $_, 'XDFCTX');
+ } elsif (/^-- $/) { # email signature begins
+ $in_diff = undef;
+ } elsif (m!^diff --git ("?a/.+) ("?b/.+)\z!) {
+ my ($fa, $fb) = ($1, $2);
+ my $fn = (split('/', git_unquote($fa), 2))[1];
+ index_text_inc($tg, $fn, 'XDFN') unless $seen{$fn}++;
+ $fn = (split('/', git_unquote($fb), 2))[1];
+ index_text_inc($tg, $fn, 'XDFN') unless $seen{$fn}++;
+ $in_diff = 1;
+ # traditional diff:
+ } elsif (m/^diff -(.+) (\S+) (\S+)$/) {
+ my ($opt, $fa, $fb) = ($1, $2, $3);
+ # only support unified:
+ next unless $opt =~ /[uU]/;
+ $in_diff = index_old_diff_fn($tg, \%seen, $fa, $fb);
+ } elsif (m!^--- ("?a/.+)!) {
+ my $fn = (split('/', git_unquote($1), 2))[1];
+ index_text_inc($tg, $fn, 'XDFN') unless $seen{$fn}++;
+ $in_diff = 1;
+ } elsif (m!^\+\+\+ ("?b/.+)!) {
+ my $fn = (split('/', git_unquote($1), 2))[1];
+ index_text_inc($tg, $fn, 'XDFN') unless $seen{$fn}++;
+ $in_diff = 1;
+ } elsif (/^--- (\S+)/) {
+ $in_diff = $1;
+ } elsif (defined $in_diff && /^\+\+\+ (\S+)/) {
+ $in_diff = index_old_diff_fn($tg, \%seen, $in_diff, $1);
+ } elsif ($in_diff && s/^\+//) { # diff added
+ index_text_inc($tg, $_, 'XDFB');
+ } elsif ($in_diff && s/^-//) { # diff removed
+ index_text_inc($tg, $_, 'XDFA');
+ } elsif (m!^index ([a-f0-9]+)\.\.([a-f0-9]+)!) {
+ my ($ba, $bb) = ($1, $2);
+ index_git_blob_id($doc, 'XDFPRE', $ba);
+ index_git_blob_id($doc, 'XDFPOST', $bb);
+ $in_diff = 1;
+ } elsif (/^@@ (?:\S+) (?:\S+) @@\s*$/) {
+ # traditional diff w/o -p
+ } elsif (/^@@ (?:\S+) (?:\S+) @@\s*(\S+.*)$/) {
+ # hunk header context
+ index_text_inc($tg, $1, 'XDFHH');
+ # ignore the following lines:
+ } elsif (/^(?:dis)similarity index/) {
+ } elsif (/^(?:old|new) mode/) {
+ } elsif (/^(?:deleted|new) file mode/) {
+ } elsif (/^(?:copy|rename) (?:from|to) /) {
+ } elsif (/^(?:dis)?similarity index /) {
+ } elsif (/^\\ No newline at end of file/) {
+ } elsif (/^Binary files .* differ/) {
+ } elsif ($_ eq '') {
+ $in_diff = undef;
+ } else {
+ warn "non-diff line: $_\n" if DEBUG && $_ ne '';
+ $in_diff = undef;
+ }
+ }
+}
+
+sub index_body ($$$) {
+ my ($tg, $lines, $doc) = @_;
+ my $txt = join("\n", @$lines);
+ $tg->index_text($txt, !!$doc, $doc ? 'XNQ' : 'XQUOT');
+ $tg->increase_termpos;
+ # does it look like a diff?
+ if ($doc && $txt =~ /^(?:diff|---|\+\+\+) /ms) {
+ $txt = undef;
+ index_diff($tg, $lines, $doc);
+ }
+ @$lines = ();
+}
+