From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id YHFqCmdV8l9hDQAA0tVLHw (envelope-from ) for ; Sun, 03 Jan 2021 23:38:15 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id +BNIBmdV8l/CTQAAB5/wlQ (envelope-from ) for ; Sun, 03 Jan 2021 23:38:15 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [IPv6:2607:5300:201:3100::1657]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 8F791940222 for ; Sun, 3 Jan 2021 23:38:14 +0000 (UTC) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id A37BB29E33; Sun, 3 Jan 2021 18:37:01 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id CCA0429D6C for ; Sun, 3 Jan 2021 18:36:20 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id AD86A5FF47; Sun, 3 Jan 2021 18:36:20 -0500 (EST) Received: (nullmailer pid 126234 invoked by uid 1000); Sun, 03 Jan 2021 23:35:56 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 20/36] CLI/{search,address}: convert to new configuration framework Date: Sun, 3 Jan 2021 19:35:31 -0400 Message-Id: <20210103233547.122707-21-david@tethera.net> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210103233547.122707-1-david@tethera.net> References: <20210103233547.122707-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: QD25SESBPIMS36CAJKMY2NGRH5TWAWGN X-Message-ID-Hash: QD25SESBPIMS36CAJKMY2NGRH5TWAWGN X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -0.88 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 8F791940222 X-Spam-Score: -0.88 X-Migadu-Scanner: scn1.migadu.com X-TUID: pRDkedB0hTA6 Since we are already passing a search context around as a kind of parameter block, add a new talloc context to that to replace relying on 'config'. Convert notmuch-search and notmuch-address at the same time, because they share some code. Add a test to make sure we don't break passing configuration as a command line argument. --- notmuch-search.c | 54 ++++++++++++++++------------------------ notmuch.c | 4 +-- test/T080-search.sh | 35 ++++++++++++++++++++++++++ test/T095-address.sh | 22 ++++++++++++++++ test/T750-user-header.sh | 18 ++++++++++++++ 5 files changed, 99 insertions(+), 34 deletions(-) diff --git a/notmuch-search.c b/notmuch-search.c index 34e27058..aba22799 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -52,6 +52,7 @@ typedef enum { typedef struct { notmuch_database_t *notmuch; + void *talloc_ctx; int format_sel; sprinter_t *format; int exclude; @@ -677,28 +678,29 @@ do_search_tags (const search_context_t *ctx) } static int -_notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[]) +_notmuch_search_prepare (search_context_t *ctx, int argc, char *argv[]) { char *query_str; - unsigned int i; - char *status_string = NULL; + + if (! ctx->talloc_ctx) + ctx->talloc_ctx = talloc_new (NULL); switch (ctx->format_sel) { case NOTMUCH_FORMAT_TEXT: - ctx->format = sprinter_text_create (config, stdout); + ctx->format = sprinter_text_create (ctx->talloc_ctx, stdout); break; case NOTMUCH_FORMAT_TEXT0: if (ctx->output == OUTPUT_SUMMARY) { fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n"); return EXIT_FAILURE; } - ctx->format = sprinter_text0_create (config, stdout); + ctx->format = sprinter_text0_create (ctx->talloc_ctx, stdout); break; case NOTMUCH_FORMAT_JSON: - ctx->format = sprinter_json_create (config, stdout); + ctx->format = sprinter_json_create (ctx->talloc_ctx, stdout); break; case NOTMUCH_FORMAT_SEXP: - ctx->format = sprinter_sexp_create (config, stdout); + ctx->format = sprinter_sexp_create (ctx->talloc_ctx, stdout); break; default: /* this should never happen */ @@ -707,18 +709,6 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar notmuch_exit_if_unsupported_format (); - if (notmuch_database_open_verbose ( - notmuch_config_get_database_path (config), - NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) { - - if (status_string) { - fputs (status_string, stderr); - free (status_string); - } - - return EXIT_FAILURE; - } - notmuch_exit_if_unmatched_db_uuid (ctx->notmuch); query_str = query_string_from_args (ctx->notmuch, argc, argv); @@ -748,21 +738,20 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar } if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) { - const char **search_exclude_tags; - size_t search_exclude_tags_length; + notmuch_config_values_t *exclude_tags; notmuch_status_t status; - search_exclude_tags = notmuch_config_get_search_exclude_tags ( - config, &search_exclude_tags_length); + for (exclude_tags = notmuch_config_get_values (ctx->notmuch, NOTMUCH_CONFIG_EXCLUDE_TAGS); + notmuch_config_values_valid (exclude_tags); + notmuch_config_values_move_to_next (exclude_tags)) { - for (i = 0; i < search_exclude_tags_length; i++) { - status = notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]); + status = notmuch_query_add_tag_exclude (ctx->query, + notmuch_config_values_get (exclude_tags)); if (status && status != NOTMUCH_STATUS_IGNORED) { print_status_query ("notmuch search", ctx->query, status); return EXIT_FAILURE; } } - notmuch_query_set_omit_excluded (ctx->query, ctx->exclude); } @@ -805,7 +794,7 @@ static const notmuch_opt_desc_t common_options[] = { }; int -notmuch_search_command (notmuch_config_t *config, unused(notmuch_database_t *notmuch), int argc, char *argv[]) +notmuch_search_command (unused(notmuch_config_t *config), notmuch_database_t *notmuch, int argc, char *argv[]) { search_context_t *ctx = &search_context; int opt_index, ret; @@ -832,6 +821,7 @@ notmuch_search_command (notmuch_config_t *config, unused(notmuch_database_t *not { } }; + ctx->notmuch = notmuch; ctx->output = OUTPUT_SUMMARY; opt_index = parse_arguments (argc, argv, options, 1); if (opt_index < 0) @@ -845,8 +835,7 @@ notmuch_search_command (notmuch_config_t *config, unused(notmuch_database_t *not return EXIT_FAILURE; } - if (_notmuch_search_prepare (ctx, config, - argc - opt_index, argv + opt_index)) + if (_notmuch_search_prepare (ctx, argc - opt_index, argv + opt_index)) return EXIT_FAILURE; switch (ctx->output) { @@ -871,7 +860,7 @@ notmuch_search_command (notmuch_config_t *config, unused(notmuch_database_t *not } int -notmuch_address_command (notmuch_config_t *config, unused(notmuch_database_t *notmuch), int argc, char *argv[]) +notmuch_address_command (unused(notmuch_config_t *config), notmuch_database_t *notmuch, int argc, char *argv[]) { search_context_t *ctx = &search_context; int opt_index, ret; @@ -897,6 +886,8 @@ notmuch_address_command (notmuch_config_t *config, unused(notmuch_database_t *no { } }; + ctx->notmuch = notmuch; + opt_index = parse_arguments (argc, argv, options, 1); if (opt_index < 0) return EXIT_FAILURE; @@ -911,8 +902,7 @@ notmuch_address_command (notmuch_config_t *config, unused(notmuch_database_t *no return EXIT_FAILURE; } - if (_notmuch_search_prepare (ctx, config, - argc - opt_index, argv + opt_index)) + if (_notmuch_search_prepare (ctx, argc - opt_index, argv + opt_index)) return EXIT_FAILURE; ctx->addresses = g_hash_table_new_full (strcase_hash, strcase_equal, diff --git a/notmuch.c b/notmuch.c index 95cd4ae5..16504fc5 100644 --- a/notmuch.c +++ b/notmuch.c @@ -147,9 +147,9 @@ static command_t commands[] = { "Find and import new messages to the notmuch database." }, { "insert", notmuch_insert_command, NOTMUCH_COMMAND_DATABASE_EARLY | NOTMUCH_COMMAND_DATABASE_WRITE, "Add a new message into the maildir and notmuch database." }, - { "search", notmuch_search_command, NOTMUCH_COMMAND_CONFIG_OPEN, + { "search", notmuch_search_command, NOTMUCH_COMMAND_DATABASE_EARLY, "Search for messages matching the given search terms." }, - { "address", notmuch_address_command, NOTMUCH_COMMAND_CONFIG_OPEN, + { "address", notmuch_address_command, NOTMUCH_COMMAND_DATABASE_EARLY, "Get addresses from messages matching the given search terms." }, { "show", notmuch_show_command, NOTMUCH_COMMAND_CONFIG_OPEN, "Show all messages matching the search terms." }, diff --git a/test/T080-search.sh b/test/T080-search.sh index a3f0dead..8e82cf55 100755 --- a/test/T080-search.sh +++ b/test/T080-search.sh @@ -189,4 +189,39 @@ test_begin_subtest "parts do not have adjacent term positions" output=$(notmuch search id:termpos and '"c x"') test_expect_equal "$output" "" +backup_database +test_begin_subtest "search with alternate config" +cp notmuch-config alt-config +notmuch --config=alt-config config set search.exclude_tags foobar17 +notmuch tag -- +foobar17 '*' +output=$(notmuch --config=alt-config count '*') +test_expect_equal "$output" "0" +restore_database + +test_begin_subtest "search with saved query from config file" +query_name="test${RANDOM}" +printf "Before:\n" > OUTPUT +notmuch search query:$query_name 2>&1 | notmuch_search_sanitize >> OUTPUT +cp notmuch-config old-config +printf "\n[query]\n${query_name} = from:cworth\n" >> notmuch-config +printf "After:\n" >> OUTPUT +notmuch search query:$query_name 2>&1 | notmuch_search_sanitize >> OUTPUT +cat < EXPECTED +Before: +After: +thread:XXX 2009-11-18 [1/2] Carl Worth| Alex Botero-Lowry; [notmuch] [PATCH] Error out if no query is supplied to search instead of going into an infinite loop (attachment inbox unread) +thread:XXX 2009-11-18 [1/2] Carl Worth| Ingmar Vanhassel; [notmuch] [PATCH] Typsos (inbox unread) +thread:XXX 2009-11-18 [1/3] Carl Worth| Adrian Perez de Castro, Keith Packard; [notmuch] Introducing myself (inbox signed unread) +thread:XXX 2009-11-18 [1/3] Carl Worth| Israel Herraiz, Keith Packard; [notmuch] New to the list (inbox unread) +thread:XXX 2009-11-18 [1/3] Carl Worth| Jan Janak; [notmuch] What a great idea! (inbox unread) +thread:XXX 2009-11-18 [1/2] Carl Worth| Jan Janak; [notmuch] [PATCH] Older versions of install do not support -C. (inbox unread) +thread:XXX 2009-11-18 [1/3(4)] Carl Worth| Aron Griffis, Keith Packard; [notmuch] archive (inbox unread) +thread:XXX 2009-11-18 [1/2] Carl Worth| Keith Packard; [notmuch] [PATCH] Make notmuch-show 'X' (and 'x') commands remove inbox (and unread) tags (inbox unread) +thread:XXX 2009-11-18 [1/7] Carl Worth| Lars Kellogg-Stedman, Mikhail Gusarov, Keith Packard; [notmuch] Working with Maildir storage? (inbox signed unread) +thread:XXX 2009-11-18 [2/5] Carl Worth| Mikhail Gusarov, Keith Packard; [notmuch] [PATCH 1/2] Close message file after parsing message headers (inbox unread) +thread:XXX 2009-11-17 [1/2] Carl Worth| Alex Botero-Lowry; [notmuch] preliminary FreeBSD support (attachment inbox unread) +EOF +cp old-config notmuch-config +test_expect_equal_file EXPECTED OUTPUT + test_done diff --git a/test/T095-address.sh b/test/T095-address.sh index 817be538..ebd7c167 100755 --- a/test/T095-address.sh +++ b/test/T095-address.sh @@ -325,4 +325,26 @@ cat <EXPECTED EOF test_expect_equal_file EXPECTED OUTPUT +test_begin_subtest "saved query from config file" +query_name="test${RANDOM}" +printf "Before:\n" > OUTPUT +notmuch address --deduplicate=no --output=sender query:$query_name 2>&1 | sort >> OUTPUT +cp notmuch-config old-config +printf "\n[query]\n${query_name} = from:foo.bar@example.com\n" >> notmuch-config +printf "After:\n" >> OUTPUT +notmuch address --deduplicate=no --output=sender query:$query_name | sort >> OUTPUT +cat < EXPECTED +Before: +After: +Bar +Foo +Foo Bar +Foo Bar +Foo Bar +foo.bar@example.com +foo.bar@example.com +EOF +cp old-config notmuch-config +test_expect_equal_file EXPECTED OUTPUT + test_done diff --git a/test/T750-user-header.sh b/test/T750-user-header.sh index 204c052a..ff554b06 100755 --- a/test/T750-user-header.sh +++ b/test/T750-user-header.sh @@ -108,4 +108,22 @@ MAIL_DIR/new/04:2, EOF test_expect_equal_file EXPECTED OUTPUT +test_begin_subtest "index user header, config from file" +field_name="Test" +printf "\n[index]\nheader.${field_name} = List-Id\n" >> notmuch-config +notmuch reindex '*' +notmuch search --output=files ${field_name}:notmuch | notmuch_search_files_sanitize | sort > OUTPUT +cat < EXPECTED +MAIL_DIR/bar/baz/05:2, +MAIL_DIR/bar/baz/23:2, +MAIL_DIR/bar/baz/24:2, +MAIL_DIR/bar/cur/20:2, +MAIL_DIR/bar/new/21:2, +MAIL_DIR/bar/new/22:2, +MAIL_DIR/foo/cur/08:2, +MAIL_DIR/foo/new/03:2, +MAIL_DIR/new/04:2, +EOF +test_expect_equal_file EXPECTED OUTPUT + test_done -- 2.29.2