~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/branch_implementations/test_commit.py

  • Committer: NamNguyen
  • Date: 2007-08-01 06:14:14 UTC
  • mto: (2789.1.1 ianc-integration)
  • mto: This revision was merged to the branch mainline in revision 2790.
  • Revision ID: namnguyen-20070801061414-u0tzrfgcz6z604lz
``Branch.hooks`` now supports ``pre_commit`` hook.

The hook's signature is

::

  hook(local, master, old_revno, old_revid, new_revno, new_revid,
       deleted_paths, added_paths, future_revision_tree)

``deleted_paths`` and ``added_paths`` are lists of paths. Renamed paths are
recorded in both ``deleted_paths`` and ``added_paths`` (i.e. deleted then
added).

``future_revision_tree`` is obtained from ``CommitBuilder.revision_tree``
to save hooks from getting it from the branch again.

For example, hooks can get a file:

::

  for path in added_paths:
      id = future_revision_tree.path2id(path)
      if future_revision_tree.kind(id) == 'file':
          file = future_revision_tree.get_file(id)
          ...

or export a tree and do ``make check`` or similar

::

  import bzrlib.export
  bzrlib.export.export(future_revision_tree, 'tmp_space')


If the commit is to be rejected, hooks should raise an ``Exception``.

Show diffs side-by-side

added added

removed removed

Lines of Context:
59
59
            ('post_commit', local_base, master.base, old_revno, old_revid,
60
60
             new_revno, new_revid, local_locked, master.is_locked()))
61
61
 
 
62
    def capture_pre_commit_hook(self, local, master, old_revno, old_revid,
 
63
                                new_revno, new_revid, deleted, added, tree):
 
64
        self.hook_calls.append(('pre_commit', old_revno, old_revid,
 
65
                                new_revno, new_revid, deleted, added))
 
66
 
62
67
    def test_post_commit_to_origin(self):
63
68
        tree = self.make_branch_and_memory_tree('branch')
64
69
        Branch.hooks.install_hook('post_commit',
112
117
            ],
113
118
            self.hook_calls)
114
119
        tree.unlock()
 
120
    
 
121
    def test_pre_commit_passes(self):
 
122
        tree = self.make_branch_and_memory_tree('branch')
 
123
        tree.lock_write()
 
124
        tree.add('')
 
125
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
 
126
        revid1 = tree.commit('first revision')
 
127
        revid2 = tree.commit('second revision')
 
128
        self.assertEqual([
 
129
            ('pre_commit', 0, NULL_REVISION, 1, revid1, [], []),
 
130
            ('pre_commit', 1, revid1, 2, revid2, [], [])
 
131
            ],
 
132
            self.hook_calls)
 
133
        tree.unlock()
 
134
 
 
135
    def test_pre_commit_fails(self):
 
136
        tree = self.make_branch_and_memory_tree('branch')
 
137
        tree.lock_write()
 
138
        tree.add('')
 
139
        class PreCommitException(Exception): pass
 
140
        def hook_func(_1, _2, _3, _4, _5, new_revid, _7, _8, _9):
 
141
            raise PreCommitException(new_revid)
 
142
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
 
143
        Branch.hooks.install_hook("pre_commit", hook_func)
 
144
        revids = [None, None, None]
 
145
        try:
 
146
            tree.commit('message')
 
147
        except PreCommitException, e:
 
148
            revids[0] = e.message
 
149
        Branch.hooks["pre_commit"] = []
 
150
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
 
151
        for i in range(1, 3):
 
152
            revids[i] = tree.commit('third revision')
 
153
        self.assertEqual([
 
154
            ('pre_commit', 0, NULL_REVISION, 1, revids[0], [], []),
 
155
            ('pre_commit', 0, NULL_REVISION, 1, revids[1], [], []),
 
156
            ('pre_commit', 1, revids[1], 2, revids[2], [], [])
 
157
            ],
 
158
            self.hook_calls)
 
159
        tree.unlock()
 
160
    
 
161
    def test_pre_commit_paths(self):
 
162
        tree = self.make_branch_and_memory_tree('branch')
 
163
        tree.lock_write()
 
164
        tree.add('')
 
165
        tree.add('file', 'bang')
 
166
        tree.put_file_bytes_non_atomic('bang', 'die')
 
167
        tree.mkdir('dir', 'dirid')
 
168
        tree.add('dir/file', 'swoosh')
 
169
        tree.put_file_bytes_non_atomic('swoosh', 'swaash')
 
170
        Branch.hooks.install_hook("pre_commit", self.capture_pre_commit_hook)
 
171
        rev1 = tree.commit('first revision')
 
172
        tree.unversion(['dirid'])
 
173
        rev2 = tree.commit('second revision')
 
174
        self.assertEqual([
 
175
            ('pre_commit', 0, NULL_REVISION, 1, rev1, [], ['dir', 'dir/file', 'file']),
 
176
            ('pre_commit', 1, rev1, 2, rev2, ['dir', 'dir/file'], [])
 
177
            ],
 
178
            self.hook_calls)
 
179
        tree.unlock()