1 /* ui-diff.c: show diff between two blobs
3 * Copyright (C) 2006 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
11 #include "ui-shared.h"
12 #include "ui-ssdiff.h"
14 unsigned char old_rev_sha1
[20];
15 unsigned char new_rev_sha1
[20];
17 static int files
, slots
;
18 static int total_adds
, total_rems
, max_changes
;
19 static int lines_added
, lines_removed
;
21 static struct fileinfo
{
23 unsigned char old_sha1
[20];
24 unsigned char new_sha1
[20];
25 unsigned short old_mode
;
26 unsigned short new_mode
;
31 unsigned long old_size
;
32 unsigned long new_size
;
36 static int use_ssdiff
= 0;
37 static struct diff_filepair
*current_filepair
;
39 struct diff_filespec
*cgit_get_current_old_file(void)
41 return current_filepair
->one
;
44 struct diff_filespec
*cgit_get_current_new_file(void)
46 return current_filepair
->two
;
49 static void print_fileinfo(struct fileinfo
*info
)
53 switch (info
->status
) {
54 case DIFF_STATUS_ADDED
:
57 case DIFF_STATUS_COPIED
:
60 case DIFF_STATUS_DELETED
:
63 case DIFF_STATUS_MODIFIED
:
66 case DIFF_STATUS_RENAMED
:
69 case DIFF_STATUS_TYPE_CHANGED
:
72 case DIFF_STATUS_UNKNOWN
:
75 case DIFF_STATUS_UNMERGED
:
79 die("bug: unhandled diff status %c", info
->status
);
83 htmlf("<td class='mode'>");
84 if (is_null_sha1(info
->new_sha1
)) {
85 cgit_print_filemode(info
->old_mode
);
87 cgit_print_filemode(info
->new_mode
);
90 if (info
->old_mode
!= info
->new_mode
&&
91 !is_null_sha1(info
->old_sha1
) &&
92 !is_null_sha1(info
->new_sha1
)) {
93 html("<span class='modechange'>[");
94 cgit_print_filemode(info
->old_mode
);
97 htmlf("</td><td class='%s'>", class);
98 cgit_diff_link(info
->new_path
, NULL
, NULL
, ctx
.qry
.head
, ctx
.qry
.sha1
,
99 ctx
.qry
.sha2
, info
->new_path
, 0);
100 if (info
->status
== DIFF_STATUS_COPIED
|| info
->status
== DIFF_STATUS_RENAMED
)
101 htmlf(" (%s from %s)",
102 info
->status
== DIFF_STATUS_COPIED
? "copied" : "renamed",
104 html("</td><td class='right'>");
106 htmlf("bin</td><td class='graph'>%ld -> %ld bytes",
107 info
->old_size
, info
->new_size
);
110 htmlf("%d", info
->added
+ info
->removed
);
111 html("</td><td class='graph'>");
112 htmlf("<table summary='file diffstat' width='%d%%'><tr>", (max_changes
> 100 ? 100 : max_changes
));
113 htmlf("<td class='add' style='width: %.1f%%;'/>",
114 info
->added
* 100.0 / max_changes
);
115 htmlf("<td class='rem' style='width: %.1f%%;'/>",
116 info
->removed
* 100.0 / max_changes
);
117 htmlf("<td class='none' style='width: %.1f%%;'/>",
118 (max_changes
- info
->removed
- info
->added
) * 100.0 / max_changes
);
119 html("</tr></table></td></tr>\n");
122 static void count_diff_lines(char *line
, int len
)
124 if (line
&& (len
> 0)) {
127 else if (line
[0] == '-')
132 static void inspect_filepair(struct diff_filepair
*pair
)
135 unsigned long old_size
= 0;
136 unsigned long new_size
= 0;
140 cgit_diff_files(pair
->one
->sha1
, pair
->two
->sha1
, &old_size
, &new_size
,
141 &binary
, 0, ctx
.qry
.ignorews
, count_diff_lines
);
142 if (files
>= slots
) {
147 items
= xrealloc(items
, slots
* sizeof(struct fileinfo
));
149 items
[files
-1].status
= pair
->status
;
150 hashcpy(items
[files
-1].old_sha1
, pair
->one
->sha1
);
151 hashcpy(items
[files
-1].new_sha1
, pair
->two
->sha1
);
152 items
[files
-1].old_mode
= pair
->one
->mode
;
153 items
[files
-1].new_mode
= pair
->two
->mode
;
154 items
[files
-1].old_path
= xstrdup(pair
->one
->path
);
155 items
[files
-1].new_path
= xstrdup(pair
->two
->path
);
156 items
[files
-1].added
= lines_added
;
157 items
[files
-1].removed
= lines_removed
;
158 items
[files
-1].old_size
= old_size
;
159 items
[files
-1].new_size
= new_size
;
160 items
[files
-1].binary
= binary
;
161 if (lines_added
+ lines_removed
> max_changes
)
162 max_changes
= lines_added
+ lines_removed
;
163 total_adds
+= lines_added
;
164 total_rems
+= lines_removed
;
167 void cgit_print_diffstat(const unsigned char *old_sha1
,
168 const unsigned char *new_sha1
, const char *prefix
)
170 int i
, save_context
= ctx
.qry
.context
;
172 html("<div class='diffstat-header'>");
173 cgit_diff_link("Diffstat", NULL
, NULL
, ctx
.qry
.head
, ctx
.qry
.sha1
,
174 ctx
.qry
.sha2
, NULL
, 0);
176 html(" (limited to '");
181 ctx
.qry
.context
= (save_context
> 0 ? save_context
: 3) << 1;
182 cgit_self_link("more", NULL
, NULL
, &ctx
);
184 ctx
.qry
.context
= (save_context
> 3 ? save_context
: 3) >> 1;
185 cgit_self_link("less", NULL
, NULL
, &ctx
);
186 ctx
.qry
.context
= save_context
;
189 ctx
.qry
.ignorews
= (ctx
.qry
.ignorews
+ 1) % 2;
190 cgit_self_link(ctx
.qry
.ignorews
? "ignore" : "show", NULL
, NULL
, &ctx
);
191 ctx
.qry
.ignorews
= (ctx
.qry
.ignorews
+ 1) % 2;
192 html(" whitespace changes)");
194 html("<table summary='diffstat' class='diffstat'>");
196 cgit_diff_tree(old_sha1
, new_sha1
, inspect_filepair
, prefix
,
198 for(i
= 0; i
<files
; i
++)
199 print_fileinfo(&items
[i
]);
201 html("<div class='diffstat-summary'>");
202 htmlf("%d files changed, %d insertions, %d deletions",
203 files
, total_adds
, total_rems
);
209 * print a single line returned from xdiff
211 static void print_line(char *line
, int len
)
214 char c
= line
[len
-1];
218 else if (line
[0] == '-')
220 else if (line
[0] == '@')
223 htmlf("<div class='%s'>", class);
230 static void header(unsigned char *sha1
, char *path1
, int mode1
,
231 unsigned char *sha2
, char *path2
, int mode2
)
233 char *abbrev1
, *abbrev2
;
236 subproject
= (S_ISGITLINK(mode1
) || S_ISGITLINK(mode2
));
237 html("<div class='head'>");
238 html("diff --git a/");
243 if (is_null_sha1(sha1
))
245 if (is_null_sha1(sha2
))
249 htmlf("<br/>new file mode %.6o", mode2
);
252 htmlf("<br/>deleted file mode %.6o", mode1
);
255 abbrev1
= xstrdup(find_unique_abbrev(sha1
, DEFAULT_ABBREV
));
256 abbrev2
= xstrdup(find_unique_abbrev(sha2
, DEFAULT_ABBREV
));
257 htmlf("<br/>index %s..%s", abbrev1
, abbrev2
);
260 if (mode1
!= 0 && mode2
!= 0) {
261 htmlf(" %.6o", mode1
);
263 htmlf("..%.6o", mode2
);
267 cgit_tree_link(path1
, NULL
, NULL
, ctx
.qry
.head
,
268 sha1_to_hex(old_rev_sha1
), path1
);
273 cgit_tree_link(path2
, NULL
, NULL
, ctx
.qry
.head
,
274 sha1_to_hex(new_rev_sha1
), path2
);
281 static void print_ssdiff_link()
283 if (!strcmp(ctx
.qry
.page
, "diff")) {
285 cgit_diff_link("Unidiff", NULL
, NULL
, ctx
.qry
.head
,
286 ctx
.qry
.sha1
, ctx
.qry
.sha2
, ctx
.qry
.path
, 1);
288 cgit_diff_link("Side-by-side diff", NULL
, NULL
,
289 ctx
.qry
.head
, ctx
.qry
.sha1
,
290 ctx
.qry
.sha2
, ctx
.qry
.path
, 1);
294 static void filepair_cb(struct diff_filepair
*pair
)
296 unsigned long old_size
= 0;
297 unsigned long new_size
= 0;
299 linediff_fn print_line_fn
= print_line
;
301 current_filepair
= pair
;
303 cgit_ssdiff_header_begin();
304 print_line_fn
= cgit_ssdiff_line_cb
;
306 header(pair
->one
->sha1
, pair
->one
->path
, pair
->one
->mode
,
307 pair
->two
->sha1
, pair
->two
->path
, pair
->two
->mode
);
309 cgit_ssdiff_header_end();
310 if (S_ISGITLINK(pair
->one
->mode
) || S_ISGITLINK(pair
->two
->mode
)) {
311 if (S_ISGITLINK(pair
->one
->mode
))
312 print_line_fn(fmt("-Subproject %s", sha1_to_hex(pair
->one
->sha1
)), 52);
313 if (S_ISGITLINK(pair
->two
->mode
))
314 print_line_fn(fmt("+Subproject %s", sha1_to_hex(pair
->two
->sha1
)), 52);
316 cgit_ssdiff_footer();
319 if (cgit_diff_files(pair
->one
->sha1
, pair
->two
->sha1
, &old_size
,
320 &new_size
, &binary
, ctx
.qry
.context
,
321 ctx
.qry
.ignorews
, print_line_fn
))
322 cgit_print_error("Error running diff");
325 html("<tr><td colspan='4'>Binary files differ</td></tr>");
327 html("Binary files differ");
330 cgit_ssdiff_footer();
333 void cgit_print_diff(const char *new_rev
, const char *old_rev
, const char *prefix
)
335 enum object_type type
;
337 struct commit
*commit
, *commit2
;
340 new_rev
= ctx
.qry
.head
;
341 get_sha1(new_rev
, new_rev_sha1
);
342 type
= sha1_object_info(new_rev_sha1
, &size
);
343 if (type
== OBJ_BAD
) {
344 cgit_print_error(fmt("Bad object name: %s", new_rev
));
347 commit
= lookup_commit_reference(new_rev_sha1
);
348 if (!commit
|| parse_commit(commit
)) {
349 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1
)));
354 get_sha1(old_rev
, old_rev_sha1
);
355 else if (commit
->parents
&& commit
->parents
->item
)
356 hashcpy(old_rev_sha1
, commit
->parents
->item
->object
.sha1
);
358 hashclr(old_rev_sha1
);
360 if (!is_null_sha1(old_rev_sha1
)) {
361 type
= sha1_object_info(old_rev_sha1
, &size
);
362 if (type
== OBJ_BAD
) {
363 cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1
)));
366 commit2
= lookup_commit_reference(old_rev_sha1
);
367 if (!commit2
|| parse_commit(commit2
)) {
368 cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1
)));
373 if ((ctx
.qry
.ssdiff
&& !ctx
.cfg
.ssdiff
) || (!ctx
.qry
.ssdiff
&& ctx
.cfg
.ssdiff
))
377 cgit_print_diffstat(old_rev_sha1
, new_rev_sha1
, prefix
);
380 html("<table summary='ssdiff' class='ssdiff'>");
382 html("<table summary='diff' class='diff'>");
385 cgit_diff_tree(old_rev_sha1
, new_rev_sha1
, filepair_cb
, prefix
,