5

In order to clean our code we want to use astyle. The matter is that we want someone to do the job, but blaming previous committer (real author of the code and not the one who cleans).

Is there a way to do it safely in git ?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
Poko
  • 792
  • 2
  • 8
  • 24
  • 2
    Author and Commiter are potentially 2 different people — if they are 2 people then you should be recording both: http://stackoverflow.com/questions/18750808/difference-between-author-and-committer-in-git – cmbuckley Apr 28 '15 at 10:35
  • You should have developers format their own patches, not have some separate person come in and 'clean up' other people's patches. You can also use a pre-commit git hook to do the formatting. – bames53 Nov 24 '15 at 16:41

2 Answers2

2

The problem is when you clearly think about it. You let one people change your code but you can't see that this was that people during the cleanup. That would be very stupid.

So in my mind you shouldn't do that. Let that person change the code with a normal commit.

What you can do is you can set the author during the commit.

git checkout master
git merge my_branch
git commit --amend --author="Your name <my.adress@email.com>"
git push origin master

Or set it directly on commit.

git commit --author="Your name <my.adress@email.com>" ....

But i think thats not the best way.

Edit: There is a very intelligent phrase Don't fight the framework.

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • 1
    I think, you did'nt get my purpose, the fact is that those modifications that I am speaking about are not real refactoring, with algorithmic consequences, it is just about coding style. So I don't want to blame someone for adding spaces – Poko Apr 28 '15 at 11:38
  • 1
    Ok but the problem is when someone correct the styles its possible that he delete something which broke everything ;) so its important to add all your changes. – René Höhle Apr 28 '15 at 11:52
  • Also the "git gui blame" tool is pretty smart about how it blames authors - an indentation change will show a tooltip like "Originally By: A.Committer, Copied or Moved here by: B.Refactor". – richq Apr 28 '15 at 13:03
-1

There are at least two ways to deal with the situation.

New commit, ignored by git blame

What you cannot do is to have a "transparent commit" that would be totally ignored by git blame. But you can ask git blame to ignore whitespace changes with git blame -w. Nothing to do at the time you do the code reformat.

Rewrite history

The other option is to rewrite history, so that the new history looks like the code was correctly formatted by the time it was committed. Note that history rewriting is a dangerous operation, you should use this only if you fully understand the consequences of doing it.

You can do a batch rewrite using git rebase -i --exec.

Matthieu Moy
  • 15,151
  • 5
  • 38
  • 65