all repos — cgit @ 68cb84839f8fbc20688b22202489f4c2a54d3f55

a hyperfast web frontend for git written in c

ui-diff.c (view raw)

  1/* ui-diff.c: show diff between two blobs
  2 *
  3 * Copyright (C) 2006 Lars Hjemli
  4 *
  5 * Licensed under GNU General Public License v2
  6 *   (see COPYING for full license text)
  7 */
  8
  9#include "cgit.h"
 10#include "html.h"
 11#include "ui-shared.h"
 12
 13unsigned char old_rev_sha1[20];
 14unsigned char new_rev_sha1[20];
 15
 16/*
 17 * print a single line returned from xdiff
 18 */
 19static void print_line(char *line, int len)
 20{
 21	char *class = "ctx";
 22	char c = line[len-1];
 23
 24	if (line[0] == '+')
 25		class = "add";
 26	else if (line[0] == '-')
 27		class = "del";
 28	else if (line[0] == '@')
 29		class = "hunk";
 30
 31	htmlf("<div class='%s'>", class);
 32	line[len-1] = '\0';
 33	html_txt(line);
 34	html("</div>");
 35	line[len-1] = c;
 36}
 37
 38static void header(unsigned char *sha1, char *path1, int mode1,
 39		   unsigned char *sha2, char *path2, int mode2)
 40{
 41	char *abbrev1, *abbrev2;
 42	int subproject;
 43
 44	subproject = (S_ISGITLINK(mode1) || S_ISGITLINK(mode2));
 45	html("<div class='head'>");
 46	html("diff --git a/");
 47	html_txt(path1);
 48	html(" b/");
 49	html_txt(path2);
 50
 51	if (is_null_sha1(sha1))
 52		path1 = "dev/null";
 53	if (is_null_sha1(sha2))
 54		path2 = "dev/null";
 55
 56	if (mode1 == 0)
 57		htmlf("<br/>new file mode %.6o", mode2);
 58
 59	if (mode2 == 0)
 60		htmlf("<br/>deleted file mode %.6o", mode1);
 61
 62	if (!subproject) {
 63		abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
 64		abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
 65		htmlf("<br/>index %s..%s", abbrev1, abbrev2);
 66		free(abbrev1);
 67		free(abbrev2);
 68		if (mode1 != 0 && mode2 != 0) {
 69			htmlf(" %.6o", mode1);
 70			if (mode2 != mode1)
 71				htmlf("..%.6o", mode2);
 72		}
 73		html("<br/>--- a/");
 74		if (mode1 != 0)
 75			cgit_tree_link(path1, NULL, NULL, ctx.qry.head,
 76				       sha1_to_hex(old_rev_sha1), path1);
 77		else
 78			html_txt(path1);
 79		html("<br/>+++ b/");
 80		if (mode2 != 0)
 81			cgit_tree_link(path2, NULL, NULL, ctx.qry.head,
 82				       sha1_to_hex(new_rev_sha1), path2);
 83		else
 84			html_txt(path2);
 85	}
 86	html("</div>");
 87}
 88
 89static void filepair_cb(struct diff_filepair *pair)
 90{
 91	header(pair->one->sha1, pair->one->path, pair->one->mode,
 92	       pair->two->sha1, pair->two->path, pair->two->mode);
 93	if (S_ISGITLINK(pair->one->mode) || S_ISGITLINK(pair->two->mode)) {
 94		if (S_ISGITLINK(pair->one->mode))
 95			print_line(fmt("-Subproject %s", sha1_to_hex(pair->one->sha1)), 52);
 96		if (S_ISGITLINK(pair->two->mode))
 97			print_line(fmt("+Subproject %s", sha1_to_hex(pair->two->sha1)), 52);
 98		return;
 99	}
100	if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line))
101		cgit_print_error("Error running diff");
102}
103
104void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefix)
105{
106	enum object_type type;
107	unsigned long size;
108	struct commit *commit, *commit2;
109
110	if (!new_rev)
111		new_rev = ctx.qry.head;
112	get_sha1(new_rev, new_rev_sha1);
113	type = sha1_object_info(new_rev_sha1, &size);
114	if (type == OBJ_BAD) {
115		cgit_print_error(fmt("Bad object name: %s", new_rev));
116		return;
117	}
118	if (type != OBJ_COMMIT) {
119		cgit_print_error(fmt("Unhandled object type: %s",
120				     typename(type)));
121		return;
122	}
123
124	commit = lookup_commit_reference(new_rev_sha1);
125	if (!commit || parse_commit(commit))
126		cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
127
128	if (old_rev)
129		get_sha1(old_rev, old_rev_sha1);
130	else if (commit->parents && commit->parents->item)
131		hashcpy(old_rev_sha1, commit->parents->item->object.sha1);
132	else
133		hashclr(old_rev_sha1);
134
135	if (!is_null_sha1(old_rev_sha1)) {
136		type = sha1_object_info(old_rev_sha1, &size);
137		if (type == OBJ_BAD) {
138			cgit_print_error(fmt("Bad object name: %s", sha1_to_hex(old_rev_sha1)));
139			return;
140		}
141		commit2 = lookup_commit_reference(old_rev_sha1);
142		if (!commit2 || parse_commit(commit2))
143			cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
144	}
145	html("<table summary='diff' class='diff'>");
146	html("<tr><td>");
147	cgit_diff_tree(old_rev_sha1, new_rev_sha1, filepair_cb, prefix);
148	html("</td></tr>");
149	html("</table>");
150}