Skip to content

Inpher/git-code-auto-format

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

git-hooks-code-autoformat

A collection of git pre-commit hooks to help with code auto-formatting. This will auto format the code just before any git commit locally on the machine. The formatting for Java is following Google coding standard except that identation is of 4 spaces.

OS X

To make this work on OS X please install GNU SED:

brew install gnu-sed

How to use

You could just copy contents of this repo to $PROJECT/.git/hooks/. However, a slightly better way might be:

  1. $ cd $PROJECT
  2. `$ git remote add git-hooks-code-autoformat https://github.com/Inpher/git-code-auto-format.git
  3. $ git subtree add --prefix=git-hooks/ git-hooks-code-autoformat master

Now, you have $PROJECT/git-hooks/ directory with contents of this repo. This subtree is updateable with $ git subtree pull --prefix=git-hooks/ git-hooks-code-autoformat master. Any person can just clone your project and they'll have $PROJECT/git-hooks in place at the very moment.

What's left is to symlink $PROJECT/.git/hooks to $PROJECT/git-hooks:

  1. $ cd .git && mv hooks hooks.old && ln -s ../git-hooks hooks

IMPORTANT: this linking step has to be done manually by every developer.

The usual workflow then is:

  1. Modify some files.
  2. $ git add them.
  3. $ git commit them.

Now, for each staged ("git-added") file, an autoformatter will be called, and then the file will be re-added, and only then the git commit will continue. This might create a problem, see Known problems below.

If an error occurs (e.g. trying to format a not so well-formed XML), an error message is printed and the commit is aborted.

Adding your own formatters

  1. Take a look at the existing formatters above.
  2. Create autoformat/NAME.patterns file containing a regular expressions—1 per line—matching all file names to be treated with your NAME formatter.
  3. AND/OR create autoformat/NAME.magic file containing a regular expressions—1 per line—matching outputs of libmagic’s /usr/bin/file "$FILE" for all files to be treated with your NAME formatter.
  4. Create autoformat/NAME executable script. It might be called several times, but always with one argument, an absolute path to a file to format. When formatting, you've got to modify this file in place.
  5. If you want to abort the commit, print an error message and exit from the formatter with a non-zero exit code.

Known problems

  1. The pre-commit hook autoformats every staged ("git-added") file (if an applicable formatter exists) and then re-adds the file to the stage. Now, if you modify some file, $ git add it, then modify it again and then $ git commit—and there exists a formatter for this type of a file—second modification will also be commited.

About

Git hooks for code auto formatting

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages