Coder Social home page Coder Social logo

git-hooks-code-autoformat's Introduction

git-hooks-code-autoformat

A collection of git pre-commit hooks to help with code auto-formatting.

Licensed under Apache License, Version 2.0.

Please, share your own formatters back. PRs are welcome!

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/michalrus/git-hooks-code-autoformat.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.

IMPORTANT: for Windows users, due to limitations on Windows shell emulators, links seem to be implemented more often than not as 'copy', so any time the git-hooks directory is updated, the linking step needs to be re-done.

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.

git-hooks-code-autoformat's People

Contributors

aldahick avatar michalrus avatar mizux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

git-hooks-code-autoformat's Issues

Could I only format changed lines by this tool?

This means, if I changed some lines in one file, I don't want to format all lines in this file. I just want to format the lines changed by me. If all lines has been formatted, GIT BLAME will pointed to me!

Licensing

Hi,

I am a maintainer of the Soot Java optimization framework.
We are currently discussing the usage of your tool to enforce coding guidelines. However, I am not sure if we are allowed to do so since Soot is licensed under LGPLv2.1. As far as I know, it is incompatible with Apache 2.0. I am not sure if we might still be able to use is without directly accessing the source code and not shipping it in our repo, though. On the other hand, your current way of deployment requires including the tool into our repository.

Do you know if we would be able to incorporate it without licensing issues? If not, is there any change you might double license it to LGPLv2.1 sometime?

I would also like to know if you are making progress with your known issue of adding, modifying and committing some files?

Manuel

indent for text style elements in svg files

I would like to use this project to autoformat svg files I create with Inkscape.

Unfortunately the code is still not perfectly formated. 😢

I only added one character to the text.
See:

image

It would also be great if the style-attributes would be splittet into separate lines as well.

Is this possible?

Thanks

Only symlink pre-commit instead of whole hooks directory

I don't want to symlink my whole hooks directory. Instead, I want to symlink only the pre-commit script, so that I can add extra hooks locally without having untracked files or committing them.

Right now if I do git status, I have an untracked file:
git-hooks/prepare-commit-msg
which I don't want to commit (I don't want other developers using my prepare-commit-msg script). So if I am able to only symlink pre-commit I can have my own, local, scripts too.

Error on git-commit

Error on git-commit.
######################################################################## 100.0% http://download.eclipse.org/eclipse/updates/4.4/R-4.4.2-201502041700/plugins/org.eclipse.text_3.5.300.v20130515-1451.jar ######################################################################## 100.0% Recompiling /d/athenaC/athena-jetty-daemons/.git/hooks/tools/eclipse-java-formatter/src/ejf/Main.java ... Error: Could not find or load main class ejf.Main fatal: ejffailed for: D:/athenaC/athena-jetty-daemons/projects/Provisioning/src/main/java/com/oracle/athena/provisioning/controller/ChatCredentialsController.java

Cann't use on mac os system

I use this hook on my mac and it didn't work when i done everything written in README step by step.

The error is: find: -executable: unknown primary or operator

did i make mistake on somewhere?

our own custom formatter

Hello

Can we add our own custom formatter there ? For example I want to modify the line indentation from 3 to 5. Do you think a custom change is available with git-hooks-code-autoformat?

Regards

Can't find formatter specific to js-2016 inline HTML

Hello guys.
Have some need to keep my JS files formatted in github repo and among peoples.
Have such bloc in one file

    render() {
        const { application, services, iWantTos, stages, actions, gameplan, dispatch } = this.props;

        return (
             <div className={styles.root}>
           <div className={styles.hero}>

              <img
                alt="A staircase up to the next level of your startup."
                src={`${imgPath}mobile-landingPage-staircase.png`}
                srcSet={`
                  ${imgPath}[email protected] 2x,
                  ${imgPath}[email protected] 3x
                `}
                className={styles.heroImage}
              />

The second div line is improperly aligned as you see. It should be nested, but it does not. I've slightly modified typescript formatter, but this does not help, Any suggestion?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.