Code review utility for software porting projects


Assume this situation. You are assigned to port a code to a new hardware/os platform. Generally this involves picking the necessary files for implementing the desired functionality and porting these set of files to the new platform. While doing this, several things happen. First, you usually do not keep the same hierarchy structure, you may for example put all your .c files in a directory called src and all your includes in another directory called inc. You would also modify the sources so that the code compiles in the new build environment.

In organizations that give importance to code quality. Code review operations help minimize faults and bugs in new software. On of the methods for reviewing code in software porting projects is to create a diff between the original source tree (assumed bug free) and the new/ported source tree.

This bash script is an utility that can be used to create such diffs. The resulting output is an HTML file easy to read. It can be used by team leaders to review and validate the changes in the source code made by team members.


Scrip for generating diffs

This utility can be used for codereviews when porting software to a new platform. The utility matches source files in the new source tree with source files in the original source tree and creates a diff for each. The result is formatted as an HTML file.

The outputs are as follows :

The utility can search files in steps. This allows the use of prioritary search paths if a source file can be found in different locations in the original source tree. For example, if most of our files come from dir1, but dir2 comes first during the search operation, we can force the utility to use dir1 to lookup the original source files by doing a search pass in dir1. Files that were matched in the first pass will not be looked up in the following passes.

Files that can not be found automatically can be handled manually. For this create a file called exceptions.txt and add entries as follows :

 filename:path/to/original/version

The utility will not lookup files in exceptions.txt. only diffs will be generated.

#!/bin/bash
 
# First match files against dir1
 
ORIGNIALSOURCE=~/originalsourcetree
INDEX=0
 
 
function makelist {
 
echo creating file list
echo "" > filelist.txt
 
# First pass: look in DIR1
 
for file in src/*.c inc/*.h
do
 
base=`basename $file`
 
# process exceptions
res=`grep $base exceptions.txt`
 
if [ $res ]
then
echo skipping $base
continue
fi
 
echo -n "$base:" >> filelist.txt
 
find $ORIGNIALSOURCE/DIR1 -name $base -print | tr -d "\n" >> filelist.txt
echo >> filelist.txt
done
 
cat exceptions.txt >> filelist.txt
 
# Second pass: look in DIR2
 
echo "" > filelist2.txt
 
COUNT=0
while read line
do
 
if [ "$line" = "" ]
then
continue
fi
 
 
base=`echo $line | cut -d":" -f1`
path=`echo $line | cut -d":" -f2`
 
if [ "$path" = "" ]
then
 
	echo -n $base: >> filelist2.txt
	find $ORIGNIALSOURCE/DIR2 -name $base -print | tr -d "\n" >> filelist2.txt
	echo >> filelist2.txt
	COUNT=`expr $COUNT + 1`
else
	echo $base:$path >> filelist2.txt
fi
 
done <  filelist.txt
 
 
# Third pass: look in DIR3
 
echo "" > filelist3.txt
 
COUNT=0
while read line
do
 
if [ "$line" = "" ]
then
continue
fi
 
base=`echo $line | cut -d":" -f1`
path=`echo $line | cut -d":" -f2`
 
if [ "$path" = "" ]
then
 
	echo -n $base: >> filelist3.txt
	find $ORIGNIALSOURCE/DIR3 -name $base -print | tr -d "\n" >> filelist3.txt
	echo >> filelist3.txt
	COUNT=`expr $COUNT + 1`
else
	echo $base:$path >> filelist3.txt
fi
 
done <  filelist2.txt
 
 
 
while read line
do
if [ "$line" = "" ]
then
continue
fi
 
base=`echo $line | cut -d":" -f1`
path=`echo $line | cut -d":" -f2`
if [ "$path" = "" ]
then
	echo "$base @$line@ not found ! Aborting."
	exit -1
fi
 
done <  filelist3.txt
 
 
echo "file list generated (filelist.txt)"
 
 
mv filelist3.txt filelist.txt
rm filelist2.txt
}
 
 
# For each source/header file, make a diff with the original file and store the
# diff as filename.extension.diff
 
function makediff {
 
echo creating diffs
 
if ! [ -d diffs ]
then
mkdir diffs
fi
 
while read line
do
if [ "$line" = "" ]
then
continue
fi
 
base=`echo $line | cut -d":" -f1`
path=`echo $line | cut -d":" -f2`
 
if [ -f src/$base ]
then
diff  -uBwb $path src/$base > diffs/$base.diff
else
diff  -uBwb $path inc/$base > diffs/$base.diff
fi
 
 
 
done <  filelist.txt
}
 
 
# Build HTML output
 
function makehtml {
echo creating HTML
 
echo "<table border="1">" > diff.html
 
echo "<tr>" >> diff.html
echo "<th>File number</th>" >> diff.html
echo "<th>File name</th>" >> diff.html
echo "<th>Path</th>" >> diff.html
echo "<th>Diff</th>" >> diff.html
echo "<td>" >> diff.html
 
COUNT=0
 
while read line
do
if [ "$line" = "" ]
then
continue
fi
 
COUNT=`expr $COUNT + 1`
 
base=`echo $line | cut -d":" -f1`
path=`echo $line | cut -d":" -f2`
 
echo "<tr>" >> diff.html
echo "<td>$COUNT</td>" >> diff.html
echo "<td>$base</td>" >> diff.html
echo "<td>$path</td>" >> diff.html
echo "<td>" >> diff.html
echo "<PRE>" >> diff.html
cat diffs/$base.diff >> diff.html
echo "</PRE>" >> diff.html
echo "<td>"  >> diff.html
echo "</tr>" >> diff.html
 
done <  filelist.txt
 
echo "</table>" >> diff.html
 
}
 
makelist
makediff
makehtml


Labels: , Wireless Internet Security Coding Network Monitoring