summaryrefslogtreecommitdiffabout
Side-by-side diff
Diffstat (more/less context) (show whitespace changes)
-rw-r--r--cgit.css2
-rw-r--r--ui-diff.c12
-rw-r--r--ui-diff.h6
-rw-r--r--ui-ssdiff.c32
4 files changed, 42 insertions, 10 deletions
diff --git a/cgit.css b/cgit.css
index a2a685b..3ed1989 100644
--- a/cgit.css
+++ b/cgit.css
@@ -230,97 +230,97 @@ table#downloads th {
div#blob {
border: solid 1px black;
}
div.error {
color: red;
font-weight: bold;
margin: 1em 2em;
}
a.ls-blob, a.ls-dir, a.ls-mod {
font-family: monospace;
}
td.ls-size {
text-align: right;
font-family: monospace;
width: 10em;
}
td.ls-mode {
font-family: monospace;
width: 10em;
}
table.blob {
margin-top: 0.5em;
border-top: solid 1px black;
}
table.blob td.lines {
margin: 0; padding: 0 0 0 0.5em;
vertical-align: top;
color: black;
}
table.blob td.linenumbers {
margin: 0; padding: 0 0.5em 0 0.5em;
vertical-align: top;
text-align: right;
border-right: 1px solid gray;
}
table.blob pre {
padding: 0; margin: 0;
}
-table.blob a.no {
+table.blob a.no, table.ssdiff a.no {
color: gray;
text-align: right;
text-decoration: none;
}
table.blob a.no a:hover {
color: black;
}
table.bin-blob {
margin-top: 0.5em;
border: solid 1px black;
}
table.bin-blob th {
font-family: monospace;
white-space: pre;
border: solid 1px #777;
padding: 0.5em 1em;
}
table.bin-blob td {
font-family: monospace;
white-space: pre;
border-left: solid 1px #777;
padding: 0em 1em;
}
table.nowrap td {
white-space: nowrap;
}
table.commit-info {
border-collapse: collapse;
margin-top: 1.5em;
}
table.commit-info th {
text-align: left;
font-weight: normal;
padding: 0.1em 1em 0.1em 0.1em;
vertical-align: top;
}
table.commit-info td {
font-weight: normal;
padding: 0.1em 1em 0.1em 0.1em;
}
diff --git a/ui-diff.c b/ui-diff.c
index 7ff7e46..a53425d 100644
--- a/ui-diff.c
+++ b/ui-diff.c
@@ -1,84 +1,95 @@
/* ui-diff.c: show diff between two blobs
*
* Copyright (C) 2006 Lars Hjemli
*
* Licensed under GNU General Public License v2
* (see COPYING for full license text)
*/
#include "cgit.h"
#include "html.h"
#include "ui-shared.h"
#include "ui-ssdiff.h"
unsigned char old_rev_sha1[20];
unsigned char new_rev_sha1[20];
static int files, slots;
static int total_adds, total_rems, max_changes;
static int lines_added, lines_removed;
static struct fileinfo {
char status;
unsigned char old_sha1[20];
unsigned char new_sha1[20];
unsigned short old_mode;
unsigned short new_mode;
char *old_path;
char *new_path;
unsigned int added;
unsigned int removed;
unsigned long old_size;
unsigned long new_size;
int binary:1;
} *items;
static int use_ssdiff = 0;
+static struct diff_filepair *current_filepair;
+
+struct diff_filespec *cgit_get_current_old_file(void)
+{
+ return current_filepair->one;
+}
+
+struct diff_filespec *cgit_get_current_new_file(void)
+{
+ return current_filepair->two;
+}
static void print_fileinfo(struct fileinfo *info)
{
char *class;
switch (info->status) {
case DIFF_STATUS_ADDED:
class = "add";
break;
case DIFF_STATUS_COPIED:
class = "cpy";
break;
case DIFF_STATUS_DELETED:
class = "del";
break;
case DIFF_STATUS_MODIFIED:
class = "upd";
break;
case DIFF_STATUS_RENAMED:
class = "mov";
break;
case DIFF_STATUS_TYPE_CHANGED:
class = "typ";
break;
case DIFF_STATUS_UNKNOWN:
class = "unk";
break;
case DIFF_STATUS_UNMERGED:
class = "stg";
break;
default:
die("bug: unhandled diff status %c", info->status);
}
html("<tr>");
htmlf("<td class='mode'>");
if (is_null_sha1(info->new_sha1)) {
cgit_print_filemode(info->old_mode);
} else {
cgit_print_filemode(info->new_mode);
}
if (info->old_mode != info->new_mode &&
!is_null_sha1(info->old_sha1) &&
!is_null_sha1(info->new_sha1)) {
html("<span class='modechange'>[");
cgit_print_filemode(info->old_mode);
html("]</span>");
@@ -239,96 +250,97 @@ static void header(unsigned char *sha1, char *path1, int mode1,
if (!subproject) {
abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
htmlf("<br/>index %s..%s", abbrev1, abbrev2);
free(abbrev1);
free(abbrev2);
if (mode1 != 0 && mode2 != 0) {
htmlf(" %.6o", mode1);
if (mode2 != mode1)
htmlf("..%.6o", mode2);
}
html("<br/>--- a/");
if (mode1 != 0)
cgit_tree_link(path1, NULL, NULL, ctx.qry.head,
sha1_to_hex(old_rev_sha1), path1);
else
html_txt(path1);
html("<br/>+++ b/");
if (mode2 != 0)
cgit_tree_link(path2, NULL, NULL, ctx.qry.head,
sha1_to_hex(new_rev_sha1), path2);
else
html_txt(path2);
}
html("</div>");
}
static void print_ssdiff_link()
{
if (!strcmp(ctx.qry.page, "diff")) {
if (use_ssdiff)
cgit_diff_link("Unidiff", NULL, NULL, ctx.qry.head,
ctx.qry.sha1, ctx.qry.sha2, ctx.qry.path, 1);
else
cgit_diff_link("Side-by-side diff", NULL, NULL,
ctx.qry.head, ctx.qry.sha1,
ctx.qry.sha2, ctx.qry.path, 1);
}
}
static void filepair_cb(struct diff_filepair *pair)
{
unsigned long old_size = 0;
unsigned long new_size = 0;
int binary = 0;
linediff_fn print_line_fn = print_line;
+ current_filepair = pair;
if (use_ssdiff) {
cgit_ssdiff_header_begin();
print_line_fn = cgit_ssdiff_line_cb;
}
header(pair->one->sha1, pair->one->path, pair->one->mode,
pair->two->sha1, pair->two->path, pair->two->mode);
if (use_ssdiff)
cgit_ssdiff_header_end();
if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
if (S_ISGITLINK(pair->one->mode))
print_line_fn(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
if (S_ISGITLINK(pair->two->mode))
print_line_fn(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
if (use_ssdiff)
cgit_ssdiff_footer();
return;
}
if (cgit_diff_files(pair->one->sha1, pair->two->sha1, &old_size,
&new_size, &binary, ctx.qry.context,
ctx.qry.ignorews, print_line_fn))
cgit_print_error("Error running diff");
if (binary) {
if (use_ssdiff)
html("<tr><td colspan='4'>Binary files differ</td></tr>");
else
html("Binary files differ");
}
if (use_ssdiff)
cgit_ssdiff_footer();
}
void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix)
{
enum object_type type;
unsigned long size;
struct commit *commit, *commit2;
if (!new_rev)
new_rev = ctx.qry.head;
get_sha1(new_rev, new_rev_sha1);
type = sha1_object_info(new_rev_sha1, &size);
if (type == OBJ_BAD) {
cgit_print_error(fmt("Bad object name: %s", new_rev));
return;
}
commit = lookup_commit_reference(new_rev_sha1);
if (!commit || parse_commit(commit))
cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
diff --git a/ui-diff.h b/ui-diff.h
index 70b2926..12d0c62 100644
--- a/ui-diff.h
+++ b/ui-diff.h
@@ -1,10 +1,16 @@
#ifndef UI_DIFF_H
#define UI_DIFF_H
extern void cgit_print_diffstat(const unsigned char *old_sha1,
const unsigned char *new_sha1);
extern void cgit_print_diff(const char *new_hex, const char *old_hex,
const char *prefix);
+extern struct diff_filespec *cgit_get_current_old_file(void);
+extern struct diff_filespec *cgit_get_current_new_file(void);
+
+extern unsigned char old_rev_sha1[20];
+extern unsigned char new_rev_sha1[20];
+
#endif /* UI_DIFF_H */
diff --git a/ui-ssdiff.c b/ui-ssdiff.c
index 408e620..2481585 100644
--- a/ui-ssdiff.c
+++ b/ui-ssdiff.c
@@ -1,51 +1,52 @@
#include "cgit.h"
#include "html.h"
#include "ui-shared.h"
+#include "ui-diff.h"
extern int use_ssdiff;
static int current_old_line, current_new_line;
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;
static char *longest_common_subsequence(char *A, char *B)
{
int i, j, ri;
int m = strlen(A);
int n = strlen(B);
int L[m + 1][n + 1];
int tmp1, tmp2;
int lcs_length;
char *result;
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];
@@ -146,122 +147,135 @@ static void deferred_old_add(char *line, int line_no)
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;
}
}
static void print_part_with_lcs(char *class, char *line, char *lcs)
{
int line_len = strlen(line);
int i, j;
char c[2] = " ";
int same = 1;
j = 0;
for (i = 0; i < line_len; i++) {
c[0] = line[i];
if (same) {
if (line[i] == lcs[j])
j += 1;
else {
same = 0;
htmlf("<span class='%s'>", class);
}
} else if (line[i] == lcs[j]) {
same = 1;
htmlf("</span>");
j += 1;
}
html_txt(c);
}
}
static void print_ssdiff_line(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("<tr>");
- if (old_line_no > 0)
- htmlf("<td class='lineno'>%d</td><td class='%s'>",
- old_line_no, class);
- else if (old_line)
+ html("<tr>\n");
+ if (old_line_no > 0) {
+ struct diff_filespec *old_file = cgit_get_current_old_file();
+ char *lineno_str = fmt("n%d", old_line_no);
+ char *id_str = fmt("%s#%s", is_null_sha1(old_file->sha1)?"HEAD":sha1_to_hex(old_rev_sha1), lineno_str);
+ html("<td class='lineno'><a class='no' href='");
+ html(cgit_fileurl(ctx.repo->url, "tree", old_file->path, id_str));
+ htmlf("' id='%s' name='%s'>%s</a>", lineno_str, lineno_str, lineno_str + 1);
+ html("</td>");
+ htmlf("<td class='%s'>", class);
+ } else if (old_line)
htmlf("<td class='lineno'></td><td class='%s'>", class);
else
htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
if (old_line) {
if (lcs)
print_part_with_lcs("del", old_line, lcs);
else
html_txt(old_line);
}
+ html("</td>\n");
+ if (new_line_no > 0) {
+ struct diff_filespec *new_file = cgit_get_current_new_file();
+ char *lineno_str = fmt("n%d", new_line_no);
+ char *id_str = fmt("%s#%s", is_null_sha1(new_file->sha1)?"HEAD":sha1_to_hex(new_rev_sha1), lineno_str);
+ html("<td class='lineno'><a class='no' href='");
+ html(cgit_fileurl(ctx.repo->url, "tree", new_file->path, id_str));
+ htmlf("' id='%s' name='%s'>%s</a>", lineno_str, lineno_str, lineno_str + 1);
html("</td>");
- if (new_line_no > 0)
- htmlf("<td class='lineno'>%d</td><td class='%s'>",
- new_line_no, class);
- else if (new_line)
+ htmlf("<td class='%s'>", class);
+ } else if (new_line)
htmlf("<td class='lineno'></td><td class='%s'>", class);
else
htmlf("<td class='lineno'></td><td class='%s_dark'>", class);
if (new_line) {
if (lcs)
print_part_with_lcs("add", new_line, lcs);
else
html_txt(new_line);
}
html("</td></tr>");
if (lcs)
free(lcs);
if (new_line)
free(new_line);
if (old_line)
free(old_line);
}
static void print_deferred_old_lines()
{
struct deferred_lines *iter_old, *tmp;
iter_old = deferred_old;
while (iter_old) {
print_ssdiff_line("del", iter_old->line_no,
iter_old->line, -1, NULL, 0);
tmp = iter_old->next;
free(iter_old);
iter_old = tmp;
}
}
static void print_deferred_new_lines()
{
struct deferred_lines *iter_new, *tmp;
iter_new = deferred_new;
while (iter_new) {
print_ssdiff_line("add", -1, NULL,
iter_new->line_no, iter_new->line, 0);
tmp = iter_new->next;
free(iter_new);
iter_new = tmp;
}
}
static void print_deferred_changed_lines()
{
struct deferred_lines *iter_old, *iter_new, *tmp;