git whitespace woes

21,190

Solution 1

Git1.6.0.4 seems a bit old, especially if you consider that:

  • in 1.6.3.4, "git apply --whitespace=fix" did not fix trailing whitespace on an incomplete line
  • in 1.6.3.2, "whitespace" attribute that is set was meant to detect all errors known to git, but it told git to ignore trailing carriage-returns.

Could you try with Git1.6.4.1, and rather than setting a global config, set an attribute on the files you want a special whitespace handle, like this patch describes.

In a given directory, create a .gitattributes file.

* -whitespace

which will ignore any 'whitespace' errors.

Now that will not prevent any conflict due to lack of consistency but that may be worth trying.


The patch was a test about:

Only ignore whitespace errors in t/tNNNN-*.sh and the t/tNNNN subdirectories.
Other files (like test libraries) should still be checked.

t/.gitattributes
t[0-9][0-9][0-9][0-9]-*.sh  -whitespace
t[0-9][0-9][0-9][0-9]/*     -whitespace

Note (Git 2.3.2+, Q1 2015, commit 0a80bc9, by Junio C Hamano aka gitster) "git apply --whitespace=fix" is no longer silent:

"git apply --whitespace=fix" fixed whitespace errors in the common context lines but did so without reporting.

When the incoming patch has whitespace errors in a common context line (i.e. a line that is expected to be found and is not modified by the patch), "apply --whitespace=fix" corrects the whitespace errors the line has, in addition to the whitespace error on a line that is updated by the patch.
However, we did not count and report that we fixed whitespace errors on such lines.

Solution 2

If you are going to turn on those settings, you need to schedule a day where ALL source code in your project gets its whitespace uniformly stripped, by running a script, or by saving every file from an editor that will perform the strip on save. Then, all future commits will be policed by the settings, so all should be well going forward.

Share:
21,190
Kzqai
Author by

Kzqai

I make a ninja game at ninjawars.net, am hirable at bitlucid.com, do art & origami when I can at dnaexmosn.deviantart.com, and code open source stuff on github at github.com/tchalvak Code in: node react php javascript python (some) html/css Learning currently: Css-grid npm packaging aka Tchalvak

Updated on April 27, 2020

Comments

  • Kzqai
    Kzqai about 4 years

    Conflicts on whitespace suck

    Whitespace has ended up being a horrible pain for me while using git.

    git config apply.whitespace=strip
    

    seems to highten your chances of getting conflicts (as you strip unneeded whitespace and then other collaborators see the stripped whitespace as a change to their commits?)

    I've tried a few other configurations for apply.whitespace in the past, and maybe one of the other configs solves this, or maybe there's other settings to deal with whitespace that I just haven't come across, but I haven't yet found a clear way to get where I want to be.

    I want to silently resolve whitespace conflicts:

    I never want to have another conflict on whitespace. If another committer alters whitespace, or I alter whitespace and then have to merge against my own conflicts, i really don't want to know about it. If someone changes my code from K&R style to One True Brace style by changing whitespace, I'd prefer git allow either whitespace setup to win out over having to see conflicts about it. I just don't care enough about whitespace to want to see conflicts about it.

    So... ...is there any way that I can configure git to do that?

    If it makes any difference, here's my git version, and my current config:

    tchalvak:~/ninjawars$ git --version
    git version 1.6.0.4
    
    tchalvak:~/ninjawars$
    git config --list
    color.branch=auto
    color.diff=auto
    color.status=auto
    color.branch.current=yellow reverse
    color.branch.local=yellow
    color.branch.remote=green
    color.diff.meta=yellow bold
    color.diff.frag=magenta bold
    color.diff.old=red bold
    color.diff.new=green bold
    color.status.added=yellow
    color.status.changed=green
    color.status.untracked=cyan
    gui.recentrepo=/home/tchalvak/zd/htdocs/cms
    apply.whitespace=strip
    user.name=****
    user.email=****
    alias.co=checkout
    github.user=tchalvak
    github.token=****
    core.repositoryformatversion=0
    core.filemode=true
    core.bare=false
    core.logallrefupdates=true
    [email protected]:tchalvak/ninjawars.git
    remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
    branch.master.remote=origin
    branch.master.merge=refs/heads/master 
    
  • Kzqai
    Kzqai over 14 years
    nods Yeah, I've been using the git binary in the ubuntu repositories, but perhaps it's time that I got a bit closer to the most recent versions, maybe that'll help with the issue. I'll also see what difference the .gitattributes setting makes on the situation.
  • Kzqai
    Kzqai over 14 years
    'lright, I've (happily) added the launchpad ppa for the latest stable version of git in ubuntu, hopefully that'll fix things up.
  • Kzqai
    Kzqai almost 13 years
    Upgrading does seem to have solved some of the issues that I had in the past.
  • VonC
    VonC almost 13 years
    @Karl: I am not able to find that same article again, so I link to a Git patch which illustrate the same idea (setting a git attribute for a specific file in order to ignore whitespace warnings/errors)