vico
vico

Reputation: 18175

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?

Upvotes: 22

Views: 24520

Answers (6)

Vishal Goyal
Vishal Goyal

Reputation: 55

.vscode/settings.json

{ "editor.formatOnSave": true }

Worked great for me!!

Upvotes: 0

David Wolf
David Wolf

Reputation: 1738

When all developers are using VS Code you can add configuration to the repo, that applies formatting on each save.

This requires that a formatter is installed, which could be an extension like Prettier.

.vscode/settings.json

{
    "editor.formatOnSave": true
}

To make sure everybody has a formatter available you could make use of devcontainers instead, extensions can be listed for installation here, and will be installed when anybody from the team opens the devcontainer.

.devcontainer.json or .devcontainer/devcontainer.json

{
  // …
  "customizations": {
    "vscode": {
      "settings": {
        "editor.formatOnSave": true
      },
      "extensions": [
        "esbenp.prettier-vscode"
      ]
    }
  }
}

The latter one requires that the devcontainer extension is installed and Docker is available.


Note that these settings override the user settings from VS Code, while working in a workspace or devcontainer these settings are applied prior to your custom user settings.

Upvotes: 1

Kim
Kim

Reputation: 596

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

Yes and there might be several tools which can help you with just that.

Som editors and environments support a file format called .editorconfig where you can specify the coding style. e.g. tabs or spaces and if spaces how many spaces for one code indent. https://editorconfig.org/

I use this with visual studio where it works very well. Other environments or editors might require you to add a plugin before they support it (like visual code) or have their own way of defining code style in the project.

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

If you want to enforce a code style rather than just agree upon it (.editorconfig) then you might want to look into git hooks.

I do not personally have any experience with them but one of the examples given is to "Enforce project coding standards." so you might want to look into them.

https://githooks.com/

Upvotes: 2

paddy
paddy

Reputation: 63451

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.

Upvotes: 9

Gernot
Gernot

Reputation: 384

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.

Upvotes: 4

pguetschow
pguetschow

Reputation: 5337

As far as I'm informed, BitBucket doesn't feature this.

But I think it's a good idea to find a common style, in order to provide fast excange.

For this it's sometimes useful to use an IDE with integrated formatting features and share the settings, if this is possible. I think Eclipse would be a good solution, as there are a lot of languages supported.

In my team's case we're using MS Visual Studio and the Allmann Style, as It's natively supported by the autoformatting.

Upvotes: 1

Related Questions