Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

automated code formatting git

I'm working on a project(PHP) and on every commit there are some breaks on code convention. I'm using git for version control. Is there a way for automated code formatting so that all the code stays clean?

like image 605
soupdiver Avatar asked Feb 23 '12 12:02

soupdiver


People also ask

How do I enable auto format in VS code?

VS Code Auto Format On SaveOpen Visual Studio Code editor. Click the “Settings” gear icon in the bottom-left corner. Search “Formatter” and click the “Editor: Default Formatter” option. From the drop-down menu, select whichever code formatter you want to use.

How do I Auto beautify code in Visual Studio?

Auto formatting settings in Visual Studio Show activity on this post. Select the text you want to automatically indent. Click menu Edit → Advanced → *Format Selection, or press Ctrl + K , Ctrl + F . Format Selection applies the smart indenting rules for the language in which you are programming to the selected text.

What is the code for beautify flutter?

Install the Flutter extension (see Editor setup) to get automatic formatting of code in VS Code. To automatically format the code in the current source code window, right-click in the code window and select Format Document . You can add a keyboard shortcut to this VS Code Preferences.


2 Answers

There are two parts to the question: automatically formatting the code, and detecting when it doesn't conform to your coding standards.

Automatically formatting the code is not really something you want to put inbetween you and your repo directly. Modifying files, or attempting to modify files, in a pre-commit hook is likely to make a mess. As such it doesn't matter what vcs you are using.

Using a tool to format the code via your editor or as a process you run (manually, or semi-automated) as part of your development workflow would be appropriate. For example, vim has the = function to auto-indent code, and as mentioned by others Pear's beautifier is one possibility to do this.

Detecting code standard devitions requires a cli tool that tells you when a file does not conform to coding standards - the obvious choice is PHP Code Sniffer (phpcs) - though it could simply be the same tool you use to beautify your code manually (if you use one) and checking that it doesn't change the file contents.

You may need to write your own standard to use with phpcs if none of the existing standards match your style.

You can use a pre-commit hook to trigger a check on the code right before you commit it - If there are code errors found, you'll be notified about them and the commit aborted. You can bypass your pre-commit hooks using git commit --no-verify

You may find this repo useful: https://github.com/AD7six/git-hooks

Example:

$ more foo.php 
<?php
    function bar() {
    }
$ git add foo.php
$ git commit -v
running php/lint.php ...    OK
running php/phpcs.php ...   FAIL
phpcs -n -s --extensions=php,ctp --encoding=UTF-8 --standard=Cake '/tmp/cakephp-git-hooks'

FILE: foo.php
---------------------------------------
FOUND 3 ERROR(S) AFFECTING 2 LINE(S)
---------------------------------------
 2 | ERROR | Space indented: Tabs for indents, spaces for alignment (Cake.WhiteSpace.ForceTabIndent)
 2 | ERROR | Line indented incorrectly; expected 0 spaces, found 4 (Cake.WhiteSpace.ScopeIndent.Incorrect)
 3 | ERROR | Space indented: Tabs for indents, spaces for alignment (Cake.WhiteSpace.ForceTabIndent)
---------------------------------------

Time: 0 seconds, Memory: 3.75Mb

$

(commit aborted, code does not meet code standards)

$ git commit -v --no-verify -m "dummy commit"
running misc/happy-commits ...  OK
[2.1 2c432f1] dummy commit
 1 files changed, 3 insertions(+), 0 deletions(-)
 create mode 100644 foo.php
$

(commit succeeded - even though code standards were not met)

like image 167
AD7six Avatar answered Sep 23 '22 18:09

AD7six


Git knows hooks. You could leverage them to have some form of checkstyle application running before each commit is accepted into the repository. Check the files in .git/hooks/ within your repository. It's possible to refuse a commit this way. I'm not sure if you can modify the commit though.

like image 2
Till Helge Avatar answered Sep 19 '22 18:09

Till Helge