Jim
Jim

Reputation: 1568

Using git, how to auto-format when pull/clone/fetching from github

OK, so our team is new and we're working on determining a style guide for our code. We are a GoLang shop, so formatting that is simple.

Shell script code (among many) don't have a single canonical style guide by which to go.

We will use shfmt (because it's seemingly decent) in a commit hook, which will at least keep our code style homogenous in the repo. But what about some way of being able to "bend" the code back from the homogenous style by shfmt to our individual styles? Does something like that exist?

I guess I could just use shfmt or one of the other 10-odd tools out there that "beautify" shell-code and configure that tool to the closest settings possible to get back my style. That is a likely direction in which I'll need to go.

Two questions:

  1. anyone know of a tool that can go foward or backward in formatting?
  2. What about an auto-clone/pull/fetch hook or way that would do this automatically?

Obviously, I can write a wrapper script that I exec after a pull, but just wondering if anyone has ever done anything like this before.

Upvotes: 1

Views: 617

Answers (1)

VonC
VonC

Reputation: 1323203

That would not be a practical approach ("forward or backward in formatting"), because it involves rewriting the commits (changing their content, hence their SHA1)

The usual approach is simply to refuse any pushed commit, if its shfmt output differ from the commit content pushed (meaning the code was not properly formatted before pushing.

Still, should you want to make sure that past commits are formatted, you would use git filter-branch, as described in "Reformatting Your Codebase with git filter-branch" by Elliot Chance.

git filter-branch --tree-filter 'shfmt $(\
  git show $GIT_COMMIT --name-status | egrep ^[AM] |\
    grep .sh| cut -f2)' -- --all

However, all developer would need to reset his/her local clone to the new rewritten repo.

I'm simply seeking a way to display my style in my local repo.

The issue is to display your style while not changing the content.
You can try a smudge/clean content filter approach as in here.

Upvotes: 1

Related Questions