scripted perl undo of a code modification to test using diff.
Posted by peeterjoot on September 15, 2011
We’ve got some code that uses an external product that allows us to put some of our info in a generic structure with integer members name1, name2, name3, name4.
Unfortunately, when we started using this, we didn’t use macros or inlines to encode exactly what we’d chosen to put in these fields, so now we’ve got a lots of code that tries to compensate by using comments like so:
sqmIncrObjMetricsCounter( agtCB, pCaWobEntry->n.name2, // poolID pCaWobEntry->n.name3, // objectID 0, SQLB_OBJTYPE(pCaWobEntry->n.name4), // objectType 1, &objMetric ) ;
I chose to retrofit in some inline helper methods to eliminate the requirement for adding (and relying on!) comments to describe what was going on. Although I did figure out some partially automated methods, I was unfortunately not clever enough to figure out how to do this code modification in an entirely scripted fashion. I came up with what I thought was a good test strategy, where I was able to write a script to undo my changes, and then run diff against the ancestor in our version control repository to verify that I’d not actually introduced any changes of substance. I thought the method was a neat technique, yet another example of using ‘perl -p -i’ ad-hoc scripting. Here’s a fragment of the undo script:
s/SAL_ExtractPageNumFromPageName *\( *&([^ \)]+) *\)/$1.name1/g; s/SAL_ExtractPageNumFromPageName *\( *([^ \)]+) *\)/$1->name1/g; s/SAL_AssignPageNumToPageName *\( *&([^ ,]+) *, *([^ \)]+) *\)/$1.name1 = $2/g; s/SAL_AssignPageNumToPageName *\( *([^ ,]+) *, *([^ \)]+) *\)/$1->name1 = $2/g;
After running this on all the files that I modified I was able to make sure that all the name1’s I’d changed to access via a helper method were still name1’s and that I’d not made any copy and paste errors, using the wrong new inlines anywhere.
Of course this presumes that I didn’t make a corresponding bug in my undo script, but that’s a much smaller task to review than all the other code in question, and I’m now much more comfortable committing this change to our version control system.