From: David Bremner Date: Sat, 25 Oct 2014 16:55:25 +0000 (+0200) Subject: Merge tag '0.18.2' X-Git-Tag: 0.19_rc1~40 X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=commitdiff_plain;h=f5db7ad7d243785c274a99734c681e69d13313d0;hp=d53f759456608005de929dde4b7692f54b9e05bf Merge tag '0.18.2' notmuch 0.18.2 release --- diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..dbd6434e --- /dev/null +++ b/.travis.yml @@ -0,0 +1,23 @@ +language: c +before_install: + - sudo apt-get update -qq + - sudo apt-get install dtach libxapian-dev libgmime-2.6-dev libtalloc-dev python-sphinx + + # Notmuch requires zlib 1.2.5.2, unfortunately travis runs on Ubuntu 12.04LTS which + # ships with zlib 1.2.3.3. We need to update to zlib 1.2.5.2 to be able to build. + # TODO: Watch https://github.com/travis-ci/travis-ci/issues/2046 and remove + # this hack once travis-ci switches to Ubuntu 14.04 + - wget 'https://github.com/notmuch/travis-files/raw/master/zlib1g-dev_1.2.8.dfsg-1ubuntu1_amd64.deb' + - wget 'https://github.com/notmuch/travis-files/raw/master/zlib1g_1.2.8.dfsg-1ubuntu1_amd64.deb' + - sudo dpkg -i zlib1g-dev_1.2.8.dfsg-1ubuntu1_amd64.deb zlib1g_1.2.8.dfsg-1ubuntu1_amd64.deb + - sudo apt-get install -f + +script: + - ./configure + - make test + +notifications: + irc: + channels: + - "chat.freenode.net#notmuch" + on_success: change diff --git a/Makefile.local b/Makefile.local index 4f8f4640..81ee3477 100644 --- a/Makefile.local +++ b/Makefile.local @@ -10,10 +10,10 @@ # repository), we let git append identification of the actual commit. PACKAGE=notmuch -IS_GIT=$(shell if [ -d .git ] ; then echo yes ; else echo no; fi) +IS_GIT=$(shell if [ -d ${srcdir}/.git ] ; then echo yes ; else echo no; fi) ifeq ($(IS_GIT),yes) -DATE:=$(shell git log --date=short -1 --pretty=format:%cd) +DATE:=$(shell git --git-dir=${srcdir}/.git log --date=short -1 --pretty=format:%cd) else DATE:=$(shell date +%F) endif @@ -21,7 +21,7 @@ endif VERSION:=$(shell cat ${srcdir}/version) ifeq ($(filter release release-message pre-release update-versions,$(MAKECMDGOALS)),) ifeq ($(IS_GIT),yes) -VERSION:=$(shell git describe --match '[0-9.]*'|sed -e s/_/~/ -e s/-/+/ -e s/-/~/) +VERSION:=$(shell git --git-dir=${srcdir}/.git describe --abbrev=7 --match '[0-9.]*'|sed -e s/_/~/ -e s/-/+/ -e s/-/~/) # Write the file 'version.stamp' in case its contents differ from $(VERSION) FILE_VERSION:=$(shell test -f version.stamp && read vs < version.stamp || vs=; echo $$vs) ifneq ($(FILE_VERSION),$(VERSION)) @@ -201,11 +201,11 @@ verify-source-tree-and-version: verify-no-dirty-code verify-no-dirty-code: release-checks ifeq ($(IS_GIT),yes) @printf "Checking that source tree is clean..." -ifneq ($(shell git ls-files -m),) +ifneq ($(shell git --git-dir=${srcdir}/.git ls-files -m),) @echo "No" @echo "The following files have been modified since the most recent git commit:" @echo "" - @git ls-files -m + @git --git-dir=${srcdir}/.git ls-files -m @echo "" @echo "The release will be made from the committed state, but perhaps you meant" @echo "to commit this code first? Please clean this up to make it more clear." @@ -262,6 +262,10 @@ clean: distclean: clean rm -rf $(DISTCLEAN) +.PHONY: dataclean +dataclean: distclean + rm -rf $(DATACLEAN) + notmuch_client_srcs = \ command-line-arguments.c\ debugger.c \ @@ -331,9 +335,10 @@ install-desktop: desktop-file-install --mode 0644 --dir "$(DESTDIR)$(desktop_dir)" notmuch.desktop SRCS := $(SRCS) $(notmuch_client_srcs) -CLEAN := $(CLEAN) notmuch notmuch-shared $(notmuch_client_modules) version.stamp +CLEAN := $(CLEAN) notmuch notmuch-shared $(notmuch_client_modules) +CLEAN := $(CLEAN) version.stamp notmuch-*.tar.gz.tmp -DISTCLEAN := $(DISTCLEAN) .first-build-message Makefile.config +DISTCLEAN := $(DISTCLEAN) .first-build-message Makefile.config sh.config DEPS := $(SRCS:%.c=.deps/%.d) DEPS := $(DEPS:%.cc=.deps/%.d) diff --git a/NEWS b/NEWS index 61c6424e..47188381 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,64 @@ +Notmuch 0.19 (UNRELEASED) +========================= + +Emacs Interface +--------------- + +Use the `j` key to access saved searches from anywhere in notmuch + + `j` is now globally bound to `notmuch-jump`, which provides fast, + interactive keyboard shortcuts to saved searches. For example, + with the default saved searches `j i` from anywhere in notmuch will + bring up the inbox. + +Expanded default saved search settings + + The default saved searches now include several more common searches, + as well as shortcut keys for `notmuch-jump`. + +Library changes +--------------- + +Add return status to notmuch_database_close and +notmuch_database_destroy + +nmbug +----- + +The Perl script has been translated to Python; you'll need Python 2.7 +or anything from the 3.x line. Most of the user-facing interface is +the same, but `nmbug help` is not `nmbug --help`, and the following nmbug +commands have slightly different interfaces: `archive`, `commit`, +`fetch`, `log`, `pull`, `push`, and `status`. For details on the +new interface for a given command, run `nmbug COMMAND --help`. + +nmbug-status +------------ + +`nmbug-status` can now optionally load header and footer templates +from the config file. Use something like: + + { + "meta": { + "header": "\n\n...", + "footer": "", + ... + }, + ... + }, + +Python Bindings +--------------- + +Add support for `notmuch_query_add_tag_exclude` + +Build System +------------ + +The notmuch binaries and libraries are now build with debugging symbols +by default. Users concerned with disk space should change the +defaults when configuring or use the strip(1) command. + Notmuch 0.18.2 (2014-10-25) =========================== diff --git a/README.rst b/README.rst new file mode 100644 index 00000000..7417ddcb --- /dev/null +++ b/README.rst @@ -0,0 +1,11 @@ +If you're reading this on http://github.com/notmuch/notmuch, this is a +read-only mirror of the notmuch project. + +For more information about the project, see http://notmuchmail.org. + +Please don't send us pull requests on github. If you have a feature +branch that you want us to look at, use ``git send-email`` to send it +to notmuch@notmuchmail.org. + +For more information about contributing to the project, see +http://notmuchmail.org/contributing/. diff --git a/bindings/go/src/notmuch/notmuch.go b/bindings/go/src/notmuch/notmuch.go index 00bd53ac..b9230ad2 100644 --- a/bindings/go/src/notmuch/notmuch.go +++ b/bindings/go/src/notmuch/notmuch.go @@ -144,8 +144,8 @@ func OpenDatabase(path string, mode DatabaseMode) (*Database, Status) { /* Close the given notmuch database, freeing all associated * resources. See notmuch_database_open. */ -func (self *Database) Close() { - C.notmuch_database_destroy(self.db) +func (self *Database) Close() Status { + return Status(C.notmuch_database_destroy(self.db)) } /* Return the database path of the given database. diff --git a/bindings/python/docs/source/query.rst b/bindings/python/docs/source/query.rst index ddfc3485..044b5735 100644 --- a/bindings/python/docs/source/query.rst +++ b/bindings/python/docs/source/query.rst @@ -32,6 +32,8 @@ :attr:`Query.SORT`) if explicitely specified via :meth:`set_sort`. By default it is set to `None`. + .. automethod:: exclude_tag + .. automethod:: search_threads .. automethod:: search_messages diff --git a/bindings/python/notmuch/database.py b/bindings/python/notmuch/database.py index 7ddf5cfe..5b58e099 100644 --- a/bindings/python/notmuch/database.py +++ b/bindings/python/notmuch/database.py @@ -157,11 +157,13 @@ class Database(object): _destroy = nmlib.notmuch_database_destroy _destroy.argtypes = [NotmuchDatabaseP] - _destroy.restype = None + _destroy.restype = c_uint def __del__(self): if self._db: - self._destroy(self._db) + status = self._destroy(self._db) + if status != STATUS.SUCCESS: + raise NotmuchError(status) def _assert_db_is_initialized(self): """Raises :exc:`NotInitializedError` if self._db is `None`""" @@ -217,7 +219,7 @@ class Database(object): _close = nmlib.notmuch_database_close _close.argtypes = [NotmuchDatabaseP] - _close.restype = None + _close.restype = c_uint def close(self): ''' @@ -231,7 +233,9 @@ class Database(object): NotmuchError. ''' if self._db: - self._close(self._db) + status = self._close(self._db) + if status != STATUS.SUCCESS: + raise NotmuchError(status) def __enter__(self): ''' diff --git a/bindings/python/notmuch/globals.py b/bindings/python/notmuch/globals.py index 2deb87cf..24b25d36 100644 --- a/bindings/python/notmuch/globals.py +++ b/bindings/python/notmuch/globals.py @@ -24,9 +24,9 @@ from ctypes import CDLL, Structure, POINTER try: from os import uname if uname()[0] == 'Darwin': - nmlib = CDLL("libnotmuch.3.dylib") + nmlib = CDLL("libnotmuch.4.dylib") else: - nmlib = CDLL("libnotmuch.so.3") + nmlib = CDLL("libnotmuch.so.4") except: raise ImportError("Could not find shared 'notmuch' library.") diff --git a/bindings/python/notmuch/query.py b/bindings/python/notmuch/query.py index b11a399d..94773ac5 100644 --- a/bindings/python/notmuch/query.py +++ b/bindings/python/notmuch/query.py @@ -118,6 +118,21 @@ class Query(object): self.sort = sort self._set_sort(self._query, sort) + _exclude_tag = nmlib.notmuch_query_add_tag_exclude + _exclude_tag.argtypes = [NotmuchQueryP, c_char_p] + _exclude_tag.resttype = None + + def exclude_tag(self, tagname): + """Add a tag that will be excluded from the query results by default. + + This exclusion will be overridden if this tag appears explicitly in the + query. + + :param tagname: Name of the tag to be excluded + """ + self._assert_query_is_initialized() + self._exclude_tag(self._query, _str(tagname)) + """notmuch_query_search_threads""" _search_threads = nmlib.notmuch_query_search_threads _search_threads.argtypes = [NotmuchQueryP] diff --git a/bindings/ruby/database.c b/bindings/ruby/database.c index e84f726d..c03d7011 100644 --- a/bindings/ruby/database.c +++ b/bindings/ruby/database.c @@ -113,11 +113,13 @@ notmuch_rb_database_open (int argc, VALUE *argv, VALUE klass) VALUE notmuch_rb_database_close (VALUE self) { + notmuch_status_t ret; notmuch_database_t *db; Data_Get_Notmuch_Database (self, db); - notmuch_database_destroy (db); + ret = notmuch_database_destroy (db); DATA_PTR (self) = NULL; + notmuch_rb_status_raise (ret); return Qnil; } diff --git a/bindings/ruby/defs.h b/bindings/ruby/defs.h index 5b44585a..f4901a04 100644 --- a/bindings/ruby/defs.h +++ b/bindings/ruby/defs.h @@ -231,6 +231,9 @@ notmuch_rb_query_search_messages (VALUE self); VALUE notmuch_rb_query_count_messages (VALUE self); +VALUE +notmuch_rb_query_count_threads (VALUE self); + /* threads.c */ VALUE notmuch_rb_threads_destroy (VALUE self); diff --git a/bindings/ruby/init.c b/bindings/ruby/init.c index 663271d4..ab3f22df 100644 --- a/bindings/ruby/init.c +++ b/bindings/ruby/init.c @@ -271,6 +271,7 @@ Init_notmuch (void) rb_define_method (notmuch_rb_cQuery, "search_threads", notmuch_rb_query_search_threads, 0); /* in query.c */ rb_define_method (notmuch_rb_cQuery, "search_messages", notmuch_rb_query_search_messages, 0); /* in query.c */ rb_define_method (notmuch_rb_cQuery, "count_messages", notmuch_rb_query_count_messages, 0); /* in query.c */ + rb_define_method (notmuch_rb_cQuery, "count_threads", notmuch_rb_query_count_threads, 0); /* in query.c */ /* * Document-class: Notmuch::Threads diff --git a/bindings/ruby/query.c b/bindings/ruby/query.c index 1658edee..a7dacba3 100644 --- a/bindings/ruby/query.c +++ b/bindings/ruby/query.c @@ -182,3 +182,22 @@ notmuch_rb_query_count_messages (VALUE self) */ return UINT2NUM(notmuch_query_count_messages(query)); } + +/* + * call-seq: QUERY.count_threads => Fixnum + * + * Return an estimate of the number of threads matching a search + */ +VALUE +notmuch_rb_query_count_threads (VALUE self) +{ + notmuch_query_t *query; + + Data_Get_Notmuch_Query (self, query); + + /* Xapian exceptions are not handled properly. + * (function may return 0 after printing a message) + * Thus there is nothing we can do here... + */ + return UINT2NUM(notmuch_query_count_threads(query)); +} diff --git a/completion/notmuch-completion.bash b/completion/notmuch-completion.bash index d88c5e7d..0571dc9d 100644 --- a/completion/notmuch-completion.bash +++ b/completion/notmuch-completion.bash @@ -395,6 +395,10 @@ _notmuch() { local _notmuch_commands="compact config count dump help insert new reply restore search setup show tag" local arg cur prev words cword split + + # require bash-completion with _init_completion + type -t _init_completion >/dev/null 2>&1 || return + _init_completion || return COMPREPLY=() diff --git a/configure b/configure index 99ab74dc..331f29bd 100755 --- a/configure +++ b/configure @@ -43,9 +43,9 @@ fi # Set several defaults (optionally specified by the user in # environment variables) -CC=${CC:-gcc} -CXX=${CXX:-g++} -CFLAGS=${CFLAGS:--O2} +CC=${CC:-cc} +CXX=${CXX:-c++} +CFLAGS=${CFLAGS:--g -O2} CPPFLAGS=${CPPFLAGS:-} CXXFLAGS=${CXXFLAGS:-\$(CFLAGS)} LDFLAGS=${LDFLAGS:-} @@ -417,6 +417,15 @@ else have_emacs=0 fi +printf "Checking if doxygen is available... " +if command -v doxygen > /dev/null 2>&1; then + printf "Yes.\n" + have_doxygen=1 +else + printf "No (so will not install api docs)\n" + have_doxygen=0 +fi + printf "Checking if sphinx is available and supports nroff output... " if hash sphinx-build > /dev/null 2>&1 && python -m sphinx.writers.manpage > /dev/null 2>&1 ; then printf "Yes.\n" @@ -829,6 +838,9 @@ HAVE_SPHINX=${have_sphinx} # Whether there's a rst2man binary available for building documentation HAVE_RST2MAN=${have_rst2man} +# Whether there's a doxygen binary available for building api documentation +HAVE_DOXYGEN=${have_doxygen} + # The directory to which desktop files should be installed desktop_dir = \$(prefix)/share/applications @@ -944,3 +956,16 @@ CONFIGURE_CXXFLAGS = -DHAVE_GETLINE=\$(HAVE_GETLINE) \$(GMIME_CFLAGS) \\ CONFIGURE_LDFLAGS = \$(GMIME_LDFLAGS) \$(TALLOC_LDFLAGS) \$(ZLIB_LDFLAGS) \$(XAPIAN_LDFLAGS) EOF + +# construct the sh.config +cat > sh.config < Tue, 16 Sep 2014 21:02:17 +0200 + notmuch (0.18.2-1) unstable; urgency=medium * Rebuild for unstable. diff --git a/debian/control b/debian/control index 50de2ff7..5e4947d7 100644 --- a/debian/control +++ b/debian/control @@ -20,7 +20,7 @@ Build-Depends: ruby, ruby-dev (>>1:1.9.3~), emacs24-nox | emacs24 (>=24~) | emacs24-lucid (>=24~) | emacs23-nox | emacs23 (>=23~) | emacs23-lucid (>=23~), - gdb [!s390x !ia64 !armel !arm64], + gdb [!s390x !ia64 !armel], dtach (>= 0.8), bash-completion (>=1.9.0~) Standards-Version: 3.9.4 @@ -30,7 +30,7 @@ Vcs-Browser: http://git.notmuchmail.org/git/notmuch Package: notmuch Architecture: any -Depends: libnotmuch3 (= ${binary:Version}), ${shlibs:Depends}, ${misc:Depends} +Depends: libnotmuch4 (= ${binary:Version}), ${shlibs:Depends}, ${misc:Depends} Recommends: notmuch-emacs | notmuch-vim | notmuch-mutt | alot, gnupg-agent Description: thread-based email index, search and tagging Notmuch is a system for indexing, searching, reading, and tagging @@ -40,7 +40,7 @@ Description: thread-based email index, search and tagging . This package contains the notmuch command-line interface -Package: libnotmuch3 +Package: libnotmuch4 Section: libs Architecture: any Depends: ${shlibs:Depends}, ${misc:Depends} @@ -57,7 +57,7 @@ Description: thread-based email index, search and tagging (runtime) Package: libnotmuch-dev Section: libdevel Architecture: any -Depends: ${misc:Depends}, libnotmuch3 (= ${binary:Version}) +Depends: ${misc:Depends}, libnotmuch4 (= ${binary:Version}) Description: thread-based email index, search and tagging (development) Notmuch is a system for indexing, searching, reading, and tagging large collections of email messages in maildir or mh format. It uses @@ -70,7 +70,7 @@ Description: thread-based email index, search and tagging (development) Package: python-notmuch Architecture: all Section: python -Depends: ${misc:Depends}, ${python:Depends}, libnotmuch3 (>= ${source:Version}) +Depends: ${misc:Depends}, ${python:Depends}, libnotmuch4 (>= ${source:Version}) Description: python interface to the notmuch mail search and index library Notmuch is a system for indexing, searching, reading, and tagging large collections of email messages in maildir or mh format. It uses @@ -83,7 +83,7 @@ Description: python interface to the notmuch mail search and index library Package: python3-notmuch Architecture: all Section: python -Depends: ${misc:Depends}, ${python3:Depends}, libnotmuch3 (>= ${source:Version}) +Depends: ${misc:Depends}, ${python3:Depends}, libnotmuch4 (>= ${source:Version}) Description: Python 3 interface to the notmuch mail search and index library Notmuch is a system for indexing, searching, reading, and tagging large collections of email messages in maildir or mh format. It uses @@ -163,7 +163,7 @@ Package: notmuch-dbg Architecture: any Section: debug Priority: extra -Depends: ${shlibs:Depends}, ${misc:Depends}, libnotmuch3 (= ${binary:Version}) +Depends: ${shlibs:Depends}, ${misc:Depends}, libnotmuch4 (= ${binary:Version}) Description: thread-based email index, search and tagging - debugging symbols Notmuch is a system for indexing, searching, reading, and tagging large collections of email messages in maildir or mh format. It uses diff --git a/debian/libnotmuch3.install b/debian/libnotmuch3.install deleted file mode 100644 index a513b475..00000000 --- a/debian/libnotmuch3.install +++ /dev/null @@ -1 +0,0 @@ -usr/lib/*/libnotmuch.so.* diff --git a/debian/libnotmuch3.symbols b/debian/libnotmuch3.symbols deleted file mode 100644 index ef6ae9db..00000000 --- a/debian/libnotmuch3.symbols +++ /dev/null @@ -1,91 +0,0 @@ -libnotmuch.so.3 libnotmuch3 #MINVER# - notmuch_database_add_message@Base 0.3 - notmuch_database_begin_atomic@Base 0.9~rc1 - notmuch_database_close@Base 0.13~rc1 - notmuch_database_compact@Base 0.17~rc1 - notmuch_database_create@Base 0.3 - notmuch_database_destroy@Base 0.13~rc1 - notmuch_database_end_atomic@Base 0.9~rc1 - notmuch_database_find_message@Base 0.9~rc2 - notmuch_database_find_message_by_filename@Base 0.9~rc2 - notmuch_database_get_all_tags@Base 0.3 - notmuch_database_get_directory@Base 0.3 - notmuch_database_get_path@Base 0.3 - notmuch_database_get_version@Base 0.3 - notmuch_database_needs_upgrade@Base 0.3 - notmuch_database_open@Base 0.3 - notmuch_database_remove_message@Base 0.3 - notmuch_database_upgrade@Base 0.3 - notmuch_directory_destroy@Base 0.3 - notmuch_directory_get_child_directories@Base 0.3 - notmuch_directory_get_child_files@Base 0.3 - notmuch_directory_get_mtime@Base 0.3 - notmuch_directory_set_mtime@Base 0.3 - notmuch_filenames_destroy@Base 0.3 - notmuch_filenames_get@Base 0.3 - notmuch_filenames_move_to_next@Base 0.3 - notmuch_filenames_valid@Base 0.3 - notmuch_message_add_tag@Base 0.3 - notmuch_message_destroy@Base 0.3 - notmuch_message_freeze@Base 0.3 - notmuch_message_get_date@Base 0.3 - notmuch_message_get_filename@Base 0.3 - notmuch_message_get_filenames@Base 0.5 - notmuch_message_get_flag@Base 0.3 - notmuch_message_get_header@Base 0.3 - notmuch_message_get_message_id@Base 0.3 - notmuch_message_get_replies@Base 0.3 - notmuch_message_get_tags@Base 0.3 - notmuch_message_get_thread_id@Base 0.3 - notmuch_message_maildir_flags_to_tags@Base 0.5 - notmuch_message_remove_all_tags@Base 0.3 - notmuch_message_remove_tag@Base 0.3 - notmuch_message_set_flag@Base 0.3 - notmuch_message_tags_to_maildir_flags@Base 0.5 - notmuch_message_thaw@Base 0.3 - notmuch_messages_collect_tags@Base 0.3 - notmuch_messages_destroy@Base 0.3 - notmuch_messages_get@Base 0.3 - notmuch_messages_move_to_next@Base 0.3 - notmuch_messages_valid@Base 0.3 - notmuch_query_add_tag_exclude@Base 0.12~rc1 - notmuch_query_count_messages@Base 0.3 - notmuch_query_count_threads@Base 0.10~rc1 - notmuch_query_create@Base 0.3 - notmuch_query_destroy@Base 0.3 - notmuch_query_get_query_string@Base 0.4 - notmuch_query_get_sort@Base 0.4 - notmuch_query_search_messages@Base 0.3 - notmuch_query_search_threads@Base 0.3 - notmuch_query_set_omit_excluded@Base 0.13~rc1 - notmuch_query_set_sort@Base 0.3 - notmuch_status_to_string@Base 0.3 - notmuch_tags_destroy@Base 0.3 - notmuch_tags_get@Base 0.3 - notmuch_tags_move_to_next@Base 0.3 - notmuch_tags_valid@Base 0.3 - notmuch_thread_destroy@Base 0.3 - notmuch_thread_get_authors@Base 0.3 - notmuch_thread_get_matched_messages@Base 0.3 - notmuch_thread_get_messages@Base 0.16 - notmuch_thread_get_newest_date@Base 0.3 - notmuch_thread_get_oldest_date@Base 0.3 - notmuch_thread_get_subject@Base 0.3 - notmuch_thread_get_tags@Base 0.3 - notmuch_thread_get_thread_id@Base 0.3 - notmuch_thread_get_toplevel_messages@Base 0.3 - notmuch_thread_get_total_messages@Base 0.3 - notmuch_threads_destroy@Base 0.3 - notmuch_threads_get@Base 0.3 - notmuch_threads_move_to_next@Base 0.3 - notmuch_threads_valid@Base 0.3 - (c++)"typeinfo for Xapian::LogicError@Base" 0.6.1 - (c++)"typeinfo for Xapian::RuntimeError@Base" 0.6.1 - (c++)"typeinfo for Xapian::DocNotFoundError@Base" 0.6.1 - (c++)"typeinfo for Xapian::InvalidArgumentError@Base" 0.6.1 - (c++)"typeinfo for Xapian::Error@Base" 0.6.1 - (c++)"typeinfo name for Xapian::LogicError@Base" 0.6.1 - (c++)"typeinfo name for Xapian::RuntimeError@Base" 0.6.1 - (c++)"typeinfo name for Xapian::DocNotFoundError@Base" 0.6.1 - (c++)"typeinfo name for Xapian::InvalidArgumentError@Base" 0.6.1 - (c++)"typeinfo name for Xapian::Error@Base" 0.6.1 diff --git a/debian/libnotmuch4.install b/debian/libnotmuch4.install new file mode 100644 index 00000000..a513b475 --- /dev/null +++ b/debian/libnotmuch4.install @@ -0,0 +1 @@ +usr/lib/*/libnotmuch.so.* diff --git a/debian/libnotmuch4.symbols b/debian/libnotmuch4.symbols new file mode 100644 index 00000000..e127c0ce --- /dev/null +++ b/debian/libnotmuch4.symbols @@ -0,0 +1,91 @@ +libnotmuch.so.4 libnotmuch4 #MINVER# + notmuch_database_add_message@Base 0.3 + notmuch_database_begin_atomic@Base 0.9~rc1 + notmuch_database_close@Base 0.13~rc1 + notmuch_database_compact@Base 0.17~rc1 + notmuch_database_create@Base 0.3 + notmuch_database_destroy@Base 0.13~rc1 + notmuch_database_end_atomic@Base 0.9~rc1 + notmuch_database_find_message@Base 0.9~rc2 + notmuch_database_find_message_by_filename@Base 0.9~rc2 + notmuch_database_get_all_tags@Base 0.3 + notmuch_database_get_directory@Base 0.3 + notmuch_database_get_path@Base 0.3 + notmuch_database_get_version@Base 0.3 + notmuch_database_needs_upgrade@Base 0.3 + notmuch_database_open@Base 0.3 + notmuch_database_remove_message@Base 0.3 + notmuch_database_upgrade@Base 0.3 + notmuch_directory_destroy@Base 0.3 + notmuch_directory_get_child_directories@Base 0.3 + notmuch_directory_get_child_files@Base 0.3 + notmuch_directory_get_mtime@Base 0.3 + notmuch_directory_set_mtime@Base 0.3 + notmuch_filenames_destroy@Base 0.3 + notmuch_filenames_get@Base 0.3 + notmuch_filenames_move_to_next@Base 0.3 + notmuch_filenames_valid@Base 0.3 + notmuch_message_add_tag@Base 0.3 + notmuch_message_destroy@Base 0.3 + notmuch_message_freeze@Base 0.3 + notmuch_message_get_date@Base 0.3 + notmuch_message_get_filename@Base 0.3 + notmuch_message_get_filenames@Base 0.5 + notmuch_message_get_flag@Base 0.3 + notmuch_message_get_header@Base 0.3 + notmuch_message_get_message_id@Base 0.3 + notmuch_message_get_replies@Base 0.3 + notmuch_message_get_tags@Base 0.3 + notmuch_message_get_thread_id@Base 0.3 + notmuch_message_maildir_flags_to_tags@Base 0.5 + notmuch_message_remove_all_tags@Base 0.3 + notmuch_message_remove_tag@Base 0.3 + notmuch_message_set_flag@Base 0.3 + notmuch_message_tags_to_maildir_flags@Base 0.5 + notmuch_message_thaw@Base 0.3 + notmuch_messages_collect_tags@Base 0.3 + notmuch_messages_destroy@Base 0.3 + notmuch_messages_get@Base 0.3 + notmuch_messages_move_to_next@Base 0.3 + notmuch_messages_valid@Base 0.3 + notmuch_query_add_tag_exclude@Base 0.12~rc1 + notmuch_query_count_messages@Base 0.3 + notmuch_query_count_threads@Base 0.10~rc1 + notmuch_query_create@Base 0.3 + notmuch_query_destroy@Base 0.3 + notmuch_query_get_query_string@Base 0.4 + notmuch_query_get_sort@Base 0.4 + notmuch_query_search_messages@Base 0.3 + notmuch_query_search_threads@Base 0.3 + notmuch_query_set_omit_excluded@Base 0.13~rc1 + notmuch_query_set_sort@Base 0.3 + notmuch_status_to_string@Base 0.3 + notmuch_tags_destroy@Base 0.3 + notmuch_tags_get@Base 0.3 + notmuch_tags_move_to_next@Base 0.3 + notmuch_tags_valid@Base 0.3 + notmuch_thread_destroy@Base 0.3 + notmuch_thread_get_authors@Base 0.3 + notmuch_thread_get_matched_messages@Base 0.3 + notmuch_thread_get_messages@Base 0.16 + notmuch_thread_get_newest_date@Base 0.3 + notmuch_thread_get_oldest_date@Base 0.3 + notmuch_thread_get_subject@Base 0.3 + notmuch_thread_get_tags@Base 0.3 + notmuch_thread_get_thread_id@Base 0.3 + notmuch_thread_get_toplevel_messages@Base 0.3 + notmuch_thread_get_total_messages@Base 0.3 + notmuch_threads_destroy@Base 0.3 + notmuch_threads_get@Base 0.3 + notmuch_threads_move_to_next@Base 0.3 + notmuch_threads_valid@Base 0.3 + (c++)"typeinfo for Xapian::LogicError@Base" 0.6.1 + (c++)"typeinfo for Xapian::RuntimeError@Base" 0.6.1 + (c++)"typeinfo for Xapian::DocNotFoundError@Base" 0.6.1 + (c++)"typeinfo for Xapian::InvalidArgumentError@Base" 0.6.1 + (c++)"typeinfo for Xapian::Error@Base" 0.6.1 + (c++)"typeinfo name for Xapian::LogicError@Base" 0.6.1 + (c++)"typeinfo name for Xapian::RuntimeError@Base" 0.6.1 + (c++)"typeinfo name for Xapian::DocNotFoundError@Base" 0.6.1 + (c++)"typeinfo name for Xapian::InvalidArgumentError@Base" 0.6.1 + (c++)"typeinfo name for Xapian::Error@Base" 0.6.1 diff --git a/devel/news2wiki.pl b/devel/news2wiki.pl index 8066ba7f..d966babf 100755 --- a/devel/news2wiki.pl +++ b/devel/news2wiki.pl @@ -32,8 +32,7 @@ while () { warn "$ARGV[0]:$.: tab(s) in line!\n" if /\t/; warn "$ARGV[0]:$.: trailing whitespace\n" if /\s\s$/; - # The date part in regex recognizes wip version dates like: (201x-xx-xx). - if (/^Notmuch\s+(\S+)\s+\((\w\w\w\w-\w\w-\w\w)\)\s*$/) { + if (/^Notmuch\s+(\S+)\s+\((\d\d\d\d-\d\d-\d\d|UNRELEASED)\)\s*$/) { # open O... autocloses previously opened file. open O, '>', "$ARGV[1]/release-$1.mdwn" or die $!; print "+ release-$1.mdwn...\n"; diff --git a/devel/nmbug/nmbug b/devel/nmbug/nmbug index b18ded7b..9402eade 100755 --- a/devel/nmbug/nmbug +++ b/devel/nmbug/nmbug @@ -1,698 +1,807 @@ -#!/usr/bin/env perl -# Copyright (c) 2011 David Bremner -# License: same as notmuch - -use strict; -use warnings; -use File::Temp qw(tempdir); -use Pod::Usage; - -no encoding; - -my $NMBGIT = $ENV{NMBGIT} || $ENV{HOME}.'/.nmbug'; - -$NMBGIT .= '/.git' if (-d $NMBGIT.'/.git'); - -my $TAGPREFIX = defined($ENV{NMBPREFIX}) ? $ENV{NMBPREFIX} : 'notmuch::'; - -# for encoding - -my $ESCAPE_CHAR = '%'; -my $NO_ESCAPE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'. - '0123456789+-_@=.:,'; -my $MUST_ENCODE = qr{[^\Q$NO_ESCAPE\E]}; -my $ESCAPED_RX = qr{$ESCAPE_CHAR([A-Fa-f0-9]{2})}; - -my %command = ( - archive => \&do_archive, - checkout => \&do_checkout, - clone => \&do_clone, - commit => \&do_commit, - fetch => \&do_fetch, - help => \&do_help, - log => \&do_log, - merge => \&do_merge, - pull => \&do_pull, - push => \&do_push, - status => \&do_status, - ); - -# Convert prefix into form suitable for literal matching against -# notmuch dump --format=batch-tag output. -my $ENCPREFIX = encode_for_fs ($TAGPREFIX); -$ENCPREFIX =~ s/:/%3a/g; - -my $subcommand = shift || usage (); - -if (!exists $command{$subcommand}) { - usage (); -} - -# magic hash for git -my $EMPTYBLOB = git (qw{hash-object -t blob /dev/null}); - -&{$command{$subcommand}}(@ARGV); - -sub git_pipe { - my $envref = (ref $_[0] eq 'HASH') ? shift : {}; - my $ioref = (ref $_[0] eq 'ARRAY') ? shift : undef; - my $dir = ($_[0] eq '-|' or $_[0] eq '|-') ? shift : undef; - - unshift @_, 'git'; - $envref->{GIT_DIR} ||= $NMBGIT; - spawn ($envref, defined $ioref ? $ioref : (), defined $dir ? $dir : (), @_); -} - -sub git { - my $fh = git_pipe (@_); - my $str = join ('', <$fh>); - unless (close $fh) { - die "'git @_' exited with nonzero value\n"; - } - chomp($str); - return $str; -} - -sub spawn { - my $envref = (ref $_[0] eq 'HASH') ? shift : {}; - my $ioref = (ref $_[0] eq 'ARRAY') ? shift : undef; - my $dir = ($_[0] eq '-|' or $_[0] eq '|-') ? shift : '-|'; - - die unless @_; - - if (open my $child, $dir) { - return $child; - } - # child - while (my ($key, $value) = each %{$envref}) { - $ENV{$key} = $value; - } - - if (defined $ioref && $dir eq '-|') { - open my $fh, '|-', @_ or die "open |- @_: $!"; - foreach my $line (@{$ioref}) { - print $fh $line, "\n"; - } - exit ! close $fh; - } else { - if ($dir ne '|-') { - open STDIN, '<', '/dev/null' or die "reopening stdin: $!" - } - exec @_; - die "exec @_: $!"; - } -} - - -sub get_tags { - my $prefix = shift; - my @tags; - - my $fh = spawn ('-|', qw/notmuch search --output=tags/, "*") - or die 'error dumping tags'; - - while (<$fh>) { - chomp (); - push @tags, $_ if (m/^$prefix/); - } - unless (close $fh) { - die "'notmuch search --output=tags *' exited with nonzero value\n"; - } - return @tags; -} - - -sub do_archive { - system ('git', "--git-dir=$NMBGIT", 'archive', 'HEAD'); -} - -sub do_clone { - my $repository = shift; - - my $tempwork = tempdir ('/tmp/nmbug-clone.XXXXXX', CLEANUP => 1); - system ('git', 'clone', '--no-checkout', '--separate-git-dir', $NMBGIT, - $repository, $tempwork) == 0 - or die "'git clone' exited with nonzero value\n"; - git ('config', '--unset', 'core.worktree'); - git ('config', 'core.bare', 'true'); -} - -sub is_committed { - my $status = shift; - return scalar (@{$status->{added}} ) + scalar (@{$status->{deleted}} ) == 0; -} - - -sub do_commit { - my @args = @_; - - my $status = compute_status (); - - if ( is_committed ($status) ) { - print "Nothing to commit\n"; - return; - } - - my $index = read_tree ('HEAD'); - - update_index ($index, $status); - - my $tree = git ( { GIT_INDEX_FILE => $index }, 'write-tree') - or die 'no output from write-tree'; - - my $parent = git ( 'rev-parse', 'HEAD' ) - or die 'no output from rev-parse'; - - my $commit = git ([ @args ], 'commit-tree', $tree, '-p', $parent) - or die 'commit-tree'; - - git ('update-ref', 'HEAD', $commit); - - unlink $index || die "unlink: $!"; - -} - -sub read_tree { - my $treeish = shift; - my $index = $NMBGIT.'/nmbug.index'; - git ({ GIT_INDEX_FILE => $index }, 'read-tree', '--empty'); - git ({ GIT_INDEX_FILE => $index }, 'read-tree', $treeish); - return $index; -} - -sub update_index { - my $index = shift; - my $status = shift; - - my $git = spawn ({ GIT_DIR => $NMBGIT, GIT_INDEX_FILE => $index }, - '|-', qw/git update-index --index-info/) - or die 'git update-index'; - - foreach my $pair (@{$status->{deleted}}) { - index_tags_for_msg ($git, $pair->{id}, 'D', $pair->{tag}); - } - - foreach my $pair (@{$status->{added}}) { - index_tags_for_msg ($git, $pair->{id}, 'A', $pair->{tag}); - } - unless (close $git) { - die "'git update-index --index-info' exited with nonzero value\n"; - } - -} - - -sub do_fetch { - my $remote = shift || 'origin'; - - git ('fetch', $remote); -} - - -sub notmuch { - my @args = @_; - system ('notmuch', @args) == 0 or die "notmuch @args failed: $?"; -} - - -sub index_tags { - - my $index = $NMBGIT.'/nmbug.index'; - - my $query = join ' ', map ("tag:\"$_\"", get_tags ($TAGPREFIX)); - - my $fh = spawn ('-|', qw/notmuch dump --format=batch-tag --/, $query) - or die "notmuch dump: $!"; - - git ('read-tree', '--empty'); - my $git = spawn ({ GIT_DIR => $NMBGIT, GIT_INDEX_FILE => $index }, - '|-', qw/git update-index --index-info/) - or die 'git update-index'; - - while (<$fh>) { - - chomp(); - my ($rest,$id) = split(/ -- id:/); - - if ($id =~ s/^"(.*)"\s*$/$1/) { - # xapian quoted string, dequote. - $id =~ s/""/"/g; - } - - #strip prefixes from tags before writing - my @tags = grep { s/^[+]$ENCPREFIX//; } split (' ', $rest); - index_tags_for_msg ($git,$id, 'A', @tags); - } - unless (close $git) { - die "'git update-index --index-info' exited with nonzero value\n"; - } - unless (close $fh) { - die "'notmuch dump --format=batch-tag -- $query' exited with nonzero value\n"; - } - return $index; -} - -# update the git index to either create or delete an empty file. -# Neither argument should be encoded/escaped. -sub index_tags_for_msg { - my $fh = shift; - my $msgid = shift; - my $mode = shift; - - my $hash = $EMPTYBLOB; - my $blobmode = '100644'; - - if ($mode eq 'D') { - $blobmode = '0'; - $hash = '0000000000000000000000000000000000000000'; - } - - foreach my $tag (@_) { - my $tagpath = 'tags/' . encode_for_fs ($msgid) . '/' . encode_for_fs ($tag); - print $fh "$blobmode $hash\t$tagpath\n"; - } -} - - -sub do_checkout { - do_sync (action => 'checkout'); -} - -sub quote_for_xapian { - my $str = shift; - $str =~ s/"/""/g; - return '"' . $str . '"'; -} - -sub pair_to_batch_line { - my ($action, $pair) = @_; - - # the tag should already be suitably encoded - - return $action . $ENCPREFIX . $pair->{tag} . - ' -- id:' . quote_for_xapian ($pair->{id})."\n"; -} - -sub do_sync { - - my %args = @_; - - my $status = compute_status (); - my ($A_action, $D_action); - - if ($args{action} eq 'checkout') { - $A_action = '-'; - $D_action = '+'; - } else { - $A_action = '+'; - $D_action = '-'; - } - - my $notmuch = spawn ({}, '|-', qw/notmuch tag --batch/) - or die 'notmuch tag --batch'; - - foreach my $pair (@{$status->{added}}) { - print $notmuch pair_to_batch_line ($A_action, $pair); - } - - foreach my $pair (@{$status->{deleted}}) { - print $notmuch pair_to_batch_line ($D_action, $pair); - } - - unless (close $notmuch) { - die "'notmuch tag --batch' exited with nonzero value\n"; - } -} - - -sub insist_committed { - - my $status=compute_status(); - if ( !is_committed ($status) ) { - print "Uncommitted changes to $TAGPREFIX* tags in notmuch - -For a summary of changes, run 'nmbug status' -To save your changes, run 'nmbug commit' before merging/pull -To discard your changes, run 'nmbug checkout' -"; - exit (1); - } - -} - - -sub do_pull { - my $remote = shift || 'origin'; - my $branch = shift || 'master'; - - git ( 'fetch', $remote); - - do_merge ("$remote/$branch"); -} - - -sub do_merge { - my $commit = shift || '@{upstream}'; - - insist_committed (); - - my $tempwork = tempdir ('/tmp/nmbug-merge.XXXXXX', CLEANUP => 1); - - git ( { GIT_WORK_TREE => $tempwork }, 'checkout', '-f', 'HEAD'); - - git ( { GIT_WORK_TREE => $tempwork }, 'merge', $commit); - - do_checkout (); -} - - -sub do_log { - # we don't want output trapping here, because we want the pager. - system ( 'git', "--git-dir=$NMBGIT", 'log', '--name-status', @_); -} - - -sub do_push { - my $remote = shift || 'origin'; - - git ('push', $remote, 'master'); -} - - -sub do_status { - my $status = compute_status (); - - my %output = (); - foreach my $pair (@{$status->{added}}) { - $output{$pair->{id}} ||= {}; - $output{$pair->{id}}{$pair->{tag}} = 'A' - } - - foreach my $pair (@{$status->{deleted}}) { - $output{$pair->{id}} ||= {}; - $output{$pair->{id}}{$pair->{tag}} = 'D' - } - - foreach my $pair (@{$status->{missing}}) { - $output{$pair->{id}} ||= {}; - $output{$pair->{id}}{$pair->{tag}} = 'U' - } - - if (is_unmerged ()) { - foreach my $pair (diff_refs ('A')) { - $output{$pair->{id}} ||= {}; - $output{$pair->{id}}{$pair->{tag}} ||= ' '; - $output{$pair->{id}}{$pair->{tag}} .= 'a'; - } - - foreach my $pair (diff_refs ('D')) { - $output{$pair->{id}} ||= {}; - $output{$pair->{id}}{$pair->{tag}} ||= ' '; - $output{$pair->{id}}{$pair->{tag}} .= 'd'; - } - } - - foreach my $id (sort keys %output) { - foreach my $tag (sort keys %{$output{$id}}) { - printf "%s\t%s\t%s\n", $output{$id}{$tag}, $id, $tag; - } - } -} - - -sub is_unmerged { - my $commit = shift || '@{upstream}'; - - my $fetch_head = git ('rev-parse', $commit); - my $base = git ( 'merge-base', 'HEAD', $commit); - - return ($base ne $fetch_head); - -} - -sub compute_status { - my %args = @_; - - my @added; - my @deleted; - my @missing; - - my $index = index_tags (); - - my @maybe_deleted = diff_index ($index, 'D'); - - foreach my $pair (@maybe_deleted) { - - my $id = $pair->{id}; - - my $fh = spawn ('-|', qw/notmuch search --output=files/,"id:$id") - or die "searching for $id"; - if (!<$fh>) { - push @missing, $pair; - } else { - push @deleted, $pair; - } - unless (close $fh) { - die "'notmuch search --output=files id:$id' exited with nonzero value\n"; - } - } - - - @added = diff_index ($index, 'A'); - - unlink $index || die "unlink $index: $!"; - - return { added => [@added], deleted => [@deleted], missing => [@missing] }; -} - - -sub diff_index { - my $index = shift; - my $filter = shift; - - my $fh = git_pipe ({ GIT_INDEX_FILE => $index }, - qw/diff-index --cached/, - "--diff-filter=$filter", qw/--name-only HEAD/ ); - - my @lines = unpack_diff_lines ($fh); - unless (close $fh) { - die "'git diff-index --cached --diff-filter=$filter --name-only HEAD' ", - "exited with nonzero value\n"; - } - return @lines; -} - - -sub diff_refs { - my $filter = shift; - my $ref1 = shift || 'HEAD'; - my $ref2 = shift || '@{upstream}'; - - my $fh= git_pipe ( 'diff', "--diff-filter=$filter", '--name-only', - $ref1, $ref2); - - my @lines = unpack_diff_lines ($fh); - unless (close $fh) { - die "'git diff --diff-filter=$filter --name-only $ref1 $ref2' ", - "exited with nonzero value\n"; - } - return @lines; -} - - -sub unpack_diff_lines { - my $fh = shift; - - my @found; - while(<$fh>) { - chomp (); - my ($id,$tag) = m|tags/ ([^/]+) / ([^/]+) |x; - - $id = decode_from_fs ($id); - $tag = decode_from_fs ($tag); - - push @found, { id => $id, tag => $tag }; - } - - return @found; -} - - -sub encode_for_fs { - my $str = shift; - - $str =~ s/($MUST_ENCODE)/"$ESCAPE_CHAR".sprintf ("%02x",ord ($1))/ge; - return $str; -} - - -sub decode_from_fs { - my $str = shift; - - $str =~ s/$ESCAPED_RX/ chr (hex ($1))/eg; - - return $str; - -} - - -sub usage { - pod2usage (); - exit (1); -} - - -sub do_help { - pod2usage ( -verbose => 2 ); - exit (0); -} - -__END__ - -=head1 NAME - -nmbug - manage notmuch tags about notmuch - -=head1 SYNOPSIS - -nmbug subcommand [options] - -B for more help - -=head1 OPTIONS - -=head2 Most common commands - -=over 8 - -=item B [message] - -Commit appropriately prefixed tags from the notmuch database to -git. Any extra arguments are used (one per line) as a commit message. - -=item B [remote] - -push local nmbug git state to remote repo - -=item B [remote] [branch] - -pull (merge) remote repo changes to notmuch. B is equivalent to -B followed by B. The default remote is C, and -the default branch is C. - -=back - -=head2 Other Useful Commands - -=over 8 - -=item B repository - -Create a local nmbug repository from a remote source. This wraps -C, adding some options to avoid creating a working tree -while preserving remote-tracking branches and upstreams. - -=item B - -Update the notmuch database from git. This is mainly useful to discard -your changes in notmuch relative to git. - -=item B [remote] - -Fetch changes from the remote repo (see merge to bring those changes -into notmuch). - -=item B [subcommand] - -print help [for subcommand] - -=item B [parameters] - -A simple wrapper for git log. After running C, you can -inspect the changes with C - -=item B [commit] - -Merge changes from C into HEAD, and load the result into -notmuch. The default commit is C<@{upstream}>. - -=item B - -Show pending updates in notmuch or git repo. See below for more -information about the output format. - -=back - -=head2 Less common commands - -=over 8 - -=item B - -Dump a tar archive (using git archive) of the current nmbug tag set. - -=back - -=head1 STATUS FORMAT - -B prints lines of the form - - ng Message-Id tag - -where n is a single character representing notmuch database status - -=over 8 - -=item B - -Tag is present in notmuch database, but not committed to nmbug -(equivalently, tag has been deleted in nmbug repo, e.g. by a pull, but -not restored to notmuch database). - -=item B - -Tag is present in nmbug repo, but not restored to notmuch database -(equivalently, tag has been deleted in notmuch) - -=item B - -Message is unknown (missing from local notmuch database) - -=back - -The second character (if present) represents a difference between remote -git and local. Typically C needs to be run to update this. - -=over 8 - - -=item B - -Tag is present in remote, but not in local git. - - -=item B - -Tag is present in local git, but not in remote git. - - -=back - -=head1 DUMP FORMAT - -Each tag $tag for message with Message-Id $id is written to -an empty file - - tags/encode($id)/encode($tag) - -The encoding preserves alphanumerics, and the characters "+-_@=.:," -(not the quotes). All other octets are replaced with '%' followed by -a two digit hex number. - -=head1 ENVIRONMENT - -B specifies the location of the git repository used by nmbug. -If not specified $HOME/.nmbug is used. - -B specifies the prefix in the notmuch database for tags of -interest to nmbug. If not specified 'notmuch::' is used. +#!/usr/bin/env python +# +# Copyright (c) 2011-2014 David Bremner +# W. Trevor King +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see http://www.gnu.org/licenses/ . + +""" +Manage notmuch tags with Git + +Environment variables: + +* NMBGIT specifies the location of the git repository used by nmbug. + If not specified $HOME/.nmbug is used. +* NMBPREFIX specifies the prefix in the notmuch database for tags of + interest to nmbug. If not specified 'notmuch::' is used. +""" + +from __future__ import print_function +from __future__ import unicode_literals + +import codecs as _codecs +import collections as _collections +import inspect as _inspect +import locale as _locale +import logging as _logging +import os as _os +import re as _re +import shutil as _shutil +import subprocess as _subprocess +import sys as _sys +import tempfile as _tempfile +import textwrap as _textwrap +try: # Python 3 + from urllib.parse import quote as _quote + from urllib.parse import unquote as _unquote +except ImportError: # Python 2 + from urllib import quote as _quote + from urllib import unquote as _unquote + + +__version__ = '0.2' + +_LOG = _logging.getLogger('nmbug') +_LOG.setLevel(_logging.ERROR) +_LOG.addHandler(_logging.StreamHandler()) + +NMBGIT = _os.path.expanduser( + _os.getenv('NMBGIT', _os.path.join('~', '.nmbug'))) +_NMBGIT = _os.path.join(NMBGIT, '.git') +if _os.path.isdir(_NMBGIT): + NMBGIT = _NMBGIT + +TAG_PREFIX = _os.getenv('NMBPREFIX', 'notmuch::') +_HEX_ESCAPE_REGEX = _re.compile('%[0-9A-F]{2}') +_TAG_FILE_REGEX = _re.compile('tags/(?P[^/]*)/(?P[^/]*)') + +# magic hash for Git (git hash-object -t blob /dev/null) +_EMPTYBLOB = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391' + + +try: + getattr(_tempfile, 'TemporaryDirectory') +except AttributeError: # Python < 3.2 + class _TemporaryDirectory(object): + """ + Fallback context manager for Python < 3.2 + + See PEP 343 for details on context managers [1]. + + [1]: http://legacy.python.org/dev/peps/pep-0343/ + """ + def __init__(self, **kwargs): + self.name = _tempfile.mkdtemp(**kwargs) + + def __enter__(self): + return self.name + + def __exit__(self, type, value, traceback): + _shutil.rmtree(self.name) + + + _tempfile.TemporaryDirectory = _TemporaryDirectory + + +def _hex_quote(string, safe='+@=:,'): + """ + quote('abc def') -> 'abc%20def'. + + Wrap urllib.parse.quote with additional safe characters (in + addition to letters, digits, and '_.-') and lowercase hex digits + (e.g. '%3a' instead of '%3A'). + """ + uppercase_escapes = _quote(string, safe) + return _HEX_ESCAPE_REGEX.sub( + lambda match: match.group(0).lower(), + uppercase_escapes) + + +_ENCODED_TAG_PREFIX = _hex_quote(TAG_PREFIX, safe='+@=,') # quote ':' + + +def _xapian_quote(string): + """ + Quote a string for Xapian's QueryParser. + + Xapian uses double-quotes for quoting strings. You can escape + internal quotes by repeating them [1,2,3]. + + [1]: http://trac.xapian.org/ticket/128#comment:2 + [2]: http://trac.xapian.org/ticket/128#comment:17 + [3]: http://trac.xapian.org/changeset/13823/svn + """ + return '"{0}"'.format(string.replace('"', '""')) + + +def _xapian_unquote(string): + """ + Unquote a Xapian-quoted string. + """ + if string.startswith('"') and string.endswith('"'): + return string[1:-1].replace('""', '"') + return string + + +class SubprocessError(RuntimeError): + "A subprocess exited with a nonzero status" + def __init__(self, args, status, stdout=None, stderr=None): + self.status = status + self.stdout = stdout + self.stderr = stderr + msg = '{args} exited with {status}'.format(args=args, status=status) + if stderr: + msg = '{msg}: {stderr}'.format(msg=msg, stderr=stderr) + super(SubprocessError, self).__init__(msg) + + +class _SubprocessContextManager(object): + """ + PEP 343 context manager for subprocesses. + + 'expect' holds a tuple of acceptable exit codes, otherwise we'll + raise a SubprocessError in __exit__. + """ + def __init__(self, process, args, expect=(0,)): + self._process = process + self._args = args + self._expect = expect + + def __enter__(self): + return self._process + + def __exit__(self, type, value, traceback): + for name in ['stdin', 'stdout', 'stderr']: + stream = getattr(self._process, name) + if stream: + stream.close() + setattr(self._process, name, None) + status = self._process.wait() + _LOG.debug('collect {args} with status {status}'.format( + args=self._args, status=status)) + if status not in self._expect: + raise SubprocessError(args=self._args, status=status) + + def wait(self): + return self._process.wait() + + +def _spawn(args, input=None, additional_env=None, wait=False, stdin=None, + stdout=None, stderr=None, encoding=_locale.getpreferredencoding(), + expect=(0,), **kwargs): + """Spawn a subprocess, and optionally wait for it to finish. + + This wrapper around subprocess.Popen has two modes, depending on + the truthiness of 'wait'. If 'wait' is true, we use p.communicate + internally to write 'input' to the subprocess's stdin and read + from it's stdout/stderr. If 'wait' is False, we return a + _SubprocessContextManager instance for fancier handling + (e.g. piping between processes). + + For 'wait' calls when you want to write to the subprocess's stdin, + you only need to set 'input' to your content. When 'input' is not + None but 'stdin' is, we'll automatically set 'stdin' to PIPE + before calling Popen. This avoids having the subprocess + accidentally inherit the launching process's stdin. + """ + _LOG.debug('spawn {args} (additional env. var.: {env})'.format( + args=args, env=additional_env)) + if not stdin and input is not None: + stdin = _subprocess.PIPE + if additional_env: + if not kwargs.get('env'): + kwargs['env'] = dict(_os.environ) + kwargs['env'].update(additional_env) + p = _subprocess.Popen( + args, stdin=stdin, stdout=stdout, stderr=stderr, **kwargs) + if wait: + if hasattr(input, 'encode'): + input = input.encode(encoding) + (stdout, stderr) = p.communicate(input=input) + status = p.wait() + _LOG.debug('collect {args} with status {status}'.format( + args=args, status=status)) + if stdout is not None: + stdout = stdout.decode(encoding) + if stderr is not None: + stderr = stderr.decode(encoding) + if status: + raise SubprocessError( + args=args, status=status, stdout=stdout, stderr=stderr) + return (status, stdout, stderr) + if p.stdin and not stdin: + p.stdin.close() + p.stdin = None + if p.stdin: + p.stdin = _codecs.getwriter(encoding=encoding)(stream=p.stdin) + stream_reader = _codecs.getreader(encoding=encoding) + if p.stdout: + p.stdout = stream_reader(stream=p.stdout) + if p.stderr: + p.stderr = stream_reader(stream=p.stderr) + return _SubprocessContextManager(args=args, process=p, expect=expect) + + +def _git(args, **kwargs): + args = ['git', '--git-dir', NMBGIT] + list(args) + return _spawn(args=args, **kwargs) + + +def _get_current_branch(): + """Get the name of the current branch. + + Return 'None' if we're not on a branch. + """ + try: + (status, branch, stderr) = _git( + args=['symbolic-ref', '--short', 'HEAD'], + stdout=_subprocess.PIPE, stderr=_subprocess.PIPE, wait=True) + except SubprocessError as e: + if 'not a symbolic ref' in e: + return None + raise + return branch.strip() + + +def _get_remote(): + "Get the default remote for the current branch." + local_branch = _get_current_branch() + (status, remote, stderr) = _git( + args=['config', 'branch.{0}.remote'.format(local_branch)], + stdout=_subprocess.PIPE, wait=True) + return remote.strip() + + +def get_tags(prefix=None): + "Get a list of tags with a given prefix." + if prefix is None: + prefix = TAG_PREFIX + (status, stdout, stderr) = _spawn( + args=['notmuch', 'search', '--output=tags', '*'], + stdout=_subprocess.PIPE, wait=True) + return [tag for tag in stdout.splitlines() if tag.startswith(prefix)] + + +def archive(treeish='HEAD', args=()): + """ + Dump a tar archive of the current nmbug tag set. + + Using 'git archive'. + + Each tag $tag for message with Message-Id $id is written to + an empty file + + tags/encode($id)/encode($tag) + + The encoding preserves alphanumerics, and the characters + "+-_@=.:," (not the quotes). All other octets are replaced with + '%' followed by a two digit hex number. + """ + _git(args=['archive', treeish] + list(args), wait=True) + + +def clone(repository): + """ + Create a local nmbug repository from a remote source. + + This wraps 'git clone', adding some options to avoid creating a + working tree while preserving remote-tracking branches and + upstreams. + """ + with _tempfile.TemporaryDirectory(prefix='nmbug-clone.') as workdir: + _spawn( + args=[ + 'git', 'clone', '--no-checkout', '--separate-git-dir', NMBGIT, + repository, workdir], + wait=True) + _git(args=['config', '--unset', 'core.worktree'], wait=True) + _git(args=['config', 'core.bare', 'true'], wait=True) + + +def _is_committed(status): + return len(status['added']) + len(status['deleted']) == 0 + + +def commit(treeish='HEAD', message=None): + """ + Commit prefix-matching tags from the notmuch database to Git. + """ + status = get_status() + + if _is_committed(status=status): + _LOG.warning('Nothing to commit') + return + + _git(args=['read-tree', '--empty'], wait=True) + _git(args=['read-tree', treeish], wait=True) + try: + _update_index(status=status) + (_, tree, _) = _git( + args=['write-tree'], + stdout=_subprocess.PIPE, + wait=True) + (_, parent, _) = _git( + args=['rev-parse', treeish], + stdout=_subprocess.PIPE, + wait=True) + (_, commit, _) = _git( + args=['commit-tree', tree.strip(), '-p', parent.strip()], + input=message, + stdout=_subprocess.PIPE, + wait=True) + _git( + args=['update-ref', treeish, commit.strip()], + stdout=_subprocess.PIPE, + wait=True) + except Exception as e: + _git(args=['read-tree', '--empty'], wait=True) + _git(args=['read-tree', treeish], wait=True) + raise + +def _update_index(status): + with _git( + args=['update-index', '--index-info'], + stdin=_subprocess.PIPE) as p: + for id, tags in status['deleted'].items(): + for line in _index_tags_for_message(id=id, status='D', tags=tags): + p.stdin.write(line) + for id, tags in status['added'].items(): + for line in _index_tags_for_message(id=id, status='A', tags=tags): + p.stdin.write(line) + + +def fetch(remote=None): + """ + Fetch changes from the remote repository. + + See 'merge' to bring those changes into notmuch. + """ + args = ['fetch'] + if remote: + args.append(remote) + _git(args=args, wait=True) + + +def checkout(): + """ + Update the notmuch database from Git. + + This is mainly useful to discard your changes in notmuch relative + to Git. + """ + status = get_status() + with _spawn( + args=['notmuch', 'tag', '--batch'], stdin=_subprocess.PIPE) as p: + for id, tags in status['added'].items(): + p.stdin.write(_batch_line(action='-', id=id, tags=tags)) + for id, tags in status['deleted'].items(): + p.stdin.write(_batch_line(action='+', id=id, tags=tags)) + + +def _batch_line(action, id, tags): + """ + 'notmuch tag --batch' line for adding/removing tags. + + Set 'action' to '-' to remove a tag or '+' to add the tags to a + given message id. + """ + tag_string = ' '.join( + '{action}{prefix}{tag}'.format( + action=action, prefix=_ENCODED_TAG_PREFIX, tag=_hex_quote(tag)) + for tag in tags) + line = '{tags} -- id:{id}\n'.format( + tags=tag_string, id=_xapian_quote(string=id)) + return line + + +def _insist_committed(): + "Die if the the notmuch tags don't match the current HEAD." + status = get_status() + if not _is_committed(status=status): + _LOG.error('\n'.join([ + 'Uncommitted changes to {prefix}* tags in notmuch', + '', + "For a summary of changes, run 'nmbug status'", + "To save your changes, run 'nmbug commit' before merging/pull", + "To discard your changes, run 'nmbug checkout'", + ]).format(prefix=TAG_PREFIX)) + _sys.exit(1) + + +def pull(repository=None, refspecs=None): + """ + Pull (merge) remote repository changes to notmuch. + + 'pull' is equivalent to 'fetch' followed by 'merge'. We use the + Git-configured repository for your current branch + (branch..repository, likely 'origin', and + branch..merge, likely 'master'). + """ + _insist_committed() + if refspecs and not repository: + repository = _get_remote() + args = ['pull'] + if repository: + args.append(repository) + if refspecs: + args.extend(refspecs) + with _tempfile.TemporaryDirectory(prefix='nmbug-pull.') as workdir: + for command in [ + ['reset', '--hard'], + args]: + _git( + args=command, + additional_env={'GIT_WORK_TREE': workdir}, + wait=True) + checkout() + + +def merge(reference='@{upstream}'): + """ + Merge changes from 'reference' into HEAD and load the result into notmuch. + + The default reference is '@{upstream}'. + """ + _insist_committed() + with _tempfile.TemporaryDirectory(prefix='nmbug-merge.') as workdir: + for command in [ + ['reset', '--hard'], + ['merge', reference]]: + _git( + args=command, + additional_env={'GIT_WORK_TREE': workdir}, + wait=True) + checkout() + + +def log(args=()): + """ + A simple wrapper for 'git log'. + + After running 'nmbug fetch', you can inspect the changes with + 'nmbug log HEAD..@{upstream}'. + """ + # we don't want output trapping here, because we want the pager. + args = ['log', '--name-status'] + list(args) + with _git(args=args, expect=(0, 1, -13)) as p: + p.wait() + + +def push(repository=None, refspecs=None): + "Push the local nmbug Git state to a remote repository." + if refspecs and not repository: + repository = _get_remote() + args = ['push'] + if repository: + args.append(repository) + if refspecs: + args.extend(refspecs) + _git(args=args, wait=True) + + +def status(): + """ + Show pending updates in notmuch or git repo. + + Prints lines of the form + + ng Message-Id tag + + where n is a single character representing notmuch database status + + * A + + Tag is present in notmuch database, but not committed to nmbug + (equivalently, tag has been deleted in nmbug repo, e.g. by a + pull, but not restored to notmuch database). + + * D + + Tag is present in nmbug repo, but not restored to notmuch + database (equivalently, tag has been deleted in notmuch). + + * U + + Message is unknown (missing from local notmuch database). + + The second character (if present) represents a difference between + local and upstream branches. Typically 'nmbug fetch' needs to be + run to update this. + + * a + + Tag is present in upstream, but not in the local Git branch. + + * d + + Tag is present in local Git branch, but not upstream. + """ + status = get_status() + # 'output' is a nested defaultdict for message status: + # * The outer dict is keyed by message id. + # * The inner dict is keyed by tag name. + # * The inner dict values are status strings (' a', 'Dd', ...). + output = _collections.defaultdict( + lambda : _collections.defaultdict(lambda : ' ')) + for id, tags in status['added'].items(): + for tag in tags: + output[id][tag] = 'A' + for id, tags in status['deleted'].items(): + for tag in tags: + output[id][tag] = 'D' + for id, tags in status['missing'].items(): + for tag in tags: + output[id][tag] = 'U' + if _is_unmerged(): + for id, tag in _diff_refs(filter='A'): + output[id][tag] += 'a' + for id, tag in _diff_refs(filter='D'): + output[id][tag] += 'd' + for id, tag_status in sorted(output.items()): + for tag, status in sorted(tag_status.items()): + print('{status}\t{id}\t{tag}'.format( + status=status, id=id, tag=tag)) + + +def _is_unmerged(ref='@{upstream}'): + try: + (status, fetch_head, stderr) = _git( + args=['rev-parse', ref], + stdout=_subprocess.PIPE, stderr=_subprocess.PIPE, wait=True) + except SubprocessError as e: + if 'No upstream configured' in e.stderr: + return + raise + (status, base, stderr) = _git( + args=['merge-base', 'HEAD', ref], + stdout=_subprocess.PIPE, wait=True) + return base != fetch_head + + +def get_status(): + status = { + 'deleted': {}, + 'missing': {}, + } + index = _index_tags() + maybe_deleted = _diff_index(index=index, filter='D') + for id, tags in maybe_deleted.items(): + (_, stdout, stderr) = _spawn( + args=['notmuch', 'search', '--output=files', 'id:{0}'.format(id)], + stdout=_subprocess.PIPE, + wait=True) + if stdout: + status['deleted'][id] = tags + else: + status['missing'][id] = tags + status['added'] = _diff_index(index=index, filter='A') + _os.remove(index) + return status + + +def _index_tags(): + "Write notmuch tags to the nmbug.index." + path = _os.path.join(NMBGIT, 'nmbug.index') + query = ' '.join('tag:"{tag}"'.format(tag=tag) for tag in get_tags()) + prefix = '+{0}'.format(_ENCODED_TAG_PREFIX) + _git( + args=['read-tree', '--empty'], + additional_env={'GIT_INDEX_FILE': path}, wait=True) + with _spawn( + args=['notmuch', 'dump', '--format=batch-tag', '--', query], + stdout=_subprocess.PIPE) as notmuch: + with _git( + args=['update-index', '--index-info'], + stdin=_subprocess.PIPE, + additional_env={'GIT_INDEX_FILE': path}) as git: + for line in notmuch.stdout: + (tags_string, id) = [_.strip() for _ in line.split(' -- id:')] + tags = [ + _unquote(tag[len(prefix):]) + for tag in tags_string.split() + if tag.startswith(prefix)] + id = _xapian_unquote(string=id) + for line in _index_tags_for_message( + id=id, status='A', tags=tags): + git.stdin.write(line) + return path + + +def _index_tags_for_message(id, status, tags): + """ + Update the Git index to either create or delete an empty file. + + Neither 'id' nor the tags in 'tags' should be encoded/escaped. + """ + mode = '100644' + hash = _EMPTYBLOB + + if status == 'D': + mode = '0' + hash = '0000000000000000000000000000000000000000' + + for tag in tags: + path = 'tags/{id}/{tag}'.format( + id=_hex_quote(string=id), tag=_hex_quote(string=tag)) + yield '{mode} {hash}\t{path}\n'.format(mode=mode, hash=hash, path=path) + + +def _diff_index(index, filter): + """ + Get an {id: {tag, ...}} dict for a given filter. + + For example, use 'A' to find added tags, and 'D' to find deleted tags. + """ + s = _collections.defaultdict(set) + with _git( + args=[ + 'diff-index', '--cached', '--diff-filter', filter, + '--name-only', 'HEAD'], + additional_env={'GIT_INDEX_FILE': index}, + stdout=_subprocess.PIPE) as p: + # Once we drop Python < 3.3, we can use 'yield from' here + for id, tag in _unpack_diff_lines(stream=p.stdout): + s[id].add(tag) + return s + + +def _diff_refs(filter, a='HEAD', b='@{upstream}'): + with _git( + args=['diff', '--diff-filter', filter, '--name-only', a, b], + stdout=_subprocess.PIPE) as p: + # Once we drop Python < 3.3, we can use 'yield from' here + for id, tag in _unpack_diff_lines(stream=p.stdout): + yield id, tag + + +def _unpack_diff_lines(stream): + "Iterate through (id, tag) tuples in a diff stream." + for line in stream: + match = _TAG_FILE_REGEX.match(line.strip()) + if not match: + raise ValueError( + 'Invalid line in diff: {!r}'.format(line.strip())) + id = _unquote(match.group('id')) + tag = _unquote(match.group('tag')) + yield (id, tag) + + +if __name__ == '__main__': + import argparse + + parser = argparse.ArgumentParser( + description=__doc__.strip(), + formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument( + '-v', '--version', action='version', + version='%(prog)s {}'.format(__version__)) + parser.add_argument( + '-l', '--log-level', + choices=['critical', 'error', 'warning', 'info', 'debug'], + help='Log verbosity. Defaults to {!r}.'.format( + _logging.getLevelName(_LOG.level).lower())) + + subparsers = parser.add_subparsers( + title='commands', + description=( + 'For help on a particular command, run: ' + "'%(prog)s ... --help'.")) + for command in [ + 'archive', + 'checkout', + 'clone', + 'commit', + 'fetch', + 'log', + 'merge', + 'pull', + 'push', + 'status', + ]: + func = locals()[command] + doc = _textwrap.dedent(func.__doc__).strip().replace('%', '%%') + subparser = subparsers.add_parser( + command, + help=doc.splitlines()[0], + description=doc, + formatter_class=argparse.RawDescriptionHelpFormatter) + subparser.set_defaults(func=func) + if command == 'archive': + subparser.add_argument( + 'treeish', metavar='TREE-ISH', nargs='?', default='HEAD', + help=( + 'The tree or commit to produce an archive for. Defaults ' + "to 'HEAD'.")) + subparser.add_argument( + 'args', metavar='ARG', nargs='*', + help=( + "Argument passed through to 'git archive'. Set anything " + 'before , see git-archive(1) for details.')) + elif command == 'clone': + subparser.add_argument( + 'repository', + help=( + 'The (possibly remote) repository to clone from. See the ' + 'URLS section of git-clone(1) for more information on ' + 'specifying repositories.')) + elif command == 'commit': + subparser.add_argument( + 'message', metavar='MESSAGE', default='', nargs='?', + help='Text for the commit message.') + elif command == 'fetch': + subparser.add_argument( + 'remote', metavar='REMOTE', nargs='?', + help=( + 'Override the default configured in branch..remote ' + 'to fetch from a particular remote repository (e.g. ' + "'origin').")) + elif command == 'log': + subparser.add_argument( + 'args', metavar='ARG', nargs='*', + help="Additional argument passed through to 'git log'.") + elif command == 'merge': + subparser.add_argument( + 'reference', metavar='REFERENCE', default='@{upstream}', + nargs='?', + help=( + 'Reference, usually other branch heads, to merge into ' + "our branch. Defaults to '@{upstream}'.")) + elif command == 'pull': + subparser.add_argument( + 'repository', metavar='REPOSITORY', default=None, nargs='?', + help=( + 'The "remote" repository that is the source of the pull. ' + 'This parameter can be either a URL (see the section GIT ' + 'URLS in git-pull(1)) or the name of a remote (see the ' + 'section REMOTES in git-pull(1)).')) + subparser.add_argument( + 'refspecs', metavar='REFSPEC', default=None, nargs='*', + help=( + 'Refspec (usually a branch name) to fetch and merge. See ' + 'the entry in the OPTIONS section of ' + 'git-pull(1) for other possibilities.')) + elif command == 'push': + subparser.add_argument( + 'repository', metavar='REPOSITORY', default=None, nargs='?', + help=( + 'The "remote" repository that is the destination of the ' + 'push. This parameter can be either a URL (see the ' + 'section GIT URLS in git-push(1)) or the name of a remote ' + '(see the section REMOTES in git-push(1)).')) + subparser.add_argument( + 'refspecs', metavar='REFSPEC', default=None, nargs='*', + help=( + 'Refspec (usually a branch name) to push. See ' + 'the entry in the OPTIONS section of ' + 'git-push(1) for other possibilities.')) + + args = parser.parse_args() + + if args.log_level: + level = getattr(_logging, args.log_level.upper()) + _LOG.setLevel(level) + + if not getattr(args, 'func', None): + parser.print_usage() + _sys.exit(1) + + (arg_names, varargs, varkw) = _inspect.getargs(args.func.__code__) + kwargs = {key: getattr(args, key) for key in arg_names if key in args} + try: + args.func(**kwargs) + except SubprocessError as e: + if _LOG.level == _logging.DEBUG: + raise # don't mask the traceback + _LOG.error(str(e)) + _sys.exit(1) diff --git a/devel/nmbug/nmbug-status b/devel/nmbug/nmbug-status index 03621bd5..f0809f19 100755 --- a/devel/nmbug/nmbug-status +++ b/devel/nmbug/nmbug-status @@ -1,10 +1,30 @@ #!/usr/bin/python # # Copyright (c) 2011-2012 David Bremner -# License: Same as notmuch +# # dependencies # - python 2.6 for json # - argparse; either python 2.7, or install separately +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see http://www.gnu.org/licenses/ . + +"""Generate HTML for one or more notmuch searches. + +Messages matching each search are grouped by thread. Each message +that contains both a subject and message-id will have the displayed +subject link to the Gmane view of the message. +""" from __future__ import print_function from __future__ import unicode_literals @@ -242,7 +262,7 @@ class HtmlPage (Page): def _slug(self, string): return self._slug_regexp.sub('-', string) -parser = argparse.ArgumentParser() +parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--text', help='output plain text format', action='store_true') parser.add_argument('--config', help='load config from given file', @@ -256,9 +276,7 @@ args = parser.parse_args() config = read_config(path=args.config) -_PAGES['text'] = Page() -_PAGES['html'] = HtmlPage( - header=''' +header_template = config['meta'].get('header', ''' @@ -295,22 +313,43 @@ _PAGES['html'] = HtmlPage( tbody:nth-child(4n+3) tr td {{ background-color: #bce; }} + hr {{ + border: 0; + height: 1px; + color: #ccc; + background-color: #ccc; + }}

{title}

-

-Generated: {date}
{blurb}

Views

-'''.format(date=datetime.datetime.utcnow().date(), - title=config['meta']['title'], - blurb=config['meta']['blurb'], - encoding=_ENCODING, - inter_message_padding='0.25em', - border_radius='0.5em'), - footer='\n\n', +''') + +footer_template = config['meta'].get('footer', ''' +
+

Generated: {datetime} + + +''') + +now = datetime.datetime.utcnow() +context = { + 'date': now, + 'datetime': now.strftime('%Y-%m-%d %H:%M:%SZ'), + 'title': config['meta']['title'], + 'blurb': config['meta']['blurb'], + 'encoding': _ENCODING, + 'inter_message_padding': '0.25em', + 'border_radius': '0.5em', + } + +_PAGES['text'] = Page() +_PAGES['html'] = HtmlPage( + header=header_template.format(**context), + footer=footer_template.format(**context), ) if args.list_views: diff --git a/doc/.gitignore b/doc/.gitignore index a60fb31e..f0cbb9c2 100644 --- a/doc/.gitignore +++ b/doc/.gitignore @@ -1,2 +1,3 @@ +*.pyc docdeps.mk _build diff --git a/doc/Makefile.local b/doc/Makefile.local index bbd46100..e7d0bac8 100644 --- a/doc/Makefile.local +++ b/doc/Makefile.local @@ -12,10 +12,12 @@ mkdocdeps := python $(srcdir)/$(dir)/mkdocdeps.py # Internal variables. ALLSPHINXOPTS := -d $(DOCBUILDDIR)/doctrees $(SPHINXOPTS) $(srcdir)/$(dir) +APIMAN := $(DOCBUILDDIR)/man/man3/notmuch.3 +DOXYFILE := $(srcdir)/$(dir)/doxygen.cfg .PHONY: sphinx-html sphinx-texinfo sphinx-info -.PHONY: install-man build-man +.PHONY: install-man build-man apidocs install-apidocs %.gz: % rm -f $@ && gzip --stdout $^ > $@ @@ -56,6 +58,25 @@ else endif touch ${MAN_ROFF_FILES} $@ +install-man: install-apidocs + +ifeq ($(HAVE_DOXYGEN),1) +MAN_GZIP_FILES += ${APIMAN}.gz +apidocs: $(APIMAN) +install-apidocs: apidocs + mkdir -p "$(DESTDIR)$(mandir)/man3" + install -m0644 $(DOCBUILDDIR)/man/man3/*.3.gz $(DESTDIR)/$(mandir)/man3 + +$(APIMAN): $(dir)/config.dox $(srcdir)/$(dir)/doxygen.cfg $(srcdir)/lib/notmuch.h + mkdir -p $(DOCBUILDDIR)/man/man3 + doxygen $(DOXYFILE) + rm -f $(DOCBUILDDIR)/man/man3/_*.3 + perl -pi -e 's/^[.]RI "\\fI/.RI "\\fP/' $(APIMAN) +else +apidocs: +install-apidocs: +endif + # Do not try to build or install man pages if a man page converter is # not available. ifeq ($(HAVE_SPHINX)$(HAVE_RST2MAN),00) @@ -74,8 +95,12 @@ install-man: ${MAN_GZIP_FILES} cd $(DESTDIR)/$(mandir)/man1 && ln -sf notmuch.1.gz notmuch-setup.1.gz endif +$(dir)/config.dox: version.stamp + echo "PROJECT_NAME = \"Notmuch $(VERSION)\"" > $@ + echo "INPUT=${srcdir}/lib/notmuch.h" >> $@ + $(dir)/docdeps.mk: $(dir)/conf.py $(dir)/mkdocdeps.py $(mkdocdeps) $(srcdir)/doc $(DOCBUILDDIR) $@ CLEAN := $(CLEAN) $(DOCBUILDDIR) $(dir)/docdeps.mk $(DOCBUILDDIR)/.roff.stamp -CLEAN := $(CLEAN) $(MAN_GZIP_FILES) $(MAN_ROFF_FILES) $(dir)/conf.pyc +CLEAN := $(CLEAN) $(MAN_GZIP_FILES) $(MAN_ROFF_FILES) $(dir)/conf.pyc $(dir)/config.dox diff --git a/doc/conf.py b/doc/conf.py index 70ba1b8a..495e5381 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -27,7 +27,7 @@ release = version # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. -exclude_patterns = ['_build', 'notmuch-emacs.rst'] +exclude_patterns = ['_build'] # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' diff --git a/doc/doxygen.cfg b/doc/doxygen.cfg index bfbfcab3..42b63394 100644 --- a/doc/doxygen.cfg +++ b/doc/doxygen.cfg @@ -4,11 +4,11 @@ # Project related configuration options #--------------------------------------------------------------------------- DOXYFILE_ENCODING = UTF-8 -PROJECT_NAME = "Notmuch 0.18" +@INCLUDE = "doc/config.dox" PROJECT_NUMBER = PROJECT_BRIEF = PROJECT_LOGO = -OUTPUT_DIRECTORY = +OUTPUT_DIRECTORY = doc/_build CREATE_SUBDIRS = NO OUTPUT_LANGUAGE = English BRIEF_MEMBER_DESC = YES @@ -96,7 +96,6 @@ WARN_LOGFILE = #--------------------------------------------------------------------------- # configuration options related to the input files #--------------------------------------------------------------------------- -INPUT = lib/notmuch.h INPUT_ENCODING = UTF-8 FILE_PATTERNS = RECURSIVE = NO @@ -228,8 +227,6 @@ MAN_LINKS = NO #--------------------------------------------------------------------------- GENERATE_XML = NO XML_OUTPUT = xml -XML_SCHEMA = -XML_DTD = XML_PROGRAMLISTING = YES #--------------------------------------------------------------------------- # configuration options related to the DOCBOOK output diff --git a/doc/index.rst b/doc/index.rst index b33aa9fb..ba6d5b46 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -12,6 +12,7 @@ Contents: man1/notmuch-config man1/notmuch-count man1/notmuch-dump + notmuch-emacs man5/notmuch-hooks man1/notmuch-insert man1/notmuch-new diff --git a/doc/man1/notmuch-insert.rst b/doc/man1/notmuch-insert.rst index 2be1a7b8..e396f6cf 100644 --- a/doc/man1/notmuch-insert.rst +++ b/doc/man1/notmuch-insert.rst @@ -38,16 +38,21 @@ Supported options for **insert** include does not exist. Otherwise the folder must already exist for mail delivery to succeed. + ``--keep`` + Keep the message file if indexing fails, and keep the message + indexed if applying tags or maildir flag synchronization + fails. Ignore these errors and return exit status 0 to + indicate succesful mail delivery. + EXIT STATUS =========== -This command returns exit status 0 if the message was successfully added -to the mail directory, even if the message could not be indexed and -added to the notmuch database. In the latter case, a warning will be -printed to standard error but the message file will be left on disk. - -If the message could not be written to disk then a non-zero exit status -is returned. +This command returns exit status 0 on succesful mail delivery, +non-zero otherwise. The default is to indicate failed mail delivery on +any errors, including message file delivery to the filesystem, message +indexing to Notmuch database, changing tags, and synchronizing tags to +maildir flags. The ``--keep`` option may be used to settle for +successful message file delivery. SEE ALSO ======== diff --git a/doc/notmuch-emacs.rst b/doc/notmuch-emacs.rst index 09579bf6..6f2f61e9 100644 --- a/doc/notmuch-emacs.rst +++ b/doc/notmuch-emacs.rst @@ -6,17 +6,17 @@ About this Manual ================= This manual covers only the Emacs interface to Notmuch. For information -on the command line interface, see See section “Description” in Notmuch -Manual Pager. To save typing, we will sometimes use *notmuch* in this -manual to refer to the Emacs interface to Notmuch. If the distinction -should every be important, we’ll refer to the Emacs interface as +on the command line interface, see section “Description” in the Notmuch +Manual Pages. To save typing, we will sometimes use *notmuch* in this +manual to refer to the Emacs interface to Notmuch. When this distinction +is important, we’ll refer to the Emacs interface as *notmuch-emacs*. Notmuch-emacs is highly customizable via the the Emacs customization framework (or just by setting the appropriate variables). We try to point out relevant variables in this manual, but in order to avoid -duplication of information, but you can usually find the most detailed -description in the variables docstring. +duplication of information, you can usually find the most detailed +description in the variables' docstring. notmuch-hello ============= @@ -89,15 +89,19 @@ notmuch-hello key bindings Saved Searches -------------- -Notmuch replaces the static assignment of messages with the more dynamic -notion of searching. Notmuch-hello presents the user with a customizable -set of saved searches. The initial defaults are ``tag:inbox`` and -``tag:unread``, but you can customize the following variables +Since notmuch is entirely search-based, it's often useful to organize +mail around common searches. To facilitate this, the first section of +notmuch-hello presents a customizable set of saved searches. Saved +searches can also be accessed from anywhere in notmuch by pressing +``j`` to access :ref:`notmuch-jump`. + +The saved searches default to various common searches such as +``tag:inbox`` to access the inbox and ``tag:unread`` to access all +unread mail, but there are several options for customization: :index:`notmuch-saved-searches` - A list of cons pairs, the first being the name to display, the - second being a query string for Notmuch. See section “Description” - in Notmuch Query Syntax. + The list of saved searches, including names, queries, and + additional per-query options. :index:`notmuch-saved-searches-sort-function` This variable controls how saved searches should be sorted. A value @@ -179,6 +183,26 @@ notmuch-show notmuch-tree ============ +Global key bindings +=================== + +Several features are accessible from anywhere in notmuch through the +following key bindings: + +``j`` + Jump to saved searches using :ref:`notmuch-jump`. + +notmuch-jump +------------ + +Saved searches configured through :ref:`notmuch-saved-searches` can +include a "shortcut key" that's accessible through notmuch-jump. +Pressing ``j`` anywhere in notmuch followed by the configured shortcut +key of a saved search will immediately jump to that saved search. For +example, in the default configuration ``j i`` jumps immediately to the +inbox search. When you press ``j``, notmuch-jump shows the saved +searches and their shortcut keys in the mini-buffer. + Configuration ============= diff --git a/emacs/Makefile.local b/emacs/Makefile.local index c0d6b190..1109cfa6 100644 --- a/emacs/Makefile.local +++ b/emacs/Makefile.local @@ -18,7 +18,8 @@ emacs_sources := \ $(dir)/notmuch-tag.el \ $(dir)/coolj.el \ $(dir)/notmuch-print.el \ - $(dir)/notmuch-version.el + $(dir)/notmuch-version.el \ + $(dir)/notmuch-jump.el \ $(dir)/notmuch-version.el: $(dir)/Makefile.local version.stamp $(dir)/notmuch-version.el: $(srcdir)/$(dir)/notmuch-version.el.tmpl diff --git a/emacs/notmuch-hello.el b/emacs/notmuch-hello.el index 3de52386..65d06276 100644 --- a/emacs/notmuch-hello.el +++ b/emacs/notmuch-hello.el @@ -85,6 +85,7 @@ searches so they still work in customize." (group :format "%v" :inline t (const :format " Query: " :query) (string :format "%v"))) (checklist :inline t :format "%v" + (group :format "%v" :inline t (const :format "Shortcut key: " :key) (key-sequence :format "%v")) (group :format "%v" :inline t (const :format "Count-Query: " :count-query) (string :format "%v")) (group :format "%v" :inline t (const :format "" :sort-order) (choice :tag " Sort Order" @@ -92,8 +93,13 @@ searches so they still work in customize." (const :tag "Oldest-first" oldest-first) (const :tag "Newest-first" newest-first)))))) -(defcustom notmuch-saved-searches '((:name "inbox" :query "tag:inbox") - (:name "unread" :query "tag:unread")) +(defcustom notmuch-saved-searches + `((:name "inbox" :query "tag:inbox" :key ,(kbd "i")) + (:name "unread" :query "tag:unread" :key ,(kbd "u")) + (:name "flagged" :query "tag:flagged" :key ,(kbd "f")) + (:name "sent" :query "tag:sent" :key ,(kbd "t")) + (:name "drafts" :query "tag:draft" :key ,(kbd "d")) + (:name "all mail" :query "*" :key ,(kbd "a"))) "A list of saved searches to display. The saved search can be given in 3 forms. The preferred way is as @@ -101,6 +107,7 @@ a plist. Supported properties are :name Name of the search (required). :query Search to run (required). + :key Optional shortcut key for `notmuch-jump-search'. :count-query Optional extra query to generate the count shown. If not present then the :query property is used. diff --git a/emacs/notmuch-jump.el b/emacs/notmuch-jump.el new file mode 100644 index 00000000..05ec57ec --- /dev/null +++ b/emacs/notmuch-jump.el @@ -0,0 +1,172 @@ +;; notmuch-jump.el --- User-friendly shortcut keys +;; +;; Copyright © Austin Clements +;; +;; This file is part of Notmuch. +;; +;; Notmuch is free software: you can redistribute it and/or modify it +;; under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. +;; +;; Notmuch is distributed in the hope that it will be useful, but +;; WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;; General Public License for more details. +;; +;; You should have received a copy of the GNU General Public License +;; along with Notmuch. If not, see . +;; +;; Authors: Austin Clements +;; David Edmondson + +(eval-when-compile (require 'cl)) + +(require 'notmuch-lib) +(require 'notmuch-hello) + +(eval-and-compile + (unless (fboundp 'window-body-width) + ;; Compatibility for Emacs pre-24 + (defalias 'window-body-width 'window-width))) + +;;;###autoload +(defun notmuch-jump-search () + "Jump to a saved search by shortcut key. + +This prompts for and performs a saved search using the shortcut +keys configured in the :key property of `notmuch-saved-searches'. +Typically these shortcuts are a single key long, so this is a +fast way to jump to a saved search from anywhere in Notmuch." + (interactive) + + ;; Build the action map + (let (action-map) + (dolist (saved-search notmuch-saved-searches) + (let* ((saved-search (notmuch-hello-saved-search-to-plist saved-search)) + (key (plist-get saved-search :key))) + (when key + (let ((name (plist-get saved-search :name)) + (query (plist-get saved-search :query)) + (oldest-first + (case (plist-get saved-search :sort-order) + (newest-first nil) + (oldest-first t) + (otherwise (default-value 'notmuch-search-oldest-first))))) + (push (list key name + `(lambda () (notmuch-search ',query ',oldest-first))) + action-map))))) + (setq action-map (nreverse action-map)) + + (if action-map + (notmuch-jump action-map "Search: ") + (error "To use notmuch-jump, please customize shortcut keys in notmuch-saved-searches.")))) + +(defvar notmuch-jump--action nil) + +(defun notmuch-jump (action-map prompt) + "Interactively prompt for one of the keys in ACTION-MAP. + +Displays a summary of all bindings in ACTION-MAP in the +minibuffer, reads a key from the minibuffer, and performs the +corresponding action. The prompt can be canceled with C-g or +RET. PROMPT must be a string to use for the prompt. PROMPT +should include a space at the end. + +ACTION-MAP must be a list of triples of the form + (KEY LABEL ACTION) +where KEY is a key binding, LABEL is a string label to display in +the buffer, and ACTION is a nullary function to call. LABEL may +be null, in which case the action will still be bound, but will +not appear in the pop-up buffer. +" + + (let* ((items (notmuch-jump--format-actions action-map)) + ;; Format the table of bindings and the full prompt + (table + (with-temp-buffer + (notmuch-jump--insert-items (window-body-width) items) + (buffer-string))) + (full-prompt + (concat table "\n\n" + (propertize prompt 'face 'minibuffer-prompt))) + ;; By default, the minibuffer applies the minibuffer face to + ;; the entire prompt. However, we want to clearly + ;; distinguish bindings (which we put in the prompt face + ;; ourselves) from their labels, so disable the minibuffer's + ;; own re-face-ing. + (minibuffer-prompt-properties + (notmuch-plist-delete + (copy-sequence minibuffer-prompt-properties) + 'face)) + ;; Build the keymap with our bindings + (minibuffer-map (notmuch-jump--make-keymap action-map)) + ;; The bindings save the the action in notmuch-jump--action + (notmuch-jump--action nil)) + ;; Read the action + (read-from-minibuffer full-prompt nil minibuffer-map) + + ;; If we got an action, do it + (when notmuch-jump--action + (funcall notmuch-jump--action)))) + +(defun notmuch-jump--format-actions (action-map) + "Format the actions in ACTION-MAP. + +Returns a list of strings, one for each item with a label in +ACTION-MAP. These strings can be inserted into a tabular +buffer." + + ;; Compute the maximum key description width + (let ((key-width 1)) + (dolist (entry action-map) + (setq key-width + (max key-width + (string-width (format-kbd-macro (first entry)))))) + ;; Format each action + (mapcar (lambda (entry) + (let ((key (format-kbd-macro (first entry))) + (desc (second entry))) + (concat + (propertize key 'face 'minibuffer-prompt) + (make-string (- key-width (length key)) ? ) + " " desc))) + action-map))) + +(defun notmuch-jump--insert-items (width items) + "Make a table of ITEMS up to WIDTH wide in the current buffer." + (let* ((nitems (length items)) + (col-width (+ 3 (apply #'max (mapcar #'string-width items)))) + (ncols (if (> (* col-width nitems) width) + (max 1 (/ width col-width)) + ;; Items fit on one line. Space them out + (setq col-width (/ width nitems)) + (length items)))) + (while items + (dotimes (col ncols) + (when items + (let ((item (pop items))) + (insert item) + (when (and items (< col (- ncols 1))) + (insert (make-string (- col-width (string-width item)) ? )))))) + (when items + (insert "\n"))))) + +(defvar notmuch-jump-minibuffer-map + (let ((map (make-sparse-keymap))) + (set-keymap-parent map minibuffer-local-map) + ;; Make this like a special-mode keymap, with no self-insert-command + (suppress-keymap map) + map) + "Base keymap for notmuch-jump's minibuffer keymap.") + +(defun notmuch-jump--make-keymap (action-map) + "Translate ACTION-MAP into a minibuffer keymap." + (let ((map (make-sparse-keymap))) + (set-keymap-parent map notmuch-jump-minibuffer-map) + (dolist (action action-map) + (define-key map (first action) + `(lambda () (interactive) + (setq notmuch-jump--action ',(third action)) + (exit-minibuffer)))) + map)) diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el index 2941da3e..1e166c6a 100644 --- a/emacs/notmuch-lib.el +++ b/emacs/notmuch-lib.el @@ -25,8 +25,8 @@ (require 'mm-decode) (require 'cl) -(defvar notmuch-command "notmuch" - "Command to run the notmuch binary.") +(autoload 'notmuch-jump-search "notmuch-jump" + "Jump to a saved search by shortcut key." t) (defgroup notmuch nil "Notmuch mail reader for Emacs." @@ -66,6 +66,16 @@ "Graphical attributes for displaying text" :group 'notmuch) +(defcustom notmuch-command "notmuch" + "Name of the notmuch binary. + +This can be a relative or absolute path to the notmuch binary. +If this is a relative path, it will be searched for in all of the +directories given in `exec-path' (which is, by default, based on +$PATH)." + :type 'string + :group 'notmuch-external) + (defcustom notmuch-search-oldest-first t "Show the oldest mail first when searching. @@ -77,7 +87,11 @@ search." :group 'notmuch-search) (defcustom notmuch-poll-script nil - "An external script to incorporate new mail into the notmuch database. + "[Deprecated] Command to run to incorporate new mail into the notmuch database. + +This option has been deprecated in favor of \"notmuch new\" +hooks (see man notmuch-hooks). To change the path to the notmuch +binary, customize `notmuch-command'. This variable controls the action invoked by `notmuch-poll-and-refresh-this-buffer' (bound by default to 'G') @@ -93,10 +107,7 @@ the user's needs: 1. Invoke a program to transfer mail to the local mail store 2. Invoke \"notmuch new\" to incorporate the new mail -3. Invoke one or more \"notmuch tag\" commands to classify the mail - -Note that the recommended way of achieving the same is using -\"notmuch new\" hooks." +3. Invoke one or more \"notmuch tag\" commands to classify the mail" :type '(choice (const :tag "notmuch new" nil) (const :tag "Disabled" "") (string :tag "Custom script")) @@ -130,6 +141,7 @@ For example, if you wanted to remove an \"inbox\" tag and add an (define-key map "m" 'notmuch-mua-new-mail) (define-key map "=" 'notmuch-refresh-this-buffer) (define-key map "G" 'notmuch-poll-and-refresh-this-buffer) + (define-key map "j" 'notmuch-jump-search) map) "Keymap shared by all notmuch modes.") @@ -464,6 +476,15 @@ This replaces spaces, percents, and double quotes in STR with (setq list (cdr list))) (nreverse out))) +(defun notmuch-plist-delete (plist property) + (let* ((xplist (cons nil plist)) + (pred xplist)) + (while (cdr pred) + (when (eq (cadr pred) property) + (setcdr pred (cdddr pred))) + (setq pred (cddr pred))) + (cdr xplist))) + (defun notmuch-split-content-type (content-type) "Split content/type into 'content' and 'type'" (split-string content-type "/")) @@ -518,9 +539,10 @@ the given type." (apply 'call-process (append (list notmuch-command nil (list t nil) nil) args)) (buffer-string)))))) -(defun notmuch-get-bodypart-content (msg part nth process-crypto) +(defun notmuch-get-bodypart-content (msg part process-crypto) (or (plist-get part :content) - (notmuch-get-bodypart-internal (notmuch-id-to-query (plist-get msg :id)) nth process-crypto))) + (notmuch-get-bodypart-internal (notmuch-id-to-query (plist-get msg :id)) + (plist-get part :id) process-crypto))) ;; Workaround: The call to `mm-display-part' below triggers a bug in ;; Emacs 24 if it attempts to use the shr renderer to display an HTML @@ -536,7 +558,7 @@ the given type." (ad-disable-advice 'mm-shr 'before 'load-gnus-arts) (ad-activate 'mm-shr))) -(defun notmuch-mm-display-part-inline (msg part nth content-type process-crypto) +(defun notmuch-mm-display-part-inline (msg part content-type process-crypto) "Use the mm-decode/mm-view functions to display a part in the current buffer, if possible." (let ((display-buffer (current-buffer))) @@ -552,7 +574,7 @@ current buffer, if possible." ;; test whether we are able to inline it (which includes both ;; capability and suitability tests). (when (mm-inlined-p handle) - (insert (notmuch-get-bodypart-content msg part nth process-crypto)) + (insert (notmuch-get-bodypart-content msg part process-crypto)) (when (mm-inlinable-p handle) (set-buffer display-buffer) (mm-display-part handle) diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el index 95e4a4d3..2c588860 100644 --- a/emacs/notmuch-mua.el +++ b/emacs/notmuch-mua.el @@ -346,7 +346,8 @@ the From: address first." (message-forward-make-body cur) ;; `message-forward-make-body' shows the User-agent header. Hide ;; it again. - (message-hide-headers))) + (message-hide-headers) + (set-buffer-modified-p nil))) (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all) "Compose a reply to the message identified by QUERY-STRING. diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el index df10d4ba..a9974826 100644 --- a/emacs/notmuch-show.el +++ b/emacs/notmuch-show.el @@ -46,6 +46,7 @@ (declare-function notmuch-save-attachments "notmuch" (mm-handle &optional queryp)) (declare-function notmuch-tree "notmuch-tree" (&optional query query-context target buffer-name open-target)) +(declare-function notmuch-tree-get-message-properties "notmuch-tree" nil) (defcustom notmuch-message-headers '("Subject" "To" "Cc" "Date") "Headers that should be shown in a message, in this order. @@ -180,10 +181,21 @@ each attachment handler is logged in buffers with names beginning ) "List of Mailing List Archives to use when stashing links. -These URIs are concatenated with the current message's -Message-Id in `notmuch-show-stash-mlarchive-link'." +This list is used for generating a Mailing List Archive reference +URI with the current message's Message-Id in +`notmuch-show-stash-mlarchive-link'. + +If the cdr of the alist element is not a function, the cdr is +expected to contain a URI that is concatenated with the current +message's Message-Id to create a ML archive reference URI. + +If the cdr is a function, the function is called with the +Message-Id as the argument, and the function is expected to +return the ML archive reference URI." :type '(alist :key-type (string :tag "Name") - :value-type (string :tag "URL")) + :value-type (choice + (string :tag "URL") + (function :tag "Function returning the URL"))) :group 'notmuch-show) (defcustom notmuch-show-stash-mlarchive-link-default "Gmane" @@ -211,6 +223,10 @@ For example, if you wanted to remove an \"unread\" tag and add a :type '(repeat string) :group 'notmuch-show) +(defcustom notmuch-show-mark-read-function #'notmuch-show-seen-current-message + "Function to control which messages are marked read." + :type 'function + :group 'notmuch-show) (defmacro with-current-notmuch-show-message (&rest body) "Evaluate body with current buffer set to the text of current message" @@ -695,7 +711,7 @@ message at DEPTH in the current thread." (let ((start (if button (button-start button) (point)))) - (insert (notmuch-get-bodypart-content msg part nth notmuch-show-process-crypto)) + (insert (notmuch-get-bodypart-content msg part notmuch-show-process-crypto)) (save-excursion (save-restriction (narrow-to-region start (point-max)) @@ -704,7 +720,7 @@ message at DEPTH in the current thread." (defun notmuch-show-insert-part-text/calendar (msg part content-type nth depth button) (insert (with-temp-buffer - (insert (notmuch-get-bodypart-content msg part nth notmuch-show-process-crypto)) + (insert (notmuch-get-bodypart-content msg part notmuch-show-process-crypto)) ;; notmuch-get-bodypart-content provides "raw", non-converted ;; data. Replace CRLF with LF before icalendar can use it. (goto-char (point-min)) @@ -756,7 +772,7 @@ message at DEPTH in the current thread." (defun notmuch-show-insert-part-*/* (msg part content-type nth depth button) ;; This handler _must_ succeed - it is the handler of last resort. - (notmuch-mm-display-part-inline msg part nth content-type notmuch-show-process-crypto) + (notmuch-mm-display-part-inline msg part content-type notmuch-show-process-crypto) t) ;; Functions for determining how to handle MIME parts. @@ -1145,6 +1161,8 @@ function is used." (let ((inhibit-read-only t)) (notmuch-show-mode) + (add-hook 'post-command-hook #'notmuch-show-command-hook nil t) + ;; Don't track undo information for this buffer (set 'buffer-undo-list t) @@ -1186,6 +1204,15 @@ This includes: - the current message." (list (notmuch-show-get-message-id) (notmuch-show-get-message-ids-for-open-messages))) +(defun notmuch-show-get-query () + "Return the current query in this show buffer" + (if notmuch-show-query-context + (concat notmuch-show-thread-id + " and (" + notmuch-show-query-context + ")") + notmuch-show-thread-id)) + (defun notmuch-show-apply-state (state) "Apply STATE to the current buffer. @@ -1264,46 +1291,46 @@ reset based on the original query." (fset 'notmuch-show-part-map notmuch-show-part-map) (defvar notmuch-show-mode-map - (let ((map (make-sparse-keymap))) - (set-keymap-parent map notmuch-common-keymap) - (define-key map "Z" 'notmuch-tree-from-show-current-query) - (define-key map (kbd "") 'widget-backward) - (define-key map (kbd "M-TAB") 'notmuch-show-previous-button) - (define-key map (kbd "") 'notmuch-show-previous-button) - (define-key map (kbd "TAB") 'notmuch-show-next-button) - (define-key map "f" 'notmuch-show-forward-message) - (define-key map "r" 'notmuch-show-reply-sender) - (define-key map "R" 'notmuch-show-reply) - (define-key map "|" 'notmuch-show-pipe-message) - (define-key map "w" 'notmuch-show-save-attachments) - (define-key map "V" 'notmuch-show-view-raw-message) - (define-key map "c" 'notmuch-show-stash-map) - (define-key map "h" 'notmuch-show-toggle-visibility-headers) - (define-key map "*" 'notmuch-show-tag-all) - (define-key map "-" 'notmuch-show-remove-tag) - (define-key map "+" 'notmuch-show-add-tag) - (define-key map "X" 'notmuch-show-archive-thread-then-exit) - (define-key map "x" 'notmuch-show-archive-message-then-next-or-exit) - (define-key map "A" 'notmuch-show-archive-thread-then-next) - (define-key map "a" 'notmuch-show-archive-message-then-next-or-next-thread) - (define-key map "N" 'notmuch-show-next-message) - (define-key map "P" 'notmuch-show-previous-message) - (define-key map "n" 'notmuch-show-next-open-message) - (define-key map "p" 'notmuch-show-previous-open-message) - (define-key map (kbd "M-n") 'notmuch-show-next-thread-show) - (define-key map (kbd "M-p") 'notmuch-show-previous-thread-show) - (define-key map (kbd "DEL") 'notmuch-show-rewind) - (define-key map " " 'notmuch-show-advance-and-archive) - (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all) - (define-key map (kbd "RET") 'notmuch-show-toggle-message) - (define-key map "#" 'notmuch-show-print-message) - (define-key map "!" 'notmuch-show-toggle-elide-non-matching) - (define-key map "$" 'notmuch-show-toggle-process-crypto) - (define-key map "<" 'notmuch-show-toggle-thread-indentation) - (define-key map "t" 'toggle-truncate-lines) - (define-key map "." 'notmuch-show-part-map) - map) - "Keymap for \"notmuch show\" buffers.") + (let ((map (make-sparse-keymap))) + (set-keymap-parent map notmuch-common-keymap) + (define-key map "Z" 'notmuch-tree-from-show-current-query) + (define-key map (kbd "") 'widget-backward) + (define-key map (kbd "M-TAB") 'notmuch-show-previous-button) + (define-key map (kbd "") 'notmuch-show-previous-button) + (define-key map (kbd "TAB") 'notmuch-show-next-button) + (define-key map "f" 'notmuch-show-forward-message) + (define-key map "r" 'notmuch-show-reply-sender) + (define-key map "R" 'notmuch-show-reply) + (define-key map "|" 'notmuch-show-pipe-message) + (define-key map "w" 'notmuch-show-save-attachments) + (define-key map "V" 'notmuch-show-view-raw-message) + (define-key map "c" 'notmuch-show-stash-map) + (define-key map "h" 'notmuch-show-toggle-visibility-headers) + (define-key map "*" 'notmuch-show-tag-all) + (define-key map "-" 'notmuch-show-remove-tag) + (define-key map "+" 'notmuch-show-add-tag) + (define-key map "X" 'notmuch-show-archive-thread-then-exit) + (define-key map "x" 'notmuch-show-archive-message-then-next-or-exit) + (define-key map "A" 'notmuch-show-archive-thread-then-next) + (define-key map "a" 'notmuch-show-archive-message-then-next-or-next-thread) + (define-key map "N" 'notmuch-show-next-message) + (define-key map "P" 'notmuch-show-previous-message) + (define-key map "n" 'notmuch-show-next-open-message) + (define-key map "p" 'notmuch-show-previous-open-message) + (define-key map (kbd "M-n") 'notmuch-show-next-thread-show) + (define-key map (kbd "M-p") 'notmuch-show-previous-thread-show) + (define-key map (kbd "DEL") 'notmuch-show-rewind) + (define-key map " " 'notmuch-show-advance-and-archive) + (define-key map (kbd "M-RET") 'notmuch-show-open-or-close-all) + (define-key map (kbd "RET") 'notmuch-show-toggle-message) + (define-key map "#" 'notmuch-show-print-message) + (define-key map "!" 'notmuch-show-toggle-elide-non-matching) + (define-key map "$" 'notmuch-show-toggle-process-crypto) + (define-key map "<" 'notmuch-show-toggle-thread-indentation) + (define-key map "t" 'toggle-truncate-lines) + (define-key map "." 'notmuch-show-part-map) + map) + "Keymap for \"notmuch show\" buffers.") (fset 'notmuch-show-mode-map notmuch-show-mode-map) (defun notmuch-show-mode () @@ -1448,8 +1475,18 @@ an error if there is no part containing point." (notmuch-show-set-message-properties props))) (defun notmuch-show-get-prop (prop &optional props) + "Get property PROP from current message in show or tree mode. + +It gets property PROP from PROPS or, if PROPS is nil, the current +message in either tree or show. This means that several utility +functions in notmuch-show can be used directly by notmuch-tree as +they just need the correct message properties." (let ((props (or props - (notmuch-show-get-message-properties)))) + (cond ((eq major-mode 'notmuch-show-mode) + (notmuch-show-get-message-properties)) + ((eq major-mode 'notmuch-tree-mode) + (notmuch-tree-get-message-properties)) + (t nil))))) (plist-get props prop))) (defun notmuch-show-get-message-id (&optional bare) @@ -1533,6 +1570,23 @@ marked as unread, i.e. the tag changes in (apply 'notmuch-show-tag-message (notmuch-tag-change-list notmuch-show-mark-read-tags unread)))) +(defun notmuch-show-seen-current-message (start end) + "Mark the current message read if it is open. + +We only mark it read once: if it is changed back then that is a +user decision and we should not override it." + (when (and (notmuch-show-message-visible-p) + (not (notmuch-show-get-prop :seen))) + (notmuch-show-mark-read) + (notmuch-show-set-prop :seen t))) + +(defun notmuch-show-command-hook () + (when (eq major-mode 'notmuch-show-mode) + ;; We need to redisplay to get window-start and window-end correct. + (redisplay) + (save-excursion + (funcall notmuch-show-mark-read-function (window-start) (window-end))))) + ;; Functions for getting attributes of several messages in the current ;; thread. @@ -1668,9 +1722,7 @@ If a prefix argument is given and this is the last message in the thread, navigate to the next thread in the parent search buffer." (interactive "P") (if (notmuch-show-goto-message-next) - (progn - (notmuch-show-mark-read) - (notmuch-show-message-adjust)) + (notmuch-show-message-adjust) (if pop-at-end (notmuch-show-next-thread) (goto-char (point-max))))) @@ -1681,7 +1733,6 @@ thread, navigate to the next thread in the parent search buffer." (if (= (point) (notmuch-show-message-top)) (notmuch-show-goto-message-previous) (notmuch-show-move-to-message-top)) - (notmuch-show-mark-read) (notmuch-show-message-adjust)) (defun notmuch-show-next-open-message (&optional pop-at-end) @@ -1696,9 +1747,7 @@ to show, nil otherwise." (while (and (setq r (notmuch-show-goto-message-next)) (not (notmuch-show-message-visible-p)))) (if r - (progn - (notmuch-show-mark-read) - (notmuch-show-message-adjust)) + (notmuch-show-message-adjust) (if pop-at-end (notmuch-show-next-thread) (goto-char (point-max)))) @@ -1711,9 +1760,7 @@ to show, nil otherwise." (while (and (setq r (notmuch-show-goto-message-next)) (not (notmuch-show-get-prop :match)))) (if r - (progn - (notmuch-show-mark-read) - (notmuch-show-message-adjust)) + (notmuch-show-message-adjust) (goto-char (point-max))))) (defun notmuch-show-open-if-matched () @@ -1724,8 +1771,7 @@ to show, nil otherwise." (defun notmuch-show-goto-first-wanted-message () "Move to the first open message and mark it read" (goto-char (point-min)) - (if (notmuch-show-message-visible-p) - (notmuch-show-mark-read) + (unless (notmuch-show-message-visible-p) (notmuch-show-next-open-message)) (when (eobp) ;; There are no matched non-excluded messages so open all matched @@ -1733,8 +1779,7 @@ to show, nil otherwise." (notmuch-show-mapc 'notmuch-show-open-if-matched) (force-window-update) (goto-char (point-min)) - (if (notmuch-show-message-visible-p) - (notmuch-show-mark-read) + (unless (notmuch-show-message-visible-p) (notmuch-show-next-open-message)))) (defun notmuch-show-previous-open-message () @@ -1744,15 +1789,15 @@ to show, nil otherwise." (notmuch-show-goto-message-previous) (notmuch-show-move-to-message-top)) (not (notmuch-show-message-visible-p)))) - (notmuch-show-mark-read) (notmuch-show-message-adjust)) (defun notmuch-show-view-raw-message () - "View the file holding the current message." + "View the original source of the current message." (interactive) (let* ((id (notmuch-show-get-message-id)) (buf (get-buffer-create (concat "*notmuch-raw-" id "*")))) - (call-process notmuch-command nil buf nil "show" "--format=raw" id) + (let ((coding-system-for-read 'no-conversion)) + (call-process notmuch-command nil buf nil "show" "--format=raw" id)) (switch-to-buffer buf) (goto-char (point-min)) (set-buffer-modified-p nil) @@ -2055,16 +2100,19 @@ This presumes that the message is available at the selected Mailing List Archive If optional argument MLA is non-nil, use the provided key instead of prompting the user (see `notmuch-show-stash-mlarchive-link-alist')." (interactive) - (notmuch-common-do-stash - (concat (cdr (assoc - (or mla - (let ((completion-ignore-case t)) - (completing-read - "Mailing List Archive: " - notmuch-show-stash-mlarchive-link-alist - nil t nil nil notmuch-show-stash-mlarchive-link-default))) - notmuch-show-stash-mlarchive-link-alist)) - (notmuch-show-get-message-id t)))) + (let ((url (cdr (assoc + (or mla + (let ((completion-ignore-case t)) + (completing-read + "Mailing List Archive: " + notmuch-show-stash-mlarchive-link-alist + nil t nil nil + notmuch-show-stash-mlarchive-link-default))) + notmuch-show-stash-mlarchive-link-alist)))) + (notmuch-common-do-stash + (if (functionp url) + (funcall url (notmuch-show-get-message-id t)) + (concat url (notmuch-show-get-message-id t)))))) (defun notmuch-show-stash-mlarchive-link-and-go (&optional mla) "Copy an ML Archive URI for the current message to the kill-ring and visit it. diff --git a/emacs/notmuch-tree.el b/emacs/notmuch-tree.el index 7d5f4750..e859cc24 100644 --- a/emacs/notmuch-tree.el +++ b/emacs/notmuch-tree.el @@ -290,22 +290,6 @@ Some useful entries are: (beginning-of-line) (get-text-property (point) :notmuch-message-properties))) -;; XXX This should really be a lib function but we are trying to -;; reduce impact on the code base. -(defun notmuch-show-get-prop (prop &optional props) - "This is a tree view overridden version of notmuch-show-get-prop - -It gets property PROP from PROPS or, if PROPS is nil, the current -message in either tree or show. This means that several functions -in notmuch-show now work unchanged in tree as they just need the -correct message properties." - (let ((props (or props - (cond ((eq major-mode 'notmuch-show-mode) - (notmuch-show-get-message-properties)) - ((eq major-mode 'notmuch-tree-mode) - (notmuch-tree-get-message-properties)))))) - (plist-get props prop))) - (defun notmuch-tree-set-message-properties (props) (save-excursion (beginning-of-line) @@ -897,6 +881,15 @@ the same as for the function notmuch-tree." (set-process-filter proc 'notmuch-tree-process-filter) (set-process-query-on-exit-flag proc nil)))) +(defun notmuch-tree-get-query () + "Return the current query in this tree buffer" + (if notmuch-tree-query-context + (concat notmuch-tree-basic-query + " and (" + notmuch-tree-query-context + ")") + notmuch-tree-basic-query)) + (defun notmuch-tree (&optional query query-context target buffer-name open-target) "Display threads matching QUERY in Tree View. diff --git a/emacs/notmuch.el b/emacs/notmuch.el index 1adea9c2..b44a907a 100644 --- a/emacs/notmuch.el +++ b/emacs/notmuch.el @@ -580,7 +580,8 @@ This function advances the next thread when finished." (when notmuch-archive-tags (notmuch-search-tag (notmuch-tag-change-list notmuch-archive-tags unarchive) beg end)) - (notmuch-search-next-thread)) + (when (eq beg end) + (notmuch-search-next-thread))) (defun notmuch-search-update-result (result &optional pos) "Replace the result object of the thread at POS (or point) by @@ -649,12 +650,12 @@ of the result." Here is an example of how to color search results based on tags. (the following text would be placed in your ~/.emacs file): - (setq notmuch-search-line-faces '((\"deleted\" . (:foreground \"red\" - :background \"blue\")) - (\"unread\" . (:foreground \"green\")))) + (setq notmuch-search-line-faces '((\"unread\" . (:foreground \"green\")) + (\"deleted\" . (:foreground \"red\" + :background \"blue\")))) -The attributes defined for matching tags are merged, with later -attributes overriding earlier. A message having both \"deleted\" +The attributes defined for matching tags are merged, with earlier +attributes overriding later. A message having both \"deleted\" and \"unread\" tags with the above settings would have a green foreground and blue background." :type '(alist :key-type (string) :value-type (custom-face-edit)) @@ -862,6 +863,10 @@ PROMPT is the string to prompt with." (concat "tag:" (notmuch-escape-boolean-term tag))) (process-lines notmuch-command "search" "--output=tags" "*"))))) (let ((keymap (copy-keymap minibuffer-local-map)) + (current-query (case major-mode + (notmuch-search-mode (notmuch-search-get-query)) + (notmuch-show-mode (notmuch-show-get-query)) + (notmuch-tree-mode (notmuch-tree-get-query)))) (minibuffer-completion-table (completion-table-dynamic (lambda (string) @@ -879,7 +884,11 @@ PROMPT is the string to prompt with." (define-key keymap (kbd "TAB") 'minibuffer-complete) (let ((history-delete-duplicates t)) (read-from-minibuffer prompt nil keymap nil - 'notmuch-search-history nil nil))))) + 'notmuch-search-history current-query nil))))) + +(defun notmuch-search-get-query () + "Return the current query in this search buffer" + notmuch-search-query-string) ;;;###autoload (put 'notmuch-search 'notmuch-doc "Search for messages.") diff --git a/lib/Makefile.local b/lib/Makefile.local index c56cba99..41203909 100644 --- a/lib/Makefile.local +++ b/lib/Makefile.local @@ -5,7 +5,7 @@ # the library interface, (such as the deletion of an API or a major # semantic change that breaks formerly functioning code). # -LIBNOTMUCH_VERSION_MAJOR = 3 +LIBNOTMUCH_VERSION_MAJOR = 4 # The minor version of the library interface. This should be incremented at # the time of release for any additions to the library interface, diff --git a/lib/database-private.h b/lib/database-private.h index d3e65fd6..ca0751cf 100644 --- a/lib/database-private.h +++ b/lib/database-private.h @@ -36,16 +36,106 @@ #pragma GCC visibility push(hidden) +/* Bit masks for _notmuch_database::features. Features are named, + * independent aspects of the database schema. + * + * A database stores the set of features that it "uses" (implicitly + * before database version 3 and explicitly as of version 3). + * + * A given library version will "recognize" a particular set of + * features; if a database uses a feature that the library does not + * recognize, the library will refuse to open it. It is assumed the + * set of recognized features grows monotonically over time. A + * library version will "implement" some subset of the recognized + * features: some operations may require that the database use (or not + * use) some feature, while other operations may support both + * databases that use and that don't use some feature. + * + * On disk, the database stores string names for these features (see + * the feature_names array). These enum bit values are never + * persisted to disk and may change freely. + */ +enum _notmuch_features { + /* If set, file names are stored in "file-direntry" terms. If + * unset, file names are stored in document data. + * + * Introduced: version 1. */ + NOTMUCH_FEATURE_FILE_TERMS = 1 << 0, + + /* If set, directory timestamps are stored in documents with + * XDIRECTORY terms and relative paths. If unset, directory + * timestamps are stored in documents with XTIMESTAMP terms and + * absolute paths. + * + * Introduced: version 1. */ + NOTMUCH_FEATURE_DIRECTORY_DOCS = 1 << 1, + + /* If set, the from, subject, and message-id headers are stored in + * message document values. If unset, message documents *may* + * have these values, but if the value is empty, it must be + * retrieved from the message file. + * + * Introduced: optional in version 1, required as of version 3. + */ + NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES = 1 << 2, + + /* If set, folder terms are boolean and path terms exist. If + * unset, folder terms are probabilistic and stemmed and path + * terms do not exist. + * + * Introduced: version 2. */ + NOTMUCH_FEATURE_BOOL_FOLDER = 1 << 3, +}; + +/* In C++, a named enum is its own type, so define bitwise operators + * on _notmuch_features. */ +inline _notmuch_features +operator|(_notmuch_features a, _notmuch_features b) +{ + return static_cast<_notmuch_features>( + static_cast(a) | static_cast(b)); +} + +inline _notmuch_features +operator&(_notmuch_features a, _notmuch_features b) +{ + return static_cast<_notmuch_features>( + static_cast(a) & static_cast(b)); +} + +inline _notmuch_features +operator~(_notmuch_features a) +{ + return static_cast<_notmuch_features>(~static_cast(a)); +} + +inline _notmuch_features& +operator|=(_notmuch_features &a, _notmuch_features b) +{ + a = a | b; + return a; +} + +inline _notmuch_features& +operator&=(_notmuch_features &a, _notmuch_features b) +{ + a = a & b; + return a; +} + struct _notmuch_database { notmuch_bool_t exception_reported; char *path; - notmuch_bool_t needs_upgrade; notmuch_database_mode_t mode; int atomic_nesting; Xapian::Database *xapian_db; + /* Bit mask of features used by this database. This is a + * bitwise-OR of NOTMUCH_FEATURE_* values (above). */ + enum _notmuch_features features; + unsigned int last_doc_id; uint64_t last_thread_id; @@ -55,6 +145,22 @@ struct _notmuch_database { Xapian::ValueRangeProcessor *date_range_processor; }; +/* Prior to database version 3, features were implied by the database + * version number, so hard-code them for earlier versions. */ +#define NOTMUCH_FEATURES_V0 ((enum _notmuch_features)0) +#define NOTMUCH_FEATURES_V1 (NOTMUCH_FEATURES_V0 | NOTMUCH_FEATURE_FILE_TERMS | \ + NOTMUCH_FEATURE_DIRECTORY_DOCS) +#define NOTMUCH_FEATURES_V2 (NOTMUCH_FEATURES_V1 | NOTMUCH_FEATURE_BOOL_FOLDER) + +/* Current database features. If any of these are missing from a + * database, request an upgrade. + * NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES is not included because + * upgrade doesn't currently introduce the feature (though brand new + * databases will have it). */ +#define NOTMUCH_FEATURES_CURRENT \ + (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_DIRECTORY_DOCS | \ + NOTMUCH_FEATURE_BOOL_FOLDER) + /* Return the list of terms from the given iterator matching a prefix. * The prefix will be stripped from the strings in the returned list. * The list will be allocated using ctx as the talloc context. diff --git a/lib/database.cc b/lib/database.cc index 1efb14d4..1c6ffc57 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -20,6 +20,7 @@ #include "database-private.h" #include "parse-time-vrp.h" +#include "string-util.h" #include @@ -42,7 +43,7 @@ typedef struct { const char *prefix; } prefix_t; -#define NOTMUCH_DATABASE_VERSION 2 +#define NOTMUCH_DATABASE_VERSION 3 #define STRINGIFY(s) _SUB_STRINGIFY(s) #define _SUB_STRINGIFY(s) #s @@ -54,9 +55,12 @@ typedef struct { * * Mail document * ------------- - * A mail document is associated with a particular email message file - * on disk. It is indexed with the following prefixed terms which the - * database uses to construct threads, etc.: + * A mail document is associated with a particular email message. It + * is stored in one or more files on disk (though only one has its + * content indexed) and is uniquely identified by its "id" field + * (which is generally the message ID). It is indexed with the + * following prefixed terms which the database uses to construct + * threads, etc.: * * Single terms of given prefix: * @@ -151,6 +155,17 @@ typedef struct { * changes are made to the database (such as by * indexing new fields). * + * features The set of features supported by this + * database. This consists of a set of + * '\n'-separated lines, where each is a feature + * name, a '\t', and compatibility flags. If the + * compatibility flags contain 'w', then the + * opener must support this feature to safely + * write this database. If the compatibility + * flags contain 'r', then the opener must + * support this feature to read this database. + * Introduced in database version 3. + * * last_thread_id The last thread ID generated. This is stored * as a 16-byte hexadecimal ASCII representation * of a 64-bit unsigned integer. The first ID @@ -251,6 +266,28 @@ _find_prefix (const char *name) return ""; } +static const struct { + /* NOTMUCH_FEATURE_* value. */ + _notmuch_features value; + /* Feature name as it appears in the database. This name should + * be appropriate for displaying to the user if an older version + * of notmuch doesn't support this feature. */ + const char *name; + /* Compatibility flags when this feature is declared. */ + const char *flags; +} feature_names[] = { + { NOTMUCH_FEATURE_FILE_TERMS, + "multiple paths per message", "rw" }, + { NOTMUCH_FEATURE_DIRECTORY_DOCS, + "relative directory paths", "rw" }, + /* Header values are not required for reading a database because a + * reader can just refer to the message file. */ + { NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES, + "from/subject/message-ID in database", "w" }, + { NOTMUCH_FEATURE_BOOL_FOLDER, + "exact folder:/path: search", "rw" }, +}; + const char * notmuch_status_to_string (notmuch_status_t status) { @@ -279,6 +316,8 @@ notmuch_status_to_string (notmuch_status_t status) return "Unbalanced number of calls to notmuch_database_begin_atomic/end_atomic"; case NOTMUCH_STATUS_UNSUPPORTED_OPERATION: return "Unsupported operation"; + case NOTMUCH_STATUS_UPGRADE_REQUIRED: + return "Operation requires a database upgrade"; default: case NOTMUCH_STATUS_LAST_STATUS: return "Unknown error status value"; @@ -351,12 +390,12 @@ find_document_for_doc_id (notmuch_database_t *notmuch, unsigned doc_id) * * notmuch-sha1- */ -static char * -_message_id_compressed (void *ctx, const char *message_id) +char * +_notmuch_message_id_compressed (void *ctx, const char *message_id) { char *sha1, *compressed; - sha1 = notmuch_sha1_of_string (message_id); + sha1 = _notmuch_sha1_of_string (message_id); compressed = talloc_asprintf (ctx, "notmuch-sha1-%s", sha1); free (sha1); @@ -376,7 +415,7 @@ notmuch_database_find_message (notmuch_database_t *notmuch, return NOTMUCH_STATUS_NULL_POINTER; if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX) - message_id = _message_id_compressed (notmuch, message_id); + message_id = _notmuch_message_id_compressed (notmuch, message_id); try { status = _notmuch_database_find_unique_doc_id (notmuch, "id", @@ -521,7 +560,7 @@ parse_references (void *ctx, GHashTable *hash, const char *refs) { - char *ref; + char *ref, *last_ref = NULL; if (refs == NULL || *refs == '\0') return NULL; @@ -529,20 +568,17 @@ parse_references (void *ctx, while (*refs) { ref = _parse_message_id (ctx, refs, &refs); - if (ref && strcmp (ref, message_id)) + if (ref && strcmp (ref, message_id)) { g_hash_table_insert (hash, ref, NULL); + last_ref = ref; + } } /* The return value of this function is used to add a parent * reference to the database. We should avoid making a message - * its own parent, thus the following check. + * its own parent, thus the above check. */ - - if (ref && strcmp(ref, message_id)) { - return ref; - } else { - return NULL; - } + return last_ref; } notmuch_status_t @@ -591,6 +627,11 @@ notmuch_database_create (const char *path, notmuch_database_t **database) ¬much); if (status) goto DONE; + + /* Upgrade doesn't add this feature to existing databases, but new + * databases have it. */ + notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES; + status = notmuch_database_upgrade (notmuch, NULL, NULL); if (status) { notmuch_database_close(notmuch); @@ -619,6 +660,83 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch) return NOTMUCH_STATUS_SUCCESS; } +/* Parse a database features string from the given database version. + * Returns the feature bit set. + * + * For version < 3, this ignores the features string and returns a + * hard-coded set of features. + * + * If there are unrecognized features that are required to open the + * database in mode (which should be 'r' or 'w'), return a + * comma-separated list of unrecognized but required features in + * *incompat_out suitable for presenting to the user. *incompat_out + * will be allocated from ctx. + */ +static _notmuch_features +_parse_features (const void *ctx, const char *features, unsigned int version, + char mode, char **incompat_out) +{ + _notmuch_features res = static_cast<_notmuch_features>(0); + unsigned int namelen, i; + size_t llen = 0; + const char *flags; + + /* Prior to database version 3, features were implied by the + * version number. */ + if (version == 0) + return NOTMUCH_FEATURES_V0; + else if (version == 1) + return NOTMUCH_FEATURES_V1; + else if (version == 2) + return NOTMUCH_FEATURES_V2; + + /* Parse the features string */ + while ((features = strtok_len_c (features + llen, "\n", &llen)) != NULL) { + flags = strchr (features, '\t'); + if (! flags || flags > features + llen) + continue; + namelen = flags - features; + + for (i = 0; i < ARRAY_SIZE (feature_names); ++i) { + if (strlen (feature_names[i].name) == namelen && + strncmp (feature_names[i].name, features, namelen) == 0) { + res |= feature_names[i].value; + break; + } + } + + if (i == ARRAY_SIZE (feature_names) && incompat_out) { + /* Unrecognized feature */ + const char *have = strchr (flags, mode); + if (have && have < features + llen) { + /* This feature is required to access this database in + * 'mode', but we don't understand it. */ + if (! *incompat_out) + *incompat_out = talloc_strdup (ctx, ""); + *incompat_out = talloc_asprintf_append_buffer ( + *incompat_out, "%s%.*s", **incompat_out ? ", " : "", + namelen, features); + } + } + } + + return res; +} + +static char * +_print_features (const void *ctx, unsigned int features) +{ + unsigned int i; + char *res = talloc_strdup (ctx, ""); + + for (i = 0; i < ARRAY_SIZE (feature_names); ++i) + if (features & feature_names[i].value) + res = talloc_asprintf_append_buffer ( + res, "%s\t%s\n", feature_names[i].name, feature_names[i].flags); + + return res; +} + notmuch_status_t notmuch_database_open (const char *path, notmuch_database_mode_t mode, @@ -627,7 +745,7 @@ notmuch_database_open (const char *path, notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; void *local = talloc_new (NULL); notmuch_database_t *notmuch = NULL; - char *notmuch_path, *xapian_path; + char *notmuch_path, *xapian_path, *incompat_features; struct stat st; int err; unsigned int i, version; @@ -677,7 +795,6 @@ notmuch_database_open (const char *path, if (notmuch->path[strlen (notmuch->path) - 1] == '/') notmuch->path[strlen (notmuch->path) - 1] = '\0'; - notmuch->needs_upgrade = FALSE; notmuch->mode = mode; notmuch->atomic_nesting = 0; try { @@ -686,37 +803,44 @@ notmuch_database_open (const char *path, if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) { notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path, Xapian::DB_CREATE_OR_OPEN); - version = notmuch_database_get_version (notmuch); - - if (version > NOTMUCH_DATABASE_VERSION) { - fprintf (stderr, - "Error: Notmuch database at %s\n" - " has a newer database format version (%u) than supported by this\n" - " version of notmuch (%u). Refusing to open this database in\n" - " read-write mode.\n", - notmuch_path, version, NOTMUCH_DATABASE_VERSION); - notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY; - notmuch_database_destroy (notmuch); - notmuch = NULL; - status = NOTMUCH_STATUS_FILE_ERROR; - goto DONE; - } - - if (version < NOTMUCH_DATABASE_VERSION) - notmuch->needs_upgrade = TRUE; } else { notmuch->xapian_db = new Xapian::Database (xapian_path); - version = notmuch_database_get_version (notmuch); - if (version > NOTMUCH_DATABASE_VERSION) - { - fprintf (stderr, - "Warning: Notmuch database at %s\n" - " has a newer database format version (%u) than supported by this\n" - " version of notmuch (%u). Some operations may behave incorrectly,\n" - " (but the database will not be harmed since it is being opened\n" - " in read-only mode).\n", - notmuch_path, version, NOTMUCH_DATABASE_VERSION); - } + } + + /* Check version. As of database version 3, we represent + * changes in terms of features, so assume a version bump + * means a dramatically incompatible change. */ + version = notmuch_database_get_version (notmuch); + if (version > NOTMUCH_DATABASE_VERSION) { + fprintf (stderr, + "Error: Notmuch database at %s\n" + " has a newer database format version (%u) than supported by this\n" + " version of notmuch (%u).\n", + notmuch_path, version, NOTMUCH_DATABASE_VERSION); + notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY; + notmuch_database_destroy (notmuch); + notmuch = NULL; + status = NOTMUCH_STATUS_FILE_ERROR; + goto DONE; + } + + /* Check features. */ + incompat_features = NULL; + notmuch->features = _parse_features ( + local, notmuch->xapian_db->get_metadata ("features").c_str (), + version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r', + &incompat_features); + if (incompat_features) { + fprintf (stderr, + "Error: Notmuch database at %s\n" + " requires features (%s)\n" + " not supported by this version of notmuch.\n", + notmuch_path, incompat_features); + notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY; + notmuch_database_destroy (notmuch); + notmuch = NULL; + status = NOTMUCH_STATUS_FILE_ERROR; + goto DONE; } notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid (); @@ -774,28 +898,35 @@ notmuch_database_open (const char *path, return status; } -void +notmuch_status_t notmuch_database_close (notmuch_database_t *notmuch) { - try { - if (notmuch->xapian_db != NULL && - notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE) - (static_cast (notmuch->xapian_db))->flush (); - } catch (const Xapian::Error &error) { - if (! notmuch->exception_reported) { - fprintf (stderr, "Error: A Xapian exception occurred flushing database: %s\n", - error.get_msg().c_str()); - } - } + notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; /* Many Xapian objects (and thus notmuch objects) hold references to * the database, so merely deleting the database may not suffice to * close it. Thus, we explicitly close it here. */ if (notmuch->xapian_db != NULL) { try { + /* If there's an outstanding transaction, it's unclear if + * closing the Xapian database commits everything up to + * that transaction, or may discard committed (but + * unflushed) transactions. To be certain, explicitly + * cancel any outstanding transaction before closing. */ + if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE && + notmuch->atomic_nesting) + (static_cast (notmuch->xapian_db)) + ->cancel_transaction (); + + /* Close the database. This implicitly flushes + * outstanding changes. */ notmuch->xapian_db->close(); } catch (const Xapian::Error &error) { - /* do nothing */ + status = NOTMUCH_STATUS_XAPIAN_EXCEPTION; + if (! notmuch->exception_reported) { + fprintf (stderr, "Error: A Xapian exception occurred closing database: %s\n", + error.get_msg().c_str()); + } } } @@ -809,6 +940,8 @@ notmuch_database_close (notmuch_database_t *notmuch) notmuch->value_range_processor = NULL; delete notmuch->date_range_processor; notmuch->date_range_processor = NULL; + + return status; } #if HAVE_XAPIAN_COMPACT @@ -972,8 +1105,15 @@ notmuch_database_compact (const char *path, } DONE: - if (notmuch) - notmuch_database_destroy (notmuch); + if (notmuch) { + notmuch_status_t ret2; + + ret2 = notmuch_database_destroy (notmuch); + + /* don't clobber previous error status */ + if (ret == NOTMUCH_STATUS_SUCCESS && ret2 != NOTMUCH_STATUS_SUCCESS) + ret = ret2; + } talloc_free (local); @@ -991,11 +1131,15 @@ notmuch_database_compact (unused (const char *path), } #endif -void +notmuch_status_t notmuch_database_destroy (notmuch_database_t *notmuch) { - notmuch_database_close (notmuch); + notmuch_status_t status; + + status = notmuch_database_close (notmuch); talloc_free (notmuch); + + return status; } const char * @@ -1030,7 +1174,9 @@ notmuch_database_get_version (notmuch_database_t *notmuch) notmuch_bool_t notmuch_database_needs_upgrade (notmuch_database_t *notmuch) { - return notmuch->needs_upgrade; + return notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE && + ((NOTMUCH_FEATURES_CURRENT & ~notmuch->features) || + (notmuch_database_get_version (notmuch) < NOTMUCH_DATABASE_VERSION)); } static volatile sig_atomic_t do_progress_notify = 0; @@ -1059,11 +1205,13 @@ notmuch_database_upgrade (notmuch_database_t *notmuch, double progress), void *closure) { + void *local = talloc_new (NULL); + Xapian::TermIterator t, t_end; Xapian::WritableDatabase *db; struct sigaction action; struct itimerval timerval; notmuch_bool_t timer_is_active = FALSE; - unsigned int version; + enum _notmuch_features target_features, new_features; notmuch_status_t status; unsigned int count = 0, total = 0; @@ -1073,9 +1221,10 @@ notmuch_database_upgrade (notmuch_database_t *notmuch, db = static_cast (notmuch->xapian_db); - version = notmuch_database_get_version (notmuch); + target_features = notmuch->features | NOTMUCH_FEATURES_CURRENT; + new_features = NOTMUCH_FEATURES_CURRENT & ~notmuch->features; - if (version >= NOTMUCH_DATABASE_VERSION) + if (! notmuch_database_needs_upgrade (notmuch)) return NOTMUCH_STATUS_SUCCESS; if (progress_notify) { @@ -1096,18 +1245,33 @@ notmuch_database_upgrade (notmuch_database_t *notmuch, timer_is_active = TRUE; } - /* Before version 1, each message document had its filename in the - * data field. Copy that into the new format by calling - * notmuch_message_add_filename. - */ - if (version < 1) { + /* Figure out how much total work we need to do. */ + if (new_features & + (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER)) { + notmuch_query_t *query = notmuch_query_create (notmuch, ""); + total += notmuch_query_count_messages (query); + notmuch_query_destroy (query); + } + if (new_features & NOTMUCH_FEATURE_DIRECTORY_DOCS) { + t_end = db->allterms_end ("XTIMESTAMP"); + for (t = db->allterms_begin ("XTIMESTAMP"); t != t_end; t++) + ++total; + } + + /* Perform the upgrade in a transaction. */ + db->begin_transaction (true); + + /* Set the target features so we write out changes in the desired + * format. */ + notmuch->features = target_features; + + /* Perform per-message upgrades. */ + if (new_features & + (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER)) { notmuch_query_t *query = notmuch_query_create (notmuch, ""); notmuch_messages_t *messages; notmuch_message_t *message; char *filename; - Xapian::TermIterator t, t_end; - - total = notmuch_query_count_messages (query); for (messages = notmuch_query_search_messages (query); notmuch_messages_valid (messages); @@ -1120,12 +1284,27 @@ notmuch_database_upgrade (notmuch_database_t *notmuch, message = notmuch_messages_get (messages); - filename = _notmuch_message_talloc_copy_data (message); - if (filename && *filename != '\0') { - _notmuch_message_add_filename (message, filename); - _notmuch_message_sync (message); + /* Before version 1, each message document had its + * filename in the data field. Copy that into the new + * format by calling notmuch_message_add_filename. + */ + if (new_features & NOTMUCH_FEATURE_FILE_TERMS) { + filename = _notmuch_message_talloc_copy_data (message); + if (filename && *filename != '\0') { + _notmuch_message_add_filename (message, filename); + _notmuch_message_clear_data (message); + } + talloc_free (filename); } - talloc_free (filename); + + /* Prior to version 2, the "folder:" prefix was + * probabilistic and stemmed. Change it to the current + * boolean prefix. Add "path:" prefixes while at it. + */ + if (new_features & NOTMUCH_FEATURE_BOOL_FOLDER) + _notmuch_message_upgrade_folder (message); + + _notmuch_message_sync (message); notmuch_message_destroy (message); @@ -1133,11 +1312,14 @@ notmuch_database_upgrade (notmuch_database_t *notmuch, } notmuch_query_destroy (query); + } - /* Also, before version 1 we stored directory timestamps in - * XTIMESTAMP documents instead of the current XDIRECTORY - * documents. So copy those as well. */ + /* Perform per-directory upgrades. */ + /* Before version 1 we stored directory timestamps in + * XTIMESTAMP documents instead of the current XDIRECTORY + * documents. So copy those as well. */ + if (new_features & NOTMUCH_FEATURE_DIRECTORY_DOCS) { t_end = notmuch->xapian_db->allterms_end ("XTIMESTAMP"); for (t = notmuch->xapian_db->allterms_begin ("XTIMESTAMP"); @@ -1170,106 +1352,18 @@ notmuch_database_upgrade (notmuch_database_t *notmuch, NOTMUCH_FIND_CREATE, &status); notmuch_directory_set_mtime (directory, mtime); notmuch_directory_destroy (directory); - } - } - } - /* - * Prior to version 2, the "folder:" prefix was probabilistic and - * stemmed. Change it to the current boolean prefix. Add "path:" - * prefixes while at it. - */ - if (version < 2) { - notmuch_query_t *query = notmuch_query_create (notmuch, ""); - notmuch_messages_t *messages; - notmuch_message_t *message; - - count = 0; - total = notmuch_query_count_messages (query); - - for (messages = notmuch_query_search_messages (query); - notmuch_messages_valid (messages); - notmuch_messages_move_to_next (messages)) { - if (do_progress_notify) { - progress_notify (closure, (double) count / total); - do_progress_notify = 0; + db->delete_document (*p); } - message = notmuch_messages_get (messages); - - _notmuch_message_upgrade_folder (message); - _notmuch_message_sync (message); - - notmuch_message_destroy (message); - - count++; + ++count; } - - notmuch_query_destroy (query); } + db->set_metadata ("features", _print_features (local, notmuch->features)); db->set_metadata ("version", STRINGIFY (NOTMUCH_DATABASE_VERSION)); - db->flush (); - - /* Now that the upgrade is complete we can remove the old data - * and documents that are no longer needed. */ - if (version < 1) { - notmuch_query_t *query = notmuch_query_create (notmuch, ""); - notmuch_messages_t *messages; - notmuch_message_t *message; - char *filename; - - for (messages = notmuch_query_search_messages (query); - notmuch_messages_valid (messages); - notmuch_messages_move_to_next (messages)) - { - if (do_progress_notify) { - progress_notify (closure, (double) count / total); - do_progress_notify = 0; - } - message = notmuch_messages_get (messages); - - filename = _notmuch_message_talloc_copy_data (message); - if (filename && *filename != '\0') { - _notmuch_message_clear_data (message); - _notmuch_message_sync (message); - } - talloc_free (filename); - - notmuch_message_destroy (message); - } - - notmuch_query_destroy (query); - } - - if (version < 1) { - Xapian::TermIterator t, t_end; - - t_end = notmuch->xapian_db->allterms_end ("XTIMESTAMP"); - - for (t = notmuch->xapian_db->allterms_begin ("XTIMESTAMP"); - t != t_end; - t++) - { - Xapian::PostingIterator p, p_end; - std::string term = *t; - - p_end = notmuch->xapian_db->postlist_end (term); - - for (p = notmuch->xapian_db->postlist_begin (term); - p != p_end; - p++) - { - if (do_progress_notify) { - progress_notify (closure, (double) count / total); - do_progress_notify = 0; - } - - db->delete_document (*p); - } - } - } + db->commit_transaction (); if (timer_is_active) { /* Now stop the timer. */ @@ -1284,6 +1378,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch, sigaction (SIGALRM, &action, NULL); } + talloc_free (local); return NOTMUCH_STATUS_SUCCESS; } @@ -1356,7 +1451,7 @@ _notmuch_database_get_directory_db_path (const char *path) int term_len = strlen (_find_prefix ("directory")) + strlen (path); if (term_len > NOTMUCH_TERM_MAX) - return notmuch_sha1_of_string (path); + return _notmuch_sha1_of_string (path); else return path; } @@ -1633,7 +1728,7 @@ static char * _get_metadata_thread_id_key (void *ctx, const char *message_id) { if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX) - message_id = _message_id_compressed (ctx, message_id); + message_id = _notmuch_message_id_compressed (ctx, message_id); return talloc_asprintf (ctx, NOTMUCH_METADATA_THREAD_ID_PREFIX "%s", message_id); @@ -1758,12 +1853,12 @@ _notmuch_database_link_message_to_parents (notmuch_database_t *notmuch, _my_talloc_free_for_g_hash, NULL); this_message_id = notmuch_message_get_message_id (message); - refs = notmuch_message_file_get_header (message_file, "references"); + refs = _notmuch_message_file_get_header (message_file, "references"); last_ref_message_id = parse_references (message, this_message_id, parents, refs); - in_reply_to = notmuch_message_file_get_header (message_file, "in-reply-to"); + in_reply_to = _notmuch_message_file_get_header (message_file, "in-reply-to"); in_reply_to_message_id = parse_references (message, this_message_id, parents, in_reply_to); @@ -1863,6 +1958,37 @@ _notmuch_database_link_message_to_children (notmuch_database_t *notmuch, return ret; } +/* Fetch and clear the stored thread_id for message, or NULL if none. */ +static char * +_consume_metadata_thread_id (void *ctx, notmuch_database_t *notmuch, + notmuch_message_t *message) +{ + const char *message_id; + string stored_id; + char *metadata_key; + + message_id = notmuch_message_get_message_id (message); + metadata_key = _get_metadata_thread_id_key (ctx, message_id); + + /* Check if we have already seen related messages to this one. + * If we have then use the thread_id that we stored at that time. + */ + stored_id = notmuch->xapian_db->get_metadata (metadata_key); + if (stored_id.empty ()) { + return NULL; + } else { + Xapian::WritableDatabase *db; + + db = static_cast (notmuch->xapian_db); + + /* Clear the metadata for this message ID. We don't need it + * anymore. */ + db->set_metadata (metadata_key, ""); + + return talloc_strdup (ctx, stored_id.c_str ()); + } +} + /* Given a (mostly empty) 'message' and its corresponding * 'message_file' link it to existing threads in the database. * @@ -1893,42 +2019,25 @@ _notmuch_database_link_message (notmuch_database_t *notmuch, notmuch_message_t *message, notmuch_message_file_t *message_file) { + void *local = talloc_new (NULL); notmuch_status_t status; - const char *message_id, *thread_id = NULL; - char *metadata_key; - string stored_id; - - message_id = notmuch_message_get_message_id (message); - metadata_key = _get_metadata_thread_id_key (message, message_id); + const char *thread_id; - /* Check if we have already seen related messages to this one. - * If we have then use the thread_id that we stored at that time. - */ - stored_id = notmuch->xapian_db->get_metadata (metadata_key); - if (! stored_id.empty()) { - Xapian::WritableDatabase *db; - - db = static_cast (notmuch->xapian_db); - - /* Clear the metadata for this message ID. We don't need it - * anymore. */ - db->set_metadata (metadata_key, ""); - thread_id = stored_id.c_str(); - - _notmuch_message_add_term (message, "thread", thread_id); - } - talloc_free (metadata_key); + /* Check if the message already had a thread ID */ + thread_id = _consume_metadata_thread_id (local, notmuch, message); + if (thread_id) + _notmuch_message_add_term (message, "thread", thread_id); status = _notmuch_database_link_message_to_parents (notmuch, message, message_file, &thread_id); if (status) - return status; + goto DONE; status = _notmuch_database_link_message_to_children (notmuch, message, &thread_id); if (status) - return status; + goto DONE; /* If not part of any existing thread, generate a new thread ID. */ if (thread_id == NULL) { @@ -1937,7 +2046,10 @@ _notmuch_database_link_message (notmuch_database_t *notmuch, _notmuch_message_add_term (message, "thread", thread_id); } - return NOTMUCH_STATUS_SUCCESS; + DONE: + talloc_free (local); + + return status; } notmuch_status_t @@ -1961,7 +2073,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch, if (ret) return ret; - message_file = notmuch_message_file_open (filename); + message_file = _notmuch_message_file_open (filename); if (message_file == NULL) return NOTMUCH_STATUS_FILE_ERROR; @@ -1982,9 +2094,9 @@ notmuch_database_add_message (notmuch_database_t *notmuch, * let's make sure that what we're looking at looks like an * actual email message. */ - from = notmuch_message_file_get_header (message_file, "from"); - subject = notmuch_message_file_get_header (message_file, "subject"); - to = notmuch_message_file_get_header (message_file, "to"); + from = _notmuch_message_file_get_header (message_file, "from"); + subject = _notmuch_message_file_get_header (message_file, "subject"); + to = _notmuch_message_file_get_header (message_file, "to"); if ((from == NULL || *from == '\0') && (subject == NULL || *subject == '\0') && @@ -1997,7 +2109,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch, /* Now that we're sure it's mail, the first order of business * is to find a message ID (or else create one ourselves). */ - header = notmuch_message_file_get_header (message_file, "message-id"); + header = _notmuch_message_file_get_header (message_file, "message-id"); if (header && *header != '\0') { message_id = _parse_message_id (message_file, header, NULL); @@ -2005,20 +2117,12 @@ notmuch_database_add_message (notmuch_database_t *notmuch, * better than no message-id at all. */ if (message_id == NULL) message_id = talloc_strdup (message_file, header); - - /* If a message ID is too long, substitute its sha1 instead. */ - if (message_id && strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX) { - char *compressed = _message_id_compressed (message_file, - message_id); - talloc_free (message_id); - message_id = compressed; - } } if (message_id == NULL ) { /* No message-id at all, let's generate one by taking a * hash over the file's contents. */ - char *sha1 = notmuch_sha1_of_file (filename); + char *sha1 = _notmuch_sha1_of_file (filename); /* If that failed too, something is really wrong. Give up. */ if (sha1 == NULL) { @@ -2058,7 +2162,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch, if (ret) goto DONE; - date = notmuch_message_file_get_header (message_file, "date"); + date = _notmuch_message_file_get_header (message_file, "date"); _notmuch_message_set_header_values (message, date, from, subject); ret = _notmuch_message_index_file (message, message_file); @@ -2087,7 +2191,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch, } if (message_file) - notmuch_message_file_close (message_file); + _notmuch_message_file_close (message_file); ret2 = notmuch_database_end_atomic (notmuch); if ((ret == NOTMUCH_STATUS_SUCCESS || @@ -2135,6 +2239,9 @@ notmuch_database_find_message_by_filename (notmuch_database_t *notmuch, if (message_ret == NULL) return NOTMUCH_STATUS_NULL_POINTER; + if (! (notmuch->features & NOTMUCH_FEATURE_FILE_TERMS)) + return NOTMUCH_STATUS_UPGRADE_REQUIRED; + /* return NULL on any failure */ *message_ret = NULL; diff --git a/lib/directory.cc b/lib/directory.cc index 6a3ffed7..8daaec8e 100644 --- a/lib/directory.cc +++ b/lib/directory.cc @@ -105,6 +105,11 @@ _notmuch_directory_create (notmuch_database_t *notmuch, const char *db_path; notmuch_bool_t create = (flags & NOTMUCH_FIND_CREATE); + if (! (notmuch->features & NOTMUCH_FEATURE_DIRECTORY_DOCS)) { + *status_ret = NOTMUCH_STATUS_UPGRADE_REQUIRED; + return NULL; + } + *status_ret = NOTMUCH_STATUS_SUCCESS; path = _notmuch_database_relative_path (notmuch, path); diff --git a/lib/message-file.c b/lib/message-file.c index 483ba1e9..eda1b748 100644 --- a/lib/message-file.c +++ b/lib/message-file.c @@ -99,19 +99,19 @@ _notmuch_message_file_open_ctx (void *ctx, const char *filename) FAIL: fprintf (stderr, "Error opening %s: %s\n", filename, strerror (errno)); - notmuch_message_file_close (message); + _notmuch_message_file_close (message); return NULL; } notmuch_message_file_t * -notmuch_message_file_open (const char *filename) +_notmuch_message_file_open (const char *filename) { return _notmuch_message_file_open_ctx (NULL, filename); } void -notmuch_message_file_close (notmuch_message_file_t *message) +_notmuch_message_file_close (notmuch_message_file_t *message) { talloc_free (message); } @@ -297,7 +297,7 @@ _notmuch_message_file_get_combined_header (notmuch_message_file_t *message, } const char * -notmuch_message_file_get_header (notmuch_message_file_t *message, +_notmuch_message_file_get_header (notmuch_message_file_t *message, const char *header) { const char *value; diff --git a/lib/message.cc b/lib/message.cc index d0b7351e..38bc9291 100644 --- a/lib/message.cc +++ b/lib/message.cc @@ -193,14 +193,16 @@ _notmuch_message_create (const void *talloc_owner, * There is already a document with message ID 'message_id' in the * database. The returned message can be used to query/modify the * document. + * * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND: * * No document with 'message_id' exists in the database. The * returned message contains a newly created document (not yet * added to the database) and a document ID that is known not to - * exist in the database. The caller can modify the message, and a - * call to _notmuch_message_sync will add * the document to the - * database. + * exist in the database. This message is "blank"; that is, it + * contains only a message ID and no other metadata. The caller + * can modify the message, and a call to _notmuch_message_sync + * will add the document to the database. * * If an error occurs, this function will return NULL and *status * will be set as appropriate. (The status pointer argument must @@ -224,6 +226,10 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch, else if (*status_ret) return NULL; + /* If the message ID is too long, substitute its sha1 instead. */ + if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX) + message_id = _notmuch_message_id_compressed (message, message_id); + term = talloc_asprintf (NULL, "%s%s", _find_prefix ("id"), message_id); if (term == NULL) { @@ -412,26 +418,35 @@ _notmuch_message_ensure_message_file (notmuch_message_t *message) const char * notmuch_message_get_header (notmuch_message_t *message, const char *header) { - try { - std::string value; - - /* Fetch header from the appropriate xapian value field if - * available */ - if (strcasecmp (header, "from") == 0) - value = message->doc.get_value (NOTMUCH_VALUE_FROM); - else if (strcasecmp (header, "subject") == 0) - value = message->doc.get_value (NOTMUCH_VALUE_SUBJECT); - else if (strcasecmp (header, "message-id") == 0) - value = message->doc.get_value (NOTMUCH_VALUE_MESSAGE_ID); - - if (!value.empty()) + Xapian::valueno slot = Xapian::BAD_VALUENO; + + /* Fetch header from the appropriate xapian value field if + * available */ + if (strcasecmp (header, "from") == 0) + slot = NOTMUCH_VALUE_FROM; + else if (strcasecmp (header, "subject") == 0) + slot = NOTMUCH_VALUE_SUBJECT; + else if (strcasecmp (header, "message-id") == 0) + slot = NOTMUCH_VALUE_MESSAGE_ID; + + if (slot != Xapian::BAD_VALUENO) { + try { + std::string value = message->doc.get_value (slot); + + /* If we have NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES, then + * empty values indicate empty headers. If we don't, then + * it could just mean we didn't record the header. */ + if ((message->notmuch->features & + NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES) || + ! value.empty()) return talloc_strdup (message, value.c_str ()); - } catch (Xapian::Error &error) { - fprintf (stderr, "A Xapian exception occurred when reading header: %s\n", - error.get_msg().c_str()); - message->notmuch->exception_reported = TRUE; - return NULL; + } catch (Xapian::Error &error) { + fprintf (stderr, "A Xapian exception occurred when reading header: %s\n", + error.get_msg().c_str()); + message->notmuch->exception_reported = TRUE; + return NULL; + } } /* Otherwise fall back to parsing the file */ @@ -439,7 +454,7 @@ notmuch_message_get_header (notmuch_message_t *message, const char *header) if (message->message_file == NULL) return NULL; - return notmuch_message_file_get_header (message->message_file, header); + return _notmuch_message_file_get_header (message->message_file, header); } /* Return the message ID from the In-Reply-To header of 'message'. @@ -644,6 +659,10 @@ _notmuch_message_add_filename (notmuch_message_t *message, if (filename == NULL) INTERNAL_ERROR ("Message filename cannot be NULL."); + if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) || + ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER)) + return NOTMUCH_STATUS_UPGRADE_REQUIRED; + relative = _notmuch_database_relative_path (message->notmuch, filename); status = _notmuch_database_split_path (local, relative, &directory, NULL); @@ -688,6 +707,10 @@ _notmuch_message_remove_filename (notmuch_message_t *message, notmuch_private_status_t private_status; notmuch_status_t status; + if (! (message->notmuch->features & NOTMUCH_FEATURE_FILE_TERMS) || + ! (message->notmuch->features & NOTMUCH_FEATURE_BOOL_FOLDER)) + return NOTMUCH_STATUS_UPGRADE_REQUIRED; + status = _notmuch_database_filename_to_direntry ( local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry); if (status || !direntry) @@ -873,6 +896,9 @@ notmuch_message_get_date (notmuch_message_t *message) return 0; } + if (value.empty ()) + /* sortable_unserialise is undefined on empty string */ + return 0; return Xapian::sortable_unserialise (value); } @@ -898,13 +924,13 @@ notmuch_message_get_tags (notmuch_message_t *message) } const char * -notmuch_message_get_author (notmuch_message_t *message) +_notmuch_message_get_author (notmuch_message_t *message) { return message->author; } void -notmuch_message_set_author (notmuch_message_t *message, +_notmuch_message_set_author (notmuch_message_t *message, const char *author) { if (message->author) @@ -971,7 +997,7 @@ void _notmuch_message_close (notmuch_message_t *message) { if (message->message_file) { - notmuch_message_file_close (message->message_file); + _notmuch_message_file_close (message->message_file); message->message_file = NULL; } } @@ -1032,6 +1058,8 @@ _notmuch_message_gen_terms (notmuch_message_t *message, /* Create a gap between this an the next terms so they don't * appear to be a phrase. */ message->termpos = term_gen->get_termpos () + 100; + + _notmuch_message_invalidate_metadata (message, prefix_name); } term_gen->set_termpos (message->termpos); @@ -1476,7 +1504,7 @@ notmuch_message_tags_to_maildir_flags (notmuch_message_t *message) talloc_free (to_set); talloc_free (to_clear); - return NOTMUCH_STATUS_SUCCESS; + return status; } notmuch_status_t diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h index 703ae7bb..36cc12b0 100644 --- a/lib/notmuch-private.h +++ b/lib/notmuch-private.h @@ -174,6 +174,9 @@ typedef struct _notmuch_doc_id_set notmuch_doc_id_set_t; const char * _find_prefix (const char *name); +char * +_notmuch_message_id_compressed (void *ctx, const char *message_id); + notmuch_status_t _notmuch_database_ensure_writable (notmuch_database_t *notmuch); @@ -316,11 +319,11 @@ _notmuch_message_clear_data (notmuch_message_t *message); /* Set the author member of 'message' - this is the representation used * when displaying the message */ void -notmuch_message_set_author (notmuch_message_t *message, const char *author); +_notmuch_message_set_author (notmuch_message_t *message, const char *author); /* Get the author member of 'message' */ const char * -notmuch_message_get_author (notmuch_message_t *message); +_notmuch_message_get_author (notmuch_message_t *message); /* message-file.c */ @@ -337,7 +340,7 @@ typedef struct _notmuch_message_file notmuch_message_file_t; * Returns NULL if any error occurs. */ notmuch_message_file_t * -notmuch_message_file_open (const char *filename); +_notmuch_message_file_open (const char *filename); /* Like notmuch_message_file_open but with 'ctx' as the talloc owner. */ notmuch_message_file_t * @@ -345,7 +348,7 @@ _notmuch_message_file_open_ctx (void *ctx, const char *filename); /* Close a notmuch message previously opened with notmuch_message_open. */ void -notmuch_message_file_close (notmuch_message_file_t *message); +_notmuch_message_file_close (notmuch_message_file_t *message); /* Parse the message. * @@ -386,7 +389,7 @@ _notmuch_message_file_get_mime_message (notmuch_message_file_t *message, * contain a header line matching 'header'. */ const char * -notmuch_message_file_get_header (notmuch_message_file_t *message, +_notmuch_message_file_get_header (notmuch_message_file_t *message, const char *header); /* index.cc */ @@ -455,10 +458,10 @@ _notmuch_message_add_reply (notmuch_message_t *message, /* sha1.c */ char * -notmuch_sha1_of_string (const char *str); +_notmuch_sha1_of_string (const char *str); char * -notmuch_sha1_of_file (const char *filename); +_notmuch_sha1_of_file (const char *filename); /* string-list.c */ diff --git a/lib/notmuch.h b/lib/notmuch.h index 350bed8b..dae04164 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -159,6 +159,10 @@ typedef enum _notmuch_status { * The operation is not supported. */ NOTMUCH_STATUS_UNSUPPORTED_OPERATION, + /** + * The operation requires a database upgrade. + */ + NOTMUCH_STATUS_UPGRADE_REQUIRED, /** * Not an actual status value. Just a way to find out how many * valid status values there are. @@ -277,7 +281,7 @@ notmuch_database_open (const char *path, notmuch_database_t **database); /** - * Close the given notmuch database. + * Commit changes and close the given notmuch database. * * After notmuch_database_close has been called, calls to other * functions on objects derived from this database may either behave @@ -287,8 +291,23 @@ notmuch_database_open (const char *path, * * notmuch_database_close can be called multiple times. Later calls * have no effect. + * + * For writable databases, notmuch_database_close commits all changes + * to disk before closing the database. If the caller is currently in + * an atomic section (there was a notmuch_database_begin_atomic + * without a matching notmuch_database_end_atomic), this will discard + * changes made in that atomic section (but still commit changes made + * prior to entering the atomic section). + * + * Return value: + * + * NOTMUCH_STATUS_SUCCESS: Successfully closed the database. + * + * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred; the + * database has been closed but there are no guarantees the + * changes to the database, if any, have been flushed to disk. */ -void +notmuch_status_t notmuch_database_close (notmuch_database_t *database); /** @@ -317,8 +336,11 @@ notmuch_database_compact (const char* path, /** * Destroy the notmuch database, closing it if necessary and freeing * all associated resources. + * + * Return value as in notmuch_database_close if the database was open; + * notmuch_database_destroy itself has no failure modes. */ -void +notmuch_status_t notmuch_database_destroy (notmuch_database_t *database); /** @@ -337,22 +359,27 @@ unsigned int notmuch_database_get_version (notmuch_database_t *database); /** - * Does this database need to be upgraded before writing to it? + * Can the database be upgraded to a newer database version? * - * If this function returns TRUE then no functions that modify the - * database (notmuch_database_add_message, notmuch_message_add_tag, - * notmuch_directory_set_mtime, etc.) will work unless the function - * notmuch_database_upgrade is called successfully first. + * If this function returns TRUE, then the caller may call + * notmuch_database_upgrade to upgrade the database. If the caller + * does not upgrade an out-of-date database, then some functions may + * fail with NOTMUCH_STATUS_UPGRADE_REQUIRED. This always returns + * FALSE for a read-only database because there's no way to upgrade a + * read-only database. */ notmuch_bool_t notmuch_database_needs_upgrade (notmuch_database_t *database); /** - * Upgrade the current database. + * Upgrade the current database to the latest supported version. * - * After opening a database in read-write mode, the client should - * check if an upgrade is needed (notmuch_database_needs_upgrade) and - * if so, upgrade with this function before making any modifications. + * This ensures that all current notmuch functionality will be + * available on the database. After opening a database in read-write + * mode, it is recommended that clients check if an upgrade is needed + * (notmuch_database_needs_upgrade) and if so, upgrade with this + * function before making any modifications. If + * notmuch_database_needs_upgrade returns FALSE, this will be a no-op. * * The optional progress_notify callback can be used by the caller to * provide progress indication to the user. If non-NULL it will be @@ -427,6 +454,9 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch); * * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred; * directory not retrieved. + * + * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the + * database to use this function. */ notmuch_status_t notmuch_database_get_directory (notmuch_database_t *database, @@ -479,6 +509,9 @@ notmuch_database_get_directory (notmuch_database_t *database, * * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only * mode so no message can be added. + * + * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the + * database to use this function. */ notmuch_status_t notmuch_database_add_message (notmuch_database_t *database, @@ -509,6 +542,9 @@ notmuch_database_add_message (notmuch_database_t *database, * * NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in read-only * mode so no message can be removed. + * + * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the + * database to use this function. */ notmuch_status_t notmuch_database_remove_message (notmuch_database_t *database, @@ -564,6 +600,9 @@ notmuch_database_find_message (notmuch_database_t *database, * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating the message object * * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred + * + * NOTMUCH_STATUS_UPGRADE_REQUIRED: The caller must upgrade the + * database to use this function. */ notmuch_status_t notmuch_database_find_message_by_filename (notmuch_database_t *notmuch, @@ -941,6 +980,10 @@ notmuch_thread_get_matched_messages (notmuch_thread_t *thread); * authors of mail messages in the query results that belong to this * thread. * + * The string contains authors of messages matching the query first, then + * non-matched authors (with the two groups separated by '|'). Within + * each group, authors are ordered by date. + * * The returned string belongs to 'thread' and as such, should not be * modified by the caller and will only be valid for as long as the * thread is valid, (which is until notmuch_thread_destroy or until diff --git a/lib/sha1.c b/lib/sha1.c index cc481086..94060d57 100644 --- a/lib/sha1.c +++ b/lib/sha1.c @@ -50,7 +50,7 @@ _hex_of_sha1_digest (const unsigned char digest[SHA1_DIGEST_SIZE]) * should free() when finished. */ char * -notmuch_sha1_of_string (const char *str) +_notmuch_sha1_of_string (const char *str) { sha1_ctx sha1; unsigned char digest[SHA1_DIGEST_SIZE]; @@ -74,7 +74,7 @@ notmuch_sha1_of_string (const char *str) * file not found, etc.), this function returns NULL. */ char * -notmuch_sha1_of_file (const char *filename) +_notmuch_sha1_of_file (const char *filename) { FILE *file; #define BLOCK_SIZE 4096 diff --git a/lib/thread.cc b/lib/thread.cc index 8f53e122..8922403e 100644 --- a/lib/thread.cc +++ b/lib/thread.cc @@ -284,7 +284,7 @@ _thread_add_message (notmuch_thread_t *thread, } clean_author = _thread_cleanup_author (thread, author, from); _thread_add_author (thread, clean_author); - notmuch_message_set_author (message, clean_author); + _notmuch_message_set_author (message, clean_author); } g_object_unref (G_OBJECT (list)); } @@ -373,7 +373,7 @@ _thread_add_matched_message (notmuch_thread_t *thread, NOTMUCH_MESSAGE_FLAG_MATCH, 1); } - _thread_add_matched_author (thread, notmuch_message_get_author (hashed_message)); + _thread_add_matched_author (thread, _notmuch_message_get_author (hashed_message)); } static void diff --git a/notmuch-config.c b/notmuch-config.c index 4886d366..a564bcae 100644 --- a/notmuch-config.c +++ b/notmuch-config.c @@ -217,9 +217,10 @@ get_username_from_passwd_file (void *ctx) * These default configuration settings are determined as * follows: * - * database_path: $HOME/mail + * database_path: $MAILDIR, otherwise $HOME/mail * - * user_name: From /etc/passwd + * user_name: $NAME variable if set, otherwise + * read from /etc/passwd * * user_primary_mail: $EMAIL variable if set, otherwise * constructed from the username and @@ -282,16 +283,22 @@ notmuch_config_open (void *ctx, G_KEY_FILE_KEEP_COMMENTS, &error)) { - /* If create_new is true, then the caller is prepared for a - * default configuration file in the case of FILE NOT - * FOUND. Otherwise, any read failure is an error. - */ - if (create_new && - error->domain == G_FILE_ERROR && - error->code == G_FILE_ERROR_NOENT) - { - g_error_free (error); - config->is_new = TRUE; + if (error->domain == G_FILE_ERROR && error->code == G_FILE_ERROR_NOENT) { + /* If create_new is true, then the caller is prepared for a + * default configuration file in the case of FILE NOT + * FOUND. + */ + if (create_new) { + g_error_free (error); + config->is_new = TRUE; + } else { + fprintf (stderr, "Configuration file %s not found.\n" + "Try running 'notmuch setup' to create a configuration.\n", + config->filename); + talloc_free (config); + g_error_free (error); + return NULL; + } } else { @@ -322,14 +329,22 @@ notmuch_config_open (void *ctx, if (notmuch_config_get_database_path (config) == NULL) { - char *path = talloc_asprintf (config, "%s/mail", - getenv ("HOME")); + char *path = getenv ("MAILDIR"); + if (path) + path = talloc_strdup (config, path); + else + path = talloc_asprintf (config, "%s/mail", + getenv ("HOME")); notmuch_config_set_database_path (config, path); talloc_free (path); } if (notmuch_config_get_user_name (config) == NULL) { - char *name = get_name_from_passwd_file (config); + char *name = getenv ("NAME"); + if (name) + name = talloc_strdup (config, name); + else + name = get_name_from_passwd_file (config); notmuch_config_set_user_name (config, name); talloc_free (name); } diff --git a/notmuch-dump.c b/notmuch-dump.c index 887a2082..9c6ad7f4 100644 --- a/notmuch-dump.c +++ b/notmuch-dump.c @@ -212,7 +212,7 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[]) int ret; if (notmuch_database_open (notmuch_config_get_database_path (config), - NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much)) + NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much)) return EXIT_FAILURE; char *output_file_name = NULL; diff --git a/notmuch-insert.c b/notmuch-insert.c index 6752fc8d..7074077c 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -67,26 +67,30 @@ safe_gethostname (char *hostname, size_t len) static notmuch_bool_t sync_dir (const char *dir) { - notmuch_bool_t ret; - int fd; + int fd, r; fd = open (dir, O_RDONLY); if (fd == -1) { - fprintf (stderr, "Error: open() dir failed: %s\n", strerror (errno)); + fprintf (stderr, "Error: open %s: %s\n", dir, strerror (errno)); return FALSE; } - ret = (fsync (fd) == 0); - if (! ret) { - fprintf (stderr, "Error: fsync() dir failed: %s\n", strerror (errno)); - } + + r = fsync (fd); + if (r) + fprintf (stderr, "Error: fsync %s: %s\n", dir, strerror (errno)); + close (fd); - return ret; + + return r == 0; } -/* Check the specified folder name does not contain a directory - * component ".." to prevent writes outside of the Maildir hierarchy. */ +/* + * Check the specified folder name does not contain a directory + * component ".." to prevent writes outside of the Maildir + * hierarchy. Return TRUE on valid folder name, FALSE otherwise. + */ static notmuch_bool_t -check_folder_name (const char *folder) +is_valid_folder_name (const char *folder) { const char *p = folder; @@ -100,159 +104,147 @@ check_folder_name (const char *folder) } } -/* Make the given directory, succeeding if it already exists. */ +/* + * Make the given directory and its parents as necessary, using the + * given mode. Return TRUE on success, FALSE otherwise. Partial + * results are not cleaned up on errors. + */ static notmuch_bool_t -make_directory (char *path, int mode) +mkdir_recursive (const void *ctx, const char *path, int mode) { - notmuch_bool_t ret; - char *slash; + struct stat st; + int r; + char *parent = NULL, *slash; - if (mkdir (path, mode) != 0) - return (errno == EEXIST); + /* First check the common case: directory already exists. */ + r = stat (path, &st); + if (r == 0) { + if (! S_ISDIR (st.st_mode)) { + fprintf (stderr, "Error: '%s' is not a directory: %s\n", + path, strerror (EEXIST)); + return FALSE; + } - /* Sync the parent directory for durability. */ - ret = TRUE; - slash = strrchr (path, '/'); - if (slash) { - *slash = '\0'; - ret = sync_dir (path); - *slash = '/'; + return TRUE; + } else if (errno != ENOENT) { + fprintf (stderr, "Error: stat '%s': %s\n", path, strerror (errno)); + return FALSE; } - return ret; -} - -/* Make the given directory including its parent directories as necessary. - * Return TRUE on success, FALSE on error. */ -static notmuch_bool_t -make_directory_and_parents (char *path, int mode) -{ - struct stat st; - char *start; - char *end; - notmuch_bool_t ret; - /* First check the common case: directory already exists. */ - if (stat (path, &st) == 0) - return S_ISDIR (st.st_mode) ? TRUE : FALSE; - - for (start = path; *start != '\0'; start = end + 1) { - /* start points to the first unprocessed character. - * Find the next slash from start onwards. */ - end = strchr (start, '/'); - - /* If there are no more slashes then all the parent directories - * have been made. Now attempt to make the whole path. */ - if (end == NULL) - return make_directory (path, mode); - - /* Make the path up to the next slash, unless the current - * directory component is actually empty. */ - if (end > start) { - *end = '\0'; - ret = make_directory (path, mode); - *end = '/'; - if (! ret) - return FALSE; + /* mkdir parents, if any */ + slash = strrchr (path, '/'); + if (slash && slash != path) { + parent = talloc_strndup (ctx, path, slash - path); + if (! parent) { + fprintf (stderr, "Error: %s\n", strerror (ENOMEM)); + return FALSE; } + + if (! mkdir_recursive (ctx, parent, mode)) + return FALSE; } - return TRUE; + if (mkdir (path, mode)) { + fprintf (stderr, "Error: mkdir '%s': %s\n", path, strerror (errno)); + return FALSE; + } + + return parent ? sync_dir (parent) : TRUE; } -/* Create the given maildir folder, i.e. dir and its subdirectories - * 'cur', 'new', 'tmp'. */ +/* + * Create the given maildir folder, i.e. maildir and its + * subdirectories cur/new/tmp. Return TRUE on success, FALSE + * otherwise. Partial results are not cleaned up on errors. + */ static notmuch_bool_t -maildir_create_folder (void *ctx, const char *dir) +maildir_create_folder (const void *ctx, const char *maildir) { + const char *subdirs[] = { "cur", "new", "tmp" }; const int mode = 0700; char *subdir; - char *tail; - - /* Create 'cur' directory, including parent directories. */ - subdir = talloc_asprintf (ctx, "%s/cur", dir); - if (! subdir) { - fprintf (stderr, "Out of memory.\n"); - return FALSE; - } - if (! make_directory_and_parents (subdir, mode)) - return FALSE; - - tail = subdir + strlen (subdir) - 3; + unsigned int i; - /* Create 'new' directory. */ - strcpy (tail, "new"); - if (! make_directory (subdir, mode)) - return FALSE; + for (i = 0; i < ARRAY_SIZE (subdirs); i++) { + subdir = talloc_asprintf (ctx, "%s/%s", maildir, subdirs[i]); + if (! subdir) { + fprintf (stderr, "Error: %s\n", strerror (ENOMEM)); + return FALSE; + } - /* Create 'tmp' directory. */ - strcpy (tail, "tmp"); - if (! make_directory (subdir, mode)) - return FALSE; + if (! mkdir_recursive (ctx, subdir, mode)) + return FALSE; + } - talloc_free (subdir); return TRUE; } -/* Open a unique file in the 'tmp' sub-directory of dir. - * Returns the file descriptor on success, or -1 on failure. - * On success, file paths for the message in the 'tmp' and 'new' - * directories are returned via tmppath and newpath, - * and the path of the 'new' directory itself in newdir. */ -static int -maildir_open_tmp_file (void *ctx, const char *dir, - char **tmppath, char **newpath, char **newdir) +/* + * Generate a temporary file basename, no path, do not create an + * actual file. Return the basename, or NULL on errors. + */ +static char * +tempfilename (const void *ctx) { - pid_t pid; + char *filename; char hostname[256]; struct timeval tv; - char *filename; - int fd = -1; + pid_t pid; /* We follow the Dovecot file name generation algorithm. */ pid = getpid (); safe_gethostname (hostname, sizeof (hostname)); + gettimeofday (&tv, NULL); + + filename = talloc_asprintf (ctx, "%ld.M%ldP%d.%s", + tv.tv_sec, tv.tv_usec, pid, hostname); + if (! filename) + fprintf (stderr, "Error: %s\n", strerror (ENOMEM)); + + return filename; +} + +/* + * Create a unique temporary file in maildir/tmp, return fd and full + * path to file in *path_out, or -1 on errors (in which case *path_out + * is not touched). + */ +static int +maildir_mktemp (const void *ctx, const char *maildir, char **path_out) +{ + char *filename, *path; + int fd; + do { - gettimeofday (&tv, NULL); - filename = talloc_asprintf (ctx, "%ld.M%ldP%d.%s", - tv.tv_sec, tv.tv_usec, pid, hostname); - if (! filename) { - fprintf (stderr, "Out of memory\n"); + filename = tempfilename (ctx); + if (! filename) return -1; - } - *tmppath = talloc_asprintf (ctx, "%s/tmp/%s", dir, filename); - if (! *tmppath) { - fprintf (stderr, "Out of memory\n"); + path = talloc_asprintf (ctx, "%s/tmp/%s", maildir, filename); + if (! path) { + fprintf (stderr, "Error: %s\n", strerror (ENOMEM)); return -1; } - fd = open (*tmppath, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600); + fd = open (path, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600); } while (fd == -1 && errno == EEXIST); if (fd == -1) { - fprintf (stderr, "Error: opening %s: %s\n", *tmppath, strerror (errno)); + fprintf (stderr, "Error: open '%s': %s\n", path, strerror (errno)); return -1; } - *newdir = talloc_asprintf (ctx, "%s/new", dir); - *newpath = talloc_asprintf (ctx, "%s/new/%s", dir, filename); - if (! *newdir || ! *newpath) { - fprintf (stderr, "Out of memory\n"); - close (fd); - unlink (*tmppath); - return -1; - } - - talloc_free (filename); + *path_out = path; return fd; } -/* Copy the contents of standard input (fdin) into fdout. - * Returns TRUE if a non-empty file was written successfully. - * Otherwise, return FALSE. */ +/* + * Copy fdin to fdout, return TRUE on success, and FALSE on errors and + * empty input. + */ static notmuch_bool_t -copy_stdin (int fdin, int fdout) +copy_fd (int fdout, int fdin) { notmuch_bool_t empty = TRUE; @@ -291,111 +283,167 @@ copy_stdin (int fdin, int fdout) return (!interrupted && !empty); } -/* Add the specified message file to the notmuch database, applying tags. - * The file is renamed to encode notmuch tags as maildir flags. */ -static void -add_file_to_database (notmuch_database_t *notmuch, const char *path, - tag_op_list_t *tag_ops, notmuch_bool_t synchronize_flags) +/* + * Write fdin to a new temp file in maildir/tmp, return full path to + * the file, or NULL on errors. + */ +static char * +maildir_write_tmp (const void *ctx, int fdin, const char *maildir) { - notmuch_message_t *message; - notmuch_status_t status; + char *path; + int fdout; - status = notmuch_database_add_message (notmuch, path, &message); - switch (status) { - case NOTMUCH_STATUS_SUCCESS: - case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID: - break; - default: - case NOTMUCH_STATUS_FILE_NOT_EMAIL: - case NOTMUCH_STATUS_READ_ONLY_DATABASE: - case NOTMUCH_STATUS_XAPIAN_EXCEPTION: - case NOTMUCH_STATUS_OUT_OF_MEMORY: - case NOTMUCH_STATUS_FILE_ERROR: - case NOTMUCH_STATUS_NULL_POINTER: - case NOTMUCH_STATUS_TAG_TOO_LONG: - case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW: - case NOTMUCH_STATUS_UNBALANCED_ATOMIC: - case NOTMUCH_STATUS_LAST_STATUS: - fprintf (stderr, "Error: failed to add `%s' to notmuch database: %s\n", - path, notmuch_status_to_string (status)); - return; - } + fdout = maildir_mktemp (ctx, maildir, &path); + if (fdout < 0) + return NULL; - if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) { - /* Don't change tags of an existing message. */ - if (synchronize_flags) { - status = notmuch_message_tags_to_maildir_flags (message); - if (status != NOTMUCH_STATUS_SUCCESS) - fprintf (stderr, "Error: failed to sync tags to maildir flags\n"); - } - } else { - tag_op_flag_t flags = synchronize_flags ? TAG_FLAG_MAILDIR_SYNC : 0; + if (! copy_fd (fdout, fdin)) + goto FAIL; - tag_op_list_apply (message, tag_ops, flags); + if (fsync (fdout)) { + fprintf (stderr, "Error: fsync '%s': %s\n", path, strerror (errno)); + goto FAIL; } - notmuch_message_destroy (message); + close (fdout); + + return path; + +FAIL: + close (fdout); + unlink (path); + + return NULL; } -static notmuch_bool_t -insert_message (void *ctx, notmuch_database_t *notmuch, int fdin, - const char *dir, tag_op_list_t *tag_ops, - notmuch_bool_t synchronize_flags) +/* + * Write fdin to a new file in maildir/new, using an intermediate temp + * file in maildir/tmp, return full path to the new file, or NULL on + * errors. + */ +static char * +maildir_write_new (const void *ctx, int fdin, const char *maildir) { - char *tmppath; - char *newpath; - char *newdir; - int fdout; - char *cleanup_path; - - fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath, &newdir); - if (fdout < 0) - return FALSE; + char *cleanpath, *tmppath, *newpath, *newdir; - cleanup_path = tmppath; + tmppath = maildir_write_tmp (ctx, fdin, maildir); + if (! tmppath) + return NULL; + cleanpath = tmppath; - if (! copy_stdin (fdin, fdout)) + newpath = talloc_strdup (ctx, tmppath); + if (! newpath) { + fprintf (stderr, "Error: %s\n", strerror (ENOMEM)); goto FAIL; + } + + /* sanity checks needed? */ + memcpy (newpath + strlen (maildir) + 1, "new", 3); - if (fsync (fdout) != 0) { - fprintf (stderr, "Error: fsync failed: %s\n", strerror (errno)); + if (rename (tmppath, newpath)) { + fprintf (stderr, "Error: rename '%s' '%s': %s\n", + tmppath, newpath, strerror (errno)); goto FAIL; } + cleanpath = newpath; - close (fdout); - fdout = -1; - - /* Atomically move the new message file from the Maildir 'tmp' directory - * to the 'new' directory. We follow the Dovecot recommendation to - * simply use rename() instead of link() and unlink(). - * See also: http://wiki.dovecot.org/MailboxFormat/Maildir#Mail_delivery - */ - if (rename (tmppath, newpath) != 0) { - fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno)); + newdir = talloc_asprintf (ctx, "%s/%s", maildir, "new"); + if (! newdir) { + fprintf (stderr, "Error: %s\n", strerror (ENOMEM)); goto FAIL; } - cleanup_path = newpath; - if (! sync_dir (newdir)) goto FAIL; - /* Even if adding the message to the notmuch database fails, - * the message is on disk and we consider the delivery completed. */ - add_file_to_database (notmuch, newpath, tag_ops, synchronize_flags); + return newpath; - return TRUE; +FAIL: + unlink (cleanpath); + + return NULL; +} + +/* + * Add the specified message file to the notmuch database, applying + * tags in tag_ops. If synchronize_flags is TRUE, the tags are + * synchronized to maildir flags (which may result in message file + * rename). + * + * Return NOTMUCH_STATUS_SUCCESS on success, errors otherwise. If keep + * is TRUE, errors in tag changes and flag syncing are ignored and + * success status is returned; otherwise such errors cause the message + * to be removed from the database. Failure to add the message to the + * database results in error status regardless of keep. + */ +static notmuch_status_t +add_file (notmuch_database_t *notmuch, const char *path, tag_op_list_t *tag_ops, + notmuch_bool_t synchronize_flags, notmuch_bool_t keep) +{ + notmuch_message_t *message; + notmuch_status_t status; + + status = notmuch_database_add_message (notmuch, path, &message); + if (status == NOTMUCH_STATUS_SUCCESS) { + status = tag_op_list_apply (message, tag_ops, 0); + if (status) { + fprintf (stderr, "%s: failed to apply tags to file '%s': %s\n", + keep ? "Warning" : "Error", + path, notmuch_status_to_string (status)); + goto DONE; + } + } else if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) { + status = NOTMUCH_STATUS_SUCCESS; + } else if (status == NOTMUCH_STATUS_FILE_NOT_EMAIL) { + fprintf (stderr, "Error: delivery of non-mail file: '%s'\n", path); + goto FAIL; + } else { + fprintf (stderr, "Error: failed to add '%s' to notmuch database: %s\n", + path, notmuch_status_to_string (status)); + goto FAIL; + } + + if (synchronize_flags) { + status = notmuch_message_tags_to_maildir_flags (message); + if (status != NOTMUCH_STATUS_SUCCESS) + fprintf (stderr, "%s: failed to sync tags to maildir flags for '%s': %s\n", + keep ? "Warning" : "Error", + path, notmuch_status_to_string (status)); + + /* + * Note: Unfortunately a failed maildir flag sync might + * already have renamed the file, in which case the cleanup + * path may fail. + */ + } + + DONE: + notmuch_message_destroy (message); + + if (status) { + if (keep) { + status = NOTMUCH_STATUS_SUCCESS; + } else { + notmuch_status_t cleanup_status; + + cleanup_status = notmuch_database_remove_message (notmuch, path); + if (cleanup_status != NOTMUCH_STATUS_SUCCESS && + cleanup_status != NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) { + fprintf (stderr, "Warning: failed to remove '%s' from database " + "after errors: %s. Please run 'notmuch new' to fix.\n", + path, notmuch_status_to_string (cleanup_status)); + } + } + } FAIL: - if (fdout >= 0) - close (fdout); - unlink (cleanup_path); - return FALSE; + return status; } int notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) { + notmuch_status_t status, close_status; notmuch_database_t *notmuch; struct sigaction action; const char *db_path; @@ -405,15 +453,17 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) char *query_string = NULL; const char *folder = NULL; notmuch_bool_t create_folder = FALSE; + notmuch_bool_t keep = FALSE; notmuch_bool_t synchronize_flags; const char *maildir; + char *newpath; int opt_index; unsigned int i; - notmuch_bool_t ret; notmuch_opt_desc_t options[] = { { NOTMUCH_OPT_STRING, &folder, "folder", 0, 0 }, { NOTMUCH_OPT_BOOLEAN, &create_folder, "create-folder", 0, 0 }, + { NOTMUCH_OPT_BOOLEAN, &keep, "keep", 0, 0 }, { NOTMUCH_OPT_END, 0, 0, 0, 0 } }; @@ -456,8 +506,8 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) if (folder == NULL) { maildir = db_path; } else { - if (! check_folder_name (folder)) { - fprintf (stderr, "Error: bad folder name: %s\n", folder); + if (! is_valid_folder_name (folder)) { + fprintf (stderr, "Error: invalid folder name: '%s'\n", folder); return EXIT_FAILURE; } maildir = talloc_asprintf (config, "%s/%s", db_path, folder); @@ -465,11 +515,8 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) fprintf (stderr, "Out of memory\n"); return EXIT_FAILURE; } - if (create_folder && ! maildir_create_folder (config, maildir)) { - fprintf (stderr, "Error: creating maildir %s: %s\n", - maildir, strerror (errno)); + if (create_folder && ! maildir_create_folder (config, maildir)) return EXIT_FAILURE; - } } /* Setup our handler for SIGINT. We do not set SA_RESTART so that copying @@ -484,10 +531,39 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much)) return EXIT_FAILURE; - ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops, - synchronize_flags); + /* Write the message to the Maildir new directory. */ + newpath = maildir_write_new (config, STDIN_FILENO, maildir); + if (! newpath) { + notmuch_database_destroy (notmuch); + return EXIT_FAILURE; + } + + /* Index the message. */ + status = add_file (notmuch, newpath, tag_ops, synchronize_flags, keep); + + /* Commit changes. */ + close_status = notmuch_database_destroy (notmuch); + if (close_status) { + /* Hold on to the first error, if any. */ + if (! status) + status = close_status; + fprintf (stderr, "%s: failed to commit database changes: %s\n", + keep ? "Warning" : "Error", + notmuch_status_to_string (close_status)); + } - notmuch_database_destroy (notmuch); + if (status) { + if (keep) { + status = NOTMUCH_STATUS_SUCCESS; + } else { + /* If maildir flag sync failed, this might fail. */ + if (unlink (newpath)) { + fprintf (stderr, "Warning: failed to remove '%s' from maildir " + "after errors: %s. Please run 'notmuch new' to fix.\n", + newpath, strerror (errno)); + } + } + } - return ret ? EXIT_SUCCESS : EXIT_FAILURE; + return status ? EXIT_FAILURE : EXIT_SUCCESS; } diff --git a/notmuch-new.c b/notmuch-new.c index d269c7cd..ddf42c14 100644 --- a/notmuch-new.c +++ b/notmuch-new.c @@ -923,6 +923,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]) notmuch_bool_t timer_is_active = FALSE; notmuch_bool_t no_hooks = FALSE; notmuch_bool_t quiet = FALSE, verbose = FALSE; + notmuch_status_t status; add_files_state.verbosity = VERBOSITY_NORMAL; add_files_state.debug = FALSE; @@ -1019,12 +1020,18 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]) } gettimeofday (&add_files_state.tv_start, NULL); - notmuch_database_upgrade (notmuch, - add_files_state.verbosity >= VERBOSITY_NORMAL ? upgrade_print_progress : NULL, - &add_files_state); + status = notmuch_database_upgrade ( + notmuch, + add_files_state.verbosity >= VERBOSITY_NORMAL ? upgrade_print_progress : NULL, + &add_files_state); + if (status) { + printf ("Upgrade failed: %s\n", + notmuch_status_to_string (status)); + notmuch_database_destroy (notmuch); + return EXIT_FAILURE; + } if (add_files_state.verbosity >= VERBOSITY_NORMAL) - printf ("Your notmuch database has now been upgraded to database format version %u.\n", - notmuch_database_get_version (notmuch)); + printf ("Your notmuch database has now been upgraded.\n"); } add_files_state.total_files = 0; @@ -1091,7 +1098,6 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]) } for (f = add_files_state.directory_mtimes->head; f && !interrupted; f = f->next) { - notmuch_status_t status; notmuch_directory_t *directory; status = notmuch_database_get_directory (notmuch, f->filename, &directory); if (status == NOTMUCH_STATUS_SUCCESS && directory) { diff --git a/performance-test/Makefile.local b/performance-test/Makefile.local index d97e56d9..3469aa3d 100644 --- a/performance-test/Makefile.local +++ b/performance-test/Makefile.local @@ -40,4 +40,5 @@ download-corpus: wget -O ${TXZFILE} ${DEFAULT_URL} CLEAN := $(CLEAN) $(dir)/tmp.* $(dir)/log.* -DISTCLEAN := $(dir)/corpus $(dir)/notmuch.cache.* +DISTCLEAN := $(DISTCLEAN) $(dir)/corpus $(dir)/notmuch.cache.* +DATACLEAN := $(DATACLEAN) $(TXZFILE) diff --git a/test/.gitignore b/test/.gitignore index 97e02487..0f7d5bfc 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -1,9 +1,10 @@ -test-results -corpus.mail -smtp-dummy -symbol-test arg-test +corpus.mail hex-xcode -random-corpus parse-time +random-corpus +smtp-dummy +symbol-test +make-db-version +test-results tmp.* diff --git a/test/Makefile.local b/test/Makefile.local index d622eafe..a2d58fc1 100644 --- a/test/Makefile.local +++ b/test/Makefile.local @@ -35,30 +35,23 @@ $(dir)/symbol-test: $(dir)/symbol-test.o lib/$(LINKER_NAME) $(dir)/parse-time: $(dir)/parse-time.o parse-time-string/parse-time-string.o $(call quiet,CC) $^ -o $@ -$(dir)/have-compact: Makefile.config -ifeq ($(HAVE_XAPIAN_COMPACT),1) - ln -sf /bin/true $@ -else - ln -sf /bin/false $@ -endif - -$(dir)/have-man: Makefile.config -ifeq ($(HAVE_SPHINX)$(HAVE_RST2MAN),00) - ln -sf /bin/false $@ -else - ln -sf /bin/true $@ -endif +$(dir)/make-db-version: $(dir)/make-db-version.o + $(call quiet,CXX) $^ -o $@ $(XAPIAN_LDFLAGS) .PHONY: test check -TEST_BINARIES=$(dir)/arg-test \ - $(dir)/have-compact \ - $(dir)/have-man \ - $(dir)/hex-xcode \ - $(dir)/random-corpus \ - $(dir)/parse-time \ - $(dir)/smtp-dummy \ - $(dir)/symbol-test +test_main_srcs=$(dir)/arg-test.c \ + $(dir)/hex-xcode.c \ + $(dir)/random-corpus.c \ + $(dir)/parse-time.c \ + $(dir)/smtp-dummy.c \ + $(dir)/symbol-test.cc \ + $(dir)/make-db-version.cc \ + +test_srcs=$(test_main_srcs) $(dir)/database-test.c + +TEST_BINARIES := $(test_main_srcs:.c=) +TEST_BINARIES := $(TEST_BINARIES:.cc=) test-binaries: $(TEST_BINARIES) @@ -67,7 +60,7 @@ test: all test-binaries check: test -SRCS := $(SRCS) $(smtp_dummy_srcs) +SRCS := $(SRCS) $(test_srcs) CLEAN += $(TEST_BINARIES) $(addsuffix .o,$(TEST_BINARIES)) \ $(dir)/database-test.o \ $(dir)/corpus.mail $(dir)/test-results $(dir)/tmp.* diff --git a/test/T000-basic.sh b/test/T000-basic.sh index ebbb6d2e..ef642457 100755 --- a/test/T000-basic.sh +++ b/test/T000-basic.sh @@ -91,4 +91,8 @@ test_expect_equal \ "$(dirname ${TEST_DIRECTORY})" \ "$(echo $PATH|cut -f1 -d: | sed -e 's,/test/valgrind/bin$,,')" +test_begin_subtest 'notmuch is compiled with debugging symbols' +readelf --sections $(which notmuch) | grep \.debug +test_expect_equal 0 $? + test_done diff --git a/test/T010-help-test.sh b/test/T010-help-test.sh index 77410bc5..caf8bdb0 100755 --- a/test/T010-help-test.sh +++ b/test/T010-help-test.sh @@ -7,7 +7,7 @@ test_expect_success 'notmuch --help' 'notmuch --help' test_expect_success 'notmuch help' 'notmuch help' test_expect_success 'notmuch --version' 'notmuch --version' -if ${TEST_DIRECTORY}/have-man; then +if [ $NOTMUCH_HAVE_MAN -eq 1 ]; then test_expect_success 'notmuch --help tag' 'notmuch --help tag' test_expect_success 'notmuch help tag' 'notmuch help tag' else diff --git a/test/T020-compact.sh b/test/T020-compact.sh index 77bb9632..507f7698 100755 --- a/test/T020-compact.sh +++ b/test/T020-compact.sh @@ -10,7 +10,7 @@ notmuch tag +tag1 \* notmuch tag +tag2 subject:Two notmuch tag -tag1 +tag3 subject:Three -if ! ${TEST_DIRECTORY}/have-compact; then +if [ $NOTMUCH_HAVE_XAPIAN_COMPACT -eq 0 ]; then test_begin_subtest "Compact unsupported: error message" output=$(notmuch compact --quiet 2>&1) test_expect_equal "$output" "notmuch was compiled against a xapian version lacking compaction support. diff --git a/test/T040-setup.sh b/test/T040-setup.sh index 124ef1c8..b1972e70 100755 --- a/test/T040-setup.sh +++ b/test/T040-setup.sh @@ -3,6 +3,12 @@ test_description='"notmuch setup"' . ./test-lib.sh +test_begin_subtest "Notmuch new without a config suggests notmuch setup" +output=$(notmuch --config=new-notmuch-config new 2>&1) +test_expect_equal "$output" "\ +Configuration file new-notmuch-config not found. +Try running 'notmuch setup' to create a configuration." + test_begin_subtest "Create a new config interactively" notmuch --config=new-notmuch-config > /dev/null < index-file-$code.gdb +file notmuch +set breakpoint pending on +break notmuch_database_add_message +commands +return NOTMUCH_STATUS_$code +continue +end +run +EOF +test_begin_subtest "error exit when add_message returns $code" +gdb --batch-silent --return-child-result -x index-file-$code.gdb \ + --args notmuch insert < $gen_msg_filename +test_expect_equal $? 1 + +test_begin_subtest "success exit with --keep when add_message returns $code" +gdb --batch-silent --return-child-result -x index-file-$code.gdb \ + --args notmuch insert --keep < $gen_msg_filename +test_expect_equal $? 0 +done + test_done diff --git a/test/T150-tagging.sh b/test/T150-tagging.sh index dc118f33..45471ac8 100755 --- a/test/T150-tagging.sh +++ b/test/T150-tagging.sh @@ -247,8 +247,8 @@ ${TEST_DIRECTORY}/random-corpus --config-path=${NOTMUCH_CONFIG} \ notmuch dump --format=batch-tag | sed 's/^.* -- /+common_tag -- /' | \ sort > EXPECTED -notmuch dump --format=batch-tag | sed 's/^.* -- / -- /' | \ - notmuch restore --format=batch-tag +notmuch dump --format=batch-tag | sed 's/^.* -- / -- /' > INTERMEDIATE_STEP +notmuch restore --format=batch-tag < INTERMEDIATE_STEP notmuch tag --batch < EXPECTED diff --git a/test/T260-thread-order.sh b/test/T260-thread-order.sh index 6c3a4b3f..b435d79f 100755 --- a/test/T260-thread-order.sh +++ b/test/T260-thread-order.sh @@ -2,31 +2,75 @@ test_description="threading when messages received out of order" . ./test-lib.sh -test_begin_subtest "Adding initial child message" -generate_message [body]=foo "[in-reply-to]=\" [subject]=brokenthreadtest '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' -output=$(NOTMUCH_NEW) -test_expect_equal "$output" "Added 1 new message to the database." +# Generate all single-root four message thread structures. We'll use +# this for multiple tests below. +THREADS=$(python ${TEST_DIRECTORY}/gen-threads.py 4) +nthreads=$(wc -l <<< "$THREADS") -test_begin_subtest "Searching returns the message" -output=$(notmuch search foo | notmuch_search_sanitize) -test_expect_equal "$output" "thread:XXX 2000-01-01 [1/1] Notmuch Test Suite; brokenthreadtest (inbox unread)" +test_begin_subtest "Messages with one parent get linked in all delivery orders" +# In the first variant, this delivers messages that reference only +# their immediate parent. Hence, we should only expect threads to be +# fully joined at the end. +for ((n = 0; n < 4; n++)); do + # Deliver the n'th message of every thread + thread=0 + while read -a parents; do + parent=${parents[$n]} + generate_message \ + [id]=m$n@t$thread [in-reply-to]="\" \ + [subject]=p$thread [from]=m$n + thread=$((thread + 1)) + done <<< "$THREADS" + notmuch new > /dev/null +done +output=$(notmuch search --sort=newest-first '*' | notmuch_search_sanitize) +expected=$(for ((i = 0; i < $nthreads; i++)); do + echo "thread:XXX 2001-01-05 [4/4] m3, m2, m1, m0; p$i (inbox unread)" + done) +test_expect_equal "$output" "$expected" -test_begin_subtest "Adding second child message" -generate_message [body]=foo "[in-reply-to]=\" [subject]=brokenthreadtest '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' -output=$(NOTMUCH_NEW) -test_expect_equal "$output" "Added 1 new message to the database." +test_begin_subtest "Messages with all parents get linked in all delivery orders" +test_subtest_known_broken +# Here we do the same thing as the previous test, but each message +# references all of its parents. Since every message references the +# root of the thread, each thread should always be fully joined. This +# is currently broken because of the bug detailed in +# id:8738h7kv2q.fsf@qmul.ac.uk. +rm ${MAIL_DIR}/* +notmuch new > /dev/null +output="" +expected="" +for ((n = 0; n < 4; n++)); do + # Deliver the n'th message of every thread + thread=0 + while read -a parents; do + references="" + parent=${parents[$n]} + while [[ $parent != None ]]; do + references=" $references" + parent=${parents[$parent]} + done -test_begin_subtest "Searching returns both messages in one thread" -output=$(notmuch search foo | notmuch_search_sanitize) -test_expect_equal "$output" "thread:XXX 2000-01-01 [2/2] Notmuch Test Suite; brokenthreadtest (inbox unread)" + generate_message \ + [id]=m$n@t$thread [references]="'$references'" \ + [subject]=p$thread [from]=m$n + thread=$((thread + 1)) + done <<< "$THREADS" + notmuch new > /dev/null -test_begin_subtest "Adding parent message" -generate_message [body]=foo [id]=parent-id [subject]=brokenthreadtest '[date]="Sat, 01 Jan 2000 12:00:00 -0000"' -output=$(NOTMUCH_NEW) -test_expect_equal "$output" "Added 1 new message to the database." + output="$output +$(notmuch search --sort=newest-first '*' | notmuch_search_sanitize)" -test_begin_subtest "Searching returns all three messages in one thread" -output=$(notmuch search foo | notmuch_search_sanitize) -test_expect_equal "$output" "thread:XXX 2000-01-01 [3/3] Notmuch Test Suite; brokenthreadtest (inbox unread)" + # Construct expected output + template="thread:XXX 2001-01-05 [$((n+1))/$((n+1))]" + for ((m = n; m > 0; m--)); do + template="$template m$m," + done + expected="$expected +$(for ((i = 0; i < $nthreads; i++)); do + echo "$template m0; p$i (inbox unread)" + done)" +done +test_expect_equal "$output" "$expected" test_done diff --git a/test/T310-emacs.sh b/test/T310-emacs.sh index af6b2126..d72799b4 100755 --- a/test/T310-emacs.sh +++ b/test/T310-emacs.sh @@ -483,7 +483,7 @@ test_expect_equal_file OUTPUT EXPECTED test_begin_subtest "Reply within emacs to an html-only message" add_message '[content-type]="text/html"' \ '[body]="Hi,
This is an HTML test message.

OK?"' -test_emacs "(let ((message-hidden-headers '()) (mm-text-html-renderer 'html2text)) +test_emacs "(let ((message-hidden-headers '())) (notmuch-show \"id:${gen_msg_id}\") (notmuch-show-reply) (test-output))" diff --git a/test/T455-emacs-charsets.sh b/test/T455-emacs-charsets.sh new file mode 100755 index 00000000..3078f9c9 --- /dev/null +++ b/test/T455-emacs-charsets.sh @@ -0,0 +1,140 @@ +#!/usr/bin/env bash + +test_description="emacs notmuch-show charset handling" +. ./test-lib.sh + + +UTF8_YEN=$'\xef\xbf\xa5' +BIG5_YEN=$'\xa2\x44' + +# Add four messages with unusual encoding requirements: +# +# 1) text/plain in quoted-printable big5 +generate_message \ + [id]=test-plain@example.com \ + '[content-type]="text/plain; charset=big5"' \ + '[content-transfer-encoding]=quoted-printable' \ + '[body]="Yen: =A2=44"' + +# 2) text/plain in 8bit big5 +generate_message \ + [id]=test-plain-8bit@example.com \ + '[content-type]="text/plain; charset=big5"' \ + '[content-transfer-encoding]=8bit' \ + '[body]="Yen: '$BIG5_YEN'"' + +# 3) text/html in quoted-printable big5 +generate_message \ + [id]=test-html@example.com \ + '[content-type]="text/html; charset=big5"' \ + '[content-transfer-encoding]=quoted-printable' \ + '[body]="Yen: =A2=44"' + +# 4) application/octet-stream in quoted-printable of big5 text +generate_message \ + [id]=test-binary@example.com \ + '[content-type]="application/octet-stream"' \ + '[content-transfer-encoding]=quoted-printable' \ + '[body]="Yen: =A2=44"' + +notmuch new > /dev/null + +# Test rendering + +test_begin_subtest "Text parts are decoded when rendering" +test_emacs '(notmuch-show "id:test-plain@example.com") + (test-visible-output "OUTPUT.raw")' +awk 'show {print} /^$/ {show=1}' < OUTPUT.raw > OUTPUT +cat <EXPECTED +Yen: $UTF8_YEN +EOF +test_expect_equal_file OUTPUT EXPECTED + +test_begin_subtest "8bit text parts are decoded when rendering" +test_emacs '(notmuch-show "id:test-plain-8bit@example.com") + (test-visible-output "OUTPUT.raw")' +awk 'show {print} /^$/ {show=1}' < OUTPUT.raw > OUTPUT +cat <EXPECTED +Yen: $UTF8_YEN +EOF +test_expect_equal_file OUTPUT EXPECTED + +test_begin_subtest "HTML parts are decoded when rendering" +test_emacs '(notmuch-show "id:test-html@example.com") + (test-visible-output "OUTPUT.raw")' +awk 'show {print} /^$/ {show=1}' < OUTPUT.raw > OUTPUT +cat <EXPECTED +[ text/html ] +Yen: $UTF8_YEN +EOF +test_expect_equal_file OUTPUT EXPECTED + +# Test saving + +test_begin_subtest "Text parts are not decoded when saving" +rm -f part +test_emacs '(notmuch-show "id:test-plain@example.com") + (search-forward "Yen") + (let ((standard-input "\"part\"")) + (notmuch-show-save-part))' +cat <EXPECTED +Yen: $BIG5_YEN +EOF +test_expect_equal_file part EXPECTED + +test_begin_subtest "8bit text parts are not decoded when saving" +rm -f part +test_emacs '(notmuch-show "id:test-plain-8bit@example.com") + (search-forward "Yen") + (let ((standard-input "\"part\"")) + (notmuch-show-save-part))' +cat <EXPECTED +Yen: $BIG5_YEN +EOF +test_expect_equal_file part EXPECTED + +test_begin_subtest "HTML parts are not decoded when saving" +rm -f part +test_emacs '(notmuch-show "id:test-html@example.com") + (search-forward "Yen") + (let ((standard-input "\"part\"")) + (notmuch-show-save-part))' +cat <EXPECTED +Yen: $BIG5_YEN +EOF +test_expect_equal_file part EXPECTED + +test_begin_subtest "Binary parts are not decoded when saving" +rm -f part +test_emacs '(notmuch-show "id:test-binary@example.com") + (search-forward "application/") + (let ((standard-input "\"part\"")) + (notmuch-show-save-part))' +cat <EXPECTED +Yen: $BIG5_YEN +EOF +test_expect_equal_file part EXPECTED + +# Test message viewing + +test_begin_subtest "Text message are not decoded when viewing" +test_emacs '(notmuch-show "id:test-plain@example.com") + (notmuch-show-view-raw-message) + (test-visible-output "OUTPUT.raw")' +awk 'show {print} /^$/ {show=1}' < OUTPUT.raw > OUTPUT +cat <EXPECTED +Yen: =A2=44 +EOF +test_expect_equal_file OUTPUT EXPECTED + +test_begin_subtest "8bit text message are not decoded when viewing" +test_emacs '(notmuch-show "id:test-plain-8bit@example.com") + (notmuch-show-view-raw-message) + (test-visible-output "OUTPUT.raw")' +awk 'show {print} /^$/ {show=1}' < OUTPUT.raw > OUTPUT +cat <EXPECTED +Yen: $BIG5_YEN +EOF +test_expect_equal_file OUTPUT EXPECTED + +test_done diff --git a/test/T510-thread-replies.sh b/test/T510-thread-replies.sh index eeb70d06..1392fbed 100755 --- a/test/T510-thread-replies.sh +++ b/test/T510-thread-replies.sh @@ -137,5 +137,32 @@ expected='[[[{"id": "foo@four.com", "match": true, "excluded": false, expected=`echo "$expected" | notmuch_json_show_sanitize` test_expect_equal_json "$output" "$expected" +test_begin_subtest "Ignore garbage at the end of References" +add_message '[id]="foo@five.com"' \ + '[subject]="five"' +add_message '[id]="bar@five.com"' \ + '[references]=" (garbage)"' \ + '[subject]="not-five"' +output=$(notmuch show --format=json 'subject:five' | notmuch_json_show_sanitize) +expected='[[[{"id": "XXXXX", "match": true, "excluded": false, + "filename": "YYYYY", "timestamp": 42, "date_relative": "2001-01-05", + "tags": ["inbox", "unread"], "headers": {"Subject": "five", + "From": "Notmuch Test Suite ", + "To": "Notmuch Test Suite ", + "Date": "GENERATED_DATE"}, "body": [{"id": 1, + "content-type": "text/plain", + "content": "This is just a test message (#10)\n"}]}, + [[{"id": "XXXXX", "match": true, "excluded": false, + "filename": "YYYYY", "timestamp": 42, "date_relative": "2001-01-05", + "tags": ["inbox", "unread"], + "headers": {"Subject": "not-five", + "From": "Notmuch Test Suite ", + "To": "Notmuch Test Suite ", + "Date": "GENERATED_DATE"}, + "body": [{"id": 1, "content-type": "text/plain", + "content": "This is just a test message (#11)\n"}]}, []]]]]]' +expected=`echo "$expected" | notmuch_json_show_sanitize` +test_expect_equal_json "$output" "$expected" + test_done diff --git a/test/T530-upgrade.sh b/test/T530-upgrade.sh index 7d5d5aa8..c4c4ac8b 100755 --- a/test/T530-upgrade.sh +++ b/test/T530-upgrade.sh @@ -33,7 +33,7 @@ test_expect_equal "$output" "\ Welcome to a new version of notmuch! Your database will now be upgraded. This process is safe to interrupt. Backing up tags to FILENAME -Your notmuch database has now been upgraded to database format version 2. +Your notmuch database has now been upgraded. No new mail." test_begin_subtest "tag backup matches pre-upgrade dump" diff --git a/test/T550-db-features.sh b/test/T550-db-features.sh new file mode 100755 index 00000000..5569768c --- /dev/null +++ b/test/T550-db-features.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +test_description="database version and feature compatibility" + +. ./test-lib.sh + +test_begin_subtest "future database versions abort open" +${TEST_DIRECTORY}/make-db-version ${MAIL_DIR} 9999 "" +output=$(notmuch search x 2>&1 | sed 's/\(database at\) .*/\1 FILENAME/') +rm -rf ${MAIL_DIR}/.notmuch +test_expect_equal "$output" "\ +Error: Notmuch database at FILENAME + has a newer database format version (9999) than supported by this + version of notmuch (3)." + +test_begin_subtest "unknown 'rw' feature aborts read/write open" +${TEST_DIRECTORY}/make-db-version ${MAIL_DIR} 3 $'test feature\trw' +output=$(notmuch new 2>&1 | sed 's/\(database at\) .*/\1 FILENAME/') +rm -rf ${MAIL_DIR}/.notmuch +test_expect_equal "$output" "\ +Error: Notmuch database at FILENAME + requires features (test feature) + not supported by this version of notmuch." + +test_begin_subtest "unknown 'rw' feature aborts read-only open" +${TEST_DIRECTORY}/make-db-version ${MAIL_DIR} 3 $'test feature\trw' +output=$(notmuch search x 2>&1 | sed 's/\(database at\) .*/\1 FILENAME/') +rm -rf ${MAIL_DIR}/.notmuch +test_expect_equal "$output" "\ +Error: Notmuch database at FILENAME + requires features (test feature) + not supported by this version of notmuch." + +test_begin_subtest "unknown 'w' feature aborts read/write open" +${TEST_DIRECTORY}/make-db-version ${MAIL_DIR} 3 $'test feature\tw' +output=$(notmuch new 2>&1 | sed 's/\(database at\) .*/\1 FILENAME/') +rm -rf ${MAIL_DIR}/.notmuch +test_expect_equal "$output" "\ +Error: Notmuch database at FILENAME + requires features (test feature) + not supported by this version of notmuch." + +test_begin_subtest "unknown 'w' feature does not abort read-only open" +${TEST_DIRECTORY}/make-db-version ${MAIL_DIR} 3 $'test feature\tw' +output=$(notmuch search x 2>&1 | sed 's/\(database at\) .*/\1 FILENAME/') +rm -rf ${MAIL_DIR}/.notmuch +test_expect_equal "$output" "" + +test_done diff --git a/test/emacs.expected-output/notmuch-hello b/test/emacs.expected-output/notmuch-hello index 2d698917..9ba4cfc1 100644 --- a/test/emacs.expected-output/notmuch-hello +++ b/test/emacs.expected-output/notmuch-hello @@ -2,7 +2,7 @@ Saved searches: [edit] - 52 inbox 52 unread + 52 inbox 52 unread 52 all mail Search: . diff --git a/test/emacs.expected-output/notmuch-hello-long-names b/test/emacs.expected-output/notmuch-hello-long-names index 486d0d9a..1c8d6eb6 100644 --- a/test/emacs.expected-output/notmuch-hello-long-names +++ b/test/emacs.expected-output/notmuch-hello-long-names @@ -2,7 +2,7 @@ Saved searches: [edit] - 52 inbox 52 unread + 52 inbox 52 unread 52 all mail Search: . diff --git a/test/gen-threads.py b/test/gen-threads.py new file mode 100644 index 00000000..9fbb8474 --- /dev/null +++ b/test/gen-threads.py @@ -0,0 +1,33 @@ +# Generate all possible single-root message thread structures of size +# argv[1]. Each output line is a thread structure, where the n'th +# field is either a number giving the parent of message n or "None" +# for the root. + +import sys +from itertools import chain, combinations + +def subsets(s): + return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) + +nodes = set(range(int(sys.argv[1]))) + +# Queue of (tree, free, to_expand) where tree is a {node: parent} +# dictionary, free is a set of unattached nodes, and to_expand is +# itself a queue of nodes in the tree that need to be expanded. +# The queue starts with all single-node trees. +queue = [({root: None}, nodes - {root}, (root,)) for root in nodes] + +# Process queue +while queue: + tree, free, to_expand = queue.pop() + + if len(to_expand) == 0: + # Only print full-sized trees + if len(free) == 0: + print(" ".join(map(str, [msg[1] for msg in sorted(tree.items())]))) + else: + # Expand node to_expand[0] with each possible set of children + for children in subsets(free): + ntree = dict(tree, **{child: to_expand[0] for child in children}) + nfree = free.difference(children) + queue.append((ntree, nfree, to_expand[1:] + tuple(children))) diff --git a/test/make-db-version.cc b/test/make-db-version.cc new file mode 100644 index 00000000..fa80cac9 --- /dev/null +++ b/test/make-db-version.cc @@ -0,0 +1,35 @@ +/* Create an empty notmuch database with a specific version and + * features. */ + +#include +#include +#include +#include + +#include + +int main(int argc, char **argv) +{ + if (argc != 4) { + fprintf (stderr, "Usage: %s mailpath version features\n", argv[0]); + exit (2); + } + + std::string nmpath (argv[1]); + nmpath += "/.notmuch"; + if (mkdir (nmpath.c_str (), 0777) < 0) { + perror (("failed to create " + nmpath).c_str ()); + exit (1); + } + + try { + Xapian::WritableDatabase db ( + nmpath + "/xapian", Xapian::DB_CREATE_OR_OPEN); + db.set_metadata ("version", argv[2]); + db.set_metadata ("features", argv[3]); + db.commit (); + } catch (const Xapian::Error &e) { + fprintf (stderr, "%s\n", e.get_description ().c_str ()); + exit (1); + } +} diff --git a/test/test-databases/Makefile.local b/test/test-databases/Makefile.local index 0572e784..ff333a1d 100644 --- a/test/test-databases/Makefile.local +++ b/test/test-databases/Makefile.local @@ -11,4 +11,4 @@ test_databases := $(dir)/database-v1.tar.xz download-test-databases: ${test_databases} -DISTCLEAN := $(DISTCLEAN) ${test_databases} +DATACLEAN := $(DATACLEAN) ${test_databases} diff --git a/test/test-lib-common.sh b/test/test-lib-common.sh index 892991e2..4903038d 100644 --- a/test/test-lib-common.sh +++ b/test/test-lib-common.sh @@ -38,6 +38,10 @@ find_notmuch_path () # test/ subdirectory and are run in 'trash directory' subdirectory. TEST_DIRECTORY=$(pwd) notmuch_path=`find_notmuch_path "$TEST_DIRECTORY"` + +# configure output +. $notmuch_path/sh.config + if test -n "$valgrind" then make_symlink () { diff --git a/test/test-lib.el b/test/test-lib.el index 437f83f4..04c8d634 100644 --- a/test/test-lib.el +++ b/test/test-lib.el @@ -52,12 +52,16 @@ (defun test-output (&optional filename) "Save current buffer to file FILENAME. Default FILENAME is OUTPUT." + (notmuch-post-command) (write-region (point-min) (point-max) (or filename "OUTPUT"))) (defun test-visible-output (&optional filename) "Save visible text in current buffer to file FILENAME. Default FILENAME is OUTPUT." - (let ((text (visible-buffer-string))) + (notmuch-post-command) + (let ((text (visible-buffer-string)) + ;; Tests expect output in UTF-8 encoding + (coding-system-for-write 'utf-8)) (with-temp-file (or filename "OUTPUT") (insert text)))) (defun visible-buffer-string () @@ -166,7 +170,21 @@ nothing." (t (notmuch-test-report-unexpected output expected))))) +(defun notmuch-post-command () + (run-hooks 'post-command-hook)) + +(defmacro notmuch-test-progn (&rest body) + (cons 'progn + (mapcar + (lambda (x) `(prog1 ,x (notmuch-post-command))) + body))) + ;; For historical reasons, we hide deleted tags by default in the test ;; suite (setq notmuch-tag-deleted-formats '((".*" nil))) + +;; force a common html renderer, to avoid test variations between +;; environments + +(setq mm-text-html-renderer 'html2text) diff --git a/test/test-lib.sh b/test/test-lib.sh index 72559cce..53db9caa 100644 --- a/test/test-lib.sh +++ b/test/test-lib.sh @@ -1139,7 +1139,7 @@ test_emacs () { rm -f OUTPUT touch OUTPUT - ${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(progn $@)" + ${TEST_EMACSCLIENT} --socket-name="$EMACS_SERVER" --eval "(notmuch-test-progn $@)" } test_python() { diff --git a/util/hex-escape.c b/util/hex-escape.c index b7e2e07a..b4a2a02a 100644 --- a/util/hex-escape.c +++ b/util/hex-escape.c @@ -25,8 +25,6 @@ #include "error_util.h" #include "hex-escape.h" -static const size_t default_buf_size = 1024; - static const char *output_charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_@=.,"; diff --git a/util/string-util.c b/util/string-util.c index 3e7066cd..a90501ee 100644 --- a/util/string-util.c +++ b/util/string-util.c @@ -37,6 +37,14 @@ strtok_len (char *s, const char *delim, size_t *len) return *len ? s : NULL; } +const char * +strtok_len_c (const char *s, const char *delim, size_t *len) +{ + /* strtok_len is already const-safe, but we can't express both + * versions in the C type system. */ + return strtok_len ((char*)s, delim, len); +} + char * sanitize_string (const void *ctx, const char *str) { diff --git a/util/string-util.h b/util/string-util.h index 8a3ad19e..e409cb3d 100644 --- a/util/string-util.h +++ b/util/string-util.h @@ -3,6 +3,10 @@ #include +#ifdef __cplusplus +extern "C" { +#endif + /* like strtok(3), but without state, and doesn't modify s. Return * value is indicated by pointer and length, not null terminator. * @@ -19,6 +23,9 @@ char *strtok_len (char *s, const char *delim, size_t *len); +/* Const version of strtok_len. */ +const char *strtok_len_c (const char *s, const char *delim, size_t *len); + /* Return a talloced string with str sanitized. * * Whitespace characters (tabs and newlines) are replaced with spaces, @@ -57,4 +64,8 @@ int parse_boolean_term (void *ctx, const char *str, char **prefix_out, char **term_out); +#ifdef __cplusplus +} +#endif + #endif diff --git a/vim/notmuch.vim b/vim/notmuch.vim index 331e9300..cad95178 100644 --- a/vim/notmuch.vim +++ b/vim/notmuch.vim @@ -59,6 +59,7 @@ let s:notmuch_datetime_format_default = '%d.%m.%y %H:%M:%S' let s:notmuch_reader_default = 'mutt -f %s' let s:notmuch_sendmail_default = 'sendmail' let s:notmuch_folders_count_threads_default = 0 +let s:notmuch_compose_start_insert_default = 1 function! s:new_file_buffer(type, fname) exec printf('edit %s', a:fname) @@ -132,7 +133,9 @@ function! s:show_reply() let b:compose_done = 0 call s:set_map(g:notmuch_compose_maps) autocmd BufDelete call s:on_compose_delete() - startinsert! + if g:notmuch_compose_start_insert + startinsert! + end endfunction function! s:compose() @@ -140,7 +143,9 @@ function! s:compose() let b:compose_done = 0 call s:set_map(g:notmuch_compose_maps) autocmd BufDelete call s:on_compose_delete() - startinsert! + if g:notmuch_compose_start_insert + startinsert! + end endfunction function! s:show_info() @@ -428,6 +433,10 @@ function! s:set_defaults() endif endif + if !exists('g:notmuch_compose_start_insert') + let g:notmuch_compose_start_insert = s:notmuch_compose_start_insert_default + endif + if !exists('g:notmuch_custom_search_maps') && exists('g:notmuch_rb_custom_search_maps') let g:notmuch_custom_search_maps = g:notmuch_rb_custom_search_maps endif @@ -471,28 +480,21 @@ ruby << EOF $searches = [] $threads = [] $messages = [] - $config = {} $mail_installed = defined?(Mail) - def get_config - group = nil - config = ENV['NOTMUCH_CONFIG'] || '~/.notmuch-config' - File.open(File.expand_path(config)).each do |l| - l.chomp! - case l - when /^\[(.*)\]$/ - group = $1 - when '' - when /^(.*)=(.*)$/ - key = "%s.%s" % [group, $1] - value = $2 - $config[key] = value - end - end + def get_config_item(item) + result = '' + IO.popen(['notmuch', 'config', 'get', item]) { |out| + result = out.read + } + return result.rstrip + end - $db_name = $config['database.path'] - $email_name = $config['user.name'] - $email_address = $config['user.primary_email'] + def get_config + $db_name = get_config_item('database.path') + $email_name = get_config_item('user.name') + $email_address = get_config_item('user.primary_email') + $email_name = get_config_item('user.name') $email = "%s <%s>" % [$email_name, $email_address] end