| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import os |
| 4 |
|
| 5 |
|
| 6 |
def cd_to_repo_root(): |
| 7 |
abspath = os.path.abspath(__file__) |
| 8 |
dname = os.path.join(os.path.dirname(abspath), "..", "..") |
| 9 |
os.chdir(dname) |
| 10 |
|
| 11 |
|
| 12 |
# Space based indentation levels are not tracked in .gitattributes, so |
| 13 |
# we hardcode them here for the relevant filetypes. |
| 14 |
space_based_indent_sizes = { |
| 15 |
"*.py": 4, |
| 16 |
"*.sgml": 1, |
| 17 |
"*.xsl": 1, |
| 18 |
"*.xml": 2, |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
def main(): |
| 23 |
cd_to_repo_root() |
| 24 |
|
| 25 |
with open(".gitattributes", "r") as f: |
| 26 |
lines = f.read().splitlines() |
| 27 |
|
| 28 |
new_contents = """root = true |
| 29 |
|
| 30 |
[*] |
| 31 |
indent_size = tab |
| 32 |
""" |
| 33 |
|
| 34 |
for line in lines: |
| 35 |
if line.startswith("#") or len(line) == 0: |
| 36 |
continue |
| 37 |
name, git_rules = line.split() |
| 38 |
if git_rules == "-whitespace": |
| 39 |
rules = [ |
| 40 |
"indent_style = unset", |
| 41 |
"indent_size = unset", |
| 42 |
"trim_trailing_whitespace = unset", |
| 43 |
"insert_final_newline = unset", |
| 44 |
] |
| 45 |
elif git_rules.startswith("whitespace="): |
| 46 |
git_whitespace_rules = git_rules.replace("whitespace=", "").split(",") |
| 47 |
rules = [] |
| 48 |
if "-blank-at-eol" in git_whitespace_rules: |
| 49 |
rules += ["trim_trailing_whitespace = unset"] |
| 50 |
else: |
| 51 |
rules += ["trim_trailing_whitespace = true"] |
| 52 |
|
| 53 |
if "-blank-at-eof" in git_whitespace_rules: |
| 54 |
rules += ["insert_final_newline = unset"] |
| 55 |
else: |
| 56 |
rules += ["insert_final_newline = true"] |
| 57 |
|
| 58 |
if "tab-in-indent" in git_whitespace_rules: |
| 59 |
rules += ["indent_style = space"] |
| 60 |
elif "indent-with-non-tab" in git_whitespace_rules: |
| 61 |
rules += ["indent_style = tab"] |
| 62 |
elif name in ["*.pl", "*.pm"]: |
| 63 |
# We want editors to use tabs for indenting Perl |
| 64 |
# files, but we cannot add it such a rule to |
| 65 |
# .gitattributes, because certain lines are still |
| 66 |
# indented with spaces (e.g. SYNOPSIS blocks). So we |
| 67 |
# hardcode the rule here. |
| 68 |
|
| 69 |
rules += ["indent_style = tab"] |
| 70 |
else: |
| 71 |
rules += ["indent_style = unset"] |
| 72 |
|
| 73 |
tab_width = "unset" |
| 74 |
for rule in git_whitespace_rules: |
| 75 |
if rule.startswith("tabwidth="): |
| 76 |
tab_width = rule.replace("tabwidth=", "") |
| 77 |
rules += [f"tab_width = {tab_width}"] |
| 78 |
|
| 79 |
if name in space_based_indent_sizes: |
| 80 |
indent_size = space_based_indent_sizes[name] |
| 81 |
rules += [f"indent_size = {indent_size}"] |
| 82 |
|
| 83 |
else: |
| 84 |
continue |
| 85 |
|
| 86 |
rules = "\n".join(rules) |
| 87 |
new_contents += f"\n[{name}]\n{rules}\n" |
| 88 |
|
| 89 |
with open(".editorconfig", "w") as f: |
| 90 |
f.write(new_contents) |
| 91 |
|
| 92 |
|
| 93 |
if __name__ == "__main__": |
| 94 |
main() |