[PATCH 2/3] cli: Introduce the add command

Subject: [PATCH 2/3] cli: Introduce the add command

Date: Sat, 13 Jul 2013 18:45:45 -0600

To: notmuch@notmuchmail.org

Cc:

From: Adam Wolfe Gordon


Introduce a new add command, which works the same as insert, except
that it is for adding files that already exist in the maildir. This
provides an alternative to notmuch new for situations where the user
knows what has changed. For example, if the user uses inotify to check
for new files in the maildir, the new files can be indexed using
notmuch add, avoiding the need to scan the whole maildir.
---
 notmuch-client.h |    3 +++
 notmuch-insert.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 notmuch.c        |    2 ++
 3 files changed, 54 insertions(+)

diff --git a/notmuch-client.h b/notmuch-client.h
index da332f3..09d7e5c 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -183,6 +183,9 @@ int
 notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]);
 
 int
+notmuch_add_command (notmuch_config_t *config, int argc, char *argv[]);
+
+int
 notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[]);
 
 int
diff --git a/notmuch-insert.c b/notmuch-insert.c
index d32a535..c141450 100644
--- a/notmuch-insert.c
+++ b/notmuch-insert.c
@@ -479,3 +479,52 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
 
     return (ret) ? 0 : 1;
 }
+
+int
+notmuch_add_command (notmuch_config_t *config, int argc, char *argv[])
+{
+    notmuch_database_t *notmuch;
+    const char *db_path;
+    const char **new_tags;
+    size_t new_tags_length;
+    tag_op_list_t *tag_ops;
+    const char *filename = NULL;
+    unsigned int i;
+    notmuch_bool_t ret;
+
+    if (argc != 2) {
+	fprintf (stderr, "Error: no filename given.\n");
+	return 1;
+    }
+    filename = argv[1];
+
+    db_path = notmuch_config_get_database_path (config);
+    new_tags = notmuch_config_get_new_tags (config, &new_tags_length);
+
+    tag_ops = tag_op_list_create (config);
+    if (tag_ops == NULL) {
+	fprintf (stderr, "Out of memory.\n");
+	return 1;
+    }
+    for (i = 0; i < new_tags_length; i++) {
+	if (tag_op_list_append (tag_ops, new_tags[i], FALSE))
+	    return 1;
+    }
+
+    /* Check that the message given on the commandline is in the maildir. */
+    if (strncmp (filename, db_path, strlen(db_path)) != 0) {
+	fprintf(stderr, "Error: file %s is not in maildir %s.\n", filename,
+		db_path);
+	return 1;
+    }
+
+    if (notmuch_database_open (notmuch_config_get_database_path (config),
+			       NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
+	return 1;
+
+    ret = add_file_to_database (notmuch, filename, tag_ops);
+
+    notmuch_database_destroy (notmuch);
+
+    return (ret) ? 0 : 1;
+}
diff --git a/notmuch.c b/notmuch.c
index 78d29a8..b61149d 100644
--- a/notmuch.c
+++ b/notmuch.c
@@ -46,6 +46,8 @@ static command_t commands[] = {
       "Find and import new messages to the notmuch database." },
     { "insert", notmuch_insert_command, FALSE,
       "Add a new message into the maildir and notmuch database." },
+    { "add", notmuch_add_command, FALSE,
+      "Add a message from the maildir into the notmuch database." },
     { "search", notmuch_search_command, FALSE,
       "Search for messages matching the given search terms." },
     { "show", notmuch_show_command, FALSE,
-- 
1.7.9.5


Thread: