- return _notmuch_directory_create (notmuch, path, &status);
-}
-
-/* Find the thread ID to which the message with 'message_id' belongs.
- *
- * Returns NULL if no message with message ID 'message_id' is in the
- * database.
- *
- * Otherwise, returns a newly talloced string belonging to 'ctx'.
- */
-static const char *
-_resolve_message_id_to_thread_id (notmuch_database_t *notmuch,
- void *ctx,
- const char *message_id)
-{
- notmuch_message_t *message;
- const char *ret = NULL;
-
- message = notmuch_database_find_message (notmuch, message_id);
- if (message == NULL)
- goto DONE;
-
- ret = talloc_steal (ctx, notmuch_message_get_thread_id (message));
-
- DONE:
- if (message)
- notmuch_message_destroy (message);
-
- return ret;
-}
-
-static notmuch_status_t
-_merge_threads (notmuch_database_t *notmuch,
- const char *winner_thread_id,
- const char *loser_thread_id)
-{
- Xapian::PostingIterator loser, loser_end;
- notmuch_message_t *message = NULL;
- notmuch_private_status_t private_status;
- notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
-
- find_doc_ids (notmuch, "thread", loser_thread_id, &loser, &loser_end);
-
- for ( ; loser != loser_end; loser++) {
- message = _notmuch_message_create (notmuch, notmuch,
- *loser, &private_status);
- if (message == NULL) {
- ret = COERCE_STATUS (private_status,
- "Cannot find document for doc_id from query");
- goto DONE;
- }
-
- _notmuch_message_remove_term (message, "thread", loser_thread_id);
- _notmuch_message_add_term (message, "thread", winner_thread_id);
- _notmuch_message_sync (message);
-
- notmuch_message_destroy (message);
- message = NULL;
- }
-
- DONE:
- if (message)
- notmuch_message_destroy (message);
-
- return ret;
-}
-
-static void
-_my_talloc_free_for_g_hash (void *ptr)
-{
- talloc_free (ptr);
-}
-
-static notmuch_status_t
-_notmuch_database_link_message_to_parents (notmuch_database_t *notmuch,
- notmuch_message_t *message,
- notmuch_message_file_t *message_file,
- const char **thread_id)
-{
- GHashTable *parents = NULL;
- const char *refs, *in_reply_to, *in_reply_to_message_id;
- GList *l, *keys = NULL;
- notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
-
- parents = g_hash_table_new_full (g_str_hash, g_str_equal,
- _my_talloc_free_for_g_hash, NULL);
-
- refs = notmuch_message_file_get_header (message_file, "references");
- parse_references (message, notmuch_message_get_message_id (message),
- parents, refs);
-
- in_reply_to = notmuch_message_file_get_header (message_file, "in-reply-to");
- parse_references (message, notmuch_message_get_message_id (message),
- parents, in_reply_to);
-
- /* Carefully avoid adding any self-referential in-reply-to term. */
- in_reply_to_message_id = _parse_message_id (message, in_reply_to, NULL);
- if (in_reply_to_message_id &&
- strcmp (in_reply_to_message_id,
- notmuch_message_get_message_id (message)))
- {
- _notmuch_message_add_term (message, "replyto",
- _parse_message_id (message, in_reply_to, NULL));
- }
-
- keys = g_hash_table_get_keys (parents);
- for (l = keys; l; l = l->next) {
- char *parent_message_id;
- const char *parent_thread_id;
-
- parent_message_id = (char *) l->data;
- parent_thread_id = _resolve_message_id_to_thread_id (notmuch,
- message,
- parent_message_id);
-
- if (parent_thread_id == NULL) {
- _notmuch_message_add_term (message, "reference",
- parent_message_id);
- } else {
- if (*thread_id == NULL) {
- *thread_id = talloc_strdup (message, parent_thread_id);
- _notmuch_message_add_term (message, "thread", *thread_id);
- } else if (strcmp (*thread_id, parent_thread_id)) {
- ret = _merge_threads (notmuch, *thread_id, parent_thread_id);
- if (ret)
- goto DONE;
- }
- }
- }
-
- DONE:
- if (keys)
- g_list_free (keys);
- if (parents)
- g_hash_table_unref (parents);
-
- return ret;
-}
-
-static notmuch_status_t
-_notmuch_database_link_message_to_children (notmuch_database_t *notmuch,
- notmuch_message_t *message,
- const char **thread_id)
-{
- const char *message_id = notmuch_message_get_message_id (message);
- Xapian::PostingIterator child, children_end;
- notmuch_message_t *child_message = NULL;
- const char *child_thread_id;
- notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
- notmuch_private_status_t private_status;
-
- find_doc_ids (notmuch, "reference", message_id, &child, &children_end);
-
- for ( ; child != children_end; child++) {
-
- child_message = _notmuch_message_create (message, notmuch,
- *child, &private_status);
- if (child_message == NULL) {
- ret = COERCE_STATUS (private_status,
- "Cannot find document for doc_id from query");
- goto DONE;
- }
-
- child_thread_id = notmuch_message_get_thread_id (child_message);
- if (*thread_id == NULL) {
- *thread_id = talloc_strdup (message, child_thread_id);
- _notmuch_message_add_term (message, "thread", *thread_id);
- } else if (strcmp (*thread_id, child_thread_id)) {
- _notmuch_message_remove_term (child_message, "reference",
- message_id);
- _notmuch_message_sync (child_message);
- ret = _merge_threads (notmuch, *thread_id, child_thread_id);
- if (ret)
- goto DONE;
- }