#include "cgit.h" #include "ui-ssdiff.h" #include "html.h" #include "ui-shared.h" #include "ui-diff.h" extern int use_ssdiff; static int current_old_line, current_new_line; static int **L = NULL; struct deferred_lines { int line_no; char *line; struct deferred_lines *next; }; static struct deferred_lines *deferred_old, *deferred_old_last; static struct deferred_lines *deferred_new, *deferred_new_last; /* * The table does not need clearing between calls. The fill below assigns * every cell in [0,m] x [0,n] before anything reads it: a read only happens * where both lines still have a character, so it never reaches past row m or * column n, and the loops run downwards so the neighbour is always already * written. Clearing the whole table on every changed line pair cost more than * the comparison it was preparing for. */ static void create_lcs_table(void) { int i; if (L != NULL) return; // xcalloc will die if we ran out of memory; // not very helpful for debugging L = (int**)xcalloc(MAX_SSDIFF_M, sizeof(int *)); *L = (int*)xcalloc(MAX_SSDIFF_SIZE, sizeof(int)); for (i = 1; i < MAX_SSDIFF_M; i++) { L[i] = *L + i * MAX_SSDIFF_N; } } static char *longest_common_subsequence(char *A, char *B) { int i, j, ri; int m = strlen(A); int n = strlen(B); int tmp1, tmp2; int lcs_length; char *result; // We bail if the lines are too long if (m >= MAX_SSDIFF_M || n >= MAX_SSDIFF_N) return NULL; create_lcs_table(); for (i = m; i >= 0; i--) { for (j = n; j >= 0; j--) { if (A[i] == '\0' || B[j] == '\0') { L[i][j] = 0; } else if (A[i] == B[j]) { L[i][j] = 1 + L[i + 1][j + 1]; } else { tmp1 = L[i + 1][j]; tmp2 = L[i][j + 1]; L[i][j] = (tmp1 > tmp2 ? tmp1 : tmp2); } } } lcs_length = L[0][0]; result = xmalloc(lcs_length + 2); memset(result, 0, sizeof(*result) * (lcs_length + 2)); ri = 0; i = 0; j = 0; while (i < m && j < n) { if (A[i] == B[j]) { result[ri] = A[i]; ri += 1; i += 1; j += 1; } else if (L[i + 1][j] >= L[i][j + 1]) { i += 1; } else { j += 1; } } return result; } static int line_from_hunk(char *line, char type) { char *p; long res; p = strchr(line, type); if (p == NULL) return 0; p += 1; // git omits the length when a hunk covers a single line, as in // "@@ -1 +1 @@", so the number runs to whatever follows it rather than // to a comma that may belong to the other side of the header or be // missing altogether. res = strtol(p, NULL, 10); if (res < 0 || res > INT_MAX) return 0; return (int)res; } static char *replace_tabs(char *line) { struct strbuf out = STRBUF_INIT; const char *p; // Each tab runs to the next eight-column stop. Walking the line once // and appending replaces a loop that measured the result and rescanned // the rest of the input at every tab, which made a tab-heavy line // quadratic in its own length. for (p = line; *p; p++) { if (*p == '\t') strbuf_addchars(&out, ' ', 8 - (out.len % 8)); else strbuf_addch(&out, *p); } return strbuf_detach(&out, NULL); } static int calc_deferred_lines(struct deferred_lines *start) { struct deferred_lines *item = start; int result = 0; while (item) { result += 1; item = item->next; } return result; } static void deferred_old_add(char *line, int line_no) { struct deferred_lines *item = xmalloc(sizeof(struct deferred_lines)); item->line = xstrdup(line); item->line_no = line_no; item->next = NULL; if (deferred_old) { deferred_old_last->next = item; deferred_old_last = item; } else { deferred_old = deferred_old_last = item; } } static void deferred_new_add(char *line, int line_no) { struct deferred_lines *item = xmalloc(sizeof(struct deferred_lines)); item->line = xstrdup(line); item->line_no = line_no; item->next = NULL; if (deferred_new) { deferred_new_last->next = item; deferred_new_last = item; } else { deferred_new = deferred_new_last = item; } } /* The item owns the copy of the line taken when it was deferred, so both go * together. print_ssdiff_line only reads the line and frees what it derives * from it, and never keeps the pointer it was handed. */ static void free_deferred(struct deferred_lines *item) { free(item->line); free(item); } static void flush_run(struct strbuf *run) { if (!run->len) return; html_txt(run->buf); strbuf_reset(run); } static void print_part_with_lcs(const char *class, char *line, char *lcs) { int line_len = strlen(line); int i, j; int same = 1; struct strbuf run = STRBUF_INIT; // Collect each stretch that is wholly inside or wholly outside the // common subsequence and escape it in one go. Escaping a character at a // time meant a write syscall per character of every changed line, which // dominated this page. j = 0; for (i = 0; i < line_len; i++) { if (same) { if (line[i] == lcs[j]) j += 1; else { same = 0; flush_run(&run); htmlf("", class); } } else if (line[i] == lcs[j]) { same = 1; flush_run(&run); html(""); j += 1; } strbuf_addch(&run, line[i]); } flush_run(&run); if (!same) html(""); strbuf_release(&run); } static void print_ssdiff_line(const char *class, int old_line_no, char *old_line, int new_line_no, char *new_line, int individual_chars) { char *lcs = NULL; if (old_line) old_line = replace_tabs(old_line + 1); if (new_line) new_line = replace_tabs(new_line + 1); if (individual_chars && old_line && new_line) lcs = longest_common_subsequence(old_line, new_line); html("