Skip to content

Instantly share code, notes, and snippets.

@stopyoukid
Last active August 10, 2024 15:42
Show Gist options
  • Save stopyoukid/5888146 to your computer and use it in GitHub Desktop.
Save stopyoukid/5888146 to your computer and use it in GitHub Desktop.
Script that takes a git diff and outputs an html file in GitHub style
#!/bin/bash
#
# Convert diff output to colorized HTML.
# (C) Mitch Frazier, 2008-08-27
# http://www.linuxjournal.com/content/convert-diff-output-colorized-html
# Modified by stopyoukid
#
html="<html><head><meta charset=\"utf-8\"><title>Pretty Diff</title><style>body {text-align: center;}#wrapper {display: inline-block;margin-top: 1em;min-width: 800px;text-align: left;}h2 {background: #fafafa;background: -moz-linear-gradient(#fafafa, #eaeaea);background: -webkit-linear-gradient(#fafafa, #eaeaea);-ms-filter: \"progid:DXImageTransform.Microsoft.gradient(startColorstr='#fafafa',endColorstr='#eaeaea')\";border: 1px solid #d8d8d8;border-bottom: 0;color: #555;font: 14px sans-serif;overflow: hidden;padding: 10px 6px;text-shadow: 0 1px 0 white;margin: 0;}.file-diff {border: 1px solid #d8d8d8;margin-bottom: 1em;overflow: auto;padding: 0.5em 0;}.file-diff > div {width: 100%:}pre {margin: 0;font-family: \"Bitstream Vera Sans Mono\", Courier, monospace;font-size: 12px;line-height: 1.4em;text-indent: 0.5em;}.file {color: #aaa;}.delete {background-color: #fdd;}.insert {background-color: #dfd;}.info {color: #a0b;}</style></head><body><div id=\"wrapper\">"
first=1
diffseen=0
lastonly=0
currSection=''
currFile=''
function addDiffToPage {
html+="<h2>"$1"</h2>"
html+="<div class=\"file-diff\">"
html+=$2
html+="</div>"
}
OIFS=$IFS
IFS='
'
# The -r option keeps the backslash from being an escape char.
read -r s
while [[ $? -eq 0 ]]; do
# 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}
t7=${s:0:7}
# Determine HTML class to use.
if [[ "$t7" == 'Only in' ]]; then
cls='only'
if [[ $diffseen -eq 0 ]]; then
diffseen=1
else
if [[ $lastonly -eq 0 ]]; then
addDiffToPage $currFile $currSection
fi
fi
if [[ $lastonly -eq 0 ]]; then
currSection=""
fi
lastonly=1
elif [[ "$t4" == 'diff' ]]; then
cls='file'
if [[ $diffseen -eq 1 ]]; then
addDiffToPage $currFile $currSection
fi
diffseen=1
currSection=""
lastonly=0
elif [[ "$t3" == '+++' ]]; then
# --- always comes before +++
# currFile=${s#+++ */}
cls='insert'
lastonly=0
elif [[ "$t3" == '---' ]]; then
currFile=${s#--- */}
cls='delete'
lastonly=0
elif [[ "$t2" == '@@' ]]; then
cls='info'
lastonly=0
elif [[ "$t1" == '+' ]]; then
cls='insert'
lastonly=0
elif [[ "$t1" == '-' ]]; then
cls='delete'
lastonly=0
else
cls='context'
lastonly=0
fi
# Convert &, <, > to HTML entities.
s=$(sed -e 's/\&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g' <<<"$s")
if [[ $first -eq 1 ]]; then
first=0
fi
# Output the line.
if [[ "$cls" ]]; then
currSection+='<pre class="'${cls}'">'${s}'</pre>'
else
currSection+='<pre>'${s}'</pre>'
fi
read -r s
done
#if [[ $diffseen -eq 1 && $onlyseen -eq 0 ]]; then
if [[ "$currSection" ]]; then
addDiffToPage $currFile $currSection
fi
html+="</div></body></html>"
echo "$html"
IFS=$OIFS
@istian
Copy link

istian commented Mar 10, 2016

This is awesome! Thanks for the script!

@pditommaso
Copy link

Great script. Thanks!

@jq-li
Copy link

jq-li commented Feb 27, 2017

sweet and simple, awesome!

@scm20008
Copy link

scm20008 commented Aug 25, 2017

Thanks, this script is very useful!

@kim3er
Copy link

kim3er commented Sep 7, 2017

Really useful script.

@cb-pradeep
Copy link

Can someone tell me how to use this script?.
I have downloaded the above script and also I have created a patch file using git diff dev master > diff.patch
And I'm trying to run this script using sh diff2html.sh diff.patch
However the process keeps on running .

Any help much appreciated . Thanks!

@thomasortonbrady
Copy link

Same here. I've done the same as @cb-pradeep and it seems to just run without returning. I'm running it in the bash on Windows.

@cb-pradeep
Copy link

cb-pradeep commented May 10, 2019

git diff $compareBranch -- $argument | sh $script_folder/diff2html.sh > $diffHtmlFilePath .
This worked for me

Same here. I've done the same as @cb-pradeep and it seems to just run without returning. I'm running it in the bash on Windows.

@thomasortonbrady
Copy link

@cb-pradeep Thank you for that. Working for me too now.

@sravanc75
Copy link

hi @cb-pradeep can you explain me the above command and can used for email notifications as well ?

@ssurikuchi
Copy link

ssurikuchi commented Jun 13, 2023

Thanks a million @stopyoukid for the script and it saved my day.

Commands used:
Approach1: (discreet commands)

1. git diff branch1 branch 2 >> gitdiff.txt
2. cat gitdiff.txt | <absolute path to diff2html.sh>/diff2html.sh

Approach2: (combine both commands)
git diff branch1 branch 2 | <absolute path to diff2html.sh>/diff2html.sh

Here are few errors I encounted on ubuntu before I became successful.

  1. Function not found
  2. Syntax error: redirection unexpected

Both the errors are encounted while running the script using command sh diff2html.sh which forces ubuntu to choose dash instead of bash. Instead use the command <absolute path to diff2html.sh>/diff2html.sh

@dan-kruk
Copy link

nice, works well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment