Recently I wanted an easy way to generate diffs from svn that I could easily read — i.e. with some sort of markup. TextMate automatically displays diffs in an attractive manner, and in a manner that makes them much easier to decipher visually. It also allows you to export any code file as html, which is also nice; however, I didn’t want to keep having to open all my diffs in TextMate just for the sake of formatting them. I also wanted to be able to generate them quickly and be able to distribute them as standalone files that could ostensibly be viewed in a web browser.
I did a little research and found a script over at http://www.linuxjournal.com/content/convert-diff-output-colorized-html which was fairly close to what I wanted to do, so I started with that and modified its processing algorithm and css styling to match my TextMate output. After getting that working with static svn diff output files, I added the ability to simply navigate to the desired directory in the command line and invoke the script to output the html formatted diff.
At that point, I decided that there were 2 key comparisons that were important to me generally — svn diff -r HEAD and svn diff -r PREV:HEAD — which output the diff for the local file to the repository version, and the diff for the head as compared to the previous revision. In other words, one compares the stuff on your machine to the repository, and the other can compare what you just committed to what was there before. I added those, and a help command, and that become svndiff2html. I hope this helps someone save some time. Code follows after the break…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
#!/bin/bash
# svndiff2html runs svn diff on the current directory path and outputs a
# formatted HTML file to provide easier legibility.
diff="";
out="";
html="";
args="";
# ******************************************************************************
# FUNCTIONS
# ******************************************************************************
function displayHelp
{
echo " ";
echo "diff2html -- outputs an html encoded diff file for the current svn repo.";
echo " ";
echo "USAGE:";
echo " diff2html [-local or -l] outputPath";
echo " ";
echo "PARAMETERS:"
echo " -local (optional)"
echo " This parameter tells diff2html to make the comparison between "
echo " the local file changes and the current state of the svn repository.";
echo " If -local is not present, the script will compare the most recent ";
echo " commit to the repository with the repo version previous to it.";
echo " ";
echo " outputPath";
echo " This is simply the output path for the html file that is generated";
echo " by the script.";
echo " ";
echo "To use, add to PATH, chmod to 755, navigate to folder of svn files";
echo "that are to be diff'ed, and run diff2html. (diff2html must be run within";
echo "the directory of the files to be diffed.)";
}
function pipeout
{
# echo -n "$1";
echo -n "$1" >> $out;
}
# ******************************************************************************
# Parse the parameters
if [[ "$1" == "-h" || "$1" == "-help" || "$1" == "help" ]]; then
displayHelp;
exit;
elif [[ "$1" == "-local" || "$1" == "-l" ]]; then
# for comparing local to repo
args="HEAD";
# diff=`svn diff --no-diff-deleted -x "-u -b -w --ignore-eol-style" -r HEAD`;
out="$2";
else
# for comparing the most recent commit to the one previous
args="PREV:HEAD";
# diff=`svn diff --no-diff-deleted -x "-u -b -w --ignore-eol-style" -r PREV:HEAD`;
out="$1";
fi
# Error if not enough parameters have been entered
if [ "$out" == "" ]; then
echo;
echo "ERROR! *************************************";
echo "You must at least enter an output file path.";
echo "Enter 'diff2html help' for usage."
echo;
exit;
fi
echo;
if [ "$args" == "HEAD" ]; then
echo "NOTE: Keep in mind files not 'added' to source control will not be ";
echo "included in the diff. Please first 'add' those files to run local diff ";
echo "on them. (These files with be displayed in their entirety.)";
echo;
echo "Sending local diff to: $out";
else
echo "Sending head to previous diff to: $out";
fi
echo "" > $out;
# Send the header of the html file to the output file
pipeout "
<html>
<head>
<title>Diff for $PWD</title>
</head>
<style media=\"screen\" type=\"text/css\">
body {
margin: 0;
padding: 0;
background-color: #000000;
}
pre {
color: #F8F8F8;
background-color: #0B161D;
margin: 0;
padding: 0 0 0 2px;
font-family: PanicSans, monospace;
font-size: 12px;
line-height: 1.3em;
word-wrap: break-word;
white-space: pre;
white-space: pre-wrap;
white-space: -moz-pre-wrap;
white-space: -o-pre-wrap;
padding-top: 0.2em;
padding-bottom: 0.1em;
}
pre span {
padding-top: 0.2em;
padding-bottom: 0.1em;
}
/* Comment */
pre .comment {
color: #F8F8F8;
font-style: italic;
background-color: #0B161D;
}
/* diff.header */
pre.textmate-source.plasticcodewrap .meta_diff, pre.textmate-source.plasticcodewrap .meta_diff_header {
color: #F8F8F8;
font-style: italic;
background-color: #0E2231;
}
/* diff.header */
pre .at2 {
color: #F8F8F8;
font-style: italic;
font-weight: bold;
background-color: #0B161D;
}
/* diff.deleted */
pre .minus {
color: #F8F8F8;
background-color: #D03620;
}
/* diff.inserted */
pre .plus {
color: #F8F8F8;
background-color: #41A83E;
}
pre span .index {
background-color: #0E2231;
}
pre span .index:before {
/* content:\"<div></div>\";*/
}
.break {
padding-top:15px;
}
.headerDivider {
background-color: #0E2231;
padding-bottom: 4px;
}
pre .minus3 {
font-weight: bold;
background-color: #280A06; /*#0B161D;*/
}
pre .plus3 {
font-weight: bold;
background-color: #0B230E; /*#0B161D;*/
}
pre .only { color: purple; }
pre .diff { color: #8A2BE2; }
/* diff.changed */
pre.textmate-source.plasticcodewrap .markup_changed {
color: #F8F8F8;
background-color: #C4B14A;
}
</style>
<body>
<pre>
<span class=\"comment\">
========= DIFFS =========";
echo -n ''
first=1
diffseen=0
lastonly=0
folderPropertiesSkipped=0
OIFS=$IFS
IFS='
'
# Pipe out the svn diff to our processing loop to add html tags
svn diff --no-diff-deleted -x "-u -b -w --ignore-eol-style" -r $args | while read -r s
do
# echo "$line";
# Get beginning of line to determine what type
# of diff line it is.
t1=${s:0:1}
t2=${s:0:2}
t3=${s:0:3}
t4=${s:0:4}
t6=${s:0:6}
t7=${s:0:7}
# Determine HTML class to use.
if [[ "$t7" == 'Only in' ]]; then
cls='only'
if [[ $diffseen -eq 0 ]]; then
diffseen=1
if [ $folderPropertiesSkipped -eq 1 ]; then
echo -n '</span>' >> $out;
fi
else
if [[ $lastonly -eq 0 ]]; then
if [ $folderPropertiesSkipped -eq 1 ]; then
echo "</div>" >> $out;
fi
fi
fi
if [[ $lastonly -eq 0 ]]; then
if [ $folderPropertiesSkipped -eq 1 ]; then
echo "<div class='diffdiv'>" >> $out;
fi
fi
lastonly=1
elif [[ "$t4" == 'diff' ]]; then
cls='diff'
if [[ $diffseen -eq 0 ]]; then
diffseen=1
if [ $folderPropertiesSkipped -eq 1 ]; then
echo -n '</span>' >> $out;
fi
else
if [ $folderPropertiesSkipped -eq 1 ]; then
echo "</div>" >> $out;
fi
fi
if [ $folderPropertiesSkipped -eq 1 ]; then
echo "<div class='diffdiv'>" >> $out;
fi
lastonly=0
elif [[ "$t3" == '+++' ]]; then
cls='plus3'
lastonly=0
elif [[ "$t6" == 'Index:' ]]; then
cls='index'
lastonly=0
folderPropertiesSkipped=1
elif [[ "$t6" == '======' ]]; then
cls='headerDivider'
lastonly=0
elif [[ "$t3" == '---' ]]; then
cls='minus3'
lastonly=0
elif [[ "$t2" == '@@' ]]; then
cls='at2'
lastonly=0
elif [[ "$t1" == '+' ]]; then
cls='plus'
lastonly=0
elif [[ "$t1" == '-' ]]; then
cls='minus'
lastonly=0
else
cls=
lastonly=0
fi
# Convert &, <, > to HTML entities.
s=$(sed -e 's/\&/\&/g' -e 's/</\</g' -e 's/>/\>/g' <<<"$s")
if [[ $first -eq 1 ]]; then
first=0
else
if [ $folderPropertiesSkipped -eq 1 ]; then
echo >> $out;
fi
fi
# Output the line.
if [ $folderPropertiesSkipped -eq 1 ]; then
if [ "$cls" == "index" ]; then
echo -n '<div class="break"><span> </span></div><span class="'${cls}'">'${s}'</span>' >> $out;
elif [ "$cls" == "headerDivider" ]; then
echo -n '<div class="headerDivider"><span class="index">'${s}'</span></div>' >> $out;
elif [ "$cls" == "at2" ]; then
echo -n '<div style="padding-top:2px"><span> </span></div><span class="'${cls}'">'${s}'</span>' >> $out;
elif [[ "$cls" ]]; then
echo -n '<span class="'${cls}'">'${s}'</span>' >> $out;
else
echo -n ${s} >> $out;
fi
fi
done
IFS=$OIFS
if [[ $diffseen -eq 0 && $onlyseen -eq 0 ]]; then
echo -n '</span>' >> $out;
else
echo "</div>" >> $out;
fi
echo >> $out;
# Add the footer to the output file
pipeout "
========= END OF DIFFS =========
</span>
</pre>
</body>
</html>
";
echo "HTML diff complete.";
|
Posted by way of Plains of the Plastic Intafon




