1

Me and a friend of mine are working on a codebase and a problem has arisen. So, I gladly adhere to any common style standard which has some tooling support for auto-format. My friend has harder to read code and strongly prefers things like functions in the following format

function foo()
{
    console.log('bar');
}

This helps him to distinguish the beginning and end of blocks.

Compare to this format where the opening bracket is on the same line as the function keyword.

function foo() {
    console.log('bar');
}

Is there any smooth way of enforcing a code style in the project while allowing a developer to view and seemingly add code in their preferred format? I've read about git hooks which I assume could be a part of formatting the code before commit. I cannot however find a full solution for the described scenario.

E. Sundin
  • 4,103
  • 21
  • 30

1 Answers1

1

Yes, this is called content filter driver, using .gitattributes declaration.

smudge
https://git-scm.com/book/en/v2/images/clean.png
(image from "Customizing Git - Git Attributes", from "Pro Git book")

The idea is do apply a code format on checkout, and apply another one on checkin, automatically.

So for any *.java file, you would called a formatter able to display the function block with the right curly brace policy on checkout, or with the original policy on commit.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250