Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id A9C2E431FD0 for ; Mon, 5 Sep 2011 12:14:30 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_NONE=-0.0001] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Ko57vJQykeGw for ; Mon, 5 Sep 2011 12:14:28 -0700 (PDT) X-Greylist: delayed 379 seconds by postgrey-1.32 at olra; Mon, 05 Sep 2011 12:14:28 PDT Received: from smtprelay03.ispgateway.de (smtprelay03.ispgateway.de [80.67.31.37]) by olra.theworths.org (Postfix) with ESMTP id 7E473431FB6 for ; Mon, 5 Sep 2011 12:14:28 -0700 (PDT) Received: from [87.180.32.105] (helo=stokes.schwinge.homeip.net) by smtprelay03.ispgateway.de with esmtpa (Exim 4.68) (envelope-from ) id 1R0eWb-0005OW-TD for notmuch@notmuchmail.org; Mon, 05 Sep 2011 21:08:06 +0200 Received: (qmail 14781 invoked from network); 5 Sep 2011 19:07:36 -0000 Received: from kepler.schwinge.homeip.net (192.168.111.7) by stokes.schwinge.homeip.net with QMQP; 5 Sep 2011 19:07:36 -0000 Received: (nullmailer pid 20213 invoked by uid 1000); Mon, 05 Sep 2011 19:07:35 -0000 From: Thomas Schwinge To: notmuch@notmuchmail.org Subject: =?UTF-8?q?=5BPATCH=5D=20notmuch=20restore=20--accumulate?= Date: Mon, 5 Sep 2011 21:07:17 +0200 Message-Id: <1315249637-20179-1-git-send-email-thomas@schwinge.name> X-Mailer: git-send-email 1.7.5.4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Df-Sender: dGhvbWFzQHNjaHdpbmdlLm5hbWU= Cc: Thomas Schwinge X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 05 Sep 2011 19:14:30 -0000 From: Thomas Schwinge Also enhance the dump-restore testsuite, and make it generally more failure-proof. Signed-off-by: Thomas Schwinge --- 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 . 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 " " +.BR restore " [--accumulate] []" 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, "[]", "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, - "", + "[--accumulate] []", "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]
. [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)