Blog

Converting tabs to spaces for CVS

This seems like a problem I run into infrequently enough that I forget how I worked around it previously. Some organizations implement CVS pre-commit checks that disallow hard tabs in source files. I use Eclipse for editing Java and it’s relatively easy to reformat code according to rules, but those rules ignore hard tabs within comments while this particular CVS pre-commit check did not.

Below are the steps I used to check in code riddled with hard tabs. This was done using a typical Cygwin installation on Windows, but should be pretty close to what would be required on a Linux installation.

  1. Record the errors from cvs commit

    cvs commit -m 'some message' > commit-log.txt 2>&1
  2. Grab just the hard tab error messages

    grep 'hard tab' commit-log.txt > hard-tabs.txt
  3. Convert remote path to local path

    vi hard-tabs.txt:1,$ s/remote path/local path/g:wq

    ex:

    vi hard-tabs.txt:1,$ s/data2/CVShome/home/Scott/Clients/SAIC/dev2/g:wq

    (note: use backslash to escape the slashes in the path above)

    • Convert the hard tabs in the files with the expand command (using bash)
    for file in $(cat hard-tab-files-sorted.txt); do expand -t 4 $file > /tmp/expanded.txt; mv /tmp/expanded.txt $file; done
    • Retry the commit
    cvs commit -m 'some message'

    There’s a quicker way to do the search and replace if you also have Perl.

    I’m always interested in these sorts of shortcuts, so if you know of some please include them in comments below!