Merge lp:~jr/bzr/707274-commit-message-hook into lp:bzr

Proposed by Jonathan Riddell
Status: Merged
Approved by: Jonathan Riddell
Approved revision: no longer in the source branch.
Merged at revision: 5965
Proposed branch: lp:~jr/bzr/707274-commit-message-hook
Merge into: lp:bzr
Diff against target: 127 lines (+51/-5)
5 files modified
bzrlib/builtins.py (+7/-4)
bzrlib/msgeditor.py (+18/-0)
bzrlib/tests/blackbox/test_commit.py (+10/-1)
bzrlib/tests/test_msgeditor.py (+12/-0)
doc/en/release-notes/bzr-2.4.txt (+4/-0)
To merge this branch: bzr merge lp:~jr/bzr/707274-commit-message-hook
Reviewer Review Type Date Requested Status
Jelmer Vernooij (community) code Approve
John A Meinel Needs Fixing
Vincent Ladeuil Needs Information
Andrew Bennetts Pending
Review via email: mp+62334@code.launchpad.net

Commit message

New hook set_commit_message in bzrlib.msgeditor to set a commit message and revision properties. (Jonathan Riddell, #274578)

Description of the change

Proposed new hook set_commit_message() to set a commit message

For bug 707274

called as part of the get_message() callback which is too late to set properties directory on the Commit object, although it can be done through editing commit.builder._revprops

No option to confirm commit message yet
Needs bzr-builddeb updated to use it
Needs qbzr updated to use it

To post a comment you must log in.
Revision history for this message
Jonathan Riddell (jr) wrote :

Submitted bug https://bugs.launchpad.net/qbzr/+bug/788216 on qbzr to use the commit message hooks

Revision history for this message
Jonathan Riddell (jr) wrote :

It needs to be called as part of get_message(), any earlier and there is no Commit object to get interesting data out of (such as the changes to debian/changelog that bzr-buiddeb needs)

Revision history for this message
Vincent Ladeuil (vila) wrote :

@spiv: I seem to remember you had a deep discussion in this area but I didn't participate nor remember the details. I have a vague feeling that this proposal is not doing the Right Thing but I can't put my finger on it. Care to review it ?

@Jonathan: I really hate spreading FUD on proposals, so wait for spiv input before worrying about me ;)

review: Needs Information
Revision history for this message
Martin Packman (gz) wrote :

You're probably thinking of these two threads from Gary and Alexander trying to rationalise the way qcommit works:

<https://lists.ubuntu.com/archives/bazaar/2009q3/062169.html>
<https://lists.ubuntu.com/archives/bazaar/2010q2/068315.html>

Revision history for this message
John A Meinel (jameinel) wrote :

There is a small issue if 2 plugins both provide "set_commit_message". Since neither one can see that another plugin has set it, and the ordering of hooks is intentionally undefined.

So I think we at least want the hook to take 2 parameters. The Commit object, and the current commit message.

We might want to future proof this by having the took take a Params object. But ATM I feel that it YAGNI.

Otherwise this looks fine, so "tweak".

review: Needs Fixing
Revision history for this message
Jonathan Riddell (jr) wrote :

updated

Revision history for this message
Martin Packman (gz) wrote :

Would it be helpful to get Alexander or Gary to look at this and see if it helps the qbzr use case?

Revision history for this message
Jelmer Vernooij (jelmer) wrote :

If the intend of this is to allow e.g. plugins to modify the revision properties, I think the _revprops attribute on CommitBuilder should explicitly be made part of the interface.

E.g. bzr-svn, bzr-git and bzr-hg which have custom CommitBuilders currently get the revprops passed in from Repository.get_commit_builder() and do not necessarily set them as a private CommitBuilder._revprops variable and/or assume nobody else touches them.

Revision history for this message
Jelmer Vernooij (jelmer) wrote :

I'm surprised by how small this is, well done.

A minor nitpick: You need an extra empty line after set_commit_message.

Approving, though it would indeed be useful if one of the qbzr folks could also have a look.

review: Approve (code)
Revision history for this message
John A Meinel (jameinel) wrote :

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 6/4/2011 4:34 PM, Jelmer Vernooij wrote:
> Review: Approve code
> I'm surprised by how small this is, well done.
>
> A minor nitpick: You need an extra empty line after set_commit_message.
>
> Approving, though it would indeed be useful if one of the qbzr folks could also have a look.

In talking with Jonathan about this (voice-chat no less), it doesn't fit
the qbzr use case, but they really need something custom anyway.
Specifically, they already select files before committing (so they run
'status' and then let the user use radio boxes, etc.)

They want to get the templated commit message before actually running
commit (which the current api, nor the new api support). And then they
can set the commit message with just the equivalent of '-m' or '-F'. So
they don't need a hook like this (bzr-builddeb does). What they need is
a hook that lets template messages be generated without actually having
processed a commit at all.

Note that there are lots of fuzzy bits to that. Because if you haven't
actually processed a commit, what if the code actually wants to insert
the names of the new files, or a per-file comment, or ... We can evolve
to that. Eg as a first pass, having a templated commit message that does
*not* take the list of files, or having it take the list, but qbzr needs
to re-query whenever a user changes the selection list.

All that to say, this handles what bzr-builddeb needs, and qbzr needs to
design something that is going to work for them, because it is very
different from the existing stuff. So we shouldn't block on this.

My only concern is if we would rather just have a flag on the existing
hook, that says "if the user uses this template message without changing
it, just commit the text, don't prompt them if they really want it.".
(Extend the existing hook, rather than creating a new one.)

However, I don't think the current hook is easy to extend, so I'm good
with this.

John
=:->
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk3s4wUACgkQJdeBCYSNAANd7QCg1Zjob46KFud1AJRxKDqmparL
ONUAniPGsNswxVwtUsAt0l6xAPCnk+jt
=Rx7Z
-----END PGP SIGNATURE-----

Revision history for this message
John A Meinel (jameinel) wrote :

Looking at it with fresh eyes, the only thing I see missing is a test that "bzr commit" will use the set_commit_message hook. You test that msgeditor.... works, but not that the Commit code uses it.

So small tweak and I think it is ready to land.

Revision history for this message
Jonathan Riddell (jr) wrote :

sent to pqm by email

Revision history for this message
Jonathan Riddell (jr) wrote :

sent to pqm by email

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bzrlib/builtins.py'
2--- bzrlib/builtins.py 2011-05-31 18:30:32 +0000
3+++ bzrlib/builtins.py 2011-06-09 12:02:30 +0000
4@@ -3193,7 +3193,8 @@
5 from bzrlib.msgeditor import (
6 edit_commit_message_encoded,
7 generate_commit_message_template,
8- make_commit_message_template_encoded
9+ make_commit_message_template_encoded,
10+ set_commit_message,
11 )
12
13 commit_stamp = offset = None
14@@ -3265,9 +3266,11 @@
15 # make_commit_message_template_encoded returns user encoding.
16 # We probably want to be using edit_commit_message instead to
17 # avoid this.
18- start_message = generate_commit_message_template(commit_obj)
19- my_message = edit_commit_message_encoded(text,
20- start_message=start_message)
21+ my_message = set_commit_message(commit_obj)
22+ if my_message is None:
23+ start_message = generate_commit_message_template(commit_obj)
24+ my_message = edit_commit_message_encoded(text,
25+ start_message=start_message)
26 if my_message is None:
27 raise errors.BzrCommandError("please specify a commit"
28 " message with either --message or --file")
29
30=== modified file 'bzrlib/msgeditor.py'
31--- bzrlib/msgeditor.py 2011-05-31 21:08:52 +0000
32+++ bzrlib/msgeditor.py 2011-06-09 12:02:30 +0000
33@@ -303,6 +303,13 @@
34 These are all empty initially.
35 """
36 Hooks.__init__(self, "bzrlib.msgeditor", "hooks")
37+ self.add_hook('set_commit_message',
38+ "Set a fixed commit message. "
39+ "set_commit_message is called with the "
40+ "bzrlib.commit.Commit object (so you can also change e.g. revision "
41+ "properties by editing commit.builder._revprops) and the message "
42+ "so far. set_commit_message must return the message to use or None"
43+ " if it should use the message editor as normal.", (2, 4))
44 self.add_hook('commit_message_template',
45 "Called when a commit message is being generated. "
46 "commit_message_template is called with the bzrlib.commit.Commit "
47@@ -317,6 +324,17 @@
48 hooks = MessageEditorHooks()
49
50
51+def set_commit_message(commit, start_message=None):
52+ """Sets the commit message.
53+ :param commit: Commit object for the active commit.
54+ :return: The commit message or None to continue using the message editor
55+ """
56+ start_message = None
57+ for hook in hooks['set_commit_message']:
58+ start_message = hook(commit, start_message)
59+ return start_message
60+
61+
62 def generate_commit_message_template(commit, start_message=None):
63 """Generate a commit message template.
64
65
66=== modified file 'bzrlib/tests/blackbox/test_commit.py'
67--- bzrlib/tests/blackbox/test_commit.py 2011-05-17 14:27:30 +0000
68+++ bzrlib/tests/blackbox/test_commit.py 2011-06-09 12:02:30 +0000
69@@ -754,6 +754,16 @@
70 "commit tree/hello.txt", stdin="n\n")
71 self.assertEqual(expected, tree.last_revision())
72
73+ def test_set_commit_message(self):
74+ msgeditor.hooks.install_named_hook("set_commit_message",
75+ lambda commit_obj, msg: "save me some typing\n", None)
76+ tree = self.make_branch_and_tree('tree')
77+ self.build_tree(['tree/hello.txt'])
78+ tree.add('hello.txt')
79+ out, err = self.run_bzr("commit tree/hello.txt")
80+ last_rev = tree.branch.repository.get_revision(tree.last_revision())
81+ self.assertEqual('save me some typing\n', last_rev.message)
82+
83 def test_commit_without_username(self):
84 """Ensure commit error if username is not set.
85 """
86@@ -782,4 +792,3 @@
87 self.assertEqual(out, '')
88 self.assertContainsRe(err,
89 'Branch.*test_checkout.*appears to be bound to itself')
90-
91
92=== modified file 'bzrlib/tests/test_msgeditor.py'
93--- bzrlib/tests/test_msgeditor.py 2011-05-11 11:35:28 +0000
94+++ bzrlib/tests/test_msgeditor.py 2011-06-09 12:02:30 +0000
95@@ -344,6 +344,18 @@
96 self.assertRaises(errors.BadCommitMessageEncoding,
97 msgeditor.edit_commit_message, '')
98
99+ def test_set_commit_message_no_hooks(self):
100+ commit_obj = commit.Commit()
101+ self.assertIs(None,
102+ msgeditor.set_commit_message(commit_obj))
103+
104+ def test_set_commit_message_hook(self):
105+ msgeditor.hooks.install_named_hook("set_commit_message",
106+ lambda commit_obj, existing_message: "save me some typing\n", None)
107+ commit_obj = commit.Commit()
108+ self.assertEquals("save me some typing\n",
109+ msgeditor.set_commit_message(commit_obj))
110+
111 def test_generate_commit_message_template_no_hooks(self):
112 commit_obj = commit.Commit()
113 self.assertIs(None,
114
115=== modified file 'doc/en/release-notes/bzr-2.4.txt'
116--- doc/en/release-notes/bzr-2.4.txt 2011-06-08 13:51:30 +0000
117+++ doc/en/release-notes/bzr-2.4.txt 2011-06-09 12:02:30 +0000
118@@ -24,6 +24,10 @@
119 exception caused while running bzr serve. (Jonathan Riddell,
120 #274578)
121
122+* New hook set_commit_message in bzrlib.msgeditor to set
123+ a commit message and revision properties. (Jonathan Riddell,
124+ #274578)
125+
126 * Support ``-S`` as an alias for ``--short`` for the ``log`` and
127 ``missing`` commands. (Martin von Gagern, #38655)
128