summaryrefslogtreecommitdiffstatshomepage
path: root/ui-commit.c
blob: 1c0e7e5f593361e19712070e0367886eb99e1830 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "cgit.h"

void cgit_print_date(unsigned long secs)
{
	char buf[32];
	struct tm *time;

	time = gmtime(&secs);
	strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", time);
	html_txt(buf);
	
}

void cgit_print_commit(const char *hex)
{
	struct commit *commit;
	struct commitinfo *info;
	struct commit_list *p;
	unsigned long size;
	char type[20];
	char *buf;

	unsigned char sha1[20];

	if (get_sha1(hex, sha1)) {
		cgit_print_error(fmt("Bad object id: %s", hex));
		return;
	}

	buf = read_sha1_file(sha1, type, &size);
	if (!buf) {
		cgit_print_error(fmt("Bad object reference: %s", hex));
		return;
	}

	commit = lookup_commit(sha1);
	if (!commit) {
		cgit_print_error(fmt("Bad commit reference: %s", hex));
		return;
	}

	commit->buffer = buf;
	if (parse_commit_buffer(commit, buf, size)) {
		cgit_print_error(fmt("Malformed commit buffer: %s", hex));
		return;
	}

	info = cgit_parse_commit(commit);

	html("<table class='commit-info'>\n");
	html("<tr><th>author</th><td colspan='2'>");
	html_txt(info->author);
	html("</td></tr>\n");
	html("<tr><th>committer</th><td>");
	html_txt(info->committer);
	html("</td><td class='right'>");
	cgit_print_date(commit->date);
	html("</td></tr>\n");
	html("<tr><th>tree</th><td colspan='2' class='sha1'><a href='");
	html_attr(cgit_pageurl(cgit_query_repo, "tree", fmt("id=%s", sha1_to_hex(commit->tree->object.sha1))));
	htmlf("'>%s</a></td></tr>\n", sha1_to_hex(commit->tree->object.sha1));
	
	for (p = commit->parents; p ; p = p->next) {
		html("<tr><th>parent</th><td colspan='2' class='sha1'><a href='");
		html_attr(cgit_pageurl(cgit_query_repo, "commit", fmt("id=%s", sha1_to_hex(p->item->object.sha1))));
		htmlf("'>%s</a></td></tr>\n", 
		      sha1_to_hex(p->item->object.sha1));
	}
	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>");
	free(info->author);
	free(info->committer);
	free(info->subject);
	free(info);
}
span>char sha1[20]; enum object_type type; char *buf; unsigned long size; struct commit *commit; struct pathspec_item path_items = { .match = path, .len = strlen(path) }; struct pathspec paths = { .nr = 1, .items = &path_items }; struct walk_tree_context walk_tree_ctx = { .match_path = path, .matched_sha1 = sha1, .found_path = 0, .file_only = file_only }; if (get_sha1(head, sha1)) return -1; type = sha1_object_info(sha1, &size); if (type == OBJ_COMMIT && path) { commit = lookup_commit_reference(sha1); read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); if (!walk_tree_ctx.found_path) return -1; type = sha1_object_info(sha1, &size); } if (type == OBJ_BAD) return -1; buf = read_sha1_file(sha1, &type, &size); if (!buf) return -1; buf[size] = '\0'; html_raw(buf, size); return 0; } void cgit_print_blob(const char *hex, char *path, const char *head, int file_only) { unsigned char sha1[20]; enum object_type type; char *buf; unsigned long size; struct commit *commit; struct pathspec_item path_items = { .match = path, .len = path ? strlen(path) : 0 }; struct pathspec paths = { .nr = 1, .items = &path_items }; struct walk_tree_context walk_tree_ctx = { .match_path = path, .matched_sha1 = sha1, .found_path = 0, .file_only = file_only }; if (hex) { if (get_sha1_hex(hex, sha1)) { cgit_print_error("Bad hex value: %s", hex); return; } } else { if (get_sha1(head, sha1)) { cgit_print_error("Bad ref: %s", head); return; } } type = sha1_object_info(sha1, &size); if ((!hex) && type == OBJ_COMMIT && path) { commit = lookup_commit_reference(sha1); read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); type = sha1_object_info(sha1,&size); } if (type == OBJ_BAD) { cgit_print_error("Bad object name: %s", hex); return; } buf = read_sha1_file(sha1, &type, &size); if (!buf) { cgit_print_error("Error reading object %s", hex); return; } buf[size] = '\0'; ctx.page.mimetype = ctx.qry.mimetype; if (!ctx.page.mimetype) { if (buffer_is_binary(buf, size)) ctx.page.mimetype = "application/octet-stream"; else ctx.page.mimetype = "text/plain"; } ctx.page.filename = path; cgit_print_http_headers(&ctx); html_raw(buf, size); } class="n">p) { p[0] = '\0'; if (p[1]) cgit_query_path = trim_end(p + 1, '/'); } cgit_cmd = cgit_get_cmd_index(cmd + 1); cgit_query_page = xstrdup(cmd + 1); return; } } char *substr(const char *head, const char *tail) { char *buf; buf = xmalloc(tail - head + 1); strncpy(buf, head, tail - head); buf[tail - head] = '\0'; return buf; } struct commitinfo *cgit_parse_commit(struct commit *commit) { struct commitinfo *ret; char *p = commit->buffer, *t = commit->buffer; ret = xmalloc(sizeof(*ret)); ret->commit = commit; ret->author = NULL; ret->author_email = NULL; ret->committer = NULL; ret->committer_email = NULL; ret->subject = NULL; ret->msg = NULL; if (p == NULL) return ret; if (strncmp(p, "tree ", 5)) die("Bad commit: %s", sha1_to_hex(commit->object.sha1)); else p += 46; // "tree " + hex[40] + "\n" while (!strncmp(p, "parent ", 7)) p += 48; // "parent " + hex[40] + "\n" if (!strncmp(p, "author ", 7)) { p += 7; t = strchr(p, '<') - 1; ret->author = substr(p, t); p = t; t = strchr(t, '>') + 1; ret->author_email = substr(p, t); ret->author_date = atol(++t); p = strchr(t, '\n') + 1; } if (!strncmp(p, "committer ", 9)) { p += 9; t = strchr(p, '<') - 1; ret->committer = substr(p, t); p = t; t = strchr(t, '>') + 1; ret->committer_email = substr(p, t); ret->committer_date = atol(++t); p = strchr(t, '\n') + 1; } while (*p && (*p != '\n')) p = strchr(p, '\n') + 1; // skip unknown header fields while (*p == '\n') p = strchr(p, '\n') + 1; t = strchr(p, '\n'); if (t) { if (*t == '\0') ret->subject = "** empty **"; else ret->subject = substr(p, t); p = t + 1; while (*p == '\n') p = strchr(p, '\n') + 1; ret->msg = xstrdup(p); } else ret->subject = substr(p, p+strlen(p)); return ret; } struct taginfo *cgit_parse_tag(struct tag *tag) { void *data; enum object_type type; unsigned long size; char *p, *t; struct taginfo *ret; data = read_sha1_file(tag->object.sha1, &type, &size); if (!data || type != OBJ_TAG) { free(data); return 0; } ret = xmalloc(sizeof(*ret)); ret->tagger = NULL; ret->tagger_email = NULL; ret->tagger_date = 0; ret->msg = NULL; p = data; while (p && *p) { if (*p == '\n') break; if (!strncmp(p, "tagger ", 7)) { p += 7; t = strchr(p, '<') - 1; ret->tagger = substr(p, t); p = t; t = strchr(t, '>') + 1; ret->tagger_email = substr(p, t); ret->tagger_date = atol(++t); } p = strchr(p, '\n') + 1; } while (p && *p && (*p != '\n')) p = strchr(p, '\n') + 1; // skip unknown tag fields while (p && (*p == '\n')) p = strchr(p, '\n') + 1; if (p && *p) ret->msg = xstrdup(p); free(data); return ret; }