[PATCH, v2] notmuch restore --accumulate

Thomas Schwinge thomas at schwinge.name
Thu Sep 29 10:37:37 PDT 2011


From: Thomas Schwinge <thomas at schwinge.name>

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.

---

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.

This is v2; it addresses Jameson's and Austin's comments.


Grüße,
 Thomas

---

 NEWS              |    9 +++++++++
 notmuch-restore.c |   42 +++++++++++++++++++++++++++++++++---------
 notmuch.1         |   14 ++++++++++----
 notmuch.c         |   10 +++++++---
 test/dump-restore |   10 ----------
 5 files changed, 59 insertions(+), 26 deletions(-)

diff --git a/NEWS b/NEWS
index ee84e9a..2a184ef 100644
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,15 @@ Correct handling of interruptions during "notmuch new"
   detect messages on resume, or leave the database in a state
   temporarily or permanently inconsistent with the mail store.
 
+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.
+
 Library changes
 ---------------
 
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 f9d6629..9a9b0a2 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 d253756..be0eb2a 100755
--- a/test/dump-restore
+++ b/test/dump-restore
@@ -24,8 +24,6 @@ test_expect_success 'Clearing all tags' \
   notmuch dump clear.actual &&
   test_cmp clear.expected clear.actual'
 
-# Missing notmuch restore --accumulate.
-test_subtest_known_broken
 test_expect_success 'Accumulate original tags' \
   'notmuch tag +ABC +DEF -- $search_term &&
   notmuch restore --accumulate < dump.expected &&
@@ -42,27 +40,19 @@ test_expect_success 'Restore with nothing to do' \
   notmuch dump > dump.actual &&
   test_cmp dump.expected dump.actual'
 
-# Missing notmuch restore --accumulate.
-test_subtest_known_broken
 test_expect_success 'Restore with nothing to do, II' \
   'notmuch restore --accumulate dump.expected &&
   notmuch dump dump.actual &&
   test_cmp dump.expected dump.actual'
 
-# Missing notmuch restore --accumulate.
-test_subtest_known_broken
 test_expect_success 'Restore with nothing to do, III' \
   'notmuch restore --accumulate < clear.expected &&
   notmuch dump dump.actual &&
   test_cmp dump.expected dump.actual'
 
-# notmuch restore currently only considers the first argument.
-test_subtest_known_broken
 test_expect_success 'Invalid restore invocation' \
   'test_must_fail notmuch restore dump.expected another_one'
 
-# The follwing test already succeeds due to notmuch restore currently only
-# considering the first argument.
 test_expect_success 'Invalid restore invocation, II' \
   'test_must_fail notmuch restore --accumulate dump.expected another_one'
 
-- 
tg: (b968722..) t/restore-accumulate (depends on: master t/restore-accumulate-test)


More information about the notmuch mailing list