[PATCH] notmuch restore --accumulate

Thomas Schwinge thomas at schwinge.name
Mon Sep 5 12:07:17 PDT 2011


From: Thomas Schwinge <thomas at schwinge.name>

Also enhance the dump-restore testsuite, and make it generally more
failure-proof.

Signed-off-by: Thomas Schwinge <thomas at schwinge.name>

---

Hi!

Beware that I have not yet used this new functionality in the wild.  ;-)
(But I do plan to do so, soon.)  And, I think that the testsuite
enhancements cover quite a number of real-world scenarios.


Grüße,
 Thomas

---

 NEWS              |   13 ++++++++++
 notmuch-restore.c |   42 ++++++++++++++++++++++++++-------
 notmuch.1         |   14 ++++++++---
 notmuch.c         |   10 +++++--
 test/dump-restore |   67 ++++++++++++++++++++++++++++++++++++++++------------
 test/test-lib.sh  |    1 +
 6 files changed, 115 insertions(+), 32 deletions(-)

diff --git a/NEWS b/NEWS
index f715142..d2a788f 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,16 @@
+Notmuch TODO (TODO)
+===================
+
+New command-line features
+-------------------------
+
+Add "notmuch restore --accumulate" option
+
+  The --accumulate switch causes the union of the existing and new tags to be
+  applied, instead of replacing each message's tags as they are read in from
+  the dump file.
+
+
 Notmuch 0.7 (2011-08-01)
 ========================
 
diff --git a/notmuch-restore.c b/notmuch-restore.c
index f095f64..5aad60c 100644
--- a/notmuch-restore.c
+++ b/notmuch-restore.c
@@ -31,7 +31,8 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
     size_t line_size;
     ssize_t line_len;
     regex_t regex;
-    int rerr;
+    notmuch_bool_t accumulate;
+    int i, rerr;
 
     config = notmuch_config_open (ctx, NULL, NULL);
     if (config == NULL)
@@ -44,14 +45,28 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
 
     synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
 
-    if (argc) {
-	input = fopen (argv[0], "r");
-	if (input == NULL) {
-	    fprintf (stderr, "Error opening %s for reading: %s\n",
-		     argv[0], strerror (errno));
-	    return 1;
+    accumulate = FALSE;
+    input = NULL;
+    for (i = 0; i < argc; i++) {
+	if (STRNCMP_LITERAL (argv[i], "--accumulate") == 0) {
+	    accumulate = TRUE;
+	} else {
+	    if (input == NULL) {
+		input = fopen (argv[i], "r");
+		if (input == NULL) {
+		    fprintf (stderr, "Error opening %s for reading: %s\n",
+			     argv[i], strerror (errno));
+		    return 1;
+		}
+	    } else {
+		fprintf (stderr,
+			 "Cannot read dump from more than one file: %s\n",
+			 argv[i]);
+		return 1;
+	    }
 	}
-    } else {
+    }
+    if (input == NULL) {
 	printf ("No filename given. Reading dump from stdin.\n");
 	input = stdin;
     }
@@ -94,6 +109,13 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
 	    goto NEXT_LINE;
 	}
 
+	/* In order to detect missing messages, this check/optimization is
+	 * intentionally done *after* first finding the message.  */
+	if (accumulate && (file_tags == NULL || *file_tags == '\0'))
+	{
+	    goto NEXT_LINE;
+	}
+
 	db_tags_str = NULL;
 	for (db_tags = notmuch_message_get_tags (message);
 	     notmuch_tags_valid (db_tags);
@@ -115,7 +137,9 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
 	}
 
 	notmuch_message_freeze (message);
-	notmuch_message_remove_all_tags (message);
+
+	if (!accumulate)
+	    notmuch_message_remove_all_tags (message);
 
 	next = file_tags;
 	while (next) {
diff --git a/notmuch.1 b/notmuch.1
index 5a8c83d..883371d 100644
--- a/notmuch.1
+++ b/notmuch.1
@@ -454,7 +454,7 @@ section below for details of the supported syntax for <search-terms>.
 The
 .BR dump " and " restore
 commands can be used to create a textual dump of email tags for backup
-purposes, and to restore from that dump
+purposes, and to restore from that dump.
 
 .RS 4
 .TP 4
@@ -462,17 +462,19 @@ purposes, and to restore from that dump
 
 Creates a plain-text dump of the tags of each message.
 
-The output is to the given filename, if any, or to stdout.
+The output is written to the given filename, if any, or to stdout.
 
 These tags are the only data in the notmuch database that can't be
 recreated from the messages themselves.  The output of notmuch dump is
 therefore the only critical thing to backup (and much more friendly to
 incremental backup than the native database files.)
 .TP
-.BR restore " <filename>"
+.BR restore " [--accumulate] [<filename>]"
 
 Restores the tags from the given file (see
-.BR "notmuch dump" "."
+.BR "notmuch dump" ")."
+
+The input is read from the given filename, if any, or from stdin.
 
 Note: The dump file format is specifically chosen to be
 compatible with the format of files produced by sup-dump.
@@ -480,6 +482,10 @@ So if you've previously been using sup for mail, then the
 .B "notmuch restore"
 command provides you a way to import all of your tags (or labels as
 sup calls them).
+
+The --accumulate switch causes the union of the existing and new tags to be
+applied, instead of replacing each message's tags as they are read in from the
+dump file.
 .RE
 
 The
diff --git a/notmuch.c b/notmuch.c
index 3973e35..def52b0 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -377,20 +377,24 @@ static command_t commands[] = {
     { "dump", notmuch_dump_command,
       "[<filename>]",
       "Create a plain-text dump of the tags for each message.",
-      "\tOutput is to the given filename, if any, or to stdout.\n"
+      "\tOutput is written to the given filename, if any, or to stdout.\n"
       "\tThese tags are the only data in the notmuch database\n"
       "\tthat can't be recreated from the messages themselves.\n"
       "\tThe output of notmuch dump is therefore the only\n"
       "\tcritical thing to backup (and much more friendly to\n"
       "\tincremental backup than the native database files.)" },
     { "restore", notmuch_restore_command,
-      "<filename>",
+      "[--accumulate] [<filename>]",
       "Restore the tags from the given dump file (see 'dump').",
+      "\tInput is read from the given filename, if any, or from stdin.\n"
       "\tNote: The dump file format is specifically chosen to be\n"
       "\tcompatible with the format of files produced by sup-dump.\n"
       "\tSo if you've previously been using sup for mail, then the\n"
       "\t\"notmuch restore\" command provides you a way to import\n"
-      "\tall of your tags (or labels as sup calls them)." },
+      "\tall of your tags (or labels as sup calls them).\n"
+      "\tThe --accumulate switch causes the union of the existing and new\n"
+      "\ttags to be applied, instead of replacing each message's tags as\n"
+      "\tthey are read in from the dump file."},
     { "config", notmuch_config_command,
       "[get|set] <section>.<item> [value ...]",
       "Get or set settings in the notmuch configuration file.",
diff --git a/test/dump-restore b/test/dump-restore
index a4de370..c85f10e 100755
--- a/test/dump-restore
+++ b/test/dump-restore
@@ -4,21 +4,56 @@ test_description="\"notmuch dump\" and \"notmuch restore\""
 
 add_email_corpus
 
-test_expect_success "Dumping all tags" "generate_message &&
-notmuch new &&
-notmuch dump dump.expected"
-
-test_begin_subtest "Clearing all tags"
-sed -e "s/(\([^(]*\))$/()/" < dump.expected > clear.expected
-notmuch restore clear.expected
-notmuch dump clear.actual
-test_expect_equal "$(< clear.actual)" "$(< clear.expected)"
-
-test_begin_subtest "Restoring original tags"
-notmuch restore dump.expected
-notmuch dump dump.actual
-test_expect_equal "$(< dump.actual)" "$(< dump.expected)"
-
-test_expect_success "Restore with nothing to do" "notmuch restore dump.expected"
+test_expect_success 'Dumping all tags' \
+  'generate_message &&
+  notmuch new &&
+  notmuch dump dump.expected'
+
+# This is rather arbitrary: it matches some of the email corpus' messages, but
+# not all of them.
+search_term=from:worth
+
+test_expect_success 'Dumping all tags to stdout' \
+  'notmuch tag +ABC +DEF -- $search_term &&
+  notmuch dump > dump-ABC_DEF.expected &&
+  ! cmp dump.expected dump-ABC_DEF.expected'
+
+test_expect_success 'Clearing all tags' \
+  'sed -e "s/(\([^(]*\))$/()/" < dump.expected > clear.expected &&
+  notmuch restore clear.expected &&
+  notmuch dump clear.actual &&
+  test_cmp clear.expected clear.actual'
+
+test_expect_success 'Accumulate original tags' \
+  'notmuch tag +ABC +DEF -- $search_term &&
+  notmuch restore --accumulate < dump.expected &&
+  notmuch dump dump.actual &&
+  test_cmp dump-ABC_DEF.expected dump.actual'
+
+test_expect_success 'Restoring original tags' \
+  'notmuch restore dump.expected &&
+  notmuch dump dump.actual &&
+  test_cmp dump.expected dump.actual'
+
+test_expect_success 'Restore with nothing to do' \
+  'notmuch restore < dump.expected &&
+  notmuch dump > dump.actual &&
+  test_cmp dump.expected dump.actual'
+
+test_expect_success 'Restore with nothing to do, II' \
+  'notmuch restore --accumulate dump.expected &&
+  notmuch dump dump.actual &&
+  test_cmp dump.expected dump.actual'
+
+test_expect_success 'Restore with nothing to do, III' \
+  'notmuch restore --accumulate < clear.expected &&
+  notmuch dump dump.actual &&
+  test_cmp dump.expected dump.actual'
+
+test_expect_success 'Invalid restore invocation' \
+  '! notmuch restore one two'
+
+test_expect_success 'Invalid restore invocation, II' \
+  '! notmuch restore --accumulate one two'
 
 test_done
diff --git a/test/test-lib.sh b/test/test-lib.sh
index 22e387e..56bbce4 100755
--- a/test/test-lib.sh
+++ b/test/test-lib.sh
@@ -461,6 +461,7 @@ test_expect_equal ()
     fi
 }
 
+# Like test_expect_equal, but takes two filenames.
 test_expect_equal_file ()
 {
 	exec 1>&6 2>&7		# Restore stdout and stderr
-- 
tg: (8e2a14b..) t/restore-accumulate (depends on: master)


More information about the notmuch mailing list