Tuesday 27 September 2011

Bash script to extract a range of revision diffs

A quick Bash script hack to extract all CVS/SVN diffs from a range of revisions:

for start_ver in {1..25};
do end_ver=`echo "$start_ver + 1" | bc`;
cvs diff -kk -c -r 1.$start_ver -r 1.$end_ver source.file >> diff_output.txt;

This script will incrementally extract all differences between version 1.1 and 1.26 and write them to the diff_output.txt file. Incremental in this context means changes between each revision, so the output will contain the diff between version 1.1 and 1.2 followed by the diff between 1.2 and 1.3, then 1.3 and 1.4 etc.

A few notes (as always):

  • The script should be run in the directory containing the file the diff is being run for (source.file in the example)
  • The physical revision numbers can be replaced with tags (assuming sequential numerical naming of the tags)
  • The diff options (-kk -c) can be changed for the required diff output format
  • This was a quick hack and can almost certainly be improved

Thursday 8 September 2011

Generating SQL insert commands using SELECT

A quick hack for generating a list of SQL insert commands from an existing data set. Useful for copying specific data between databases/tables.

select 'insert into DestTable values(''' + Value1 + ''',''' + Value2 + ''')' from SourceTable

This query will return a result set in the format:
insert into DestTable values('a','b')
insert into DestTable values('x','y')

A few notes:
  • The '+' might need to be replaced with a concat function on certain databases.
  • A convert or cast operation may be required for non-character values
  • The result set can be limited by adding a where class to the select query