Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

indentation configuration only for some files

Tags:

git

I want to use

git config core.whitespace tab-in-indent,tabwidth=4

I want to have these settings for c++ files, so that I get warnings when there are wrong indentations when I use git diff. However, I have also Makefiles which need tabs. Is there a way to configure different whitespace settings for different files?

like image 240
pyfex Avatar asked Dec 11 '12 11:12

pyfex


3 Answers

You can use gitattributes to tweak these settings. Here's a snippet of my .gitattributes file:

*.c     text diff=cpp whitespace=trailing-space,space-before-tab,tab-in-indent
*.cpp   text diff=cpp whitespace=trailing-space,space-before-tab,tab-in-indent
*.h     text diff=cpp whitespace=trailing-space,space-before-tab,tab-in-indent
*.hpp   text diff=cpp whitespace=trailing-space,space-before-tab,tab-in-indent
*.py    text diff=python whitespace=trailing-space,space-before-tab,tab-in-indent
*.tex   text diff=tex whitespace=trailing-space,space-before-tab,tab-in-indent
*.java  text diff=java whitespace=trailing-space,space-before-tab,tab-in-indent
*.pl    text diff=perl whitespace=trailing-space,space-before-tab,tab-in-indent
*.php   text diff=php whitespace=trailing-space,space-before-tab,tab-in-indent
*.rb    text diff=ruby whitespace=trailing-space,space-before-tab,tab-in-indent

*.vcproj    eol=crlf
*.dsp       eol=crlf
*.dsw       eol=crlf

*.sh    eol=lf

*.jpg   binary
*.png   binary
*.gif   binary
*.tiff  binary

To adjust your whitespace settings, you could use something like:

*.ext   whitespace=tab-in-indent,tabwidth=4

The *.ext can point at paths, contain globs, etc. It's pretty flexible mechanism.

like image 145
John Szakmeister Avatar answered Nov 06 '22 04:11

John Szakmeister


Some additions to the answers by John Szakmeister and UpAndAdam.

To set file-specific rules you'll have to add a .gitattributes file in the root of you project. docs
(If you do not want it to be version controlled you can add it as: .git/info/attributes.)

# Macro's
[attr]cpp       diff=cpp whitespace=trailing-space,space-before-tab,indent-with-non-tab,tabwidth=4
[attr]makefile  whitespace=trailing-space,indent-with-non-tab,space-before-tab,tabwidth=4

*.[ch]        cpp
*.[ch]pp      cpp
makefile      makefile
s.makefile    makefile
  • Match files using the syntax for .gitignore files. i.e. * matches everything and [ch] matches both characters c and h.
  • The whitespace option lists things to warn for.
    • Setting the tabwidth in the whitespace option is used to determine when and how to replace the tab and space characters. (The default value is 8 characters.)
    • tab-indent considers indentation using tabs an error.
    • indent-with-non-tab warns here when 4 or more spaces are used at the start of a line. Note that indentation with 3 spaces is accepted!
      Add space-before-tab to catch spaces hidden before and between the tabs.
      Note that spaces following tab are
  • Aside: diff=cpp enables smarter diffs for C and C++ files.
  • Aside 2: trailing-space warns on trailing white space characters at the end-of-line and end-of-file.

Verify Rules

  • To verify what rules from .gitattributes are applied use:
git check-attr --all -- <pathname>

<pathname> does not have to be an existing file. (i.e. some.cpp works)

  • To test the whitespace rules:
    Create a dummy file matching the filename rule and call: git diff.
trailing space 
trailing tab    
  2 spaces
    4 spaces
    tab
     tab and space
    space and tab
        tab, space, tab

Usage

Issues are marked when calling git diff and checked/ fixed when calling git -apply ---whitespace=[warn|error|fix]. Configure default behaviour of git apply with:

git config --[global|local] apply.whitespace [warn|error|fix]
like image 34
Farway Avatar answered Nov 06 '22 04:11

Farway


As jszakmeister updated from my comment, here's just a sub-section for what you asked about.

  • No tab-in-indent allowed for .cpp/.hpp files
  • Tab's allowed for Makefiles/makefiles and *.mk files
  • No trailing spaces allowed in all
  • tabwidth of 4 in all

Notice the usage of the - modifier in the 'makefile-ish' entries to say don't call tab-in-indent an error.

makefile text          whitespace=-tab-in-indent,trailing-space,tabwidth=4
Makefile text          whitespace=-tab-in-indent,trailing-space,tabwidth=4
*.mk     text          whitespace=-tab-in-indent,trailing-space,tabwidth=4
*.cpp    text diff=cpp whitespace=tab-in-indent,trailing-space,tabwidth=4
*.hpp    text diff=cpp whitespace=tab-in-indent,trailing-space,tabwidth=4
like image 2
UpAndAdam Avatar answered Nov 06 '22 06:11

UpAndAdam