[RFC PATCH v2 6/8] WIP: support tag --output=lastmod

Subject: [RFC PATCH v2 6/8] WIP: support tag --output=lastmod

Date: Sat, 29 Jan 2022 15:44:38 -0400

To: notmuch@notmuchmail.org

Cc:

From: David Bremner


needs documentation, possibly more tests.

output format subject to change.

See the tests for examples of how this could be used in an undo facility
---
 notmuch-tag.c        | 25 ++++++++++++++++++++-----
 test/T150-tagging.sh | 31 +++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 5 deletions(-)

diff --git a/notmuch-tag.c b/notmuch-tag.c
index c6ac51d2..95598091 100644
--- a/notmuch-tag.c
+++ b/notmuch-tag.c
@@ -124,12 +124,14 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,
  */
 static int
 tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,
-	   tag_op_list_t *tag_ops, tag_op_flag_t flags)
+	   tag_op_list_t *tag_ops, tag_op_flag_t flags, int output)
 {
     notmuch_query_t *query;
     notmuch_messages_t *messages;
     notmuch_message_t *message;
     notmuch_status_t status;
+    unsigned long before;
+    const char* uuid_before = NULL;
 
     int ret = NOTMUCH_STATUS_SUCCESS;
 
@@ -159,6 +161,10 @@ tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,
     if (print_status_query ("notmuch tag", query, status))
 	return status;
 
+    if (output == OUTPUT_LASTMOD) {
+	before = notmuch_database_get_revision (notmuch, &uuid_before);
+    }
+
     for (;
 	 notmuch_messages_valid (messages) && ! interrupted;
 	 notmuch_messages_move_to_next (messages)) {
@@ -171,12 +177,21 @@ tag_query (void *ctx, notmuch_database_t *notmuch, const char *query_string,
 
     notmuch_query_destroy (query);
 
+    if (output == OUTPUT_LASTMOD) {
+	const char *uuid_after = NULL;
+	unsigned long after = notmuch_database_get_revision (notmuch, &uuid_after);
+	if (strcmp (uuid_before, uuid_after))
+	    INTERNAL_ERROR ("uuid changed while holding write lock %s %s", uuid_before, uuid_after);
+
+	printf ("%s\t%lu\t%lu\n", uuid_before, before + 1, after);
+    }
+
     return ret || interrupted;
 }
 
 static int
 tag_file (void *ctx, notmuch_database_t *notmuch, tag_op_flag_t flags,
-	  FILE *input)
+	  FILE *input, int output)
 {
     char *line = NULL;
     char *query_string = NULL;
@@ -209,7 +224,7 @@ tag_file (void *ctx, notmuch_database_t *notmuch, tag_op_flag_t flags,
 	if (ret < 0)
 	    break;
 
-	ret = tag_query (ctx, notmuch, query_string, tag_ops, flags);
+	ret = tag_query (ctx, notmuch, query_string, tag_ops, flags, output);
 	if (ret)
 	    break;
     }
@@ -314,9 +329,9 @@ notmuch_tag_command (notmuch_database_t *notmuch, int argc, char *argv[])
 	tag_flags |= TAG_FLAG_REMOVE_ALL;
 
     if (batch)
-	ret = tag_file (notmuch, notmuch, tag_flags, input);
+	ret = tag_file (notmuch, notmuch, tag_flags, input, output);
     else
-	ret = tag_query (notmuch, notmuch, query_string, tag_ops, tag_flags);
+	ret = tag_query (notmuch, notmuch, query_string, tag_ops, tag_flags, output);
 
     notmuch_database_destroy (notmuch);
 
diff --git a/test/T150-tagging.sh b/test/T150-tagging.sh
index ce33dade..baf55598 100755
--- a/test/T150-tagging.sh
+++ b/test/T150-tagging.sh
@@ -53,6 +53,21 @@ output=$(notmuch tag --output=none +tag7 '*' 2>&1)
 notmuch tag -tag7 '*'
 test_expect_equal "$output" ""
 
+test_begin_subtest "--output=lastmod produces 3 columns"
+output=$(notmuch tag --output=lastmod +tag7 '*' | sed 's/[0-9a-f][0-9a-f]*/n/g' )
+notmuch tag -tag7 '*'
+test_expect_equal "$output" "n-n-n-n-n	n	n"
+
+test_begin_subtest "--output=lastmod provides undo"
+notmuch dump > BEFORE
+tag=${RANDOM}
+notmuch count --lastmod '*' | cut -f 2 >> BEFORE
+notmuch tag --output=lastmod "+$tag" '*' > OUTPUT
+read uuid from to < OUTPUT
+notmuch tag --uuid="$uuid" "-$tag" lastmod:"$from".."$to"
+notmuch dump > AFTER
+echo "$uuid" >> AFTER
+test_expect_equal_file BEFORE AFTER
 
 test_begin_subtest "Remove all"
 notmuch tag --remove-all One
@@ -118,6 +133,13 @@ cat > batch.in <<EOF
 -tag5 +tag6 Two
 EOF
 
+# partial input for use in undoing above changes
+cat > restore.in <<EOF
+-%40 +tag5 -tag6 --
+-tag1 +tag1 +tag4 +tag4 --
++tag5 -tag6 --
+EOF
+
 cat > batch.expected <<EOF
 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; One (@ inbox tag6 unread)
 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Two (inbox tag4 tag6 unread)
@@ -151,6 +173,15 @@ notmuch search \* | notmuch_search_sanitize >> OUTPUT
 notmuch restore --format=batch-tag < backup.tags
 test_expect_equal_file batch.expected OUTPUT
 
+backup_database
+test_begin_subtest "--batch --input --output=lastmod"
+notmuch dump > EXPECTED
+notmuch tag --batch --input=batch.in --output=lastmod | awk '{printf " lastmod:%d..%d\n", $2, $3}' > queries
+paste restore.in queries | notmuch tag --batch
+notmuch dump > OUTPUT
+test_expect_equal_file EXPECTED OUTPUT
+restore_database
+
 test_begin_subtest "--batch --input --remove-all"
 notmuch dump --format=batch-tag > backup.tags
 notmuch tag +foo +bar -- One
-- 
2.34.1

_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: