[PATCH 4/4] notmuch-restore: implement --match functionality

Subject: [PATCH 4/4] notmuch-restore: implement --match functionality

Date: Sat, 29 Oct 2011 00:04:51 -0300

To: notmuch@notmuchmail.org

Cc: David Bremner

From: David Bremner


From: David Bremner <bremner@debian.org>

notmuch restore --match=<regex> will for each message in the dump
file, delete any tags from the database matching <regex> and add any
from the dump file matching <regex>.

I tried to keep this simple, so in particular turning on regex
matching disables one of the optimizations.
---
 notmuch-restore.c |   16 +++++++++++++---
 test/dump-restore |    4 ----
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/notmuch-restore.c b/notmuch-restore.c
index e5ac162..a1b6b81 100644
--- a/notmuch-restore.c
+++ b/notmuch-restore.c
@@ -64,7 +64,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
 	    break;
 	case 'm':
 	    match_enabled = TRUE;
-	    if ( xregcomp (&match_regex, optarg, REG_EXTENDED) )
+	    if ( xregcomp (&match_regex, optarg, REG_EXTENDED|REG_NOSUB) )
 		return 1;
 	    break;
 	case '?':
@@ -147,6 +147,10 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
 	{
 	    const char *tag = notmuch_tags_get (db_tags);
 
+	    if (match_enabled && !accumulate &&
+		!regexec (&match_regex, tag, 0, NULL, 0) )
+		notmuch_message_remove_tag (message, tag);
+
 	    if (db_tags_str)
 		db_tags_str = talloc_asprintf_append (db_tags_str, " %s", tag);
 	    else
@@ -155,14 +159,15 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
 
 	if (((file_tags == NULL || *file_tags == '\0') &&
 	     (db_tags_str == NULL || *db_tags_str == '\0')) ||
-	    (file_tags && db_tags_str && strcmp (file_tags, db_tags_str) == 0))
+	    (!match_enabled && file_tags && db_tags_str && 
+	     strcmp (file_tags, db_tags_str) == 0))
 	{
 	    goto NEXT_LINE;
 	}
 
 	notmuch_message_freeze (message);
 
-	if (!accumulate)
+	if (!accumulate && !match_enabled)
 	    notmuch_message_remove_all_tags (message);
 
 	next = file_tags;
@@ -170,6 +175,11 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
 	    tag = strsep (&next, " ");
 	    if (*tag == '\0')
 		continue;
+
+	    if (match_enabled &&
+		regexec (&match_regex, tag, 0, NULL, 0))
+		continue;
+
 	    status = notmuch_message_add_tag (message, tag);
 	    if (status) {
 		fprintf (stderr,
diff --git a/test/dump-restore b/test/dump-restore
index 18925a4..a4517f6 100755
--- a/test/dump-restore
+++ b/test/dump-restore
@@ -72,7 +72,6 @@ test_expect_success 'Restoring with trivial match' \
 
 sed 's/inbox\|unread\|signed//g' < dump-ABC_DEF.expected > dump-ABC_DEF-only
 
-test_subtest_known_broken
 test_expect_success 'Simulate accumulate with match' \
   'notmuch restore < dump.expected &&
    notmuch restore --match="(ABC|DEF)" < dump-ABC_DEF-only &&
@@ -80,7 +79,6 @@ test_expect_success 'Simulate accumulate with match' \
    notmuch restore < dump.expected &&
    test_cmp dump-ABC_DEF.expected dump-simulate.actual'
 
-test_subtest_known_broken
 test_expect_success 'clear only matched tags' \
   'notmuch restore < dump-ABC_DEF.expected &&
    notmuch restore --match="(ABC|DEF)" < clear.expected &&
@@ -88,7 +86,6 @@ test_expect_success 'clear only matched tags' \
    notmuch restore < dump.expected &&
    test_cmp dump.expected dump-clear-match.actual'
 
-test_subtest_known_broken
 test_expect_success 'import only matched tags' \
   'notmuch restore < dump.expected &&
    notmuch restore --match="(inbox|unread)" < dump-ABC_DEF.expected &&
@@ -99,7 +96,6 @@ test_expect_success 'import only matched tags' \
 sed 's/inbox\|unread\|signed\|ABC//g' < dump-ABC_DEF.expected > dump-DEF-only
 sed 's/inbox\|unread\|signed\|DEF//g' < dump-ABC_DEF.expected > dump-ABC-only
 
-test_subtest_known_broken
 test_expect_success 'combine --match and --accumulate' \
   'notmuch restore < dump.expected &&
    notmuch restore --match="(ABC|DEF)" < dump-ABC-only &&
-- 
1.7.6.3


Thread: