mirror of https://github.com/postgres/postgres
Our repo already contained an .editorconfig file, but it was not kept up to date with .gitattributes. This adds a script that keeps these files in sync. A big advantage of the editorconfig file is that it many editors/IDEs get automatically configured to trim trailing newlines and add a final newline on save, while .gitattributes only complains about these problems instead of automatically fixing them. This also adds rules to .gitattributes for Python files as well as for C files in pg_bsd_indent directory (which have a different tab_width than most C files due to being vendored in). Author: Jelte Fennema-Nio <github-tech@jeltef.nl> Discussion: https://www.postgresql.org/message-id/flat/CAGECzQQGzbroAXi+Yicp3HvcCo4=g84kaOgjuvQ5MW9F0ubOGg@mail.gmail.compull/198/head
parent
79e872fedb
commit
2452e71ff2
@ -1,14 +1,176 @@ |
|||||||
root = true |
root = true |
||||||
|
|
||||||
[*.{c,h,l,y,pl,pm}] |
[*] |
||||||
indent_style = tab |
|
||||||
indent_size = tab |
indent_size = tab |
||||||
|
|
||||||
|
[*] |
||||||
|
trim_trailing_whitespace = true |
||||||
|
insert_final_newline = true |
||||||
|
indent_style = unset |
||||||
|
tab_width = unset |
||||||
|
|
||||||
|
[*.[chly]] |
||||||
|
trim_trailing_whitespace = true |
||||||
|
insert_final_newline = true |
||||||
|
indent_style = tab |
||||||
|
tab_width = 4 |
||||||
|
|
||||||
|
[*.cpp] |
||||||
|
trim_trailing_whitespace = true |
||||||
|
insert_final_newline = true |
||||||
|
indent_style = tab |
||||||
|
tab_width = 4 |
||||||
|
|
||||||
|
[*.pl] |
||||||
|
trim_trailing_whitespace = true |
||||||
|
insert_final_newline = true |
||||||
|
indent_style = tab |
||||||
tab_width = 4 |
tab_width = 4 |
||||||
|
|
||||||
[*.{sgml,xml}] |
[*.pm] |
||||||
|
trim_trailing_whitespace = true |
||||||
|
insert_final_newline = true |
||||||
|
indent_style = tab |
||||||
|
tab_width = 4 |
||||||
|
|
||||||
|
[*.po] |
||||||
|
trim_trailing_whitespace = true |
||||||
|
insert_final_newline = unset |
||||||
|
indent_style = space |
||||||
|
tab_width = unset |
||||||
|
|
||||||
|
[*.py] |
||||||
|
trim_trailing_whitespace = true |
||||||
|
insert_final_newline = true |
||||||
indent_style = space |
indent_style = space |
||||||
|
tab_width = unset |
||||||
|
indent_size = 4 |
||||||
|
|
||||||
|
[*.sgml] |
||||||
|
trim_trailing_whitespace = true |
||||||
|
insert_final_newline = true |
||||||
|
indent_style = space |
||||||
|
tab_width = unset |
||||||
indent_size = 1 |
indent_size = 1 |
||||||
|
|
||||||
[*.xsl] |
[*.xml] |
||||||
|
trim_trailing_whitespace = true |
||||||
|
insert_final_newline = true |
||||||
indent_style = space |
indent_style = space |
||||||
|
tab_width = unset |
||||||
indent_size = 2 |
indent_size = 2 |
||||||
|
|
||||||
|
[*.xsl] |
||||||
|
trim_trailing_whitespace = true |
||||||
|
insert_final_newline = true |
||||||
|
indent_style = space |
||||||
|
tab_width = unset |
||||||
|
indent_size = 1 |
||||||
|
|
||||||
|
[*.data] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[contrib/pgcrypto/sql/pgp-armor.sql] |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = true |
||||||
|
indent_style = unset |
||||||
|
tab_width = unset |
||||||
|
|
||||||
|
[src/backend/catalog/sql_features.txt] |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = true |
||||||
|
indent_style = unset |
||||||
|
tab_width = unset |
||||||
|
|
||||||
|
[*.out] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[src/interfaces/ecpg/test/expected/*] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[configure] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[ppport.h] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[src/backend/jit/llvm/SectionMemoryManager.cpp] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[src/backend/jit/llvm/SectionMemoryManager.LICENSE] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[src/backend/regex/COPYRIGHT] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[src/backend/snowball/libstemmer/*.c] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[src/backend/utils/mb/Unicode/*-std.txt] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[src/include/jit/SectionMemoryManager.h] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[src/include/snowball/libstemmer/*] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[src/timezone/data/*] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[src/tools/pg_bsd_indent/*] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[src/tools/pg_bsd_indent/tests/*] |
||||||
|
indent_style = unset |
||||||
|
indent_size = unset |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
|
||||||
|
[src/tools/pg_bsd_indent/*.[ch]] |
||||||
|
trim_trailing_whitespace = unset |
||||||
|
insert_final_newline = unset |
||||||
|
indent_style = unset |
||||||
|
tab_width = 8 |
||||||
|
@ -0,0 +1,94 @@ |
|||||||
|
#!/usr/bin/env python3 |
||||||
|
|
||||||
|
import os |
||||||
|
|
||||||
|
|
||||||
|
def cd_to_repo_root(): |
||||||
|
abspath = os.path.abspath(__file__) |
||||||
|
dname = os.path.join(os.path.dirname(abspath), "..", "..") |
||||||
|
os.chdir(dname) |
||||||
|
|
||||||
|
|
||||||
|
# Space based indentation levels are not tracked in .gitattributes, so |
||||||
|
# we hardcode them here for the relevant filetypes. |
||||||
|
space_based_indent_sizes = { |
||||||
|
"*.py": 4, |
||||||
|
"*.sgml": 1, |
||||||
|
"*.xsl": 1, |
||||||
|
"*.xml": 2, |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
def main(): |
||||||
|
cd_to_repo_root() |
||||||
|
|
||||||
|
with open(".gitattributes", "r") as f: |
||||||
|
lines = f.read().splitlines() |
||||||
|
|
||||||
|
new_contents = """root = true |
||||||
|
|
||||||
|
[*] |
||||||
|
indent_size = tab |
||||||
|
""" |
||||||
|
|
||||||
|
for line in lines: |
||||||
|
if line.startswith("#") or len(line) == 0: |
||||||
|
continue |
||||||
|
name, git_rules = line.split() |
||||||
|
if git_rules == "-whitespace": |
||||||
|
rules = [ |
||||||
|
"indent_style = unset", |
||||||
|
"indent_size = unset", |
||||||
|
"trim_trailing_whitespace = unset", |
||||||
|
"insert_final_newline = unset", |
||||||
|
] |
||||||
|
elif git_rules.startswith("whitespace="): |
||||||
|
git_whitespace_rules = git_rules.replace("whitespace=", "").split(",") |
||||||
|
rules = [] |
||||||
|
if "-blank-at-eol" in git_whitespace_rules: |
||||||
|
rules += ["trim_trailing_whitespace = unset"] |
||||||
|
else: |
||||||
|
rules += ["trim_trailing_whitespace = true"] |
||||||
|
|
||||||
|
if "-blank-at-eof" in git_whitespace_rules: |
||||||
|
rules += ["insert_final_newline = unset"] |
||||||
|
else: |
||||||
|
rules += ["insert_final_newline = true"] |
||||||
|
|
||||||
|
if "tab-in-indent" in git_whitespace_rules: |
||||||
|
rules += ["indent_style = space"] |
||||||
|
elif "indent-with-non-tab" in git_whitespace_rules: |
||||||
|
rules += ["indent_style = tab"] |
||||||
|
elif name in ["*.pl", "*.pm"]: |
||||||
|
# We want editors to use tabs for indenting Perl |
||||||
|
# files, but we cannot add it such a rule to |
||||||
|
# .gitattributes, because certain lines are still |
||||||
|
# indented with spaces (e.g. SYNOPSIS blocks). So we |
||||||
|
# hardcode the rule here. |
||||||
|
|
||||||
|
rules += ["indent_style = tab"] |
||||||
|
else: |
||||||
|
rules += ["indent_style = unset"] |
||||||
|
|
||||||
|
tab_width = "unset" |
||||||
|
for rule in git_whitespace_rules: |
||||||
|
if rule.startswith("tabwidth="): |
||||||
|
tab_width = rule.replace("tabwidth=", "") |
||||||
|
rules += [f"tab_width = {tab_width}"] |
||||||
|
|
||||||
|
if name in space_based_indent_sizes: |
||||||
|
indent_size = space_based_indent_sizes[name] |
||||||
|
rules += [f"indent_size = {indent_size}"] |
||||||
|
|
||||||
|
else: |
||||||
|
continue |
||||||
|
|
||||||
|
rules = "\n".join(rules) |
||||||
|
new_contents += f"\n[{name}]\n{rules}\n" |
||||||
|
|
||||||
|
with open(".editorconfig", "w") as f: |
||||||
|
f.write(new_contents) |
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__": |
||||||
|
main() |
Loading…
Reference in new issue