Mercurial > hg
annotate contrib/check-commit @ 24725:ee751d47cf2c
vfs: add walk
To eliminate "path prefix" (= "the root of vfs") part from "dirpath"
yielded by "os.walk()" correctly, "path prefix" should have "os.sep"
at the end of own string, but it isn't easy to ensure it, because:
- examination by "path.endswith(os.sep)" isn't portable
Some problematic encodings use 0x5c (= "os.sep" on Windows) as the
tail byte of some multi-byte characters.
- "os.path.join(path, '')" isn't portable
With Python 2.7.9, this invocation doesn't add "os.sep" at the end
of UNC path (see issue4557 for detail).
Python 2.7.9 changed also behavior of "os.path.normpath()" (see *) and
"os.path.splitdrive()" for UNC path.
vfs root normpath splitdrive os.sep required
=============== ============== =================== ============
z:\ z:\ z: + \ no
z:\foo z:\foo z: + \foo yes
z:\foo\ z:\foo z: + \foo yes
[before Python 2.7.9]
\\foo\bar \\foo\bar '' + \\foo\bar yes
\\foo\bar\ \\foo\bar (*) '' + \\foo\bar yes
\\foo\bar\baz \\foo\bar\baz '' + \\foo\bar\baz yes
\\foo\bar\baz\ \\foo\bar\baz '' + \\foo\bar\baz yes
[Python 2.7.9]
\\foo\bar \\foo\bar \\foo\bar + '' yes
\\foo\bar\ \\foo\bar\ (*) \\foo\bar + \ no
\\foo\bar\baz \\foo\bar\baz \\foo\bar + \baz yes
\\foo\bar\baz\ \\foo\bar\baz \\foo\bar + \baz yes
If it is ensured that "normpath()"-ed vfs root is passed to
"splitdrive()", adding "os.sep" is required only when "path" part of
"splitdrive()" result isn't "os.sep" itself. This is just what
"pathutil.nameasprefix()" examines.
This patch applies "os.path.normpath()" on "self.join(None)"
explicitly, because it isn't ensured that vfs root is already
normalized: vfs itself is constructed with "realpath=False" (= avoid
normalizing in "vfs.__init__()") in many code paths.
This normalization should be much cheaper than subsequent file I/O for
directory traversal.
author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
---|---|
date | Sat, 11 Apr 2015 23:00:04 +0900 |
parents | 868cec6409c4 |
children | 85fc79707cb2 |
rev | line source |
---|---|
22043
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
2 # |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
3 # Copyright 2014 Matt Mackall <mpm@selenic.com> |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
4 # |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
5 # A tool/hook to run basic sanity checks on commits/patches for |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
6 # submission to Mercurial. Install by adding the following to your |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
7 # .hg/hgrc: |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
8 # |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
9 # [hooks] |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
10 # pretxncommit = contrib/check-commit |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
11 # |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
12 # The hook can be temporarily bypassed with: |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
13 # |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
14 # $ BYPASS= hg commit |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
15 # |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
16 # See also: http://mercurial.selenic.com/wiki/ContributingChanges |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
17 |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
18 import re, sys, os |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
19 |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
20 errors = [ |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
21 (r"[(]bc[)]", "(BC) needs to be uppercase"), |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
22 (r"[(]issue \d\d\d", "no space allowed between issue and number"), |
24703
868cec6409c4
check-commit: be more picky about detection of wrong bug tag
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
24049
diff
changeset
|
23 (r"[(]bug(\d|\s)", "use (issueDDDD) instead of bug"), |
22043
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
24 (r"^# User [^@\n]+$", "username is not an email address"), |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
25 (r"^# .*\n(?!merge with )[^#]\S+[^:] ", |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
26 "summary line doesn't start with 'topic: '"), |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
27 (r"^# .*\n[A-Z][a-z]\S+", "don't capitalize summary lines"), |
24049
ba272156113f
check-commit: check capitalization in summary lines
Eric Sumner <ericsumner@fb.com>
parents:
22058
diff
changeset
|
28 (r"^# .*\n[^\n]*: *[A-Z][a-z]\S+", "don't capitalize summary lines"), |
22043
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
29 (r"^# .*\n.*\.\s+$", "don't add trailing period on summary line"), |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
30 (r"^# .*\n.{78,}", "summary line too long"), |
22058
15d0390a27fe
check-commit: spot growing whitespace
Matt Mackall <mpm@selenic.com>
parents:
22043
diff
changeset
|
31 (r"^\+\n \n", "adds double empty line"), |
22043
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
32 (r"\+\s+def [a-z]+_[a-z]", "adds a function with foo_bar naming"), |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
33 ] |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
34 |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
35 node = os.environ.get("HG_NODE") |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
36 |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
37 if node: |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
38 commit = os.popen("hg export %s" % node).read() |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
39 else: |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
40 commit = sys.stdin.read() |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
41 |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
42 exitcode = 0 |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
43 for exp, msg in errors: |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
44 m = re.search(exp, commit, re.MULTILINE) |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
45 if m: |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
46 pos = 0 |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
47 for n, l in enumerate(commit.splitlines(True)): |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
48 pos += len(l) |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
49 if pos >= m.end(): |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
50 print "%d: %s" % (n, msg) |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
51 print " %s" % l[:-1] |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
52 if "BYPASS" not in os.environ: |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
53 exitcode = 1 |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
54 break |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
55 |
1274ff3f20a8
contrib: add check-commit hook script to sanity-check commits
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
56 sys.exit(exitcode) |