Skip to content

Commit

Permalink
Set up: Add githooks for autoformatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kononovk committed Mar 27, 2023
1 parent 7ad3d71 commit e5457f8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .githooks/how_to_use.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# How to use git hooks for this repository:

* Set `.githooks` directory as root directory for git hooks:

```bash
git config --local core.hooksPath ./.githooks
```

or

* Copy `pre-commit` file to `.git/hooks`:

```bash
cp ./.githooks/pre-commit ./.git/hooks/
```
58 changes: 58 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python

import subprocess


def last_line_valid(path):
"""
Check last file line have linebreak
:param path: path to file
:return: have or haven't
"""
with open(path, 'r') as f:
text = f.readlines()
try:
last_char = text[-1][-1]
return last_char == '\n'
except IndexError:
return False


def fix_last_line(path):
"""
Add a linebreak to the end of file if it doesn't have one
"""
if last_line_valid(path):
return
with open(path, 'a') as f:
print(f'Add linebreak to the file: {path}')
f.write('\n')


def fix_clang_format(path):
"""
Fix clang-format
"""
if path.endswith('.cpp') or path.endswith('.hpp'):
print(f'clang-format fix: {path}')
subprocess.Popen(['clang-format', path, '-i'], stdout=subprocess.DEVNULL).communicate()


def fix_format():
git_diff = subprocess.Popen('git diff-index --name-status --cached HEAD'.split(), stdout=subprocess.PIPE)
remove_deleted = subprocess.Popen('grep -v ^D'.split(), stdin=git_diff.stdout, stdout=subprocess.PIPE)
get_second_column = subprocess.Popen(['awk', '{ print $2 }'], stdin=remove_deleted.stdout, stdout=subprocess.PIPE)
git_diff.stdout.close()
remove_deleted.stdout.close()
output = get_second_column.communicate()[0].decode('utf-8').strip()
if not output:
return
for file in output.split('\n'):
path = f'./{file}'
fix_last_line(path)
fix_clang_format(path)
subprocess.Popen(['git', 'add'] + output.split('\n')).communicate()


if __name__ == '__main__':
fix_format()

0 comments on commit e5457f8

Please sign in to comment.