user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 3/3] search: allow searching within mail diffs
Date: Wed, 14 Jun 2017 00:14:48 +0000	[thread overview]
Message-ID: <20170614001448.27098-4-e@80x24.org> (raw)
In-Reply-To: <20170614001448.27098-1-e@80x24.org>

This can be tied into a repository browser to browse
in-flight topics on a mailing list.
---
 lib/PublicInbox/Search.pm    |  20 +++++--
 lib/PublicInbox/SearchIdx.pm | 137 +++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 148 insertions(+), 9 deletions(-)

diff --git a/lib/PublicInbox/Search.pm b/lib/PublicInbox/Search.pm
index 67837f4..c7c5455 100644
--- a/lib/PublicInbox/Search.pm
+++ b/lib/PublicInbox/Search.pm
@@ -75,6 +75,14 @@ my %prob_prefix = (
 
 	q => 'XQUOT',
 	nq => 'XNQ',
+	dfn => 'XDFN',
+	dfa => 'XDFA',
+	dfb => 'XDFB',
+	dfhh => 'XDFHH',
+	dfctx => 'XDFCTX',
+	dfpre => 'XDFPRE',
+	dfpost => 'XDFPOST',
+	dfblob => 'XDFPRE XDFPOST',
 
 	# default:
 	'' => 'XMID S A XNQ XQUOT XFN',
@@ -98,12 +106,16 @@ EOF
 	'a:' => 'match within the To, Cc, and From headers',
 	'tc:' => 'match within the To and Cc headers',
 	'bs:' => 'match within the Subject and body',
+	'dfn:' => 'match filename from diff',
+	'dfa:' => 'match diff removed (-) lines',
+	'dfb:' => 'match diff added (+) lines',
+	'dfhh:' => 'match diff hunk header context (usually a function name)',
+	'dfctx:' => 'match diff context lines',
+	'dfpre:' => 'match pre-image git blob ID',
+	'dfpost:' => 'match post-image git blob ID',
+	'dfblob:' => 'match either pre or post-image git blob ID',
 );
 chomp @HELP;
-# TODO:
-# df (filenames from diff)
-# da (diff a/ removed lines)
-# db (diff b/ added lines)
 
 my $mail_query = Search::Xapian::Query->new('T' . 'mail');
 
diff --git a/lib/PublicInbox/SearchIdx.pm b/lib/PublicInbox/SearchIdx.pm
index 30d3fe9..9ba9437 100644
--- a/lib/PublicInbox/SearchIdx.pm
+++ b/lib/PublicInbox/SearchIdx.pm
@@ -28,8 +28,28 @@ use constant {
 	PERM_GROUP => 0660,
 	PERM_EVERYBODY => 0664,
 	BATCH_BYTES => 1_000_000,
+	DEBUG => !!$ENV{DEBUG},
 };
 
+my %GIT_ESC = (
+	a => "\a",
+	b => "\b",
+	f => "\f",
+	n => "\n",
+	r => "\r",
+	t => "\t",
+	v => "\013",
+);
+
+sub git_unquote ($) {
+	my ($s) = @_;
+	return $s unless ($s =~ /\A"(.*)"\z/);
+	$s = $1;
+	$s =~ s/\\([abfnrtv])/$GIT_ESC{$1}/g;
+	$s =~ s/\\([0-7]{1,3})/chr(oct($1))/ge;
+	$s;
+}
+
 sub new {
 	my ($class, $inbox, $creat) = @_;
 	my $git_dir = $inbox;
@@ -134,11 +154,108 @@ sub index_users ($$) {
 	$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, $inc) = @_;
-	$tg->index_text(join("\n", @$lines), $inc, $inc ? 'XNQ' : 'XQUOT');
-	@$lines = ();
+	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 = ();
 }
 
 sub add_message {
@@ -205,7 +322,7 @@ sub add_message {
 			my @lines = split(/\n/, $body);
 			while (defined(my $l = shift @lines)) {
 				if ($l =~ /^>/) {
-					index_body($tg, \@orig, 1) if @orig;
+					index_body($tg, \@orig, $doc) if @orig;
 					push @quot, $l;
 				} else {
 					index_body($tg, \@quot, 0) if @quot;
@@ -213,7 +330,7 @@ sub add_message {
 				}
 			}
 			index_body($tg, \@quot, 0) if @quot;
-			index_body($tg, \@orig, 1) if @orig;
+			index_body($tg, \@orig, $doc) if @orig;
 		});
 
 		link_message($self, $smsg, $old_tid);
@@ -339,6 +456,16 @@ sub index_blob {
 	$self->add_message($mime, $bytes, $num, $blob);
 }
 
+sub index_git_blob_id {
+	my ($doc, $pfx, $objid) = @_;
+
+	my $len = length($objid);
+	for (my $len = length($objid); $len >= 7; ) {
+		$doc->add_term($pfx.$objid);
+		$objid = substr($objid, 0, --$len);
+	}
+}
+
 sub unindex_blob {
 	my ($self, $mime) = @_;
 	my $mid = eval { mid_clean(mid_mime($mime)) };
-- 
EW


      parent reply	other threads:[~2017-06-14  0:14 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-14  0:14 [PATCH 0/3] search improvements Eric Wong
2017-06-14  0:14 ` [PATCH 1/3] search: remove unnecessary abstractions and functionality Eric Wong
2017-06-15 23:11   ` [PATCH 4/3] searchidx: remove messages correctly from Xapian index Eric Wong
2017-06-14  0:14 ` [PATCH 2/3] searchidx: switch to accounting by message bytes Eric Wong
2017-06-14  0:14 ` Eric Wong [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170614001448.27098-4-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).