top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Git: Looking for pre-commit hook to check whitespace errors but not for all files

+2 votes
902 views

In coreboot we try to check for whitespace errors before committing. Of course a pre-commit hook is the way to go, but unfortunately it is not so simple (at least for me) as the following requirements exist.

  1. Only the files actually committed should be checked. That means running git commit -a, abort that and then running git commit some/file should only check some/file for whitespace errors.

  2. There are certain files that are allowed to have whitespace errors. In our case these are *.patch and *.diff files which by design seem to contain whitespace error.

Currently the whole tree is checked, which takes a lot of time. I tried to come up with a patch, but failed so far. Best would be to have

$ git diff --check --only-committed-files --exclude "*patch$"

where I could not find a way for the last to switches.

Currently, I would use

$ git diff-index --cached --name-only $against -- | grep -v patch$

and pass that list to some whitespace check program. Unfortunately that still does not fulfill the first requirement. What am I missing to solve this elegantly?

posted Dec 14, 2013 by Sumit Pokharna

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
I think you want to combine .gitattributes with something like the diff-index above:

 $ cat >.gitattributes

1 Answer

+1 vote

Ans 1) Doesn't hooks/pre-commit.sample we ship already gets this right?

 $ git init foobar & do echo $i >$i.txt; done
 $ git commit -m initial
 $ for i in foo bar; do echo "$i " >$i.txt; done
 $ git commit -a
 bar.txt:1: trailing whitespace.
 +bar
 foo.txt:1: trailing whitespace.
 +foo
 $ git commit foo.txt
 foo.txt:1: trailing whitespace.
 +foo
answer Dec 16, 2013 by Abhay Kulkarni
Similar Questions
0 votes

There are shorthands for going back from HEAD, but not for the initial commit, AFAICT.

I often want to do this when rebasing, and have come to tagging the initial commit in my repos with INITIAL.

Is there a better syntax I'm missing?

+2 votes

Can someone help me on how to delete an intermediate commit?

+1 vote

We're attempting to write some scripts to perform automatic commits for certain files under certain conditions. We wish to use the just committed revision number to perform some other logging operation. The --xml command line option on many of the other commands (info, status, etc.) are fantastic, but the commit command does not allow this.

I'm currently attempting to parse the standard output of the commit message to grab it, but feel this is less than optima as it seems very error prone.

Is there any consideration to add an --xml option to the commit command output or a better programmatic means to grab the commit revision?

$ svn commit 1.txt -m "Checkin message". 
Sending 1.txt 
Transmitting file data . 
Committed revision 3272. 
+1 vote

I have many commits: A, B, C, ..., Z. I plan to do some housekeeping using rebase. One thing I want to do is to reorder Z to be the first commit. However, other commits will be in conflict with Z's changes. I know I can go through and interactively resolve the conflicts manually. But this is tedious and error prone. I would like to be able to mark Z's changes as authoritative while rebasing. That is, I want to tell git, "whenever a commit conflicts with Z (during this rebase operation) I want you to keep Z's changes and ignore the other." Is this possible? Is there another (hopefully better) way to accomplish what I'm trying to do?

+1 vote

I have a commit I am trying to cherry pick that removes a number of files. It seems to generate conflicts for those files that have been modified on this branch since the common ancestor. Since they are being removed, I don't care about what changes have been made on this branch, just remove them. Even git cherry-pick -Xtheirs does not help. How can I avoid these conflicts, or accept the deletions? I tried git add -u, but that seems to take my version rather than accept the deletion, and there is no -u switch to git rm.

...