Merge lp:~jr.allen/unifield-server/pre-commit into lp:unifield-server

Proposed by Jeff Allen
Status: Needs review
Proposed branch: lp:~jr.allen/unifield-server/pre-commit
Merge into: lp:unifield-server
Diff against target: 78 lines (+68/-0)
2 files modified
tools/precommit/README (+4/-0)
tools/precommit/__init__.py (+64/-0)
To merge this branch: bzr merge lp:~jr.allen/unifield-server/pre-commit
Reviewer Review Type Date Requested Status
jftempo Pending
Review via email: mp+309677@code.launchpad.net
To post a comment you must log in.
4005. By Jeff Allen

Solve problems with the _ builtin used by gettext.

Unmerged revisions

4005. By Jeff Allen

Solve problems with the _ builtin used by gettext.

4004. By Jeff Allen

Can skip by setting SKIP_PRECOMMIT=1. Essential for merge commits.

4003. By Jeff Allen

Merge

4002. By Jeff Allen

Fixed for bzr 2.1

4001. By Jeff Allen

Fix for Python 2.6

4000. By Jeff Allen

Remove non-working exclude option.

3999. By Jeff Allen

Fixed a different typo.

3998. By Jeff Allen

Fixed a typo.

3997. By Jeff Allen

Changed name to reflect what it does
Added a bzr command to do a check before trying to commit: bzr precommit

3996. By Jeff Allen

More helpful error messages.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'tools/precommit'
2=== added file 'tools/precommit/README'
3--- tools/precommit/README 1970-01-01 00:00:00 +0000
4+++ tools/precommit/README 2017-05-19 10:29:56 +0000
5@@ -0,0 +1,4 @@
6+To start using this bazaar pre-commit hook:
7+
8+ mkdir $HOME/.bazaar/plugins
9+ ln -s `pwd`/tools/precommit $HOME/.bazaar/plugins
10
11=== added file 'tools/precommit/__init__.py'
12--- tools/precommit/__init__.py 1970-01-01 00:00:00 +0000
13+++ tools/precommit/__init__.py 2017-05-19 10:29:56 +0000
14@@ -0,0 +1,64 @@
15+#!/usr/bin/env python
16+
17+import subprocess
18+import os
19+
20+from bzrlib import branch, errors
21+from bzrlib.commands import Command, register_command
22+from bzrlib.workingtree import WorkingTree
23+
24+from distutils.spawn import find_executable
25+
26+def precommit(local_branch, master_branch, old_revision_number, old_revision_id, future_revision_number, future_revision_id, tree_delta, future_tree):
27+ if 'SKIP_PRECOMMIT' in os.environ:
28+ return
29+
30+ files_to_check = []
31+ for i in tree_delta.added, tree_delta.modified:
32+ for f in i:
33+ if f[0].endswith('.py'):
34+ files_to_check.append(f[0])
35+
36+ if len(files_to_check) == 0:
37+ return
38+
39+ for exe in [ 'autopep8', 'pyflakes' ]:
40+ if find_executable(exe) is None:
41+ raise errors.BzrError("Required command %s is missing." % exe)
42+
43+ cmd = [ 'autopep8', '--select=E1', '--diff' ]
44+ cmd.extend(files_to_check)
45+ res = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
46+
47+ if res != '':
48+ cmd[2] = '--in-place'
49+ raise errors.BzrError("Fix autopep8 warnings before committing. You might want to use this command: %s" % " ".join(cmd))
50+
51+ cmd = [ 'pyflakes' ]
52+ cmd.extend(files_to_check)
53+ env = os.environ.copy()
54+ env["PYFLAKES_BUILTINS"]="_"
55+ result = subprocess.call(cmd, env=env)
56+
57+ if result != 0:
58+ raise errors.BzrError("Fix pyflakes warnings before committing.")
59+
60+
61+class cmd_precommit(Command):
62+ __doc__ = """Check changes with autopep8 and pyflakes.
63+ """
64+ takes_args = ['selected*']
65+
66+ def run(self, message=None, selected_list=None):
67+ if hasattr(WorkingTree, "open_containing_paths"):
68+ tree, selected_list = WorkingTree.open_containing_paths(selected_list)
69+ else:
70+ # For bzr 2.1.x
71+ from bzrlib.builtins import tree_files
72+ tree, selected_list = tree_files(".")
73+
74+ precommit(None, None, None, None, None, None, tree.changes_from(tree.basis_tree()), None)
75+ print "No problems found. Ready to commit."
76+
77+register_command(cmd_precommit)
78+branch.Branch.hooks.install_named_hook('pre_commit', precommit, 'pyflakes warnings prevent check-in')

Subscribers

People subscribed via source and target branches

to all changes: