/*
* The diff page, which shows what one revision changed against another as a
* summary table of the files it touched followed by the body of each change,
* either unified or side by side. Both halves come out of a single walk over
* the tree, because the summary has to be printed first while the bodies are
* only cheap to produce while each file is already open. A commit or a file
* whose diff would be too large stops at a link to a narrower view, and the
* raw form of the same request is handed to git and served as a plain patch.
*/
#define USE_THE_REPOSITORY_VARIABLE
#include "cgit.h"
#include "html.h"
#include "shared.h"
#include "ui-diff.h"
#include "ui-shared.h"
#include "ui-ssdiff.h"
// A file's body is collected while the walk still has that file open,
// since the stat table above it has to be printed first. This bounds what
// one request may hold that way. Past it the collected bodies are dropped
// and a second walk renders the page instead.
#define BODY_BUDGET (8 * 1024 * 1024)
// What a context of zero means once the diff runs, mirroring the fallback in
// cgit_diff_files, so the control offers the value the diff will really use.
#define DEFAULT_CONTEXT_LINES 3
struct fileinfo {
char status;
struct object_id old_oid[1];
struct object_id new_oid[1];
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;
unsigned int binary:1;
struct strbuf body;
};
struct object_id old_rev_oid[1];
struct object_id new_rev_oid[1];
static struct fileinfo *items;
static int files, slots;
static int total_adds, total_rems, max_changes;
static int lines_added, lines_removed;
static size_t body_bytes;
static int bodies_usable;
static linediff_fn render_line_fn;
static int render_suppressed;
static struct diff_filepair *current_filepair;
static const char *current_prefix;
static int use_ssdiff;
// Caps apply only to a whole commit view, since a single file diff is where
// the capped views send the reader.
static int cap_diffs;
static int item_idx;
static void release_bodies(void)
{
int i;
for (i = 0; i < files; i++)
strbuf_release(&items[i].body);
}
/*
* One bar segment of the per-file diffstat graph. The bar is a fixed-layout
* table whose row always spans 1000 columns, so a segment takes its width
* through colspan rather than an inline style a CSP would have to allow.
*/
static void print_graph_cell(const char *class, int span)
{
if (span > 0)
htmlf("
| ", class, span);
}
static void print_fileinfo(struct fileinfo *info)
{
int scale = max_changes ? max_changes : 1;
int add_span, rem_span;
const 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("");
html("| ");
if (is_null_oid(info->new_oid)) {
cgit_print_filemode(info->old_mode);
} else {
cgit_print_filemode(info->new_mode);
}
if (info->old_mode != info->new_mode &&
!is_null_oid(info->old_oid) &&
!is_null_oid(info->new_oid)) {
html("[");
cgit_print_filemode(info->old_mode);
html("]");
}
htmlf(" | ", class);
cgit_diff_link(info->new_path, NULL, NULL, ctx.qry.head, ctx.qry.oid,
ctx.qry.oid2, info->new_path);
if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED) {
htmlf(" (%s from ",
info->status == DIFF_STATUS_COPIED ? "copied" : "renamed");
html_txt(info->old_path);
html(")");
}
html(" | ");
if (info->binary) {
htmlf("bin | %lu -> %lu bytes |
\n",
info->old_size, info->new_size);
return;
}
htmlf("%d", info->added + info->removed);
html("");
html("");
add_span = (int)(info->added * 1000.0 / scale + 0.5);
rem_span = (int)(info->removed * 1000.0 / scale + 0.5);
print_graph_cell("add", add_span);
print_graph_cell("rem", rem_span);
print_graph_cell("none", 1000 - add_span - rem_span);
html("
| \n");
}
/*
* Counting is only half of what this does. It also renders each line until
* max-diff-lines is passed, so the two cannot be split into separate passes.
*/
static void count_diff_lines(char *line, int len)
{
if (line && (len > 0)) {
if (line[0] == '+')
lines_added++;
else if (line[0] == '-')
lines_removed++;
}
if (!render_line_fn || render_suppressed)
return;
if (cap_diffs && ctx.cfg.max_diff_lines > 0 &&
lines_added + lines_removed > ctx.cfg.max_diff_lines) {
render_suppressed = 1;
return;
}
render_line_fn(line, len);
}
static int show_filepair(struct diff_filepair *pair)
{
if (!current_prefix)
return 1;
return starts_with(pair->one->path, current_prefix) ||
starts_with(pair->two->path, current_prefix);
}
/*
* The line xdiff produces ends in its newline, which is swapped for a
* terminator while the text is escaped and then put back, because the line
* points into a buffer xdiff still owns.
*/
static void print_line(char *line, int len)
{
const char *class = "ctx";
char c = line[len - 1];
if (line[0] == '+')
class = "add";
else if (line[0] == '-')
class = "del";
else if (line[0] == '@')
class = "hunk";
htmlf("", class);
line[len - 1] = '\0';
html_txt(line);
html("
");
line[len - 1] = c;
}
/*
* The buffer repo_find_unique_abbrev returns is reused, so a caller printing
* two abbreviated names together keeps a copy of each.
*/
static char *abbrev_oid(const struct object_id *oid)
{
return xstrdup(repo_find_unique_abbrev(the_repository, oid,
DEFAULT_ABBREV));
}
static void print_file_header(const struct object_id *old_oid, char *old_path,
int old_mode, const struct object_id *new_oid,
char *new_path, int new_mode)
{
char *old_abbrev, *new_abbrev;
int subproject;
subproject = (S_ISGITLINK(old_mode) || S_ISGITLINK(new_mode));
html("");
html("diff --git a/");
html_txt(old_path);
html(" b/");
html_txt(new_path);
if (old_mode == 0)
htmlf("
new file mode %.6o", new_mode);
if (new_mode == 0)
htmlf("
deleted file mode %.6o", old_mode);
if (!subproject) {
old_abbrev = abbrev_oid(old_oid);
new_abbrev = abbrev_oid(new_oid);
htmlf("
index %s..%s", old_abbrev, new_abbrev);
free(old_abbrev);
free(new_abbrev);
if (old_mode != 0 && new_mode != 0) {
htmlf(" %.6o", old_mode);
if (new_mode != old_mode)
htmlf("..%.6o", new_mode);
}
if (is_null_oid(old_oid)) {
old_path = "dev/null";
html("
--- /");
} else
html("
--- a/");
if (old_mode != 0)
cgit_tree_link(old_path, NULL, NULL, ctx.qry.head,
oid_to_hex(old_rev_oid), old_path);
else
html_txt(old_path);
if (is_null_oid(new_oid)) {
new_path = "dev/null";
html("
+++ /");
} else
html("
+++ b/");
if (new_mode != 0)
cgit_tree_link(new_path, NULL, NULL, ctx.qry.head,
oid_to_hex(new_rev_oid), new_path);
else
html_txt(new_path);
}
html("
");
}
/*
* The length passed counts the terminator, because a renderer strips the last
* byte of every line it is given.
*/
static void print_subproject_lines(struct diff_filepair *pair,
linediff_fn line_fn)
{
if (S_ISGITLINK(pair->one->mode)) {
char *text = cgit_fmt("-Subproject %s",
oid_to_hex(&pair->one->oid));
line_fn(text, strlen(text) + 1);
}
if (S_ISGITLINK(pair->two->mode)) {
char *text = cgit_fmt("+Subproject %s",
oid_to_hex(&pair->two->oid));
line_fn(text, strlen(text) + 1);
}
}
static void print_binary_differs(void)
{
if (use_ssdiff)
html("| Binary files differ |
");
else
html("Binary files differ");
}
static void print_truncated(const char *path)
{
if (use_ssdiff)
html("| ");
else
html(" ");
html("This diff is too large to be rendered inline. ");
cgit_diff_link("View it on its own page", NULL, NULL, ctx.qry.head,
ctx.qry.oid, ctx.qry.oid2, path);
html(".");
if (use_ssdiff) {
html(" |
");
cgit_ssdiff_footer();
} else
html("");
}
static struct fileinfo *reserve_item(void)
{
files++;
if (files >= slots) {
if (slots == 0)
slots = 4;
else
slots = slots * 2;
items = xrealloc(items, slots * sizeof(struct fileinfo));
}
memset(&items[files - 1], 0, sizeof(items[files - 1]));
strbuf_init(&items[files - 1].body, 0);
return &items[files - 1];
}
/*
* Renders one file's body into its own buffer, the way filepair_cb would have
* written it straight out on a second walk.
*/
static void collect_body(struct diff_filepair *pair, struct fileinfo *item,
int *binary, unsigned long *old_size,
unsigned long *new_size)
{
struct strbuf *body = &item->body;
linediff_fn line_fn = use_ssdiff ? cgit_ssdiff_line_cb : print_line;
size_t header_len;
current_filepair = pair;
html_capture_begin(body);
if (use_ssdiff)
cgit_ssdiff_header_begin();
print_file_header(&pair->one->oid, pair->one->path, pair->one->mode,
&pair->two->oid, pair->two->path, pair->two->mode);
if (use_ssdiff)
cgit_ssdiff_header_end();
header_len = body->len;
// Cleared for the whole file rather than in the branch below, because
// the submodule branch never runs count_diff_lines with a render
// function. A submodule following a file that tripped the cap would
// otherwise inherit the flag and report itself as too large to render.
render_suppressed = 0;
if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
// The stat counts what a diff of the pair produces rather
// than the two lines the body shows, so run that diff for
// the count alone.
render_line_fn = NULL;
cgit_diff_files(&pair->one->oid, &pair->two->oid, old_size,
new_size, binary, 0, ctx.qry.ignorews,
count_diff_lines);
print_subproject_lines(pair, line_fn);
} else {
render_line_fn = line_fn;
if (cgit_diff_files(&pair->one->oid, &pair->two->oid, old_size,
new_size, binary, ctx.qry.context,
ctx.qry.ignorews, count_diff_lines))
cgit_print_error("Error running diff");
render_line_fn = NULL;
if (*binary)
print_binary_differs();
}
if (use_ssdiff)
cgit_ssdiff_footer();
if (render_suppressed) {
// Setting the length back would keep the grown allocation,
// which the budget below cannot see because it only counts
// what is kept, so rebuild the buffer at the kept size.
char *header_text = xmemdupz(body->buf, header_len);
strbuf_release(body);
strbuf_attach(body, header_text, header_len, header_len + 1);
print_truncated(pair->two->path);
}
html_capture_end();
body_bytes += body->len;
if (body_bytes > BODY_BUDGET) {
bodies_usable = 0;
release_bodies();
}
}
static void inspect_filepair(struct diff_filepair *pair)
{
struct fileinfo *item;
int binary = 0;
unsigned long old_size = 0;
unsigned long new_size = 0;
if (!show_filepair(pair))
return;
item = reserve_item();
lines_added = 0;
lines_removed = 0;
if (bodies_usable)
collect_body(pair, item, &binary, &old_size, &new_size);
else
cgit_diff_files(&pair->one->oid, &pair->two->oid, &old_size,
&new_size, &binary, 0, ctx.qry.ignorews,
count_diff_lines);
item->status = pair->status;
oidcpy(item->old_oid, &pair->one->oid);
oidcpy(item->new_oid, &pair->two->oid);
item->old_mode = pair->one->mode;
item->new_mode = pair->two->mode;
item->old_path = xstrdup(pair->one->path);
item->new_path = xstrdup(pair->two->path);
item->added = lines_added;
item->removed = lines_removed;
item->old_size = old_size;
item->new_size = new_size;
item->binary = binary;
if (lines_added + lines_removed > max_changes)
max_changes = lines_added + lines_removed;
total_adds += lines_added;
total_rems += lines_removed;
}
static void print_diffstat(const struct object_id *old_oid,
const struct object_id *new_oid, const char *prefix)
{
int i;
html("\n");
html("\n");
max_changes = 0;
cgit_diff_tree(old_oid, new_oid, inspect_filepair, prefix,
ctx.qry.ignorews);
for (i = 0; i < files; i++)
print_fileinfo(&items[i]);
html("
\n");
html("");
htmlf("%d file%s changed, %d insertion%s, %d deletion%s",
files, files == 1 ? "" : "s",
total_adds, total_adds == 1 ? "" : "s",
total_rems, total_rems == 1 ? "" : "s");
html("
\n");
}
static int over_line_cap(int idx)
{
return cap_diffs && ctx.cfg.max_diff_lines > 0 &&
idx < files && !items[idx].binary &&
items[idx].added + items[idx].removed >
(unsigned int)ctx.cfg.max_diff_lines;
}
static void filepair_cb(struct diff_filepair *pair)
{
unsigned long old_size = 0;
unsigned long new_size = 0;
int binary = 0;
int idx;
linediff_fn line_fn = print_line;
if (!show_filepair(pair))
return;
idx = item_idx++;
current_filepair = pair;
if (use_ssdiff) {
cgit_ssdiff_header_begin();
line_fn = cgit_ssdiff_line_cb;
}
print_file_header(&pair->one->oid, pair->one->path, pair->one->mode,
&pair->two->oid, pair->two->path, pair->two->mode);
if (use_ssdiff)
cgit_ssdiff_header_end();
if (over_line_cap(idx)) {
print_truncated(items[idx].new_path);
return;
}
if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
print_subproject_lines(pair, line_fn);
if (use_ssdiff)
cgit_ssdiff_footer();
return;
}
if (cgit_diff_files(&pair->one->oid, &pair->two->oid, &old_size,
&new_size, &binary, ctx.qry.context,
ctx.qry.ignorews, line_fn))
cgit_print_error("Error running diff");
if (binary)
print_binary_differs();
if (use_ssdiff)
cgit_ssdiff_footer();
}
static void print_raw_patch(const struct object_id *old_tree_oid,
const struct object_id *new_tree_oid)
{
struct diff_options diffopt;
repo_diff_setup(the_repository, &diffopt);
diffopt.output_format = DIFF_FORMAT_PATCH;
diffopt.flags.recursive = 1;
diff_setup_done(&diffopt);
ctx.page.mimetype = "text/plain";
cgit_print_http_headers();
if (old_tree_oid)
diff_tree_oid(old_tree_oid, new_tree_oid, "", &diffopt);
else
diff_root_tree_oid(new_tree_oid, "", &diffopt);
diffcore_std(&diffopt);
diff_flush(&diffopt);
}
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;
}
void cgit_print_diff_ctrls(void)
{
int i, selected;
html("\n");
html("
diff options");
html("
\n");
html("
\n");
}
void cgit_print_diff(const char *new_rev, const char *old_rev,
const char *prefix, int show_ctrls, int raw)
{
struct commit *new_commit, *old_commit;
const struct object_id *old_tree_oid, *new_tree_oid;
diff_type difftype;
// Decided from the caller's prefix before the follow logic below
// rewrites it to "", otherwise follow=1 silently disables the caps.
cap_diffs = !prefix;
// Detecting renames needs the diff machinery to examine the whole
// commit, so with follow set the prefix is applied in show_filepair
// instead of being passed down.
if (ctx.qry.follow && ctx.repo->enable_follow_links) {
current_prefix = prefix;
prefix = "";
} else {
current_prefix = NULL;
}
if (!new_rev)
new_rev = ctx.qry.head;
if (repo_get_oid(the_repository, new_rev, new_rev_oid)) {
cgit_print_error_page(404, "Not Found",
"Bad object name: %s", new_rev);
return;
}
new_commit = lookup_commit_reference(the_repository, new_rev_oid);
if (!new_commit || repo_parse_commit(the_repository, new_commit)) {
cgit_print_error_page(404, "Not Found",
"Bad commit: %s", oid_to_hex(new_rev_oid));
return;
}
new_tree_oid = get_commit_tree_oid(new_commit);
if (old_rev) {
if (repo_get_oid(the_repository, old_rev, old_rev_oid)) {
cgit_print_error_page(404, "Not Found",
"Bad object name: %s", old_rev);
return;
}
} else if (new_commit->parents && new_commit->parents->item) {
oidcpy(old_rev_oid, &new_commit->parents->item->object.oid);
} else {
oidclr(old_rev_oid, the_repository->hash_algo);
}
if (!is_null_oid(old_rev_oid)) {
old_commit = lookup_commit_reference(the_repository,
old_rev_oid);
if (!old_commit ||
repo_parse_commit(the_repository, old_commit)) {
cgit_print_error_page(404, "Not Found",
"Bad commit: %s", oid_to_hex(old_rev_oid));
return;
}
old_tree_oid = get_commit_tree_oid(old_commit);
} else {
old_tree_oid = NULL;
}
if (raw) {
print_raw_patch(old_tree_oid, new_tree_oid);
return;
}
difftype = ctx.qry.has_difftype ? ctx.qry.difftype : ctx.cfg.difftype;
use_ssdiff = difftype == DIFF_SSDIFF;
// Without max-diff-lines to stop it a single body grows with the file
// it came from, which for a side by side diff is several times the blob
// itself, and the budget cannot help there because it is only reached
// once a body is complete.
bodies_usable = difftype != DIFF_STATONLY && cap_diffs &&
ctx.cfg.max_diff_lines > 0;
if (show_ctrls) {
cgit_print_layout_start();
cgit_print_diff_ctrls();
}
// Every link from here on leads to a single file, and a stat limited
// to one file is useless, so the difftype those links carry is reset
// rather than propagating DIFF_STATONLY to them.
if (difftype == DIFF_STATONLY)
ctx.qry.difftype = ctx.cfg.difftype;
print_diffstat(old_rev_oid, new_rev_oid, prefix);
if (difftype == DIFF_STATONLY) {
if (show_ctrls)
cgit_print_layout_end();
return;
}
if (cap_diffs && ctx.cfg.max_diff_files > 0 &&
files > ctx.cfg.max_diff_files) {
html("");
html("This diff is too large to be rendered inline. "
"Follow a file above, or the ");
cgit_patch_link("patch", NULL, NULL, NULL, ctx.qry.oid, NULL);
html(" link for the whole commit.");
html("
");
if (show_ctrls)
cgit_print_layout_end();
return;
}
html("