summaryrefslogtreecommitdiffabout
path: root/ui-diff.c
Unidiff
Diffstat (limited to 'ui-diff.c') (more/less context) (ignore whitespace changes)
-rw-r--r--ui-diff.c5
1 files changed, 2 insertions, 3 deletions
diff --git a/ui-diff.c b/ui-diff.c
index b6486f1..0ad9faf 100644
--- a/ui-diff.c
+++ b/ui-diff.c
@@ -1,130 +1,129 @@
1/* ui-diff.c: show diff between two blobs 1/* ui-diff.c: show diff between two blobs
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10#include "xdiff.h"
11 10
12char *diff_buffer; 11char *diff_buffer;
13int diff_buffer_size; 12int diff_buffer_size;
14 13
15 14
16/* 15/*
17 * print a single line returned from xdiff 16 * print a single line returned from xdiff
18 */ 17 */
19static void print_line(char *line, int len) 18static void print_line(char *line, int len)
20{ 19{
21 char *class = "ctx"; 20 char *class = "ctx";
22 char c = line[len-1]; 21 char c = line[len-1];
23 22
24 if (line[0] == '+') 23 if (line[0] == '+')
25 class = "add"; 24 class = "add";
26 else if (line[0] == '-') 25 else if (line[0] == '-')
27 class = "del"; 26 class = "del";
28 else if (line[0] == '@') 27 else if (line[0] == '@')
29 class = "hunk"; 28 class = "hunk";
30 29
31 htmlf("<div class='%s'>", class); 30 htmlf("<div class='%s'>", class);
32 line[len-1] = '\0'; 31 line[len-1] = '\0';
33 html_txt(line); 32 html_txt(line);
34 html("</div>"); 33 html("</div>");
35 line[len-1] = c; 34 line[len-1] = c;
36} 35}
37 36
38/* 37/*
39 * Receive diff-buffers from xdiff and concatenate them as 38 * Receive diff-buffers from xdiff and concatenate them as
40 * needed across multiple callbacks. 39 * needed across multiple callbacks.
41 * 40 *
42 * This is basically a copy of xdiff-interface.c/xdiff_outf(), 41 * This is basically a copy of xdiff-interface.c/xdiff_outf(),
43 * ripped from git and modified to use globals instead of 42 * ripped from git and modified to use globals instead of
44 * a special callback-struct. 43 * a special callback-struct.
45 */ 44 */
46int diff_cb(void *priv_, mmbuffer_t *mb, int nbuf) 45int diff_cb(void *priv_, mmbuffer_t *mb, int nbuf)
47{ 46{
48 int i; 47 int i;
49 48
50 for (i = 0; i < nbuf; i++) { 49 for (i = 0; i < nbuf; i++) {
51 if (mb[i].ptr[mb[i].size-1] != '\n') { 50 if (mb[i].ptr[mb[i].size-1] != '\n') {
52 /* Incomplete line */ 51 /* Incomplete line */
53 diff_buffer = xrealloc(diff_buffer, 52 diff_buffer = xrealloc(diff_buffer,
54 diff_buffer_size + mb[i].size); 53 diff_buffer_size + mb[i].size);
55 memcpy(diff_buffer + diff_buffer_size, 54 memcpy(diff_buffer + diff_buffer_size,
56 mb[i].ptr, mb[i].size); 55 mb[i].ptr, mb[i].size);
57 diff_buffer_size += mb[i].size; 56 diff_buffer_size += mb[i].size;
58 continue; 57 continue;
59 } 58 }
60 59
61 /* we have a complete line */ 60 /* we have a complete line */
62 if (!diff_buffer) { 61 if (!diff_buffer) {
63 print_line(mb[i].ptr, mb[i].size); 62 print_line(mb[i].ptr, mb[i].size);
64 continue; 63 continue;
65 } 64 }
66 diff_buffer = xrealloc(diff_buffer, 65 diff_buffer = xrealloc(diff_buffer,
67 diff_buffer_size + mb[i].size); 66 diff_buffer_size + mb[i].size);
68 memcpy(diff_buffer + diff_buffer_size, mb[i].ptr, mb[i].size); 67 memcpy(diff_buffer + diff_buffer_size, mb[i].ptr, mb[i].size);
69 print_line(diff_buffer, diff_buffer_size + mb[i].size); 68 print_line(diff_buffer, diff_buffer_size + mb[i].size);
70 free(diff_buffer); 69 free(diff_buffer);
71 diff_buffer = NULL; 70 diff_buffer = NULL;
72 diff_buffer_size = 0; 71 diff_buffer_size = 0;
73 } 72 }
74 if (diff_buffer) { 73 if (diff_buffer) {
75 print_line(diff_buffer, diff_buffer_size); 74 print_line(diff_buffer, diff_buffer_size);
76 free(diff_buffer); 75 free(diff_buffer);
77 diff_buffer = NULL; 76 diff_buffer = NULL;
78 diff_buffer_size = 0; 77 diff_buffer_size = 0;
79 } 78 }
80 return 0; 79 return 0;
81} 80}
82 81
83static int load_mmfile(mmfile_t *file, const unsigned char *sha1) 82static int load_mmfile(mmfile_t *file, const unsigned char *sha1)
84{ 83{
85 char type[20]; 84 enum object_type type;
86 85
87 if (is_null_sha1(sha1)) { 86 if (is_null_sha1(sha1)) {
88 file->ptr = (char *)""; 87 file->ptr = (char *)"";
89 file->size = 0; 88 file->size = 0;
90 } else { 89 } else {
91 file->ptr = read_sha1_file(sha1, type, &file->size); 90 file->ptr = read_sha1_file(sha1, &type, &file->size);
92 } 91 }
93 return 1; 92 return 1;
94} 93}
95 94
96static void run_diff(const unsigned char *sha1, const unsigned char *sha2) 95static void run_diff(const unsigned char *sha1, const unsigned char *sha2)
97{ 96{
98 mmfile_t file1, file2; 97 mmfile_t file1, file2;
99 xpparam_t diff_params; 98 xpparam_t diff_params;
100 xdemitconf_t emit_params; 99 xdemitconf_t emit_params;
101 xdemitcb_t emit_cb; 100 xdemitcb_t emit_cb;
102 101
103 if (!load_mmfile(&file1, sha1) || !load_mmfile(&file2, sha2)) { 102 if (!load_mmfile(&file1, sha1) || !load_mmfile(&file2, sha2)) {
104 cgit_print_error("Unable to load files for diff"); 103 cgit_print_error("Unable to load files for diff");
105 return; 104 return;
106 } 105 }
107 106
108 diff_params.flags = XDF_NEED_MINIMAL; 107 diff_params.flags = XDF_NEED_MINIMAL;
109 108
110 emit_params.ctxlen = 3; 109 emit_params.ctxlen = 3;
111 emit_params.flags = XDL_EMIT_FUNCNAMES; 110 emit_params.flags = XDL_EMIT_FUNCNAMES;
112 111
113 emit_cb.outf = diff_cb; 112 emit_cb.outf = diff_cb;
114 113
115 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb); 114 xdl_diff(&file1, &file2, &diff_params, &emit_params, &emit_cb);
116} 115}
117 116
118 117
119 118
120void cgit_print_diff(const char *old_hex, const char *new_hex) 119void cgit_print_diff(const char *old_hex, const char *new_hex)
121{ 120{
122 unsigned char sha1[20], sha2[20]; 121 unsigned char sha1[20], sha2[20];
123 122
124 get_sha1(old_hex, sha1); 123 get_sha1(old_hex, sha1);
125 get_sha1(new_hex, sha2); 124 get_sha1(new_hex, sha2);
126 125
127 html("<table class='diff'><tr><td>"); 126 html("<table class='diff'><tr><td>");
128 run_diff(sha1, sha2); 127 run_diff(sha1, sha2);
129 html("</td></tr></table>"); 128 html("</td></tr></table>");
130} 129}