[PATCH] Allow user to specify ignored directories

Andreas Amann andreas.amann at tyndall.ie
Thu Sep 30 15:08:23 PDT 2010


Hi list,

this is my first post. I found notmuch a couple of days ago and it works
great. However I wanted to be able to ignore certain directories (mostly
.git in my case) when "notmuch new" is running. 

With this patch the user can provide a list of directories which are
ignored during the recursive search for new messages. An "ignore"
label in the "new" section of the configuration file is added for this
purpose to enable for example something like

[new]
ignore=.git;.notmuch;
tags=unread;inbox;

in the .notmuch-config file. Feel free to apply if you find it useful. 

Andreas



---
 notmuch-client.h |    8 +++++++
 notmuch-config.c |   57 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 notmuch-new.c    |   43 +++++++++++++++++++++++++++-------------
 3 files changed, 93 insertions(+), 15 deletions(-)

diff --git a/notmuch-client.h b/notmuch-client.h
index 20be43b..9bc6ef1 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -191,6 +191,14 @@ notmuch_config_set_new_tags (notmuch_config_t *config,
                             const char *new_tags[],
                             size_t length);
 
+const char **
+notmuch_config_get_new_ignore (notmuch_config_t *config,
+                               size_t *length);
+void
+notmuch_config_set_new_ignore (notmuch_config_t *config,
+                               const char *new_ignore[],
+                               size_t length);
+
 notmuch_bool_t
 debugger_is_active (void);
 
diff --git a/notmuch-config.c b/notmuch-config.c
index cf30603..8841eaf 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -43,7 +43,10 @@ static const char new_config_comment[] =
     " The following options are supported here:\n"
     "\n"
     "\ttags    A list (separated by ';') of the tags that will be\n"
-    "\t        added to all messages incorporated by \"notmuch new\".\n";
+    "\t        added to all messages incorporated by \"notmuch new\".\n"
+    "\n"
+    "\tignore  A list (separated by ';') of directories that will not\n"
+    "\t        be searched for messages  by \"notmuch new\".\n";
 
 static const char user_config_comment[] =
     " User configuration\n"
@@ -72,6 +75,8 @@ struct _notmuch_config {
     size_t user_other_email_length;
     const char **new_tags;
     size_t new_tags_length;
+    const char **new_ignore;
+    size_t new_ignore_length;
 };
 
 static int
@@ -221,6 +226,8 @@ notmuch_config_open (void *ctx,
     config->user_other_email_length = 0;
     config->new_tags = NULL;
     config->new_tags_length = 0;
+    config->new_ignore = NULL;
+    config->new_ignore_length = 0;
 
     if (! g_key_file_load_from_file (config->key_file,
                                     config->filename,
@@ -313,6 +320,11 @@ notmuch_config_open (void *ctx,
        notmuch_config_set_new_tags (config, tags, 2);
     }
 
+    if (notmuch_config_get_new_ignore (config, &tmp) == NULL) {
+        const char *ignore[] = { ".notmuch" };
+       notmuch_config_set_new_ignore (config, ignore, 2);
+    }
+
     /* Whenever we know of configuration sections that don't appear in
      * the configuration file, we add some comments to help the user
      * understand what can be done. */
@@ -562,3 +574,46 @@ notmuch_config_set_new_tags (notmuch_config_t *config,
     config->new_tags = NULL;
 }
 
+const char **
+notmuch_config_get_new_ignore (notmuch_config_t *config,
+                               size_t *length)
+{
+    char **ignore;
+    size_t ignore_length;
+    unsigned int i;
+
+    if (config->new_ignore == NULL) {
+       ignore = g_key_file_get_string_list (config->key_file,
+                                             "new", "ignore",
+                                             &ignore_length, NULL);
+       if (ignore) {
+           config->new_ignore = talloc_size (config,
+                                              sizeof (char *) *
+                                              (ignore_length + 1));
+           for (i = 0; i < ignore_length; i++)
+               config->new_ignore[i] = talloc_strdup (config->new_ignore,
+                                                       ignore[i]);
+           config->new_ignore[i] = NULL;
+
+           g_strfreev (ignore);
+
+           config->new_ignore_length = ignore_length;
+       }
+    }
+
+    *length = config->new_ignore_length;
+    return config->new_ignore;
+}
+
+void
+notmuch_config_set_new_ignore (notmuch_config_t *config,
+                               const char *new_ignore[],
+                               size_t length)
+{
+    g_key_file_set_string_list (config->key_file,
+                               "new", "ignore",
+                               new_ignore, length);
+
+    talloc_free (config->new_ignore);
+    config->new_ignore = NULL;
+}
diff --git a/notmuch-new.c b/notmuch-new.c
index 8818728..0e9c4d7 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -164,6 +164,23 @@ _entries_resemble_maildir (struct dirent **entries, int count)
     return 0;
 }
 
+/* Check if user asked to ignore these directories */
+
+static int
+_entry_in_ignore_list ( const char *entry, notmuch_config_t *config)
+{
+    size_t j;
+    size_t ignore_length;
+    const char** ignore = notmuch_config_get_new_ignore (config, &ignore_length);
+    for (j = 0; j<ignore_length; j++)
+    {
+        if (strcmp (entry, ignore[j]) == 0 )
+            return 1;
+    }
+
+    return 0;
+}
+
 /* Examine 'path' recursively as follows:
  *
  *   o Ask the filesystem for the mtime of 'path' (fs_mtime)
@@ -202,7 +219,8 @@ _entries_resemble_maildir (struct dirent **entries, int count)
 static notmuch_status_t
 add_files_recursive (notmuch_database_t *notmuch,
                     const char *path,
-                    add_files_state_t *state)
+                    add_files_state_t *state,
+                     notmuch_config_t *config)
 {
     DIR *dir = NULL;
     struct dirent *entry = NULL;
@@ -287,18 +305,16 @@ add_files_recursive (notmuch_database_t *notmuch,
         * Also ignore the .notmuch directory and any "tmp" directory
         * that appears within a maildir.
         */
-       /* XXX: Eventually we'll want more sophistication to let the
-        * user specify files to be ignored. */
        if (strcmp (entry->d_name, ".") == 0 ||
            strcmp (entry->d_name, "..") == 0 ||
            (is_maildir && strcmp (entry->d_name, "tmp") == 0) ||
-           strcmp (entry->d_name, ".notmuch") ==0)
+           _entry_in_ignore_list (entry->d_name, config) )
        {
            continue;
        }
 
        next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name);
-       status = add_files_recursive (notmuch, next, state);
+       status = add_files_recursive (notmuch, next, state, config);
        if (status && ret == NOTMUCH_STATUS_SUCCESS)
            ret = status;
        talloc_free (next);
@@ -509,7 +525,8 @@ add_files_recursive (notmuch_database_t *notmuch,
 static notmuch_status_t
 add_files (notmuch_database_t *notmuch,
           const char *path,
-          add_files_state_t *state)
+          add_files_state_t *state,
+           notmuch_config_t *config)
 {
     notmuch_status_t status;
     struct sigaction action;
@@ -546,7 +563,7 @@ add_files (notmuch_database_t *notmuch,
        return NOTMUCH_STATUS_FILE_ERROR;
     }
 
-    status = add_files_recursive (notmuch, path, state);
+    status = add_files_recursive (notmuch, path, state, config);
 
     if (timer_is_active) {
        /* Now stop the timer. */
@@ -571,7 +588,7 @@ add_files (notmuch_database_t *notmuch,
  * initialized to zero by the top-level caller before calling
  * count_files). */
 static void
-count_files (const char *path, int *count)
+count_files (const char *path, int *count, notmuch_config_t *config)
 {
     struct dirent *entry = NULL;
     char *next;
@@ -595,11 +612,9 @@ count_files (const char *path, int *count)
        /* Ignore special directories to avoid infinite recursion.
         * Also ignore the .notmuch directory.
         */
-       /* XXX: Eventually we'll want more sophistication to let the
-        * user specify files to be ignored. */
        if (strcmp (entry->d_name, ".") == 0 ||
            strcmp (entry->d_name, "..") == 0 ||
-           strcmp (entry->d_name, ".notmuch") == 0)
+           _entry_in_ignore_list (entry->d_name, config) )
        {
            continue;
        }
@@ -620,7 +635,7 @@ count_files (const char *path, int *count)
                fflush (stdout);
            }
        } else if (S_ISDIR (st.st_mode)) {
-           count_files (next, count);
+           count_files (next, count, config);
        }
 
        free (next);
@@ -745,7 +760,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
        int count;
 
        count = 0;
-       count_files (db_path, &count);
+       count_files (db_path, &count, config);
        if (interrupted)
            return 1;
 
@@ -792,7 +807,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])
     add_files_state.removed_files = _filename_list_create (ctx);
     add_files_state.removed_directories = _filename_list_create (ctx);
 
-    ret = add_files (notmuch, db_path, &add_files_state);
+    ret = add_files (notmuch, db_path, &add_files_state, config);
 
     removed_files = 0;
     renamed_files = 0;
-- 
1.7.0.4


-- 
Dr. Andreas Amann
Tyndall National Institute 
University College Cork
Ireland



More information about the notmuch mailing list