[PATCH] Change behavior of changegroup hook for notify extension

Nikolaus.Schueler at lantiq.com Nikolaus.Schueler at lantiq.com
Mon Dec 12 06:51:34 CST 2011


Hello,

I changed the patch as suggested by Matt. All necessary information
should now be in the commit message, I renamed the config option
and unified the tests to only one big test.

Regards

Nik

# HG changeset patch
# User Nikolaus Schueler <nikolaus.schueler at lantiq.com>
# Date 1323693948 -3600
# Node ID 4d922dfd6fa22148b7160638d0e82e3e41e03fc3
# Parent  e6868bd17f24324db347a289bb2ae0120dd5cd54
Notify extension: Change behavior of "changegroup" hook

Change the behavior so that the sender (the "From" header in the notification
mail) in case of the "changegroup" hook is the user that did the first commit
in the changegroup.  The option is configurable, if you set
"notify.fromauthor" to "True" in your config, the new behavior is activated.
If you do not set the option, the behavior is as before.  The commit includes
a test that shows various aspects of the changed behavior.

diff --git a/hgext/notify.py b/hgext/notify.py
--- a/hgext/notify.py
+++ b/hgext/notify.py
@@ -108,6 +108,11 @@ notify.merge
 notify.mbox
   If set, append mails to this mbox file instead of sending. Default: None.
 
+notify.fromauthor
+  If set, use the first committer of the changegroup for the "From" field of
+  the notification mail. If not set, use the system user of the pushing repo.
+  Default: False.
+
 If set, the following entries will also be used to customize the notifications:
 
 email.from
@@ -338,11 +343,14 @@ def hook(ui, repo, hooktype, node=None, 
     ui.pushbuffer()
     data = ''
     count = 0
+    author = ''
     if hooktype == 'changegroup' or hooktype == 'outgoing':
         start, end = ctx.rev(), len(repo)
         for rev in xrange(start, end):
             if n.node(repo[rev]):
                 count += 1
+                if not author:
+                    author = repo[rev].user()
             else:
                 data += ui.popbuffer()
                 ui.note(_('notify: suppressing notification for merge %d:%s\n') %
@@ -360,5 +368,9 @@ def hook(ui, repo, hooktype, node=None, 
         n.diff(ctx)
 
     data += ui.popbuffer()
+    fromauthor = ui.config('notify', 'fromauthor')
+    if author and fromauthor:
+        data = '\n'.join(['From: %s' % author, data])
+
     if count:
         n.send(ctx, count, data)
diff --git a/tests/test-notify-changegroup-author.t b/tests/test-notify-changegroup-author.t
new file mode 100644
--- /dev/null
+++ b/tests/test-notify-changegroup-author.t
@@ -0,0 +1,186 @@
+Prove that the "From" field contains the committer, not the owner
+(== configured user) of the repo.
+This test shows that the first committer is used for the "From" field.
+
+Show that old behavior is preserved if option not set ("From" field
+still contains system user)
+  $ cat <<EOF >> $HGRCPATH
+  > [extensions]
+  > notify=
+  > 
+  > [email]
+  > from = john.doe at example.com
+  > [hooks]
+  > changegroup.notify = python:hgext.notify.hook
+  > 
+  > [notify]
+  > sources = push
+  > diffstat = False
+  > maxsubject = 10
+  > 
+  > [usersubs]
+  > foo at bar = *
+  > 
+  > [reposubs]
+  > * = baz
+  > EOF
+  $ hg init a
+
+clone
+
+  $ hg --traceback clone a b
+  updating to branch default
+  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ echo a > b/a
+
+commit
+
+  $ hg --traceback --cwd b commit -Ama
+  adding a
+  $ echo a >> b/a
+
+push
+
+  $ hg --traceback --cwd b push ../a 2>&1 |
+  >     python -c 'import sys,re; print re.sub("\n\t", " ", sys.stdin.read()),'
+  pushing to ../a
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 1 changesets with 1 changes to 1 files
+  Content-Type: text/plain; charset="us-ascii"
+  MIME-Version: 1.0
+  Content-Transfer-Encoding: 7bit
+  Date: * (glob)
+  Subject: /tmp/hg...
+  From: john.doe at example.com
+  X-Hg-Notification: changeset cb9a9f314b8b
+  Message-Id: <*> (glob)
+  To: baz, foo at bar
+  
+  changeset cb9a9f314b8b in $TESTTMP/a
+  details: $TESTTMP/a?cmd=changeset;node=cb9a9f314b8b
+  summary: a
+  
+  diffs (5 lines):
+  
+  diff -r 000000000000 -r cb9a9f314b8b a
+  --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/a	Thu Jan 01 00:00:00 1970 +0000
+  @@ -0,0 +1,1 @@
+  +a
+
+Test with flag set ("From" field should contain (first) committer now).
+This additionally shows that user that did the merge is not used for the 
+"From" field..
+
+  $ rm -fr a b
+  $ cat <<EOF >> $HGRCPATH
+  > [extensions]
+  > notify=
+  > 
+  > [email]
+  > from = john.doe at example.com
+  > [hooks]
+  > changegroup.notify = python:hgext.notify.hook
+  > 
+  > [notify]
+  > sources = push
+  > diffstat = False
+  > maxsubject = 10
+  > fromauthor = True
+  > 
+  > [usersubs]
+  > foo at bar = *
+  > 
+  > [reposubs]
+  > * = baz
+  > EOF
+  $ hg init a
+
+clone
+
+  $ hg --traceback clone a b
+  updating to branch default
+  0 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ echo a > b/a
+  $ echo b >> b/a
+  $ echo c >> b/a
+
+commit as one user
+
+  $ HGUSER=thecommitter
+  $ hg --traceback --cwd b commit -Ama
+  adding a
+  $ echo x > b/a
+  $ echo b >> b/a
+  $ echo c >> b/a
+  $ hg --traceback --cwd b commit -Amx
+
+commit as other user
+
+  $ HGUSER=theothercommitter
+  $ hg --cwd b up 0
+  1 files updated, 0 files merged, 0 files removed, 0 files unresolved
+  $ echo a > b/a
+  $ echo b >> b/a
+  $ echo y >> b/a
+  $ hg --traceback --cwd b commit -Amy
+  created new head
+
+merge as a different user
+
+  $ HGUSER=themerger
+  $ hg --cwd b merge
+  merging a
+  0 files updated, 1 files merged, 0 files removed, 0 files unresolved
+  (branch merge, don't forget to commit)
+
+  $ hg --traceback --cwd b commit -Am "merged"
+
+push
+
+  $ hg --traceback --cwd b push ../a 2>&1 |
+  >     python -c 'import sys,re; print re.sub("\n\t", " ", sys.stdin.read()),'
+  pushing to ../a
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 4 changesets with 4 changes to 1 files
+  Content-Type: text/plain; charset="us-ascii"
+  MIME-Version: 1.0
+  Content-Transfer-Encoding: 7bit
+  Date: * (glob)
+  Subject: /tmp/hg...
+  From: thecommitter
+  X-Hg-Notification: changeset 6cf7dfbc5c73
+  Message-Id: <*> (glob)
+  To: baz, foo at bar
+  
+  changeset 6cf7dfbc5c73 in $TESTTMP/a
+  details: $TESTTMP/a?cmd=changeset;node=6cf7dfbc5c73
+  summary: a
+  
+  changeset a715156cec09 in $TESTTMP/a
+  details: $TESTTMP/a?cmd=changeset;node=a715156cec09
+  summary: x
+  
+  changeset 959fdbe329fd in $TESTTMP/a
+  details: $TESTTMP/a?cmd=changeset;node=959fdbe329fd
+  summary: y
+  
+  changeset fec67fbb1d04 in $TESTTMP/a
+  details: $TESTTMP/a?cmd=changeset;node=fec67fbb1d04
+  summary: merged
+  
+  diffs (7 lines):
+  
+  diff -r 000000000000 -r fec67fbb1d04 a
+  --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+  +++ b/a	Thu Jan 01 00:00:00 1970 +0000
+  @@ -0,0 +1,3 @@
+  +x
+  +b
+  +y



More information about the Mercurial-devel mailing list