Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 1 | #include "cgit.h" |
John Keeping | 8f20879 | 2013-04-06 11:37:59 +0100 | [diff] [blame] | 2 | #include "ui-ssdiff.h" |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 3 | #include "html.h" |
| 4 | #include "ui-shared.h" |
Bernhard Reutner-Fischer | e52040b | 2010-12-23 12:47:55 +0100 | [diff] [blame] | 5 | #include "ui-diff.h" |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 6 | |
| 7 | extern int use_ssdiff; |
| 8 | |
| 9 | static int current_old_line, current_new_line; |
Jamie Couture | e19f7d7 | 2011-09-17 18:25:01 -0400 | [diff] [blame] | 10 | static int **L = NULL; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 11 | |
| 12 | struct deferred_lines { |
| 13 | int line_no; |
| 14 | char *line; |
| 15 | struct deferred_lines *next; |
| 16 | }; |
| 17 | |
| 18 | static struct deferred_lines *deferred_old, *deferred_old_last; |
| 19 | static struct deferred_lines *deferred_new, *deferred_new_last; |
| 20 | |
John Keeping | e3d3fff | 2015-03-08 16:32:16 +0000 | [diff] [blame] | 21 | static void create_or_reset_lcs_table(void) |
Jamie Couture | e19f7d7 | 2011-09-17 18:25:01 -0400 | [diff] [blame] | 22 | { |
| 23 | int i; |
| 24 | |
| 25 | if (L != NULL) { |
Jamie Couture | 6a575b8 | 2012-01-11 22:38:49 -0500 | [diff] [blame] | 26 | memset(*L, 0, sizeof(int) * MAX_SSDIFF_SIZE); |
Jamie Couture | e19f7d7 | 2011-09-17 18:25:01 -0400 | [diff] [blame] | 27 | return; |
| 28 | } |
| 29 | |
| 30 | // xcalloc will die if we ran out of memory; |
| 31 | // not very helpful for debugging |
| 32 | L = (int**)xcalloc(MAX_SSDIFF_M, sizeof(int *)); |
| 33 | *L = (int*)xcalloc(MAX_SSDIFF_SIZE, sizeof(int)); |
| 34 | |
| 35 | for (i = 1; i < MAX_SSDIFF_M; i++) { |
| 36 | L[i] = *L + i * MAX_SSDIFF_N; |
| 37 | } |
| 38 | } |
| 39 | |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 40 | static char *longest_common_subsequence(char *A, char *B) |
| 41 | { |
| 42 | int i, j, ri; |
| 43 | int m = strlen(A); |
| 44 | int n = strlen(B); |
Eric Wong | 19c3123 | 2012-01-04 08:59:15 +0000 | [diff] [blame] | 45 | int tmp1, tmp2; |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 46 | int lcs_length; |
| 47 | char *result; |
| 48 | |
Jamie Couture | e19f7d7 | 2011-09-17 18:25:01 -0400 | [diff] [blame] | 49 | // We bail if the lines are too long |
Eric Wong | 19c3123 | 2012-01-04 08:59:15 +0000 | [diff] [blame] | 50 | if (m >= MAX_SSDIFF_M || n >= MAX_SSDIFF_N) |
Jamie Couture | e19f7d7 | 2011-09-17 18:25:01 -0400 | [diff] [blame] | 51 | return NULL; |
| 52 | |
| 53 | create_or_reset_lcs_table(); |
| 54 | |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 55 | for (i = m; i >= 0; i--) { |
| 56 | for (j = n; j >= 0; j--) { |
| 57 | if (A[i] == '\0' || B[j] == '\0') { |
| 58 | L[i][j] = 0; |
| 59 | } else if (A[i] == B[j]) { |
| 60 | L[i][j] = 1 + L[i + 1][j + 1]; |
| 61 | } else { |
| 62 | tmp1 = L[i + 1][j]; |
| 63 | tmp2 = L[i][j + 1]; |
| 64 | L[i][j] = (tmp1 > tmp2 ? tmp1 : tmp2); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | lcs_length = L[0][0]; |
| 70 | result = xmalloc(lcs_length + 2); |
| 71 | memset(result, 0, sizeof(*result) * (lcs_length + 2)); |
| 72 | |
| 73 | ri = 0; |
| 74 | i = 0; |
| 75 | j = 0; |
| 76 | while (i < m && j < n) { |
| 77 | if (A[i] == B[j]) { |
| 78 | result[ri] = A[i]; |
| 79 | ri += 1; |
| 80 | i += 1; |
| 81 | j += 1; |
| 82 | } else if (L[i + 1][j] >= L[i][j + 1]) { |
| 83 | i += 1; |
| 84 | } else { |
| 85 | j += 1; |
| 86 | } |
| 87 | } |
Jamie Couture | e19f7d7 | 2011-09-17 18:25:01 -0400 | [diff] [blame] | 88 | |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 89 | return result; |
| 90 | } |
| 91 | |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 92 | static int line_from_hunk(char *line, char type) |
| 93 | { |
| 94 | char *buf1, *buf2; |
John Keeping | 7e67c64 | 2016-08-07 16:14:49 +0100 | [diff] [blame] | 95 | int len, res; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 96 | |
| 97 | buf1 = strchr(line, type); |
| 98 | if (buf1 == NULL) |
| 99 | return 0; |
| 100 | buf1 += 1; |
| 101 | buf2 = strchr(buf1, ','); |
| 102 | if (buf2 == NULL) |
| 103 | return 0; |
| 104 | len = buf2 - buf1; |
| 105 | buf2 = xmalloc(len + 1); |
Christian Hesse | 0899eb6 | 2018-08-28 18:22:26 +0200 | [diff] [blame^] | 106 | strlcpy(buf2, buf1, len + 1); |
John Keeping | 7e67c64 | 2016-08-07 16:14:49 +0100 | [diff] [blame] | 107 | res = atoi(buf2); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 108 | free(buf2); |
| 109 | return res; |
| 110 | } |
| 111 | |
| 112 | static char *replace_tabs(char *line) |
| 113 | { |
| 114 | char *prev_buf = line; |
| 115 | char *cur_buf; |
Jason A. Donenfeld | 08a2b1b | 2018-07-04 03:13:31 +0200 | [diff] [blame] | 116 | size_t linelen = strlen(line); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 117 | int n_tabs = 0; |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 118 | int i; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 119 | char *result; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 120 | |
| 121 | if (linelen == 0) { |
| 122 | result = xmalloc(1); |
| 123 | result[0] = '\0'; |
| 124 | return result; |
| 125 | } |
| 126 | |
Jason A. Donenfeld | 08a2b1b | 2018-07-04 03:13:31 +0200 | [diff] [blame] | 127 | for (i = 0; i < linelen; i++) { |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 128 | if (line[i] == '\t') |
| 129 | n_tabs += 1; |
Jason A. Donenfeld | 08a2b1b | 2018-07-04 03:13:31 +0200 | [diff] [blame] | 130 | } |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 131 | result = xmalloc(linelen + n_tabs * 8 + 1); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 132 | result[0] = '\0'; |
| 133 | |
Jason A. Donenfeld | 08a2b1b | 2018-07-04 03:13:31 +0200 | [diff] [blame] | 134 | for (;;) { |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 135 | cur_buf = strchr(prev_buf, '\t'); |
| 136 | if (!cur_buf) { |
| 137 | strcat(result, prev_buf); |
| 138 | break; |
| 139 | } else { |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 140 | strncat(result, prev_buf, cur_buf - prev_buf); |
Jason A. Donenfeld | 08a2b1b | 2018-07-04 03:13:31 +0200 | [diff] [blame] | 141 | linelen = strlen(result); |
| 142 | memset(&result[linelen], ' ', 8 - (linelen % 8)); |
| 143 | result[linelen + 8 - (linelen % 8)] = '\0'; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 144 | } |
| 145 | prev_buf = cur_buf + 1; |
| 146 | } |
| 147 | return result; |
| 148 | } |
| 149 | |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 150 | static int calc_deferred_lines(struct deferred_lines *start) |
| 151 | { |
| 152 | struct deferred_lines *item = start; |
| 153 | int result = 0; |
| 154 | while (item) { |
| 155 | result += 1; |
| 156 | item = item->next; |
| 157 | } |
| 158 | return result; |
| 159 | } |
| 160 | |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 161 | static void deferred_old_add(char *line, int line_no) |
| 162 | { |
| 163 | struct deferred_lines *item = xmalloc(sizeof(struct deferred_lines)); |
| 164 | item->line = xstrdup(line); |
| 165 | item->line_no = line_no; |
| 166 | item->next = NULL; |
| 167 | if (deferred_old) { |
| 168 | deferred_old_last->next = item; |
| 169 | deferred_old_last = item; |
| 170 | } else { |
| 171 | deferred_old = deferred_old_last = item; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | static void deferred_new_add(char *line, int line_no) |
| 176 | { |
| 177 | struct deferred_lines *item = xmalloc(sizeof(struct deferred_lines)); |
| 178 | item->line = xstrdup(line); |
| 179 | item->line_no = line_no; |
| 180 | item->next = NULL; |
| 181 | if (deferred_new) { |
| 182 | deferred_new_last->next = item; |
| 183 | deferred_new_last = item; |
| 184 | } else { |
| 185 | deferred_new = deferred_new_last = item; |
| 186 | } |
| 187 | } |
| 188 | |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 189 | static void print_part_with_lcs(char *class, char *line, char *lcs) |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 190 | { |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 191 | int line_len = strlen(line); |
| 192 | int i, j; |
| 193 | char c[2] = " "; |
| 194 | int same = 1; |
| 195 | |
| 196 | j = 0; |
| 197 | for (i = 0; i < line_len; i++) { |
| 198 | c[0] = line[i]; |
| 199 | if (same) { |
| 200 | if (line[i] == lcs[j]) |
| 201 | j += 1; |
| 202 | else { |
| 203 | same = 0; |
| 204 | htmlf("<span class='%s'>", class); |
| 205 | } |
| 206 | } else if (line[i] == lcs[j]) { |
| 207 | same = 1; |
| 208 | htmlf("</span>"); |
| 209 | j += 1; |
| 210 | } |
| 211 | html_txt(c); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | static void print_ssdiff_line(char *class, |
| 216 | int old_line_no, |
| 217 | char *old_line, |
| 218 | int new_line_no, |
| 219 | char *new_line, int individual_chars) |
| 220 | { |
| 221 | char *lcs = NULL; |
Bernhard Reutner-Fischer | e52040b | 2010-12-23 12:47:55 +0100 | [diff] [blame] | 222 | |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 223 | if (old_line) |
| 224 | old_line = replace_tabs(old_line + 1); |
| 225 | if (new_line) |
| 226 | new_line = replace_tabs(new_line + 1); |
| 227 | if (individual_chars && old_line && new_line) |
| 228 | lcs = longest_common_subsequence(old_line, new_line); |
Bernhard Reutner-Fischer | e52040b | 2010-12-23 12:47:55 +0100 | [diff] [blame] | 229 | html("<tr>\n"); |
| 230 | if (old_line_no > 0) { |
| 231 | struct diff_filespec *old_file = cgit_get_current_old_file(); |
| 232 | char *lineno_str = fmt("n%d", old_line_no); |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 233 | char *id_str = fmt("id=%s#%s", is_null_oid(&old_file->oid)?"HEAD":oid_to_hex(old_rev_oid), lineno_str); |
Christian Hesse | fa5810e | 2015-10-09 13:15:45 +0200 | [diff] [blame] | 234 | char *fileurl = cgit_fileurl(ctx.repo->url, "tree", old_file->path, id_str); |
Peter Wu | 4468ec1 | 2013-10-03 12:17:23 +0200 | [diff] [blame] | 235 | html("<td class='lineno'><a href='"); |
Christian Hesse | fa5810e | 2015-10-09 13:15:45 +0200 | [diff] [blame] | 236 | html(fileurl); |
Peter Wu | 4468ec1 | 2013-10-03 12:17:23 +0200 | [diff] [blame] | 237 | htmlf("' id='%s'>%s</a>", lineno_str, lineno_str + 1); |
Bernhard Reutner-Fischer | e52040b | 2010-12-23 12:47:55 +0100 | [diff] [blame] | 238 | html("</td>"); |
| 239 | htmlf("<td class='%s'>", class); |
Christian Hesse | fa5810e | 2015-10-09 13:15:45 +0200 | [diff] [blame] | 240 | free(fileurl); |
Bernhard Reutner-Fischer | e52040b | 2010-12-23 12:47:55 +0100 | [diff] [blame] | 241 | } else if (old_line) |
Ragnar Ouchterlony | 4a198e4 | 2009-09-16 18:56:26 +0200 | [diff] [blame] | 242 | htmlf("<td class='lineno'></td><td class='%s'>", class); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 243 | else |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 244 | htmlf("<td class='lineno'></td><td class='%s_dark'>", class); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 245 | if (old_line) { |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 246 | if (lcs) |
| 247 | print_part_with_lcs("del", old_line, lcs); |
| 248 | else |
| 249 | html_txt(old_line); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Bernhard Reutner-Fischer | e52040b | 2010-12-23 12:47:55 +0100 | [diff] [blame] | 252 | html("</td>\n"); |
| 253 | if (new_line_no > 0) { |
| 254 | struct diff_filespec *new_file = cgit_get_current_new_file(); |
| 255 | char *lineno_str = fmt("n%d", new_line_no); |
Christian Hesse | 11695a5 | 2016-09-04 12:38:18 +0200 | [diff] [blame] | 256 | char *id_str = fmt("id=%s#%s", is_null_oid(&new_file->oid)?"HEAD":oid_to_hex(new_rev_oid), lineno_str); |
Christian Hesse | fa5810e | 2015-10-09 13:15:45 +0200 | [diff] [blame] | 257 | char *fileurl = cgit_fileurl(ctx.repo->url, "tree", new_file->path, id_str); |
Peter Wu | 4468ec1 | 2013-10-03 12:17:23 +0200 | [diff] [blame] | 258 | html("<td class='lineno'><a href='"); |
Christian Hesse | fa5810e | 2015-10-09 13:15:45 +0200 | [diff] [blame] | 259 | html(fileurl); |
Peter Wu | 4468ec1 | 2013-10-03 12:17:23 +0200 | [diff] [blame] | 260 | htmlf("' id='%s'>%s</a>", lineno_str, lineno_str + 1); |
Bernhard Reutner-Fischer | e52040b | 2010-12-23 12:47:55 +0100 | [diff] [blame] | 261 | html("</td>"); |
| 262 | htmlf("<td class='%s'>", class); |
Christian Hesse | fa5810e | 2015-10-09 13:15:45 +0200 | [diff] [blame] | 263 | free(fileurl); |
Bernhard Reutner-Fischer | e52040b | 2010-12-23 12:47:55 +0100 | [diff] [blame] | 264 | } else if (new_line) |
Ragnar Ouchterlony | 4a198e4 | 2009-09-16 18:56:26 +0200 | [diff] [blame] | 265 | htmlf("<td class='lineno'></td><td class='%s'>", class); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 266 | else |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 267 | htmlf("<td class='lineno'></td><td class='%s_dark'>", class); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 268 | if (new_line) { |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 269 | if (lcs) |
| 270 | print_part_with_lcs("add", new_line, lcs); |
| 271 | else |
| 272 | html_txt(new_line); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | html("</td></tr>"); |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 276 | if (lcs) |
| 277 | free(lcs); |
| 278 | if (new_line) |
| 279 | free(new_line); |
| 280 | if (old_line) |
| 281 | free(old_line); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 282 | } |
| 283 | |
John Keeping | e3d3fff | 2015-03-08 16:32:16 +0000 | [diff] [blame] | 284 | static void print_deferred_old_lines(void) |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 285 | { |
| 286 | struct deferred_lines *iter_old, *tmp; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 287 | iter_old = deferred_old; |
| 288 | while (iter_old) { |
| 289 | print_ssdiff_line("del", iter_old->line_no, |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 290 | iter_old->line, -1, NULL, 0); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 291 | tmp = iter_old->next; |
| 292 | free(iter_old); |
| 293 | iter_old = tmp; |
| 294 | } |
| 295 | } |
| 296 | |
John Keeping | e3d3fff | 2015-03-08 16:32:16 +0000 | [diff] [blame] | 297 | static void print_deferred_new_lines(void) |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 298 | { |
| 299 | struct deferred_lines *iter_new, *tmp; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 300 | iter_new = deferred_new; |
| 301 | while (iter_new) { |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 302 | print_ssdiff_line("add", -1, NULL, |
| 303 | iter_new->line_no, iter_new->line, 0); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 304 | tmp = iter_new->next; |
| 305 | free(iter_new); |
| 306 | iter_new = tmp; |
| 307 | } |
| 308 | } |
| 309 | |
John Keeping | e3d3fff | 2015-03-08 16:32:16 +0000 | [diff] [blame] | 310 | static void print_deferred_changed_lines(void) |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 311 | { |
| 312 | struct deferred_lines *iter_old, *iter_new, *tmp; |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 313 | int n_old_lines = calc_deferred_lines(deferred_old); |
| 314 | int n_new_lines = calc_deferred_lines(deferred_new); |
| 315 | int individual_chars = (n_old_lines == n_new_lines ? 1 : 0); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 316 | |
| 317 | iter_old = deferred_old; |
| 318 | iter_new = deferred_new; |
| 319 | while (iter_old || iter_new) { |
| 320 | if (iter_old && iter_new) |
| 321 | print_ssdiff_line("changed", iter_old->line_no, |
| 322 | iter_old->line, |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 323 | iter_new->line_no, iter_new->line, |
| 324 | individual_chars); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 325 | else if (iter_old) |
| 326 | print_ssdiff_line("changed", iter_old->line_no, |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 327 | iter_old->line, -1, NULL, 0); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 328 | else if (iter_new) |
| 329 | print_ssdiff_line("changed", -1, NULL, |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 330 | iter_new->line_no, iter_new->line, 0); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 331 | if (iter_old) { |
| 332 | tmp = iter_old->next; |
| 333 | free(iter_old); |
| 334 | iter_old = tmp; |
| 335 | } |
| 336 | |
| 337 | if (iter_new) { |
| 338 | tmp = iter_new->next; |
| 339 | free(iter_new); |
| 340 | iter_new = tmp; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
John Keeping | e3d3fff | 2015-03-08 16:32:16 +0000 | [diff] [blame] | 345 | void cgit_ssdiff_print_deferred_lines(void) |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 346 | { |
| 347 | if (!deferred_old && !deferred_new) |
| 348 | return; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 349 | if (deferred_old && !deferred_new) |
| 350 | print_deferred_old_lines(); |
| 351 | else if (!deferred_old && deferred_new) |
| 352 | print_deferred_new_lines(); |
| 353 | else |
| 354 | print_deferred_changed_lines(); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 355 | deferred_old = deferred_old_last = NULL; |
| 356 | deferred_new = deferred_new_last = NULL; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * print a single line returned from xdiff |
| 361 | */ |
| 362 | void cgit_ssdiff_line_cb(char *line, int len) |
| 363 | { |
| 364 | char c = line[len - 1]; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 365 | line[len - 1] = '\0'; |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 366 | if (line[0] == '@') { |
| 367 | current_old_line = line_from_hunk(line, '-'); |
| 368 | current_new_line = line_from_hunk(line, '+'); |
| 369 | } |
| 370 | |
| 371 | if (line[0] == ' ') { |
| 372 | if (deferred_old || deferred_new) |
| 373 | cgit_ssdiff_print_deferred_lines(); |
| 374 | print_ssdiff_line("ctx", current_old_line, line, |
Ragnar Ouchterlony | 735e15e | 2009-10-25 18:13:22 +0100 | [diff] [blame] | 375 | current_new_line, line, 0); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 376 | current_old_line += 1; |
| 377 | current_new_line += 1; |
| 378 | } else if (line[0] == '+') { |
| 379 | deferred_new_add(line, current_new_line); |
| 380 | current_new_line += 1; |
| 381 | } else if (line[0] == '-') { |
| 382 | deferred_old_add(line, current_old_line); |
| 383 | current_old_line += 1; |
| 384 | } else if (line[0] == '@') { |
| 385 | html("<tr><td colspan='4' class='hunk'>"); |
| 386 | html_txt(line); |
| 387 | html("</td></tr>"); |
| 388 | } else { |
| 389 | html("<tr><td colspan='4' class='ctx'>"); |
| 390 | html_txt(line); |
| 391 | html("</td></tr>"); |
| 392 | } |
| 393 | line[len - 1] = c; |
| 394 | } |
| 395 | |
John Keeping | e3d3fff | 2015-03-08 16:32:16 +0000 | [diff] [blame] | 396 | void cgit_ssdiff_header_begin(void) |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 397 | { |
Ragnar Ouchterlony | 4a198e4 | 2009-09-16 18:56:26 +0200 | [diff] [blame] | 398 | current_old_line = -1; |
| 399 | current_new_line = -1; |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 400 | html("<tr><td class='space' colspan='4'><div></div></td></tr>"); |
| 401 | html("<tr><td class='head' colspan='4'>"); |
| 402 | } |
| 403 | |
John Keeping | e3d3fff | 2015-03-08 16:32:16 +0000 | [diff] [blame] | 404 | void cgit_ssdiff_header_end(void) |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 405 | { |
| 406 | html("</td><tr>"); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 407 | } |
| 408 | |
John Keeping | e3d3fff | 2015-03-08 16:32:16 +0000 | [diff] [blame] | 409 | void cgit_ssdiff_footer(void) |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 410 | { |
| 411 | if (deferred_old || deferred_new) |
| 412 | cgit_ssdiff_print_deferred_lines(); |
Ragnar Ouchterlony | 207cc34 | 2009-09-15 19:44:37 +0200 | [diff] [blame] | 413 | html("<tr><td class='foot' colspan='4'></td></tr>"); |
Ragnar Ouchterlony | 40e174d | 2009-09-13 19:36:35 +0200 | [diff] [blame] | 414 | } |