Fast bash script to check source code for basic textual anomalies

It can be convenient to check source code for some basic textual anomalies in an automated fashion.
The bash script below is a simple example of this in case of C++ code, utilizating mostly basic unix programs like grep.
This approach appears to be very fast. The include-guard check is by far the slowest, as it cannot utilize the xargs feature passing multiple filenames to grep at the same time.
Still, on a moderate source code base it is all done in a matter of seconds.

Updating local git refs the fast way

When I want to check out a local branch, it quite often happens that its corresponding remote tracking branch had updates by other people, while I didn’t make any changes to the branch.
So in this case I’m not interested in the current state of my my_branch and I just want to obtain and checkout the remote state of my_branch.

Normally, to obtain these remote changes, you would probably run

git checkout my_branch
git pull

In this scenario however, this has one downside: you’re checking out a working directory that you’re not interested in, namely the old state of my_branch.
This is a waste of time, especially when the repositories you’re dealing with are large. What we actually want to do is move the local branch pointer before checking it out.
The following custom git command takes care of that.

update = "!f() { if [ $# -eq 0 ] ; then git reset --hard @{u}; else git fetch . "origin/$1:$1" --recurse-submodules=no; fi }; f"

Now you can run

git update my_branch
git checkout my_branch

and it will not do any superfluous check outs. Moreover, if you are already on my_branch, you can run git update to explicitly move the branch pointer, instead of git merge.

Automatically fixing a whole bunch of cppcheck errors

When you start using a certain code check tool for an existing code base, the checker may at first give a lot of warnings about all kind of things. A few days ago, I fired up cppcheck for an existing c++ code base. Cppcheck gave a lot of useful feedback. Problem is however, it can be quite a bit of work to fix all these errors, depending on the size of your project. Luckily, the major part of the warnings belonged to a limited group of error types. This gives us the chance to write a script that automatically fixes most of these errors.

One of the warnings the cppcheck gave is that there should be an explicit keyword before a constructor with a single argument, to avoid it being used in implicit type conversions. If the cppcheck errors are in errors.txt, we can use the following python code in combination with vim to automatically fix this.

This is notably slow, and it took me quite some fiddling to get vim to do what I want (read: figuring out these weird command line arguments). I wanted to do the next thing using fate, an experimental text processing engine that I’ve been working on. This next thing turned out to be passing const strings by reference, for reasons of performance. The following code fixes these warnings.

Fate is notably faster in this usecase than vim, probably because it is more lightweight. At the time of writing, fate is still very experimental, but if you like you can have a look at it and fiddle around.