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)
17 * print a single line returned from xdiff
19 static void print_line(char *line, int len)
26 else if (line[0] == '-')
28 else if (line[0] == '@')
31 htmlf("<div class='%s'>", class);
39 * Receive diff-buffers from xdiff and concatenate them as
40 * needed across multiple callbacks.
42 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
43 * ripped from git and modified to use globals instead of
44 * a special callback-struct.
46 int diff_cb(void *priv_, mmbuffer_t *mb, int nbuf)
50 for (i = 0; i < nbuf; i++) {
51 if (mb[i].ptr[mb[i].size-1] != '\n') {
53 diff_buffer = xrealloc(diff_buffer,
54 diff_buffer_size + mb[i].size);
55 memcpy(diff_buffer + diff_buffer_size,
56 mb[i].ptr, mb[i].size);
57 diff_buffer_size += mb[i].size;
61 /* we have a complete line */
63 print_line(mb[i].ptr, mb[i].size);
66 diff_buffer = xrealloc(diff_buffer,
67 diff_buffer_size + mb[i].size);
68 memcpy(diff_buffer + diff_buffer_size, mb[i].ptr, mb[i].size);
69 print_line(diff_buffer, diff_buffer_size + mb[i].size);
75 print_line(diff_buffer, diff_buffer_size);
83 static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
87 if (is_null_sha1(sha1)) {
88 file->ptr = (char *)"";
91 file->ptr = read_sha1_file(sha1, type, &file->size);
96 static void run_diff(const unsigned char *sha1, const unsigned char *sha2)
98 mmfile_t file1, file2;
99 xpparam_t diff_params;
100 xdemitconf_t emit_params;
103 if (!load_mmfile(&file1, sha1) || !load_mmfile(&file2, sha2)) {
104 cgit_print_error("Unable to load files for diff");
108 diff_params.flags = XDF_NEED_MINIMAL;
110 emit_params.ctxlen = 3;
111 emit_params.flags = XDL_EMIT_FUNCNAMES;
113 emit_cb.outf = diff_cb;
115 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
120 void cgit_print_diff(const char *old_hex, const char *new_hex)
122 unsigned char sha1[20], sha2[20];
124 get_sha1(old_hex, sha1);
125 get_sha1(new_hex, sha2);
127 html("<table class='diff'><tr><td>");
128 run_diff(sha1, sha2);
129 html("</td></tr></table>");