5
\$\begingroup\$

In our hg workflow we use default as testing branch and stable as the one that contains stable code. All changes are made in feature-branches that are started from latest stable. This hook checks if there is a "direct" commit to default or stable (they are denied, only merges may change contents of default and stable) or that new feature branch has been started from default (which is denied by conventions too).

Any proposal to make it more "pythonic" (or just better)?

from mercurial import context
def check(ui, repo, hooktype, node=None, source=None, **kwargs):
 for rev in xrange(repo[node].rev(), len(repo)):
 ctx = context.changectx(repo, rev)
 parents = ctx.parents()
 if len(parents) == 1:
 if ctx.branch() in ['default', 'stable']:
 ui.warn('!!! You cannot commit directly to %s at %s !!!' % (ctx.branch(), ctx.hex()))
 return True
 if parents[0].branch() == 'default':
 ui.warn('!!! You cannot start your private branch from default at %s !!!' % (ctx.hex()))
 return True
 return False
asked Aug 30, 2011 at 6:57
\$\endgroup\$
2
  • \$\begingroup\$ The correct word is pythonic not pythonish. \$\endgroup\$ Commented Aug 30, 2011 at 12:06
  • \$\begingroup\$ @Winston Ewert: fixed ;-) \$\endgroup\$ Commented Aug 30, 2011 at 12:08

1 Answer 1

5
\$\begingroup\$
 for rev in xrange(repo[node].rev(), len(repo)):
 ctx = context.changectx(repo, rev)

In Python, I generally try to avoid iterating using xrange. I prefer to iterate over what I'm interested in.

def revisions(repo, start, end):
 for revision_number in xrange(start, end):
 yield context.changectx(repo, revision_number)
for rev in revisions(repo, repo[node].rev(), len(repo)):
 ...

Although I'm not sure its worthwhile in this case.

The only other issue is your use of abbreviations like repo and rev. They aren't that bad because its pretty clear from the context what they stand for. But I'd write them fully out.

answered Aug 30, 2011 at 12:16
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think you mean for revision_number in xrange(start, end): in that second snippet. \$\endgroup\$ Commented Aug 31, 2011 at 1:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.