summaryrefslogtreecommitdiffabout
path: root/ui-diff.c
Unidiff
Diffstat (limited to 'ui-diff.c') (more/less context) (ignore whitespace changes)
-rw-r--r--ui-diff.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/ui-diff.c b/ui-diff.c
index 999b6f3..afe1c90 100644
--- a/ui-diff.c
+++ b/ui-diff.c
@@ -1,105 +1,117 @@
1/* ui-diff.c: show diff between two blobs 1/* ui-diff.c: show diff between two blobs
2 * 2 *
3 * Copyright (C) 2006 Lars Hjemli 3 * Copyright (C) 2006 Lars Hjemli
4 * 4 *
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8 8
9#include "cgit.h" 9#include "cgit.h"
10 10
11 11
12/* 12/*
13 * print a single line returned from xdiff 13 * print a single line returned from xdiff
14 */ 14 */
15static void print_line(char *line, int len) 15static void print_line(char *line, int len)
16{ 16{
17 char *class = "ctx"; 17 char *class = "ctx";
18 char c = line[len-1]; 18 char c = line[len-1];
19 19
20 if (line[0] == '+') 20 if (line[0] == '+')
21 class = "add"; 21 class = "add";
22 else if (line[0] == '-') 22 else if (line[0] == '-')
23 class = "del"; 23 class = "del";
24 else if (line[0] == '@') 24 else if (line[0] == '@')
25 class = "hunk"; 25 class = "hunk";
26 26
27 htmlf("<div class='%s'>", class); 27 htmlf("<div class='%s'>", class);
28 line[len-1] = '\0'; 28 line[len-1] = '\0';
29 html_txt(line); 29 html_txt(line);
30 html("</div>"); 30 html("</div>");
31 line[len-1] = c; 31 line[len-1] = c;
32} 32}
33 33
34static void header(unsigned char *sha1, char *path1, 34static void header(unsigned char *sha1, char *path1,
35 unsigned char *sha2, char *path2) 35 unsigned char *sha2, char *path2)
36{ 36{
37 char *abbrev1, *abbrev2; 37 char *abbrev1, *abbrev2;
38 if (is_null_sha1(sha1)) 38 if (is_null_sha1(sha1))
39 path1 = "dev/null"; 39 path1 = "dev/null";
40 if (is_null_sha1(sha2)) 40 if (is_null_sha1(sha2))
41 path2 = "dev/null"; 41 path2 = "dev/null";
42 html("<tr><td>"); 42 html("<tr><td>");
43 html("<div class='head'>"); 43 html("<div class='head'>");
44 html("diff --git a/"); 44 html("diff --git a/");
45 html_txt(path1); 45 html_txt(path1);
46 html(" b/"); 46 html(" b/");
47 html_txt(path2); 47 html_txt(path2);
48 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV)); 48 abbrev1 = xstrdup(find_unique_abbrev(sha1, DEFAULT_ABBREV));
49 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV)); 49 abbrev2 = xstrdup(find_unique_abbrev(sha2, DEFAULT_ABBREV));
50 htmlf("\nindex %s..%s", abbrev1, abbrev2); 50 htmlf("\nindex %s..%s", abbrev1, abbrev2);
51 free(abbrev1); 51 free(abbrev1);
52 free(abbrev2); 52 free(abbrev2);
53 html("\n--- a/"); 53 html("\n--- a/");
54 html_txt(path1); 54 html_txt(path1);
55 html("\n+++ b/"); 55 html("\n+++ b/");
56 html_txt(path2); 56 html_txt(path2);
57 html("</div>"); 57 html("</div>");
58} 58}
59 59
60static void filepair_cb(struct diff_filepair *pair) 60static void filepair_cb(struct diff_filepair *pair)
61{ 61{
62 header(pair->one->sha1, pair->one->path, 62 header(pair->one->sha1, pair->one->path,
63 pair->two->sha1, pair->two->path); 63 pair->two->sha1, pair->two->path);
64 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) 64 if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line))
65 cgit_print_error("Error running diff"); 65 cgit_print_error("Error running diff");
66 html("</tr></td>"); 66 html("</tr></td>");
67} 67}
68 68
69void cgit_print_diff(const char *old_hex, const char *new_hex, char *path) 69void cgit_print_diff(const char *head, const char *old_hex, const char *new_hex, char *path)
70{ 70{
71 unsigned char sha1[20], sha2[20]; 71 unsigned char sha1[20], sha2[20];
72 enum object_type type; 72 enum object_type type;
73 unsigned long size; 73 unsigned long size;
74 struct commit *commit;
75
76 if (head && !old_hex && !new_hex) {
77 get_sha1(head, sha1);
78 commit = lookup_commit_reference(sha1);
79 if (commit && !parse_commit(commit)) {
80 html("<table class='diff'>");
81 cgit_diff_commit(commit, filepair_cb);
82 html("</td></tr></table>");
83 }
84 return;
85 }
74 86
75 get_sha1(old_hex, sha1); 87 get_sha1(old_hex, sha1);
76 get_sha1(new_hex, sha2); 88 get_sha1(new_hex, sha2);
77 89
78 type = sha1_object_info(sha1, &size); 90 type = sha1_object_info(sha1, &size);
79 if (type == OBJ_BAD) { 91 if (type == OBJ_BAD) {
80 type = sha1_object_info(sha2, &size); 92 type = sha1_object_info(sha2, &size);
81 if (type == OBJ_BAD) { 93 if (type == OBJ_BAD) {
82 cgit_print_error(fmt("Bad object names: %s, %s", old_hex, new_hex)); 94 cgit_print_error(fmt("Bad object names: %s, %s", old_hex, new_hex));
83 return; 95 return;
84 } 96 }
85 } 97 }
86 98
87 html("<table class='diff'>"); 99 html("<table class='diff'>");
88 switch(type) { 100 switch(type) {
89 case OBJ_BLOB: 101 case OBJ_BLOB:
90 html("<tr><td>"); 102 html("<tr><td>");
91 header(sha1, path, sha2, path); 103 header(sha1, path, sha2, path);
92 if (cgit_diff_files(sha1, sha2, print_line)) 104 if (cgit_diff_files(sha1, sha2, print_line))
93 cgit_print_error("Error running diff"); 105 cgit_print_error("Error running diff");
94 html("</tr></td>"); 106 html("</tr></td>");
95 break; 107 break;
96 case OBJ_TREE: 108 case OBJ_TREE:
97 cgit_diff_tree(sha1, sha2, filepair_cb); 109 cgit_diff_tree(sha1, sha2, filepair_cb);
98 break; 110 break;
99 default: 111 default:
100 cgit_print_error(fmt("Unhandled object type: %s", 112 cgit_print_error(fmt("Unhandled object type: %s",
101 typename(type))); 113 typename(type)));
102 break; 114 break;
103 } 115 }
104 html("</td></tr></table>"); 116 html("</td></tr></table>");
105} 117}