Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code formatting before commit to GIT

According to my understanding, when two developers work on the same project, but use different coding styles, there is no built-in way in GIT to uniformize commited sources. Please, correct me if I'm wrong.

Should I ask all developers to format code with the same style?

Can I ask GIT to somehow format code according to that same style? Is it possible to achieve auto code format with BitBucket?

like image 957
vico Avatar asked Oct 05 '16 12:10

vico


People also ask

What are pre-commit hooks?

The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.

How do you do a pre-commit hook?

Open a terminal window by using option + T in GitKraken Client. Once the terminal windows is open, change directory to . git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file executable.

What is pre-commit config Yaml?

Pre-commit is a management tool for pre-commit hooks. This package manages the installation and execution hooks before every commit. We will need this pre-commit package as a dev-requirement. Additionally, the hook check-added-large-files is from this pre-commit package, as you can see in our . yaml file later.

What is the purpose of prettier?

Prettier enforces a consistent code style (i.e. code formatting that won't affect the AST) across your entire codebase because it disregards the original styling* by parsing it away and re-printing the parsed AST with its own rules that take the maximum line length into account, wrapping code when necessary.


2 Answers

Should I ask all developers to format code with the same style?

Yes, it's a good idea.

Projects generally have coding style guidelines to reduce the chances of this being a problem. These can range from very loose to very strict. Guidelines include, but are not limited to layout and formatting.

Most developers I have worked with are quite happy to adopt the style of the project at hand, even if it's not their personal style. This is for the greater good. It aids readability and reduces the chance of "formatting fixes" being mingled in with actual changes. If I am editing code that has no specified style guidelines, I will try to adhere to the existing style as much as possible.

The worst thing your developers can do is run the whole source files through an auto-formatter with their own layout rules before committing. This can cause sweeping changes in places that were not actually relevant to the work they were doing, and invariably lead to painful merge conflicts when you are working in multiple branches.

Can I ask GIT to somehow format code according to that same style? Is it possible to achieve auto code format with BitBucket?

I'm going to answer this by challenging why you would want to do that. Be careful about having commit hooks that do automatic formatting or reject commits based on "incorrect" style.

This is what code reviews are for, and there are always exceptions in code where a human will do it better (e.g. in C++ land, clang-format mostly does a great job but sucks at just about everything involving non-trivial initializer lists). Forcing everybody to accept the machine's interpretation is likely to just get in the way.

like image 103
paddy Avatar answered Nov 02 '22 14:11

paddy


You could install a pre-commit-hook on each developers machine and run a linter of your choice that prevents the developer from committing if the source code does not match the team standards.

The disadvantage is that this mechanisms can be outsmarted by developers by simply not installing the hook locally.

So additionally the linter should be run as part of the build process and make the build fail if the code is not properly formatted.

like image 35
Gernot Avatar answered Nov 02 '22 13:11

Gernot