Skip to contentSkip to conversation

git-multimail: Add an option to filter on branches

PATCH In review Dave Boutcher 2 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 git-multimail: Add an option to filter on branches

Add a branches option to the config. Only changes pushed to specified branches will generate emails. Changes to tags will continue to generate emails.

---
 git-multimail/README           |  7 +++++++
 git-multimail/git_multimail.py | 44 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 43 insertions(+), 8 deletions(-)
git-multimail/README +7 -0
index 51add52..a40847a 100644
@@ -170,6 +170,13 @@ multimailhook.repoName
     for gitolite repositories, or otherwise to derive this value from
     the repository path name.
 
+multimailhook.branches
+
+    A comma separated list of branches to monitor. If not set,
+    notifications will be sent for updates to any branch.  Branch
+    names can contain regular expressions, and this configuration
+    option can be multivalued.
+
 multimailhook.mailingList
 
     The list of email addresses to which notification emails should be
git-multimail/git_multimail.py +36 -8

99 lines not on this page · raw message

Signature · 1 line
2.3.0
#1 lore raw

PATCH 2/3 git-multimail: Add a quiet option to prevent outputting email addresses from hook

We have a very log list of recipients...displaying that list on every push is annoying

Add an option to prevent printing the list of email addresses from the hook

---
 git-multimail/README           |  3 +++
 git-multimail/git_multimail.py | 11 ++++++++++-
 2 files changed, 13 insertions(+), 1 deletion(-)
git-multimail/README +3 -0
index a40847a..49e0937 100644
@@ -396,6 +396,9 @@ multimailhook.replyToRefchange
     - The value "none", in which case the Reply-To: field will be
       omitted.
 
+multimailhook.quiet
+
+    Do not output the list of email recipients from the hook
 
 Email filtering aids
 --------------------
git-multimail/git_multimail.py +10 -1
index 5ed253f..095110a 100755
@@ -1582,6 +1582,9 @@ class Environment(object):
             commit mail.  The value should be a list of strings
             representing words to be passed to the command.
 
+        quiet (bool)
+            On success do not write to stderr
+
     """
 
     REPO_NAME_RE = re.compile(r'^(?P<name>.+?)(?:\.git)$')
@@ -1594,6 +1597,7 @@ class Environment(object):
         self.logopts = []
         self.refchange_showlog = False
         self.commitlogopts = ['-C', '--stat', '-p', '--cc']
+        self.quiet = False
 
         self.COMPUTED_KEYS = [
             'administrator',
@@ -1745,6 +1749,10 @@ class ConfigOptionsEnvironmentMixin(ConfigEnvironmentMixin):
             'refchangeshowlog', default=self.refchange_showlog
             )
 
+        self.quiet = config.get_bool(
+            'quiet', default=False
+            )
+
         maxcommitemails = config.get('maxcommitemails')
         if maxcommitemails is not None:
             try:
@@ -2376,7 +2384,8 @@ class Push(object):
                     % (change.refname, change.old.sha1, change.new.sha1,)
                     )
             else:
-                sys.stderr.write('Sending notification emails to: %s\n' % (change.recipients,))
+                if not self.environment.quiet:
+                    sys.stderr.write('Sending notification emails to: %s\n' % (change.recipients,))
                 extra_values = {'send_date': send_date.next()}
                 mailer.send(
                     change.generate_email(self, body_filter, extra_values),
Signature · 1 line
2.3.0
#2 lore raw

PATCH 3/3 git-multimail: Add stdout option to config

In reply to Dave Boutcher

Add a stdout option to the config that is equivalent to the --stdout command line option. This allows for easier debugging for a hook

---
 git-multimail/README           |  5 +++++
 git-multimail/git_multimail.py | 10 +++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)
git-multimail/README +5 -0
index 49e0937..0235b83 100644
@@ -400,6 +400,11 @@ multimailhook.quiet
 
     Do not output the list of email recipients from the hook
 
+multimailhook.stdout
+
+    For debugging, send emails to stdout rather than to the
+    mailer.  Equivalent to the --stdout command line option
+
 Email filtering aids
 --------------------
 
git-multimail/git_multimail.py +9 -1
index 095110a..35a1140 100755
@@ -1585,6 +1585,9 @@ class Environment(object):
         quiet (bool)
             On success do not write to stderr
 
+        stdout (bool)
+            Write email to stdout rather than emailing. Useful for debugging
+
     """
 
     REPO_NAME_RE = re.compile(r'^(?P<name>.+?)(?:\.git)$')
@@ -1598,6 +1601,7 @@ class Environment(object):
         self.refchange_showlog = False
         self.commitlogopts = ['-C', '--stat', '-p', '--cc']
         self.quiet = False
+        self.stdout = False
 
         self.COMPUTED_KEYS = [
             'administrator',
@@ -1753,6 +1757,10 @@ class ConfigOptionsEnvironmentMixin(ConfigEnvironmentMixin):
             'quiet', default=False
             )
 
+        self.stdout = config.get_bool(
+            'stdout', default=False
+            )
+
         maxcommitemails = config.get('maxcommitemails')
         if maxcommitemails is not None:
             try:
@@ -2563,7 +2571,7 @@ def main(args):
                 sys.stderr.write('    %s : %r\n' % (k, v))
             sys.stderr.write('\n')
 
-        if options.stdout:
+        if options.stdout or environment.stdout:
             mailer = OutputMailer(sys.stdout)
         else:
             mailer = choose_mailer(config, environment)
Signature · 1 line
2.3.0
#3 lore raw
In reply to Dave Boutcher
Quoted 3 lines · Dave Boutcher

Add a branches option to the config. Only changes pushed to specified branches will generate emails. Changes to tags will continue to generate emails.

Thanks for the patches. Patches 2 and 3 seem uncontroversial, so I already merged them. Patch 1 is more interesting, and there have been other proposals for similar features, so I created a pull request for it:

https://github.com/git-multimail/git-multimail/pull/75

(Note the new URL--I just created a GitHub "organization" for git-multimail to make it easier for other people to get involved. More info soon...)

Michael

Signature · 2 lines
Michael Haggerty
[email protected]
#4 lore raw

Thanks Michael,

The only code I'm not fond of is matching on a list of regular expressions. There must be a more pythonic way to do:

+ return [x for x in [r.match(branch) for r in branches] if x]

which basically returns true if "branch" matches any regular expression in the list.

I pushed this change out to our production git server (its good to be the king.) I'll obviously update here if it does anything too unfortunate.

Quoted 21 lines · Michael Haggerty
Quoted 3 lines · Dave Boutcher

Add a branches option to the config. Only changes pushed to specified branches will generate emails. Changes to tags will continue to generate emails.

Thanks for the patches. Patches 2 and 3 seem uncontroversial, so I already merged them. Patch 1 is more interesting, and there have been other proposals for similar features, so I created a pull request for it:

https://github.com/git-multimail/git-multimail/pull/75

(Note the new URL--I just created a GitHub "organization" for git-multimail to make it easier for other people to get involved. More info soon...)

Michael

--
Michael Haggerty
[email protected]

Signature · 2 lines
Dave B
包小龙
#5 lore raw
Quoted 7 lines · Dave Boutcher

The only code I'm not fond of is matching on a list of regular expressions. There must be a more pythonic way to do:

+ return [x for x in [r.match(branch) for r in branches] if x]

which basically returns true if "branch" matches any regular expression in the list.

I think what you are looking for is

return any(r.match(branch) for r in branches)

This also has the advantage of stopping processing as soon as it finds a match.

I was also wondering why you decided to support comma-separated lists of regexps *and* multivalued config settings. It seems that supporting only multivalued settings would suffice.

Maybe you are following the precedent of our email configuration settings, which support comma-separated lists of email addresses? If so, I don't think that is necessary. The email support is there for backwards compatibility and because comma-separated email addresses are a thing in RFC 2822. Neither of these arguments apply to branch regexps.

Quoted 3 lines

I pushed this change out to our production git server (its good to be the king.) I'll obviously update here if it does anything too unfortunate.

Thanks.

Michael

Signature · 2 lines
Michael Haggerty
[email protected]
#6 lore raw
Quoted 3 lines · Michael Haggerty

I think what you are looking for is

return any(r.match(branch) for r in branches)

Yup, thats exactly what I wanted. I'll submit an updated patch

Quoted 3 lines

I was also wondering why you decided to support comma-separated lists of regexps *and* multivalued config settings. It seems that supporting only multivalued settings would suffice.

In practice we are using gitolite and git-multimail. Allowing both means our gitolite.conf file can look like

repo foo

    # always notify on master
    config multimailhook.branches     = master
   # notify for the current pre-release branches
   config multimailhook.branches     = v1.7-pre, v1.8-pre
Signature · 1 line
Dave B