<p><br>
On Jul 18, 2012 12:25 AM, "Dominik Peteler" <<a href="mailto:dominik@with-h.at">dominik@with-h.at</a>> wrote:<br>
><br>
> hello,<br>
><br>
> I attached some modifications which I made to notmuch. Comes with man pages and test.<br>
></p>
<p>Hi Dominik, please find a couple of comments below. I'm on the road, replying on a phone, so this is not very thorough...</p>
<p>BR,<br>
Jani.<br></p>
<p>> regards<br>
><br>
> dominik<br>
><br>
><br>
><br>
> There are two hooks:<br>
>  * pre-tag: Run before tagging<br>
>  * post-tag: Run after<br>
><br>
> This allows users to react on changes of tags. For example,<br>
> you might want to move a message to a special Maildir<br>
> depending on its notmuch tags.<br>
> ---<br>
>  man/man1/notmuch-tag.1   | 17 +++++++++++++++++<br>
>  man/man5/notmuch-hooks.5 | 23 +++++++++++++++++++++++<br>
>  notmuch-tag.c            | 25 +++++++++++++++++++++----<br>
>  test/hooks               | 36 ++++++++++++++++++++++++++++++++++++<br>
>  4 files changed, 97 insertions(+), 4 deletions(-)<br>
><br>
> diff --git a/man/man1/notmuch-tag.1 b/man/man1/notmuch-tag.1<br>
> index d810e1b..8d8b7b2 100644<br>
> --- a/man/man1/notmuch-tag.1<br>
> +++ b/man/man1/notmuch-tag.1<br>
> @@ -4,6 +4,7 @@ notmuch-tag \- add/remove tags for all messages matching the search terms<br>
><br>
>  .SH SYNOPSIS<br>
>  .B notmuch tag<br>
> +.RB "[" --no-hooks "]"<br>
>  .RI  "+<" tag> "|\-<" tag "> [...] [\-\-] <" search-term ">..."<br>
><br>
>  .SH DESCRIPTION<br>
> @@ -29,6 +30,22 @@ updates the maildir flags according to tag changes if the<br>
>  configuration option is enabled. See \fBnotmuch-config\fR(1) for<br>
>  details.<br>
><br>
> +The<br>
> +.B tag<br>
> +command supports hooks. See  \fBnotmuch-hooks(5)\fR<br>
> +for more details on hooks.<br>
> +<br>
> +Supported options for<br>
> +.B tag<br>
> +include<br>
> +.RS 4<br>
> +.TP 4<br>
> +.BR \-\-no\-hooks<br>
> +<br>
> +Prevents hooks from being run.<br>
> +.RE<br>
> +.RE<br>
> +<br>
>  .SH SEE ALSO<br>
><br>
>  \fBnotmuch\fR(1), \fBnotmuch-config\fR(1), \fBnotmuch-count\fR(1),<br>
> diff --git a/man/man5/notmuch-hooks.5 b/man/man5/notmuch-hooks.5<br>
> index b914a29..7399627 100644<br>
> --- a/man/man5/notmuch-hooks.5<br>
> +++ b/man/man5/notmuch-hooks.5<br>
> @@ -38,6 +38,29 @@ the scan or import.<br>
>  Typically this hook is used to perform additional query\-based tagging on the<br>
>  imported messages.<br>
>  .RE<br>
> +.RS 4<br>
> +.TP 4<br>
> +.B pre\-tag<br>
> +This hook is invoked by the<br>
> +.B tag<br>
> +command before tagging messages. If this<br>
> +hook exits with a non-zero status, notmuch will abort further processing of the<br>
> +.B tag<br>
> +command.<br>
> +<br>
> +Typically this hook is used for syncing the Maildir with notmuch tags.</p>
<p>Maildir syncing usually refers to the maildir flag and notmuch tag syncing in the notmuch context. The above is bound to be confusing.</p>
<p>> +.RE<br>
> +.RS 4<br>
> +.TP 4<br>
> +.B post\-tag<br>
> +This hook is invoked by the<br>
> +.B tag<br>
> +command after messages have been tagged. The hook will not be run if there have been any errors during<br>
> +the tagging.<br>
> +<br>
> +Typically this hook is used for syncing the Maildir with notmuch tags.</p>
<p>Ditto.</p>
<p>> +.RE<br>
> +<br>
><br>
>  .SH SEE ALSO<br>
><br>
> diff --git a/notmuch-tag.c b/notmuch-tag.c<br>
> index 7d18639..e98d3a0 100644<br>
> --- a/notmuch-tag.c<br>
> +++ b/notmuch-tag.c<br>
> @@ -174,9 +174,11 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])<br>
>      int tag_ops_count = 0;<br>
>      char *query_string;<br>
>      notmuch_config_t *config;<br>
> +    const char *db_path;<br>
>      notmuch_database_t *notmuch;<br>
>      struct sigaction action;<br>
>      notmuch_bool_t synchronize_flags;<br>
> +    notmuch_bool_t run_hooks = TRUE;<br>
>      int i;<br>
>      int ret;<br>
><br>
> @@ -198,11 +200,12 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])<br>
>      }<br>
><br>
>      for (i = 0; i < argc; i++) {<br>
> -       if (strcmp (argv[i], "--") == 0) {<br>
> +       if (strcmp (argv[i], "--no-hooks") == 0) {<br>
> +               run_hooks = FALSE;</p>
<p>This is subtler than it looks. This would allow --no-hooks to be placed in the middle of tag operations. Same for any future arguments. And it would prevent removal of a hypothetical -no-hooks tag... I think we'll want to support specifying "--" twice to separate arguments from tag ops, and ops from query. And it must be forbidden to mix them.</p>

<p>> +       } else if (strcmp (argv[i], "--") == 0) {<br>
>             i++;<br>
>             break;<br>
> -       }<br>
> -       if (argv[i][0] == '+' || argv[i][0] == '-') {<br>
> +       } else if (argv[i][0] == '+' || argv[i][0] == '-') {<br>
>             tag_ops[tag_ops_count].tag = argv[i] + 1;<br>
>             tag_ops[tag_ops_count].remove = (argv[i][0] == '-');<br>
>             tag_ops_count++;<br>
> @@ -229,7 +232,15 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])<br>
>      if (config == NULL)<br>
>         return 1;<br>
><br>
> -    if (notmuch_database_open (notmuch_config_get_database_path (config),<br>
> +    db_path = notmuch_config_get_database_path (config);<br>
> +<br>
> +    if (run_hooks) {<br>
> +       ret = notmuch_run_hook (db_path, "pre-tag");<br>
> +       if (ret)<br>
> +           return ret;<br>
> +    }<br>
> +<br>
> +    if (notmuch_database_open (db_path,<br>
>                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))<br>
>         return 1;<br>
><br>
> @@ -239,5 +250,11 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])<br>
><br>
>      notmuch_database_destroy (notmuch);<br>
><br>
> +    if (run_hooks) {</p>
<p>Can't check further context atm, are you sure not to run post-tag if there's an error?</p>
<p>> +       ret = notmuch_run_hook (db_path, "post-tag");<br>
> +       if (ret)<br>
> +           return ret;<br>
> +    }<br>
> +<br>
>      return ret;<br>
>  }<br>
> diff --git a/test/hooks b/test/hooks<br>
> index 77e8569..ae857cc 100755<br>
> --- a/test/hooks<br>
> +++ b/test/hooks<br>
> @@ -31,6 +31,7 @@ rm_hooks () {<br>
>  # add a message to generate mail dir and database<br>
>  add_message<br>
><br>
> +# {pre,post}-new hooks<br>
>  test_begin_subtest "pre-new is run"<br>
>  rm_hooks<br>
>  generate_message<br>
> @@ -101,4 +102,39 @@ EOF<br>
>  chmod +x "${HOOK_DIR}/pre-new"<br>
>  test_expect_code 1 "hook execution failure" "notmuch new"<br>
><br>
> +<br>
> +<br>
> +# {pre,post}-tag hooks<br>
> +test_begin_subtest "pre-tag is run"<br>
> +rm_hooks<br>
> +generate_message<br>
> +create_echo_hook "pre-tag" expected output<br>
> +notmuch tag +foo -- '*' > /dev/null<br>
> +test_expect_equal_file expected output<br>
> +<br>
> +test_begin_subtest "post-tag is run"<br>
> +rm_hooks<br>
> +generate_message<br>
> +create_echo_hook "post-tag" expected output<br>
> +notmuch tag +foo -- '*'  > /dev/null<br>
> +test_expect_equal_file expected output<br>
> +<br>
> +test_begin_subtest "pre-tag is run before post-new"<br>
> +rm_hooks<br>
> +generate_message<br>
> +create_echo_hook "pre-tag" pre-tag.expected pre-tag.output<br>
> +create_echo_hook "post-tag" post-tag.expected post-tag.output<br>
> +notmuch tag +foo -- '*'  > /dev/null<br>
> +test_expect_equal_file post-tag.expected post-tag.output<br>
> +<br>
> +test_begin_subtest "pre-tag non-zero exit status (hook status)"<br>
> +rm_hooks<br>
> +generate_message<br>
> +create_failing_hook "pre-tag"<br>
> +output=`notmuch tag +foo -- '*'  2>&1`<br>
> +test_expect_equal "$output" "Error: pre-tag hook failed with status 13"<br>
> +<br>
> +# depends on the previous subtest leaving broken hook behind<br>
> +test_expect_code 1 "pre-tag non-zero exit status (notmuch status)" "notmuch tag +foo -- '*'"<br>
> +<br>
>  test_done<br>
> --<br>
> 1.7.11.2<br>
><br>
> _______________________________________________<br>
> notmuch mailing list<br>
> <a href="mailto:notmuch@notmuchmail.org">notmuch@notmuchmail.org</a><br>
> <a href="http://notmuchmail.org/mailman/listinfo/notmuch">http://notmuchmail.org/mailman/listinfo/notmuch</a><br>
</p>