unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* Add support for specifying tags during "notmuch new"
@ 2014-05-02  8:15 David Edmondson
  2014-05-02  8:15 ` [PATCH 1/5] notmuch-new: Use tag_op_list_apply() rather than hand-coding the same David Edmondson
                   ` (6 more replies)
  0 siblings, 7 replies; 18+ messages in thread
From: David Edmondson @ 2014-05-02  8:15 UTC (permalink / raw)
  To: notmuch

This patch set allows a user to specify a list of tags to be
added/removed to messages discovered during "notmuch new".

Two use-cases are envisaged:
    1) A chunk of messages was just dumped into the configured
       directory by hand, and the user doesn't want the 'inbox' tag
       applied to them. Run 'notmuch new -inbox'.
    2) A periodic mail processing script wants to add new messages to
       the database, then process those newly added messages to add
       convenience tags, etc. without worrying about the user or other
       instances of the script manipulating tags at the same time. Use
       this approach:
         KEY=$RANDOM
	 notmuch new +$KEY
	 notmuch tag +notmuch tag:$KEY and to:notmuch@notmuchmail.org
	 notmuch tag +gnus tag:$KEY and to:ding@gnus.org
	 ...
	 notmuch tag -$KEY tag:$KEY

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH 1/5] notmuch-new: Use tag_op_list_apply() rather than hand-coding the same.
  2014-05-02  8:15 Add support for specifying tags during "notmuch new" David Edmondson
@ 2014-05-02  8:15 ` David Edmondson
  2014-05-02  8:15 ` [PATCH 2/5] notmuch-new: Allow the tags of new messages to be manipulated David Edmondson
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 18+ messages in thread
From: David Edmondson @ 2014-05-02  8:15 UTC (permalink / raw)
  To: notmuch

Rather than hand-coding the application of tags to new messages, use
the existing tag_op_list_apply().
---
 notmuch-new.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/notmuch-new.c b/notmuch-new.c
index d269c7c..b53401a 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -45,10 +45,9 @@ typedef struct {
     int output_is_a_tty;
     enum verbosity verbosity;
     notmuch_bool_t debug;
-    const char **new_tags;
-    size_t new_tags_length;
     const char **new_ignore;
     size_t new_ignore_length;
+    tag_op_list_t *tag_ops;
 
     int total_files;
     int processed_files;
@@ -253,7 +252,6 @@ add_file (notmuch_database_t *notmuch, const char *filename,
 	  add_files_state_t *state)
 {
     notmuch_message_t *message = NULL;
-    const char **tag;
     notmuch_status_t status;
 
     status = notmuch_database_begin_atomic (notmuch);
@@ -263,14 +261,13 @@ add_file (notmuch_database_t *notmuch, const char *filename,
     status = notmuch_database_add_message (notmuch, filename, &message);
     switch (status) {
     /* Success. */
-    case NOTMUCH_STATUS_SUCCESS:
-	state->added_messages++;
-	notmuch_message_freeze (message);
-	for (tag = state->new_tags; *tag != NULL; tag++)
-	    notmuch_message_add_tag (message, *tag);
+    case NOTMUCH_STATUS_SUCCESS:;
+	tag_op_flag_t flags = 0;
+
 	if (state->synchronize_flags)
-	    notmuch_message_maildir_flags_to_tags (message);
-	notmuch_message_thaw (message);
+	    flags |= TAG_FLAG_MAILDIR_SYNC;
+	state->added_messages++;
+	(void) tag_op_list_apply (message, state->tag_ops, flags);
 	break;
     /* Non-fatal issues (go on to next file). */
     case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
@@ -923,6 +920,8 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
     notmuch_bool_t timer_is_active = FALSE;
     notmuch_bool_t no_hooks = FALSE;
     notmuch_bool_t quiet = FALSE, verbose = FALSE;
+    const char **new_tags;
+    size_t new_tags_length;
 
     add_files_state.verbosity = VERBOSITY_NORMAL;
     add_files_state.debug = FALSE;
@@ -946,20 +945,22 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
     else if (verbose)
 	add_files_state.verbosity = VERBOSITY_VERBOSE;
 
-    add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);
+    new_tags = notmuch_config_get_new_tags (config, &new_tags_length);
     add_files_state.new_ignore = notmuch_config_get_new_ignore (config, &add_files_state.new_ignore_length);
     add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
+    add_files_state.tag_ops = tag_op_list_create (config);
     db_path = notmuch_config_get_database_path (config);
 
-    for (i = 0; i < add_files_state.new_tags_length; i++) {
+    for (i = 0; i < new_tags_length; i++) {
 	const char *error_msg;
 
-	error_msg = illegal_tag (add_files_state.new_tags[i], FALSE);
+	error_msg = illegal_tag (new_tags[i], FALSE);
 	if (error_msg) {
-	    fprintf (stderr, "Error: tag '%s' in new.tags: %s\n",
-		     add_files_state.new_tags[i], error_msg);
+	    fprintf (stderr, "Error: tag '%s' in new.tags: %s\n", new_tags[i], error_msg);
 	    return EXIT_FAILURE;
 	}
+	if (tag_op_list_append (add_files_state.tag_ops, new_tags[i], FALSE) != 0)
+	    return EXIT_FAILURE;
     }
 
     if (!no_hooks) {
-- 
1.9.2

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 2/5] notmuch-new: Allow the tags of new messages to be manipulated.
  2014-05-02  8:15 Add support for specifying tags during "notmuch new" David Edmondson
  2014-05-02  8:15 ` [PATCH 1/5] notmuch-new: Use tag_op_list_apply() rather than hand-coding the same David Edmondson
@ 2014-05-02  8:15 ` David Edmondson
  2014-05-02  8:15 ` [PATCH 3/5] notmuch-new: Manual: Add command line tags David Edmondson
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 18+ messages in thread
From: David Edmondson @ 2014-05-02  8:15 UTC (permalink / raw)
  To: notmuch

Add support for specifying a set of tags to be added/removed from
messages added to the database by 'notmuch new'. These tags are
addition to those specified in .notmuch-config. They can be used to
override the pre-configured tags (e.g. -inbox).
---
 notmuch-new.c |  5 +++++
 tag-util.c    | 20 +++++++++++++++-----
 tag-util.h    | 15 +++++++++++++++
 3 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/notmuch-new.c b/notmuch-new.c
index b53401a..855e1e2 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -951,6 +951,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
     add_files_state.tag_ops = tag_op_list_create (config);
     db_path = notmuch_config_get_database_path (config);
 
+    /* Add the configured tags to the list applied. */
     for (i = 0; i < new_tags_length; i++) {
 	const char *error_msg;
 
@@ -963,6 +964,10 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
 	    return EXIT_FAILURE;
     }
 
+    /* Add any command line tags to the list applied. */
+    if (process_tag_command_line (argc - opt_index, &argv[opt_index], add_files_state.tag_ops) < 0)
+	return EXIT_FAILURE;
+
     if (!no_hooks) {
 	ret = notmuch_run_hook (db_path, "pre-new");
 	if (ret)
diff --git a/tag-util.c b/tag-util.c
index 343c161..5ad78aa 100644
--- a/tag-util.c
+++ b/tag-util.c
@@ -151,11 +151,9 @@ parse_tag_line (void *ctx, char *line,
     return ret;
 }
 
-tag_parse_status_t
-parse_tag_command_line (void *ctx, int argc, char **argv,
-			char **query_str, tag_op_list_t *tag_ops)
+int
+process_tag_command_line (int argc, char **argv, tag_op_list_t *tag_ops)
 {
-
     int i;
 
     for (i = 0; i < argc; i++) {
@@ -173,12 +171,24 @@ parse_tag_command_line (void *ctx, int argc, char **argv,
 	msg = illegal_tag (argv[i] + 1, is_remove);
 	if (msg) {
 	    fprintf (stderr, "Error: %s\n", msg);
-	    return TAG_PARSE_INVALID;
+	    return -1;
 	}
 
 	tag_op_list_append (tag_ops, argv[i] + 1, is_remove);
     }
 
+    return i;
+}
+
+tag_parse_status_t
+parse_tag_command_line (void *ctx, int argc, char **argv,
+			char **query_str, tag_op_list_t *tag_ops)
+{
+    int i = process_tag_command_line (argc, argv, tag_ops);
+
+    if (i < 0)
+	return TAG_PARSE_INVALID;
+
     *query_str = query_string_from_args (ctx, argc - i, &argv[i]);
 
     if (*query_str == NULL) {
diff --git a/tag-util.h b/tag-util.h
index 8a4074c..9f2c18c 100644
--- a/tag-util.h
+++ b/tag-util.h
@@ -74,6 +74,21 @@ parse_tag_line (void *ctx, char *line,
 
 
 
+/* Process a command line of the following format:
+ *
+ * +<tag>|-<tag> [...] [--]
+ *
+ * Return the number of tags operations, or -1 for failure to parse.
+ *
+ * Output Parameters:
+ *	ops	contains a list of tag operations
+ *
+ * The ops argument is not cleared.
+ */
+
+int
+process_tag_command_line (int argc, char **argv, tag_op_list_t *ops);
+
 /* Parse a command line of the following format:
  *
  * +<tag>|-<tag> [...] [--] <search-terms>
-- 
1.9.2

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 3/5] notmuch-new: Manual: Add command line tags.
  2014-05-02  8:15 Add support for specifying tags during "notmuch new" David Edmondson
  2014-05-02  8:15 ` [PATCH 1/5] notmuch-new: Use tag_op_list_apply() rather than hand-coding the same David Edmondson
  2014-05-02  8:15 ` [PATCH 2/5] notmuch-new: Allow the tags of new messages to be manipulated David Edmondson
@ 2014-05-02  8:15 ` David Edmondson
  2014-05-02  8:15 ` [PATCH 4/5] NEWS: Add information about "notmuch new" " David Edmondson
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 18+ messages in thread
From: David Edmondson @ 2014-05-02  8:15 UTC (permalink / raw)
  To: notmuch

Describe the addition of command line tags for "notmuch new".
---
 doc/man1/notmuch-new.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/doc/man1/notmuch-new.rst b/doc/man1/notmuch-new.rst
index 787ed78..84203de 100644
--- a/doc/man1/notmuch-new.rst
+++ b/doc/man1/notmuch-new.rst
@@ -5,7 +5,7 @@ notmuch-new
 SYNOPSIS
 ========
 
-**notmuch** **new** [options]
+**notmuch** **new** [options] [+<*tag*>|-<*tag*> ...]
 
 DESCRIPTION
 ===========
@@ -15,7 +15,7 @@ Find and import any new messages to the database.
 The **new** command scans all sub-directories of the database,
 performing full-text indexing on new messages that are found. Each new
 message will automatically be tagged with both the **inbox** and
-**unread** tags.
+**unread** tags, as well as any tags specified on the command line.
 
 You should run **notmuch new** once after first running **notmuch
 setup** to create the initial database. The first run may take a long
-- 
1.9.2

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 4/5] NEWS: Add information about "notmuch new" command line tags.
  2014-05-02  8:15 Add support for specifying tags during "notmuch new" David Edmondson
                   ` (2 preceding siblings ...)
  2014-05-02  8:15 ` [PATCH 3/5] notmuch-new: Manual: Add command line tags David Edmondson
@ 2014-05-02  8:15 ` David Edmondson
  2014-05-02  8:15 ` [PATCH 5/5] Test: Add tests for " David Edmondson
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 18+ messages in thread
From: David Edmondson @ 2014-05-02  8:15 UTC (permalink / raw)
  To: notmuch

---
 NEWS | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/NEWS b/NEWS
index bcd311d..13ae593 100644
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,12 @@ Command-Line Interface
 
   This option suppresses the progress and summary reports.
 
+`notmuch new` allows tag addition/removal to be specified
+
+  An additional set of tags to be added or removed to messages
+  discovered by `notmuch new` can be specified. These are in addition
+  to the existing configured tags.
+
 Emacs Interface
 ---------------
 
-- 
1.9.2

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 5/5] Test: Add tests for "notmuch new" command line tags
  2014-05-02  8:15 Add support for specifying tags during "notmuch new" David Edmondson
                   ` (3 preceding siblings ...)
  2014-05-02  8:15 ` [PATCH 4/5] NEWS: Add information about "notmuch new" " David Edmondson
@ 2014-05-02  8:15 ` David Edmondson
  2014-05-02 12:11 ` [PATCH v2 0/5] Add support for specifying tags during "notmuch new" David Edmondson
  2014-05-02 15:18 ` Austin Clements
  6 siblings, 0 replies; 18+ messages in thread
From: David Edmondson @ 2014-05-02  8:15 UTC (permalink / raw)
  To: notmuch

Add a simple set of tests for adding, removing and both adding and
removing tags when running "notmuch new".
---
 test/T540-new-tags.sh | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
 create mode 100755 test/T540-new-tags.sh

diff --git a/test/T540-new-tags.sh b/test/T540-new-tags.sh
new file mode 100755
index 0000000..58e6c18
--- /dev/null
+++ b/test/T540-new-tags.sh
@@ -0,0 +1,28 @@
+#!/usr/bin/env bash
+test_description="'notmuch new' with tags"
+. ./test-lib.sh
+
+which notmuch
+
+test_begin_subtest "Add tags during new"
+generate_message
+notmuch new +happyfunball +flyfishing
+output=$(notmuch search "id:${gen_msg_id}")
+expected='thread:0000000000000001   2001-01-05 [1/1] Notmuch Test Suite; Add tags during new (flyfishing happyfunball inbox unread)'
+test_expect_equal "$output" "$expected"
+
+test_begin_subtest "Remove tags during new"
+generate_message
+notmuch new -inbox
+output=$(notmuch search "id:${gen_msg_id}")
+expected='thread:0000000000000002   2001-01-05 [1/1] Notmuch Test Suite; Remove tags during new (unread)'
+test_expect_equal "$output" "$expected"
+
+test_begin_subtest "Add and remove tags during new"
+generate_message
+notmuch new +happyfunball -inbox
+output=$(notmuch search "id:${gen_msg_id}")
+expected='thread:0000000000000003   2001-01-05 [1/1] Notmuch Test Suite; Add and remove tags during new (happyfunball unread)'
+test_expect_equal "$output" "$expected"
+
+test_done
-- 
1.9.2

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH v2 0/5] Add support for specifying tags during "notmuch new"
  2014-05-02  8:15 Add support for specifying tags during "notmuch new" David Edmondson
                   ` (4 preceding siblings ...)
  2014-05-02  8:15 ` [PATCH 5/5] Test: Add tests for " David Edmondson
@ 2014-05-02 12:11 ` David Edmondson
  2014-05-02 12:14   ` [PATCH 1/5] notmuch-new: Use tag_op_list_apply() rather than hand-coding the same David Edmondson
                     ` (5 more replies)
  2014-05-02 15:18 ` Austin Clements
  6 siblings, 6 replies; 18+ messages in thread
From: David Edmondson @ 2014-05-02 12:11 UTC (permalink / raw)
  To: notmuch

[-- Attachment #1: Type: text/plain, Size: 943 bytes --]

v1:

This patch set allows a user to specify a list of tags to be
added/removed to messages discovered during "notmuch new".

Two use-cases are envisaged:
    1) A chunk of messages was just dumped into the configured
       directory by hand, and the user doesn't want the 'inbox' tag
       applied to them. Run 'notmuch new -inbox'.
    2) A periodic mail processing script wants to add new messages to
       the database, then process those newly added messages to add
       convenience tags, etc. without worrying about the user or other
       instances of the script manipulating tags at the same time. Use
       this approach:
         KEY=$RANDOM
	 notmuch new +$KEY
	 notmuch tag +notmuch tag:$KEY and to:notmuch@notmuchmail.org
	 notmuch tag +gnus tag:$KEY and to:ding@gnus.org
	 ...
	 notmuch tag -$KEY tag:$KEY

v2: The first version synced tags to flags rather than the other way
around. Thanks to Mark for complaining at me.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 310 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* [PATCH 1/5] notmuch-new: Use tag_op_list_apply() rather than hand-coding the same.
  2014-05-02 12:11 ` [PATCH v2 0/5] Add support for specifying tags during "notmuch new" David Edmondson
@ 2014-05-02 12:14   ` David Edmondson
  2014-05-02 12:14   ` [PATCH 2/5] notmuch-new: Allow the tags of new messages to be manipulated David Edmondson
                     ` (4 subsequent siblings)
  5 siblings, 0 replies; 18+ messages in thread
From: David Edmondson @ 2014-05-02 12:14 UTC (permalink / raw)
  To: notmuch

Rather than hand-coding the application of tags to new messages, use
the existing tag_op_list_apply().
fixup.
---
 notmuch-new.c | 31 ++++++++++++++++---------------
 tag-util.c    |  8 ++++++++
 tag-util.h    | 10 +++++++---
 3 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/notmuch-new.c b/notmuch-new.c
index d269c7c..2c3d680 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -45,10 +45,9 @@ typedef struct {
     int output_is_a_tty;
     enum verbosity verbosity;
     notmuch_bool_t debug;
-    const char **new_tags;
-    size_t new_tags_length;
     const char **new_ignore;
     size_t new_ignore_length;
+    tag_op_list_t *tag_ops;
 
     int total_files;
     int processed_files;
@@ -253,7 +252,6 @@ add_file (notmuch_database_t *notmuch, const char *filename,
 	  add_files_state_t *state)
 {
     notmuch_message_t *message = NULL;
-    const char **tag;
     notmuch_status_t status;
 
     status = notmuch_database_begin_atomic (notmuch);
@@ -263,14 +261,13 @@ add_file (notmuch_database_t *notmuch, const char *filename,
     status = notmuch_database_add_message (notmuch, filename, &message);
     switch (status) {
     /* Success. */
-    case NOTMUCH_STATUS_SUCCESS:
-	state->added_messages++;
-	notmuch_message_freeze (message);
-	for (tag = state->new_tags; *tag != NULL; tag++)
-	    notmuch_message_add_tag (message, *tag);
+    case NOTMUCH_STATUS_SUCCESS:;
+	tag_op_flag_t flags = 0;
+
 	if (state->synchronize_flags)
-	    notmuch_message_maildir_flags_to_tags (message);
-	notmuch_message_thaw (message);
+	    flags |= TAG_FLAG_TAG_SYNC;
+	state->added_messages++;
+	(void) tag_op_list_apply (message, state->tag_ops, flags);
 	break;
     /* Non-fatal issues (go on to next file). */
     case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
@@ -923,6 +920,8 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
     notmuch_bool_t timer_is_active = FALSE;
     notmuch_bool_t no_hooks = FALSE;
     notmuch_bool_t quiet = FALSE, verbose = FALSE;
+    const char **new_tags;
+    size_t new_tags_length;
 
     add_files_state.verbosity = VERBOSITY_NORMAL;
     add_files_state.debug = FALSE;
@@ -946,20 +945,22 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
     else if (verbose)
 	add_files_state.verbosity = VERBOSITY_VERBOSE;
 
-    add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);
+    new_tags = notmuch_config_get_new_tags (config, &new_tags_length);
     add_files_state.new_ignore = notmuch_config_get_new_ignore (config, &add_files_state.new_ignore_length);
     add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);
+    add_files_state.tag_ops = tag_op_list_create (config);
     db_path = notmuch_config_get_database_path (config);
 
-    for (i = 0; i < add_files_state.new_tags_length; i++) {
+    for (i = 0; i < new_tags_length; i++) {
 	const char *error_msg;
 
-	error_msg = illegal_tag (add_files_state.new_tags[i], FALSE);
+	error_msg = illegal_tag (new_tags[i], FALSE);
 	if (error_msg) {
-	    fprintf (stderr, "Error: tag '%s' in new.tags: %s\n",
-		     add_files_state.new_tags[i], error_msg);
+	    fprintf (stderr, "Error: tag '%s' in new.tags: %s\n", new_tags[i], error_msg);
 	    return EXIT_FAILURE;
 	}
+	if (tag_op_list_append (add_files_state.tag_ops, new_tags[i], FALSE) != 0)
+	    return EXIT_FAILURE;
     }
 
     if (!no_hooks) {
diff --git a/tag-util.c b/tag-util.c
index 343c161..1dee2f3 100644
--- a/tag-util.c
+++ b/tag-util.c
@@ -327,6 +327,14 @@ tag_op_list_apply (notmuch_message_t *message,
 	}
     }
 
+    if (flags & TAG_FLAG_TAG_SYNC) {
+	status = notmuch_message_maildir_flags_to_tags (message);
+	if (status) {
+	    message_error (message, status, "synching maildir to tags");
+	    return status;
+	}
+    }
+
     return NOTMUCH_STATUS_SUCCESS;
 
 }
diff --git a/tag-util.h b/tag-util.h
index 8a4074c..512cfac 100644
--- a/tag-util.h
+++ b/tag-util.h
@@ -14,19 +14,23 @@ typedef enum {
      */
     TAG_FLAG_MAILDIR_SYNC = (1 << 0),
 
+    /* Operations are synced from maildir, if possible.
+     */
+    TAG_FLAG_TAG_SYNC = (1 << 1),
+
     /* Remove all tags from message before applying list.
      */
-    TAG_FLAG_REMOVE_ALL = (1 << 1),
+    TAG_FLAG_REMOVE_ALL = (1 << 2),
 
     /* Don't try to avoid database operations. Useful when we
      * know that message passed needs these operations.
      */
-    TAG_FLAG_PRE_OPTIMIZED = (1 << 2),
+    TAG_FLAG_PRE_OPTIMIZED = (1 << 3),
 
     /* Accept strange tags that might be user error;
      * intended for use by notmuch-restore.
      */
-    TAG_FLAG_BE_GENEROUS = (1 << 3)
+    TAG_FLAG_BE_GENEROUS = (1 << 4)
 
 } tag_op_flag_t;
 
-- 
1.9.2

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 2/5] notmuch-new: Allow the tags of new messages to be manipulated.
  2014-05-02 12:11 ` [PATCH v2 0/5] Add support for specifying tags during "notmuch new" David Edmondson
  2014-05-02 12:14   ` [PATCH 1/5] notmuch-new: Use tag_op_list_apply() rather than hand-coding the same David Edmondson
@ 2014-05-02 12:14   ` David Edmondson
  2014-05-02 12:14   ` [PATCH 3/5] notmuch-new: Manual: Add command line tags David Edmondson
                     ` (3 subsequent siblings)
  5 siblings, 0 replies; 18+ messages in thread
From: David Edmondson @ 2014-05-02 12:14 UTC (permalink / raw)
  To: notmuch

Add support for specifying a set of tags to be added/removed from
messages added to the database by 'notmuch new'. These tags are
addition to those specified in .notmuch-config. They can be used to
override the pre-configured tags (e.g. -inbox).
---
 notmuch-new.c |  5 +++++
 tag-util.c    | 20 +++++++++++++++-----
 tag-util.h    | 15 +++++++++++++++
 3 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/notmuch-new.c b/notmuch-new.c
index 2c3d680..b49e5b2 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -951,6 +951,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
     add_files_state.tag_ops = tag_op_list_create (config);
     db_path = notmuch_config_get_database_path (config);
 
+    /* Add the configured tags to the list applied. */
     for (i = 0; i < new_tags_length; i++) {
 	const char *error_msg;
 
@@ -963,6 +964,10 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
 	    return EXIT_FAILURE;
     }
 
+    /* Add any command line tags to the list applied. */
+    if (process_tag_command_line (argc - opt_index, &argv[opt_index], add_files_state.tag_ops) < 0)
+	return EXIT_FAILURE;
+
     if (!no_hooks) {
 	ret = notmuch_run_hook (db_path, "pre-new");
 	if (ret)
diff --git a/tag-util.c b/tag-util.c
index 1dee2f3..94061f3 100644
--- a/tag-util.c
+++ b/tag-util.c
@@ -151,11 +151,9 @@ parse_tag_line (void *ctx, char *line,
     return ret;
 }
 
-tag_parse_status_t
-parse_tag_command_line (void *ctx, int argc, char **argv,
-			char **query_str, tag_op_list_t *tag_ops)
+int
+process_tag_command_line (int argc, char **argv, tag_op_list_t *tag_ops)
 {
-
     int i;
 
     for (i = 0; i < argc; i++) {
@@ -173,12 +171,24 @@ parse_tag_command_line (void *ctx, int argc, char **argv,
 	msg = illegal_tag (argv[i] + 1, is_remove);
 	if (msg) {
 	    fprintf (stderr, "Error: %s\n", msg);
-	    return TAG_PARSE_INVALID;
+	    return -1;
 	}
 
 	tag_op_list_append (tag_ops, argv[i] + 1, is_remove);
     }
 
+    return i;
+}
+
+tag_parse_status_t
+parse_tag_command_line (void *ctx, int argc, char **argv,
+			char **query_str, tag_op_list_t *tag_ops)
+{
+    int i = process_tag_command_line (argc, argv, tag_ops);
+
+    if (i < 0)
+	return TAG_PARSE_INVALID;
+
     *query_str = query_string_from_args (ctx, argc - i, &argv[i]);
 
     if (*query_str == NULL) {
diff --git a/tag-util.h b/tag-util.h
index 512cfac..f757e6b 100644
--- a/tag-util.h
+++ b/tag-util.h
@@ -78,6 +78,21 @@ parse_tag_line (void *ctx, char *line,
 
 
 
+/* Process a command line of the following format:
+ *
+ * +<tag>|-<tag> [...] [--]
+ *
+ * Return the number of tags operations, or -1 for failure to parse.
+ *
+ * Output Parameters:
+ *	ops	contains a list of tag operations
+ *
+ * The ops argument is not cleared.
+ */
+
+int
+process_tag_command_line (int argc, char **argv, tag_op_list_t *ops);
+
 /* Parse a command line of the following format:
  *
  * +<tag>|-<tag> [...] [--] <search-terms>
-- 
1.9.2

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 3/5] notmuch-new: Manual: Add command line tags.
  2014-05-02 12:11 ` [PATCH v2 0/5] Add support for specifying tags during "notmuch new" David Edmondson
  2014-05-02 12:14   ` [PATCH 1/5] notmuch-new: Use tag_op_list_apply() rather than hand-coding the same David Edmondson
  2014-05-02 12:14   ` [PATCH 2/5] notmuch-new: Allow the tags of new messages to be manipulated David Edmondson
@ 2014-05-02 12:14   ` David Edmondson
  2014-05-02 12:14   ` [PATCH 4/5] NEWS: Add information about "notmuch new" " David Edmondson
                     ` (2 subsequent siblings)
  5 siblings, 0 replies; 18+ messages in thread
From: David Edmondson @ 2014-05-02 12:14 UTC (permalink / raw)
  To: notmuch

Describe the addition of command line tags for "notmuch new".
---
 doc/man1/notmuch-new.rst | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/doc/man1/notmuch-new.rst b/doc/man1/notmuch-new.rst
index 787ed78..84203de 100644
--- a/doc/man1/notmuch-new.rst
+++ b/doc/man1/notmuch-new.rst
@@ -5,7 +5,7 @@ notmuch-new
 SYNOPSIS
 ========
 
-**notmuch** **new** [options]
+**notmuch** **new** [options] [+<*tag*>|-<*tag*> ...]
 
 DESCRIPTION
 ===========
@@ -15,7 +15,7 @@ Find and import any new messages to the database.
 The **new** command scans all sub-directories of the database,
 performing full-text indexing on new messages that are found. Each new
 message will automatically be tagged with both the **inbox** and
-**unread** tags.
+**unread** tags, as well as any tags specified on the command line.
 
 You should run **notmuch new** once after first running **notmuch
 setup** to create the initial database. The first run may take a long
-- 
1.9.2

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 4/5] NEWS: Add information about "notmuch new" command line tags.
  2014-05-02 12:11 ` [PATCH v2 0/5] Add support for specifying tags during "notmuch new" David Edmondson
                     ` (2 preceding siblings ...)
  2014-05-02 12:14   ` [PATCH 3/5] notmuch-new: Manual: Add command line tags David Edmondson
@ 2014-05-02 12:14   ` David Edmondson
  2014-05-02 12:14   ` [PATCH 5/5] Test: Add tests for " David Edmondson
  2014-05-02 14:07   ` [PATCH v2 0/5] Add support for specifying tags during "notmuch new" Mark Walters
  5 siblings, 0 replies; 18+ messages in thread
From: David Edmondson @ 2014-05-02 12:14 UTC (permalink / raw)
  To: notmuch

---
 NEWS | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/NEWS b/NEWS
index bcd311d..13ae593 100644
--- a/NEWS
+++ b/NEWS
@@ -12,6 +12,12 @@ Command-Line Interface
 
   This option suppresses the progress and summary reports.
 
+`notmuch new` allows tag addition/removal to be specified
+
+  An additional set of tags to be added or removed to messages
+  discovered by `notmuch new` can be specified. These are in addition
+  to the existing configured tags.
+
 Emacs Interface
 ---------------
 
-- 
1.9.2

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [PATCH 5/5] Test: Add tests for "notmuch new" command line tags
  2014-05-02 12:11 ` [PATCH v2 0/5] Add support for specifying tags during "notmuch new" David Edmondson
                     ` (3 preceding siblings ...)
  2014-05-02 12:14   ` [PATCH 4/5] NEWS: Add information about "notmuch new" " David Edmondson
@ 2014-05-02 12:14   ` David Edmondson
  2014-05-02 14:07   ` [PATCH v2 0/5] Add support for specifying tags during "notmuch new" Mark Walters
  5 siblings, 0 replies; 18+ messages in thread
From: David Edmondson @ 2014-05-02 12:14 UTC (permalink / raw)
  To: notmuch

Add a simple set of tests for adding, removing and both adding and
removing tags when running "notmuch new".
---
 test/T540-new-tags.sh | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
 create mode 100755 test/T540-new-tags.sh

diff --git a/test/T540-new-tags.sh b/test/T540-new-tags.sh
new file mode 100755
index 0000000..58e6c18
--- /dev/null
+++ b/test/T540-new-tags.sh
@@ -0,0 +1,28 @@
+#!/usr/bin/env bash
+test_description="'notmuch new' with tags"
+. ./test-lib.sh
+
+which notmuch
+
+test_begin_subtest "Add tags during new"
+generate_message
+notmuch new +happyfunball +flyfishing
+output=$(notmuch search "id:${gen_msg_id}")
+expected='thread:0000000000000001   2001-01-05 [1/1] Notmuch Test Suite; Add tags during new (flyfishing happyfunball inbox unread)'
+test_expect_equal "$output" "$expected"
+
+test_begin_subtest "Remove tags during new"
+generate_message
+notmuch new -inbox
+output=$(notmuch search "id:${gen_msg_id}")
+expected='thread:0000000000000002   2001-01-05 [1/1] Notmuch Test Suite; Remove tags during new (unread)'
+test_expect_equal "$output" "$expected"
+
+test_begin_subtest "Add and remove tags during new"
+generate_message
+notmuch new +happyfunball -inbox
+output=$(notmuch search "id:${gen_msg_id}")
+expected='thread:0000000000000003   2001-01-05 [1/1] Notmuch Test Suite; Add and remove tags during new (happyfunball unread)'
+test_expect_equal "$output" "$expected"
+
+test_done
-- 
1.9.2

^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [PATCH v2 0/5] Add support for specifying tags during "notmuch new"
  2014-05-02 12:11 ` [PATCH v2 0/5] Add support for specifying tags during "notmuch new" David Edmondson
                     ` (4 preceding siblings ...)
  2014-05-02 12:14   ` [PATCH 5/5] Test: Add tests for " David Edmondson
@ 2014-05-02 14:07   ` Mark Walters
  5 siblings, 0 replies; 18+ messages in thread
From: Mark Walters @ 2014-05-02 14:07 UTC (permalink / raw)
  To: David Edmondson, notmuch


Version 2 passes all tests and LGTM. +1

Best wishes

Mark

On Fri, 02 May 2014, David Edmondson <dme@dme.org> wrote:
> v1:
>
> This patch set allows a user to specify a list of tags to be
> added/removed to messages discovered during "notmuch new".
>
> Two use-cases are envisaged:
>     1) A chunk of messages was just dumped into the configured
>        directory by hand, and the user doesn't want the 'inbox' tag
>        applied to them. Run 'notmuch new -inbox'.
>     2) A periodic mail processing script wants to add new messages to
>        the database, then process those newly added messages to add
>        convenience tags, etc. without worrying about the user or other
>        instances of the script manipulating tags at the same time. Use
>        this approach:
>          KEY=$RANDOM
> 	 notmuch new +$KEY
> 	 notmuch tag +notmuch tag:$KEY and to:notmuch@notmuchmail.org
> 	 notmuch tag +gnus tag:$KEY and to:ding@gnus.org
> 	 ...
> 	 notmuch tag -$KEY tag:$KEY
>
> v2: The first version synced tags to flags rather than the other way
> around. Thanks to Mark for complaining at me.
> _______________________________________________
> notmuch mailing list
> notmuch@notmuchmail.org
> http://notmuchmail.org/mailman/listinfo/notmuch

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Add support for specifying tags during "notmuch new"
  2014-05-02  8:15 Add support for specifying tags during "notmuch new" David Edmondson
                   ` (5 preceding siblings ...)
  2014-05-02 12:11 ` [PATCH v2 0/5] Add support for specifying tags during "notmuch new" David Edmondson
@ 2014-05-02 15:18 ` Austin Clements
  2014-05-02 15:32   ` David Edmondson
  2014-07-12 19:08   ` David Bremner
  6 siblings, 2 replies; 18+ messages in thread
From: Austin Clements @ 2014-05-02 15:18 UTC (permalink / raw)
  To: David Edmondson; +Cc: notmuch

Quoth David Edmondson on May 02 at  9:15 am:
> This patch set allows a user to specify a list of tags to be
> added/removed to messages discovered during "notmuch new".
> 
> Two use-cases are envisaged:
>     1) A chunk of messages was just dumped into the configured
>        directory by hand, and the user doesn't want the 'inbox' tag
>        applied to them. Run 'notmuch new -inbox'.
>     2) A periodic mail processing script wants to add new messages to
>        the database, then process those newly added messages to add
>        convenience tags, etc. without worrying about the user or other
>        instances of the script manipulating tags at the same time. Use
>        this approach:
>          KEY=$RANDOM
> 	 notmuch new +$KEY
> 	 notmuch tag +notmuch tag:$KEY and to:notmuch@notmuchmail.org
> 	 notmuch tag +gnus tag:$KEY and to:ding@gnus.org
> 	 ...
> 	 notmuch tag -$KEY tag:$KEY

What happens when this script dies in the middle (say, your computer
loses power or notmuch tag conflicts with something else on the write
lock)?  One advantage of the standard "new" tag approach is that it's
easy to write a stateless post-new tagging script that can be killed
at any point and restarted.  (You're right that post-new has a
concurrency issue, but we should fix that in its own right.)

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Add support for specifying tags during "notmuch new"
  2014-05-02 15:18 ` Austin Clements
@ 2014-05-02 15:32   ` David Edmondson
  2014-07-12 19:08   ` David Bremner
  1 sibling, 0 replies; 18+ messages in thread
From: David Edmondson @ 2014-05-02 15:32 UTC (permalink / raw)
  To: Austin Clements; +Cc: notmuch

[-- Attachment #1: Type: text/plain, Size: 1237 bytes --]

On Fri, May 02 2014, Austin Clements wrote:
>>     2) A periodic mail processing script wants to add new messages to
>>        the database, then process those newly added messages to add
>>        convenience tags, etc. without worrying about the user or other
>>        instances of the script manipulating tags at the same time. Use
>>        this approach:
>>          KEY=$RANDOM
>> 	 notmuch new +$KEY
>> 	 notmuch tag +notmuch tag:$KEY and to:notmuch@notmuchmail.org
>> 	 notmuch tag +gnus tag:$KEY and to:ding@gnus.org
>> 	 ...
>> 	 notmuch tag -$KEY tag:$KEY
>
> What happens when this script dies in the middle (say, your computer
> loses power or notmuch tag conflicts with something else on the write
> lock)?

It's a problem. I think that I would actually add both 'new' and
'new-$seconds' tags (given that I can't search on wildcarded tags) and
have 'notmuch tag' use only 'new-$seconds'.

The 'new' tag would help recover from failures such as you describe.

> One advantage of the standard "new" tag approach is that it's easy to
> write a stateless post-new tagging script that can be killed at any
> point and restarted.  (You're right that post-new has a concurrency
> issue, but we should fix that in its own right.)

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 310 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Add support for specifying tags during "notmuch new"
  2014-05-02 15:18 ` Austin Clements
  2014-05-02 15:32   ` David Edmondson
@ 2014-07-12 19:08   ` David Bremner
  2014-07-13  3:40     ` Austin Clements
  1 sibling, 1 reply; 18+ messages in thread
From: David Bremner @ 2014-07-12 19:08 UTC (permalink / raw)
  To: Austin Clements, David Edmondson; +Cc: notmuch

Austin Clements <amdragon@MIT.EDU> writes:

> What happens when this script dies in the middle (say, your computer
> loses power or notmuch tag conflicts with something else on the write
> lock)?  One advantage of the standard "new" tag approach is that it's
> easy to write a stateless post-new tagging script that can be killed
> at any point and restarted.  (You're right that post-new has a
> concurrency issue, but we should fix that in its own right.)

Hi David, Hi Austin;

So, what to do about this patch series? Is it actually a bad idea to
allow specifying tags for notmuch new? We already allow it via config,
right, so this just makes things more convenient.

d

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Add support for specifying tags during "notmuch new"
  2014-07-12 19:08   ` David Bremner
@ 2014-07-13  3:40     ` Austin Clements
  2014-07-13 11:58       ` David Bremner
  0 siblings, 1 reply; 18+ messages in thread
From: Austin Clements @ 2014-07-13  3:40 UTC (permalink / raw)
  To: David Bremner; +Cc: notmuch

Quoth David Bremner on Jul 12 at  4:08 pm:
> Austin Clements <amdragon@MIT.EDU> writes:
> 
> > What happens when this script dies in the middle (say, your computer
> > loses power or notmuch tag conflicts with something else on the write
> > lock)?  One advantage of the standard "new" tag approach is that it's
> > easy to write a stateless post-new tagging script that can be killed
> > at any point and restarted.  (You're right that post-new has a
> > concurrency issue, but we should fix that in its own right.)
> 
> Hi David, Hi Austin;
> 
> So, what to do about this patch series? Is it actually a bad idea to
> allow specifying tags for notmuch new? We already allow it via config,
> right, so this just makes things more convenient.

I'm not sure if it's a bad idea or not, but it's materially different
from the tags in the config.  The tags in the config persist and any
call to notmuch new from any source (cron, 'G' in Emacs, etc.) will
pick them up.  Tags specified on the command line don't persist in
this way.

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: Add support for specifying tags during "notmuch new"
  2014-07-13  3:40     ` Austin Clements
@ 2014-07-13 11:58       ` David Bremner
  0 siblings, 0 replies; 18+ messages in thread
From: David Bremner @ 2014-07-13 11:58 UTC (permalink / raw)
  To: Austin Clements; +Cc: notmuch

Austin Clements <amdragon@MIT.EDU> writes:


> I'm not sure if it's a bad idea or not, but it's materially different
> from the tags in the config.  The tags in the config persist and any
> call to notmuch new from any source (cron, 'G' in Emacs, etc.) will
> pick them up.  Tags specified on the command line don't persist in
> this way.

Well sure, but nothing prevents a motivated user from editing the config
file to have transient settings for the values. I see these patches as
replacing such a hack.

d

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2014-07-13 11:58 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-02  8:15 Add support for specifying tags during "notmuch new" David Edmondson
2014-05-02  8:15 ` [PATCH 1/5] notmuch-new: Use tag_op_list_apply() rather than hand-coding the same David Edmondson
2014-05-02  8:15 ` [PATCH 2/5] notmuch-new: Allow the tags of new messages to be manipulated David Edmondson
2014-05-02  8:15 ` [PATCH 3/5] notmuch-new: Manual: Add command line tags David Edmondson
2014-05-02  8:15 ` [PATCH 4/5] NEWS: Add information about "notmuch new" " David Edmondson
2014-05-02  8:15 ` [PATCH 5/5] Test: Add tests for " David Edmondson
2014-05-02 12:11 ` [PATCH v2 0/5] Add support for specifying tags during "notmuch new" David Edmondson
2014-05-02 12:14   ` [PATCH 1/5] notmuch-new: Use tag_op_list_apply() rather than hand-coding the same David Edmondson
2014-05-02 12:14   ` [PATCH 2/5] notmuch-new: Allow the tags of new messages to be manipulated David Edmondson
2014-05-02 12:14   ` [PATCH 3/5] notmuch-new: Manual: Add command line tags David Edmondson
2014-05-02 12:14   ` [PATCH 4/5] NEWS: Add information about "notmuch new" " David Edmondson
2014-05-02 12:14   ` [PATCH 5/5] Test: Add tests for " David Edmondson
2014-05-02 14:07   ` [PATCH v2 0/5] Add support for specifying tags during "notmuch new" Mark Walters
2014-05-02 15:18 ` Austin Clements
2014-05-02 15:32   ` David Edmondson
2014-07-12 19:08   ` David Bremner
2014-07-13  3:40     ` Austin Clements
2014-07-13 11:58       ` David Bremner

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).