-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcdiff
executable file
·176 lines (155 loc) · 7.74 KB
/
cdiff
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
#!/usr/bin/env sh
A=$1
B=$2
#echo "LOCAL: $A"
#echo "REMOTE: $B"
#echo "MERGED: $3" # unused
USE_FENESTRO=false
BROWSER="" # "Firefox"
TEMP_FILE="$(mktemp /tmp/cleandifffapp.XXXXXX).cdiff"
HTML_EXTENSION=".html"
# make sure we're only dealing with text files
unset LANG
# if you don't unset LANG then some files will cause the file command
# to return: ERROR: line 22: regexec error 17, (illegal byte sequence)
if file $A | grep 'text' >/dev/null 2>&1 || \
file $B | grep 'text' >/dev/null 2>&1 ; then
# The heart of our functionality.
# diff the files and have dwdiff break the results down into smaller "words"
dwdiff -A best -L -c -d "\x0A%,;/:._{}[]()'\!|-=~><\"\\\\" $A $B | cat > $TEMP_FILE
# Extract the name of the file we're diffing
# $A and $B aren't trustable as names because
# when used with git diff(tool) they'll just be temp files.
# There are a couple lines which start with --- and +++
# which mention the name of the "left" and "right" sides of the diff.
# I've chosen the "right" one because it's more likely to be
# what you ended up with.
#FILE_NAME=`grep -m1 --color=none +++ $TEMP_FILE | sed -e 's/+++ //' -e 's/ .*//'`
FILE_NAME=`basename $1 | sed -E 's|^.{6}_||'`
FILE_NAME_2=`basename $2 | sed -E 's|^.{6}_||'`
if [ "$FILE_NAME" != "$FILE_NAME_2" ]; then
if [ "$FILE_NAME" != "null" ] && [ "$FILE_NAME_2" != "null" ]; then
FILE_NAME="$FILE_NAME -> $FILE_NAME_2"
elif [ "$FILE_NAME" == "null" ]; then
FILE_NAME=$FILE_NAME_2
# otherwise FILE_NAME is good to go
fi
fi
# Clean up the dwdiff output.
# * Strip out the lines we don't need (like the ones showing what files
# are being diffed).
# * Remove the + at the start of some lines (see Issue #2)
# * Remove the leading whitespace before the line numbers
cat $TEMP_FILE \
| sed -e '/^\+\+\+/ d' -e '/^---/ d' -e '/^@@.*@@/d' \
-e 's/^+//'\
| sed -E 's/^ +([0-9]+:[0-9]+)/\1/' \
| sed -E "s/("$'\E'"\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]) +([0-9]+)/\1\5/g" \
> "$TEMP_FILE.2"
# ^^^ this version can be used by vim, emacs, etc
# assuming they've got a plugin to support ANSI Escape Codes
# Convert the ncurses to html
# passing in a title, even though we're also passing in the
# --no-header option, which is kind-of silly, but I'm leaving it
# because it doesn't hurt and we may want it some day.
cat $TEMP_FILE \
| aha --stylesheet --no-header -t "$FILE_NAME" \
| perl -p -e "s/\n/XXX_NEWLINE_XXX/g" \
| sed -e "s/XXX_NEWLINE_XXX<\/span>/<\/span>\n/g" -e "s/XXX_NEWLINE_XXX/\n/g" \
> $TEMP_FILE$HTML_EXTENSION
# Clean up the HTML
# NOTE:
# The weird newlines in the middle of the sed substitutions are because
# it seems to be the best way to get a newline in there.. :/
#
# * build table cells and rows around line numbers, and comments.
cat $TEMP_FILE$HTML_EXTENSION \
| sed -e 's/<span class="bold green "><\/span>//g' \
-e 's/<span class="bold red "><\/span>//g' \
| sed -E "s/^[[:space:]]+//g" \
| sed -E "s|^([0-9]+:[0-9]+) {2,4}|</span></div>\
<div class='line pre'><span class='linenums'><a class='linenum_link' name='\1'>\1</a></span><span class='source'>|" \
| sed -E "s|^([0-9]+:[0-9]+) {0,4}|</span></div>\
<div class='line pre'><span class='linenums'><a class='linenum_link' name='\1'>\1</a></span><span class='source'>|" \
| sed -e $"s/<div class='line pre'>/\n\t\t\t<div class='line pre'>/g" \
| sed -E -e '/class="bold red|class="bold green/! {s/line pre/line pre unchanged/g;}' \
> "$TEMP_FILE.partial.$HTML_EXTENSION"
# what's that you say?
# symlinks suck in bash?
# yes... yes they do.
pushd `dirname $0` > /dev/null
SCRIPT_PATH=`pwd -P`
if [ -h "$SCRIPT_PATH/cdiff" ]; then # if it's a symbolic link
SCRIPT_PATH=$(dirname $(readlink "$SCRIPT_PATH/cdiff" ))
# set the path to the folder containing the file
# the symlink points to
fi
# Haz ourselves some CSS
# for prettification.
CSS=$(cat "$SCRIPT_PATH/cleandiff.css")
JS=$(cat "$SCRIPT_PATH/cleandiff.js")
# Haz ourselves some HTML
# for browserfication.
HEADER=$(cat <<EOL
<html>
<head>
<title>$FILE_NAME</title>
<style>
$CSS
</style>
<script language="JavaScript">
$JS
</script>
</head>
<body>
<input type="checkbox" id="unchanged_checkbox">
<div class="filename">
<label for="unchanged_checkbox">
<svg id="unchanged_toggle" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0" y="0" width="100" height="63" viewBox="0, 0, 100, 63">
<g id="Layer_1">
<path d="M58.6,29 L66.7,20.9 C68.3,23.7 69.2,27 69.2,30.4 C69.2,41 60.6,49.6 50,49.6 C46.5,49.6 43.3,48.7 40.5,47.1 L48.4,39.2 C48.8,39.2 49.1,39.3 49.5,39.3 C54.6,39.3 58.7,35.2 58.7,30.1 C58.7,29.7 58.7,29.4 58.6,29 z M58.7,30.1 C58.7,35.2 54.6,39.3 49.5,39.3 C49.1,39.3 48.8,39.3 48.4,39.2 L40.5,47.1 C43.3,48.7 46.6,49.6 50,49.6 C60.6,49.6 69.2,41 69.2,30.4 C69.2,26.9 68.3,23.7 66.7,20.9 L58.6,29 C58.7,29.4 58.7,29.7 58.7,30.1 z M100,30.3 C100,30.3 100,30.2 100,30.2 C100,30.2 100,30.2 100,30.1 C100,30.1 100,30 100,30 C100,30 100,29.9 100,29.9 C100,29.9 100,29.9 100,29.9 C99.9,29.3 99.7,28.8 99.3,28.3 C99.2,28.1 99.1,28 98.9,27.9 C95.7,24.1 91.9,20.8 88.1,17.7 C87.3,17.1 86.5,16.5 85.7,15.9 C85.6,15.8 85.5,15.7 85.3,15.6 C84.5,15 83.7,14.4 82.8,13.8 C82.8,13.8 82.7,13.8 82.7,13.7 C80.9,12.5 79.1,11.3 77.3,10.3 L66.7,21 C68.3,23.8 69.2,27.1 69.2,30.5 C69.2,41.1 60.6,49.7 50,49.7 C46.5,49.7 43.3,48.8 40.5,47.2 L32.3,55.4 C33.5,55.9 34.8,56.3 36.1,56.7 C36.1,56.7 36.2,56.7 36.2,56.7 C36.8,56.9 37.4,57 38,57.2 C38.1,57.2 38.2,57.3 38.3,57.3 C39,57.5 39.6,57.6 40.3,57.8 C44.8,58.8 49.4,59 54,58.7 C55.5,58.6 57,58.4 58.6,58.1 C62.1,57.5 65.5,56.4 68.7,55.1 C70.7,54.3 72.6,53.4 74.5,52.5 C77.4,51 80.3,49.3 83,47.4 C84.8,46.1 86.6,44.8 88.3,43.4 C90.9,41.3 93.3,39.1 95.7,36.8 C96.9,35.7 98,34.5 99,33.2 C99.3,32.9 99.5,32.6 99.7,32.3 C99.9,32 100,31.6 100.1,31.2 C100.1,31.2 100.1,31.2 100.1,31.2 C100.1,31.2 100.1,31.1 100.1,31.1 C100.1,31.1 100.1,31 100.1,31 C100.1,31 100.1,31 100.1,30.9 C100.1,30.9 100.1,30.8 100.1,30.8 C100.1,30.7 100.1,30.7 100.1,30.6 C100.1,30.5 100,30.4 100,30.3 z M58.7,30.1 C58.7,29.7 58.7,29.4 58.6,29 L48.4,39.2 C48.8,39.2 49.1,39.3 49.5,39.3 C54.6,39.3 58.7,35.2 58.7,30.1 z M48.4,39.2 L58.6,29 L66.7,20.9 L77.4,10.2 L84.8,2.8 C85.4,2.2 85.4,1.3 84.8,0.7 C84.2,0.1 83.3,0.1 82.7,0.7 L23,60.4 C22.4,61 22.4,61.9 23,62.5 C23.3,62.8 23.7,62.9 24.1,62.9 C24.5,62.9 24.9,62.8 25.2,62.5 L32.4,55.3 L40.6,47.1 L48.4,39.2 z" fill="#000000"/>
<path d="M67.8,5.7 C65.2,4.7 62.5,3.9 59.8,3.3 C53.7,2 47.6,1.9 41.5,3 C36,4 30.6,6.1 25.6,8.6 C17.8,12.6 10.6,18.1 4.4,24.3 C3.2,25.4 2.1,26.6 1.1,27.9 C-0.3,29.6 -0.3,31.5 1.1,33.2 C4.3,37 8.1,40.3 11.9,43.4 C15.3,46.1 18.9,48.6 22.7,50.8 L67.8,5.7 z" fill="#000000"/>
</g>
</svg>
$FILE_NAME
</label>
</div>
<div class="diff"><div><span>
<!-- last two to compensate for sedness -->
EOL)
# Mas HTML por favor.
FOOTER=$(cat <<EOL
</div>
</body>
<script>
activateAnchors();
</script>
</html>
EOL)
# you put your header in, you put your css in, you put your footer in
# and you shake it all around.
echo "$HEADER$(<"$TEMP_FILE.partial.$HTML_EXTENSION")$FOOTER" > $TEMP_FILE$HTML_EXTENSION
rm "$TEMP_FILE.partial.$HTML_EXTENSION"
# You do the hokey pokey, and you turn yourself around.
# That's what it's all about.
if [ -e "$HOME/.config/cdiff/cdiff1.sh" ]; then
source "$HOME/.config/cdiff/cdiff1.sh"
fi
if [ "$USE_FENESTRO" == "true" ]; then
fenestro --path=$TEMP_FILE$HTML_EXTENSION --name="$FILE_NAME"
elseif [ "$BROWSER" != "" ];
open -a "$BROWSER" $TEMP_FILE$HTML_EXTENSION
else
open $TEMP_FILE$HTML_EXTENSION
fi
sleep 0.5
rm $TEMP_FILE $TEMP_FILE$HTML_EXTENSION
exit 0
else
if [ "/dev/null" == "$A" ]; then
echo "Skipping binary file $B" # an added file
else
echo "Skipping binary file $A" # a removed file
fi
exit 1 # we don't diff binary files
fi