-rw-r--r-- | cgit.c | 2 | ||||
-rw-r--r-- | cgit.css | 5 | ||||
-rw-r--r-- | cgit.h | 2 | ||||
-rw-r--r-- | ui-commit.c | 19 | ||||
-rw-r--r-- | ui-diff.c | 45 |
5 files changed, 63 insertions, 10 deletions
@@ -71,97 +71,97 @@ static void cgit_print_repo_page(struct cacheitem *item) cgit_print_pageheader(title, 0); cgit_print_error(fmt("Unable to scan repository: %s", strerror(errno))); cgit_print_docend(); return; } title = fmt("%s - %s", cgit_repo->name, cgit_repo->desc); show_search = 0; setenv("GIT_DIR", cgit_repo->path, 1); if (cgit_query_page) { if (cgit_repo->snapshots && !strcmp(cgit_query_page, "snapshot")) { cgit_print_snapshot(item, cgit_query_sha1, "zip", cgit_repo->url, cgit_query_name); return; } if (!strcmp(cgit_query_page, "blob")) { cgit_print_blob(item, cgit_query_sha1, cgit_query_path); return; } } if (cgit_query_page && !strcmp(cgit_query_page, "log")) show_search = 1; cgit_print_docstart(title, item); if (!cgit_query_page) { cgit_print_pageheader("summary", show_search); cgit_print_summary(); cgit_print_docend(); return; } cgit_print_pageheader(cgit_query_page, show_search); if (!strcmp(cgit_query_page, "log")) { cgit_print_log(cgit_query_head, cgit_query_ofs, cgit_max_commit_count, cgit_query_search); } else if (!strcmp(cgit_query_page, "tree")) { cgit_print_tree(cgit_query_sha1, cgit_query_path); } else if (!strcmp(cgit_query_page, "commit")) { cgit_print_commit(cgit_query_sha1); } else if (!strcmp(cgit_query_page, "view")) { cgit_print_view(cgit_query_sha1, cgit_query_path); } else if (!strcmp(cgit_query_page, "diff")) { - cgit_print_diff(cgit_query_sha1, cgit_query_sha2); + cgit_print_diff(cgit_query_sha1, cgit_query_sha2, cgit_query_path); } else { cgit_print_error("Invalid request"); } cgit_print_docend(); } static void cgit_fill_cache(struct cacheitem *item, int use_cache) { static char buf[PATH_MAX]; int stdout2; getcwd(buf, sizeof(buf)); item->st.st_mtime = time(NULL); if (use_cache) { stdout2 = chk_positive(dup(STDOUT_FILENO), "Preserving STDOUT"); chk_zero(close(STDOUT_FILENO), "Closing STDOUT"); chk_positive(dup2(item->fd, STDOUT_FILENO), "Dup2(cachefile)"); } if (cgit_query_repo) cgit_print_repo_page(item); else cgit_print_repolist(item); if (use_cache) { chk_zero(close(STDOUT_FILENO), "Close redirected STDOUT"); chk_positive(dup2(stdout2, STDOUT_FILENO), "Restoring original STDOUT"); chk_zero(close(stdout2), "Closing temporary STDOUT"); } chdir(buf); } static void cgit_check_cache(struct cacheitem *item) { int i = 0; top: if (++i > cgit_max_lock_attempts) { die("cgit_refresh_cache: unable to lock %s: %s", item->name, strerror(errno)); } if (!cache_exist(item)) { if (!cache_lock(item)) { sleep(1); @@ -245,78 +245,83 @@ table.diffstat tr:hover { table.diffstat th { font-weight: normal; text-align: left; text-decoration: underline; padding: 0.1em 1em 0.1em 0.1em; font-size: 100%; } table.diffstat td { padding: 0.2em 0.2em 0.1em 0.1em; font-size: 100%; border: none; border-top: solid 1px #aaa; border-bottom: solid 1px #aaa; } table.diffstat td span.modechange { padding-left: 1em; color: red; } table.diffstat td.add a { color: green; } table.diffstat td.del a { color: red; } table.diffstat td.upd a { color: blue; } table.diffstat td.graph { width: 75%; vertical-align: center; } table.diffstat td.graph img { border: none; height: 11pt; } div.diffstat-summary { color: #888; padding-top: 0.5em; } +table.diff th { + padding: 1em 0em 0.1em 0.1em; + text-align: left; +} + table.diff td { border: solid 1px black; font-family: monospace; white-space: pre; } table.diff td div.hunk { background: #ccc; } table.diff td div.add { color: green; } table.diff td div.del { color: red; } .sha1 { font-family: courier; font-size: 90%; } .left { text-align: left; } .right { text-align: right; } @@ -129,54 +129,54 @@ extern void cgit_diff_tree(const unsigned char *old_sha1, filepair_fn fn); extern void cgit_diff_commit(struct commit *commit, filepair_fn fn); extern char *fmt(const char *format,...); extern void html(const char *txt); extern void htmlf(const char *format,...); extern void html_txt(char *txt); extern void html_ntxt(int len, char *txt); extern void html_attr(char *txt); extern void html_hidden(char *name, char *value); extern void html_link_open(char *url, char *title, char *class); extern void html_link_close(void); extern void html_filemode(unsigned short mode); extern int cgit_read_config(const char *filename, configfn fn); extern int cgit_parse_query(char *txt, configfn fn); extern struct commitinfo *cgit_parse_commit(struct commit *commit); extern struct taginfo *cgit_parse_tag(struct tag *tag); extern char *cache_safe_filename(const char *unsafe); extern int cache_lock(struct cacheitem *item); extern int cache_unlock(struct cacheitem *item); extern int cache_cancel_lock(struct cacheitem *item); extern int cache_exist(struct cacheitem *item); extern int cache_expired(struct cacheitem *item); extern char *cgit_repourl(const char *reponame); extern char *cgit_pageurl(const char *reponame, const char *pagename, const char *query); extern void cgit_print_error(char *msg); extern void cgit_print_date(unsigned long secs); extern void cgit_print_docstart(char *title, struct cacheitem *item); extern void cgit_print_docend(); extern void cgit_print_pageheader(char *title, int show_search); extern void cgit_print_snapshot_start(const char *mimetype, const char *filename, struct cacheitem *item); extern void cgit_print_repolist(struct cacheitem *item); extern void cgit_print_summary(); extern void cgit_print_log(const char *tip, int ofs, int cnt, char *grep); extern void cgit_print_view(const char *hex, char *path); extern void cgit_print_blob(struct cacheitem *item, const char *hex, char *path); extern void cgit_print_tree(const char *hex, char *path); extern void cgit_print_commit(const char *hex); -extern void cgit_print_diff(const char *old_hex, const char *new_hex); +extern void cgit_print_diff(const char *old_hex, const char *new_hex, char *path); extern void cgit_print_snapshot(struct cacheitem *item, const char *hex, const char *format, const char *prefix, const char *filename); #endif /* CGIT_H */ diff --git a/ui-commit.c b/ui-commit.c index ce33cf9..b3d1c28 100644 --- a/ui-commit.c +++ b/ui-commit.c @@ -31,198 +31,209 @@ void print_fileinfo(struct fileinfo *info) char *class; double width; 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)) { html_filemode(info->old_mode); } else { html_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'>["); html_filemode(info->old_mode); html("]</span>"); } htmlf("</td><td class='%s'>", class); - query = fmt("id=%s&id2=%s", sha1_to_hex(info->old_sha1), - sha1_to_hex(info->new_sha1)); + query = fmt("id=%s&id2=%s&path=%s", sha1_to_hex(info->old_sha1), + sha1_to_hex(info->new_sha1), info->new_path); html_link_open(cgit_pageurl(cgit_query_repo, "diff", query), NULL, NULL); if (info->status == DIFF_STATUS_COPIED || info->status == DIFF_STATUS_RENAMED) { html_txt(info->new_path); htmlf("</a> (%s from ", info->status == DIFF_STATUS_COPIED ? "copied" : "renamed"); query2 = fmt("id=%s", sha1_to_hex(info->old_sha1)); html_link_open(cgit_pageurl(cgit_query_repo, "view", query2), NULL, NULL); html_txt(info->old_path); html("</a>)"); } else { html_txt(info->new_path); html("</a>"); } html("</td><td class='right'>"); htmlf("%d", info->added + info->removed); html("</td><td class='graph'>"); width = (info->added + info->removed) * 100.0 / max_changes; if (width < 0.1) width = 0.1; html_link_open(cgit_pageurl(cgit_query_repo, "diff", query), NULL, NULL); htmlf("<img src='/cgit/add.png' style='width: %.1f%%;'/>", info->added * width / (info->added + info->removed)); htmlf("<img src='/cgit/del.png' style='width: %.1f%%;'/>", info->removed * width / (info->added + info->removed)); html("</a></td></tr>\n"); } void cgit_count_diff_lines(char *line, int len) { if (line && (len > 0)) { if (line[0] == '+') lines_added++; else if (line[0] == '-') lines_removed++; } } void inspect_filepair(struct diff_filepair *pair) { files++; lines_added = 0; lines_removed = 0; cgit_diff_files(pair->one->sha1, pair->two->sha1, cgit_count_diff_lines); if (files >= slots) { if (slots == 0) slots = 4; else slots = slots * 2; items = xrealloc(items, slots * sizeof(struct fileinfo)); } items[files-1].status = pair->status; hashcpy(items[files-1].old_sha1, pair->one->sha1); hashcpy(items[files-1].new_sha1, pair->two->sha1); items[files-1].old_mode = pair->one->mode; items[files-1].new_mode = pair->two->mode; items[files-1].old_path = xstrdup(pair->one->path); items[files-1].new_path = xstrdup(pair->two->path); items[files-1].added = lines_added; items[files-1].removed = lines_removed; if (lines_added + lines_removed > max_changes) max_changes = lines_added + lines_removed; total_adds += lines_added; total_rems += lines_removed; } void cgit_print_commit(const char *hex) { - struct commit *commit; + struct commit *commit, *parent; struct commitinfo *info; struct commit_list *p; unsigned char sha1[20]; char *query; char *filename; int i; if (get_sha1(hex, sha1)) { cgit_print_error(fmt("Bad object id: %s", hex)); return; } commit = lookup_commit_reference(sha1); if (!commit) { cgit_print_error(fmt("Bad commit reference: %s", hex)); return; } info = cgit_parse_commit(commit); html("<table class='commit-info'>\n"); html("<tr><th>author</th><td>"); html_txt(info->author); html(" "); html_txt(info->author_email); html("</td><td class='right'>"); cgit_print_date(info->author_date); html("</td></tr>\n"); html("<tr><th>committer</th><td>"); html_txt(info->committer); html(" "); html_txt(info->committer_email); html("</td><td class='right'>"); cgit_print_date(info->committer_date); html("</td></tr>\n"); html("<tr><th>tree</th><td colspan='2' class='sha1'><a href='"); query = fmt("id=%s", sha1_to_hex(commit->tree->object.sha1)); html_attr(cgit_pageurl(cgit_query_repo, "tree", query)); htmlf("'>%s</a></td></tr>\n", sha1_to_hex(commit->tree->object.sha1)); for (p = commit->parents; p ; p = p->next) { + parent = lookup_commit_reference(p->item->object.sha1); + if (!parent) { + html("<tr><td colspan='3'>"); + cgit_print_error("Error reading parent commit"); + html("</td></tr>"); + continue; + } html("<tr><th>parent</th>" "<td colspan='2' class='sha1'>" "<a href='"); query = fmt("id=%s", sha1_to_hex(p->item->object.sha1)); html_attr(cgit_pageurl(cgit_query_repo, "commit", query)); - htmlf("'>%s</a></td></tr>\n", + htmlf("'>%s</a> (<a href='", sha1_to_hex(p->item->object.sha1)); + query = fmt("id=%s&id2=%s", sha1_to_hex(parent->tree->object.sha1), + sha1_to_hex(commit->tree->object.sha1)); + html_attr(cgit_pageurl(cgit_query_repo, "diff", query)); + html("'>diff</a>)</td></tr>"); } if (cgit_repo->snapshots) { htmlf("<tr><th>download</th><td colspan='2' class='sha1'><a href='"); filename = fmt("%s-%s.zip", cgit_query_repo, hex); html_attr(cgit_pageurl(cgit_query_repo, "snapshot", fmt("id=%s&name=%s", hex, filename))); htmlf("'>%s</a></td></tr>", filename); } html("</table>\n"); html("<div class='commit-subject'>"); html_txt(info->subject); html("</div>"); html("<div class='commit-msg'>"); html_txt(info->msg); html("</div>"); if (!(commit->parents && commit->parents->next && commit->parents->next->next)) { html("<table class='diffstat'>"); max_changes = 0; cgit_diff_commit(commit, inspect_filepair); for(i = 0; i<files; i++) print_fileinfo(&items[i]); html("</table>"); html("<div class='diffstat-summary'>"); htmlf("%d files changed, %d insertions, %d deletions\n", files, total_adds, total_rems); html("</div>"); } cgit_free_commitinfo(info); } @@ -1,45 +1,82 @@ /* 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" /* * print a single line returned from xdiff */ static void print_line(char *line, int len) { 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("<div class='%s'>", class); line[len-1] = '\0'; html_txt(line); html("</div>"); line[len-1] = c; } -void cgit_print_diff(const char *old_hex, const char *new_hex) +static void filepair_cb(struct diff_filepair *pair) +{ + html("<tr><th>"); + html_txt(pair->two->path); + html("</th></tr>"); + html("<tr><td>"); + if (cgit_diff_files(pair->one->sha1, pair->two->sha1, print_line)) + cgit_print_error("Error running diff"); + html("</tr></td>"); +} + +void cgit_print_diff(const char *old_hex, const char *new_hex, char *path) { unsigned char sha1[20], sha2[20]; + enum object_type type; + unsigned long size; get_sha1(old_hex, sha1); get_sha1(new_hex, sha2); - html("<table class='diff'><tr><td>"); - if (cgit_diff_files(sha1, sha2, print_line)) - cgit_print_error("Error running diff"); + type = sha1_object_info(sha1, &size); + if (type == OBJ_BAD) { + type = sha1_object_info(sha2, &size); + if (type == OBJ_BAD) { + cgit_print_error(fmt("Bad object names: %s, %s", old_hex, new_hex)); + return; + } + } + + html("<table class='diff'>"); + switch(type) { + case OBJ_BLOB: + if (path) + htmlf("<tr><th>%s</th></tr>", path); + html("<tr><td>"); + if (cgit_diff_files(sha1, sha2, print_line)) + cgit_print_error("Error running diff"); + html("</tr></td>"); + break; + case OBJ_TREE: + cgit_diff_tree(sha1, sha2, filepair_cb); + break; + default: + cgit_print_error(fmt("Unhandled object type: %s", + typename(type))); + break; + } html("</td></tr></table>"); } |