Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id B6133421161 for ; Thu, 24 Oct 2013 08:19:38 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id tXz8pfWyvPed for ; Thu, 24 Oct 2013 08:19:33 -0700 (PDT) Received: from dmz-mailsec-scanner-7.mit.edu (dmz-mailsec-scanner-7.mit.edu [18.7.68.36]) by olra.theworths.org (Postfix) with ESMTP id 07496429E48 for ; Thu, 24 Oct 2013 08:19:22 -0700 (PDT) X-AuditID: 12074424-b7f528e0000009aa-23-52693a7a95f6 Received: from mailhub-auth-2.mit.edu ( [18.7.62.36]) by dmz-mailsec-scanner-7.mit.edu (Symantec Messaging Gateway) with SMTP id C1.25.02474.A7A39625; Thu, 24 Oct 2013 11:19:22 -0400 (EDT) Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11]) by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id r9OFJF9H011798; Thu, 24 Oct 2013 11:19:15 -0400 Received: from drake.dyndns.org (216-15-114-40.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com [216.15.114.40]) (authenticated bits=0) (User authenticated as amdragon@ATHENA.MIT.EDU) by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id r9OFJCqk012919 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); Thu, 24 Oct 2013 11:19:14 -0400 Received: from amthrax by drake.dyndns.org with local (Exim 4.77) (envelope-from ) id 1VZMgq-0006dz-Qd; Thu, 24 Oct 2013 11:19:12 -0400 From: Austin Clements To: notmuch@notmuchmail.org Subject: [PATCH v2 07/11] emacs: Use notmuch tag --batch for large tag queries Date: Thu, 24 Oct 2013 11:19:07 -0400 Message-Id: <1382627951-25252-8-git-send-email-amdragon@mit.edu> X-Mailer: git-send-email 1.8.4.rc3 In-Reply-To: <1382627951-25252-1-git-send-email-amdragon@mit.edu> References: <1382627951-25252-1-git-send-email-amdragon@mit.edu> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrGIsWRmVeSWpSXmKPExsUixG6noltllRlksPSIkkXTdGeL1XN5LK7f nMnswOyxc9Zddo9b91+zezxbdYs5gDmKyyYlNSezLLVI3y6BK+PT+WVsBT9kKs4s6mZtYOyV 6GLk5JAQMJHYMeEjM4QtJnHh3nq2LkYuDiGBfYwS6x/vhnI2MkrM2nKFHcK5wySxccZcqMxc RonDvVsZQfrZBDQktu1fDmaLCEhL7Lw7mxXEZhaIljhyeQYbiC0s4Cfxq2M1O4jNIqAq0XRy J1icV8BBYkLbCUaIO5QkFp7aBtbLKeAo8fP1NbC4EFDNr99nWCYw8i9gZFjFKJuSW6Wbm5iZ U5yarFucnJiXl1qka66Xm1mil5pSuokRHFwuKjsYmw8pHWIU4GBU4uHV+JAeJMSaWFZcmXuI UZKDSUmU955pZpAQX1J+SmVGYnFGfFFpTmrxIUYJDmYlEd5pekA53pTEyqrUonyYlDQHi5I4 7y0O+yAhgfTEktTs1NSC1CKYrAwHh5IEb6clUKNgUWp6akVaZk4JQpqJgxNkOA/Q8HsgNbzF BYm5xZnpEPlTjIpS4rzzQBICIImM0jy4Xlj0v2IUB3pFmHcTSBUPMHHAdb8CGswENHjKkjSQ wSWJCCmpBsa6bV/Lyg/510RU1Wkbyq6aKWB07Nb2E2eN7eRvSRT/Yl63ZdE9vv1cPLvsfymx eT7uKar9+HjK8ehVKX71EycEfEm9pf0iQ6Npd9zKEw2vbwiwFX3q2P1x1R5upqeqKn3+nJGC 8zbtbuV/u/lI6XMBNb7A8pTYX83CwidEvtben/Cs5K5D7F0lluKMREMt5qLiRACIqFfS2QIA AA== X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 24 Oct 2013 15:19:39 -0000 (Unfortunately, it's difficult to first demonstrate this problem with a known-broken test because modern Linux kernels have argument length limits in the megabytes, which makes Emacs really slow!) --- emacs/notmuch-lib.el | 8 ++++++++ emacs/notmuch-tag.el | 15 +++++++++++++-- test/emacs | 8 ++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el index 22156f1..348112b 100644 --- a/emacs/notmuch-lib.el +++ b/emacs/notmuch-lib.el @@ -261,6 +261,14 @@ user-friendly queries." "Return a query that matches the message with id ID." (concat "id:" (notmuch-escape-boolean-term id))) +(defun notmuch-hex-encode (str) + "Hex-encode STR (e.g., as used by batch tagging). + +This replaces spaces, percents, and double quotes in STR with +%NN where NN is the hexadecimal value of the character." + (replace-regexp-in-string + "[ %\"]" (lambda (match) (format "%%%02x" (aref match 0))) str)) + ;; (defun notmuch-common-do-stash (text) diff --git a/emacs/notmuch-tag.el b/emacs/notmuch-tag.el index 7b21006..cbb935c 100644 --- a/emacs/notmuch-tag.el +++ b/emacs/notmuch-tag.el @@ -252,6 +252,12 @@ from TAGS if present." (error "Changed tag must be of the form `+this_tag' or `-that_tag'"))))) (sort result-tags 'string<))) +(defconst notmuch-tag-argument-limit 1000 + "Use batch tagging if the tagging query is longer than this. + +This limits the length of arguments passed to the notmuch CLI to +avoid system argument length limits and performance problems.") + (defun notmuch-tag (query tag-changes) "Add/remove tags in TAG-CHANGES to messages matching QUERY. @@ -270,8 +276,13 @@ notmuch-after-tag-hook will be run." tag-changes) (unless (null tag-changes) (run-hooks 'notmuch-before-tag-hook) - (apply 'notmuch-call-notmuch-process "tag" - (append tag-changes (list "--" query))) + (if (<= (length query) notmuch-tag-argument-limit) + (apply 'notmuch-call-notmuch-process "tag" + (append tag-changes (list "--" query))) + ;; Use batch tag mode to avoid argument length limitations + (let ((batch-op (concat (mapconcat #'notmuch-hex-encode tag-changes " ") + " -- " query))) + (notmuch-call-notmuch-process :stdin-string batch-op "tag" "--batch"))) (run-hooks 'notmuch-after-tag-hook))) (defun notmuch-tag-change-list (tags &optional reverse) diff --git a/test/emacs b/test/emacs index 05295af..5fbee12 100755 --- a/test/emacs +++ b/test/emacs @@ -122,6 +122,14 @@ test_emacs "(notmuch-search \"$os_x_darwin_thread\") output=$(notmuch search $os_x_darwin_thread | notmuch_search_sanitize) test_expect_equal "$output" "thread:XXX 2009-11-18 [4/4] Jjgod Jiang, Alexander Botero-Lowry; [notmuch] Mac OS X/Darwin compatibility issues (inbox unread)" +test_begin_subtest "Add tag (large query)" +# We use a long query to force us into batch mode and use a funny tag +# that requires escaping for batch tagging. +test_emacs "(notmuch-tag (concat \"$os_x_darwin_thread\" \" or \" (make-string notmuch-tag-argument-limit ?x)) (list \"+tag-from-%-large-query\"))" +output=$(notmuch search $os_x_darwin_thread | notmuch_search_sanitize) +test_expect_equal "$output" "thread:XXX 2009-11-18 [4/4] Jjgod Jiang, Alexander Botero-Lowry; [notmuch] Mac OS X/Darwin compatibility issues (inbox tag-from-%-large-query unread)" +notmuch tag -tag-from-%-large-query $os_x_darwin_thread + test_begin_subtest "notmuch-show: add single tag to single message" test_emacs "(notmuch-show \"$os_x_darwin_thread\") (execute-kbd-macro \"+tag-from-show-view\")" -- 1.8.4.rc3