automated code formatting git
Asked Answered
A

5

13

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?

Acrophobia answered 23/2, 2012 at 12:54 Comment(2)
There will be inevitable cases when you will want to break the automated rules, so it's best to run a formatter manually on your code before committing your change.Karolynkaron
yeah indeed this is a good pointAcrophobia
V
19

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)

Vanderbilt answered 23/2, 2012 at 13:35 Comment(0)
T
2

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.

Tema answered 23/2, 2012 at 12:56 Comment(0)
L
2

There are a few 'PHP beautifier' scripts around (one of them in PEAR), that you could call from git's commit hook.

Lamina answered 23/2, 2012 at 12:56 Comment(0)
B
2

You can use PHP_Beautifier to beautify your php script.

But I think beautifying in every commit might end up lots of modified line. This will make tough to understand the output of diff command.

Ben answered 23/2, 2012 at 13:4 Comment(3)
do you have any experiences with this tool? version is 0.1 and last update is 1.5 years agoAcrophobia
I still use it @skelle. At least to format each unformatted code segment comes with SO question. Normally I use Netbeans and a shortcut <kbd>Ctrl</kdb>+<kbd>Shift</kdb>+<kbd>F</kdb>Ben
NetBeans' formatting style is set in Options>Editor>Formatting, and run with shortcut Ctrl+Alt+F on whole file or selection.Frankiefrankincense
L
1

Git has commit hooks which allow you to execute command before your commit. It is not recommended to use these commit hooks to change the source code as this may cause unwanted results.

You can however make use of a git pre-commit hook to execute PHP Code sniffer. PHP code sniffer is a tool, install-able via PEAR, which will indicate where you wondered off standard. You can setup your own standards or make use of existing standards like the PEAR standard. The commit hook can be set up to display all violations (non standard code) and halt the commit. If code is according to standards, it can allow the commit.

Here is the link to the PHP Code sniffer:

http://pear.php.net/package/PHP_CodeSniffer/redirected

Here is nice example of a git pre-commit hook for php code sniffer:

https://gist.github.com/1892861

Some documentation of git commit hooks:

http://book.git-scm.com/5_git_hooks.html

Loot answered 23/2, 2012 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.