TAB tricks and trivia

TAB tricks and trivia

by Martín Langhoff -
Number of replies: 1
Preparing a patch for Moodle, I came across the issue of ensuring no tabs had sliped into my code. If you are wondering why this matters, check out the Coding Style, halfway down http://moodle.org/doc/?file=coding.html

Unfortunately, grepping for tabs isn't that easy. Here's a handy trick to do it:

  # create a file with a tab
  $ perl -e 'print "\t" ' > tab

  # create two test files
  $ perl -e 'print "foo\tbar" ' > test_with_tab
  $ echo 'foo bar'              > test_no_tab

  # test the trick: grep using the file as reference
  $ grep -f tab test*
  # (this should find a tab only in test_with_tab_)

now check the moodle tree for tabs,
just in case you've put one in there...

  # this grep will skip binary files (GIFs have tab characters)
  # and give you a count of how many tabs are found in each file
  # the 2nd grep skips clean files from the report.
  $ grep -f tab -rcI * | grep -v ':0' 

Ok, so that was the good news.

The bad news? Moodle CVS (both HEAD and MOODLE_14_STABLE) have heaps of tab characters in the code.

Martin, is there a cleanup policy? We can always do something along the lines of

  $ grep -f tab -rlI * | xargs perl -pi -e 's/\t/    /g'
  # check all is good, diff -w should come out clean
  $ cvs diff -w

cheers,





martin
Average of ratings: -
In reply to Martín Langhoff

Re: TAB tricks and trivia

by Martín Langhoff -
Replacing all tabs with 4 spaces across the project is probably a risky move. At the very least, it'll conflict with a lot of local/private patches people have.

So, if you want to 'clean' your patch before sending it to Martin Dougiamas, so that your patch doesn't add tabs, but doesn't change lines you didn't modify:

  # on a 'unified-diff' patch
  perl -pi -e 'if(/^\+/){s/\t/    /g}' fancynewcode.patch

Hmmm. So you've cleaned the patch, but not your CVS working copy. Not good enough, I say.

To cleanup your CVS working copy, but only on the lines you've changed...

  # update just in case
  cvs update

  # generate a patch
  cvs diff -u > mynewcode.patch

  # copy and cleanup
  cp mynewcode.patch mynewcleancode.patch
  perl -pi -e 'if(/^\+/){s/\t/    /g}' mynewcleancode.patch

  # revert the files to their unmodified state
  patch --reverse  < mynewcode.patch

  # reapply your changes, now with proper spaces
  patch < mynewcleancode.patch

Of course, this is all moot if you are using a good editor, and you have it configured to use 4 spaces instead of tabs.

cheers,





martin