all repos — cgit @ 747b035dda97ae359ed00d84744acfa8cc009fb2

a hyperfast web frontend for git written in c

ui-blob.c (view raw)

  1/* ui-blob.c: show blob content
  2 *
  3 * Copyright (C) 2008 Lars Hjemli
  4 * Copyright (C) 2010-2013 Jason A. Donenfeld <Jason@zx2c4.com>
  5 *
  6 * Licensed under GNU General Public License v2
  7 *   (see COPYING for full license text)
  8 */
  9
 10#include "cgit.h"
 11#include "ui-blob.h"
 12#include "html.h"
 13#include "ui-shared.h"
 14
 15struct walk_tree_context {
 16	const char *match_path;
 17	unsigned char *matched_sha1;
 18	int found_path:1;
 19	int file_only:1;
 20};
 21
 22static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
 23	const char *pathname, unsigned mode, int stage, void *cbdata)
 24{
 25	struct walk_tree_context *walk_tree_ctx = cbdata;
 26
 27	if (walk_tree_ctx->file_only && !S_ISREG(mode))
 28		return READ_TREE_RECURSIVE;
 29	if (strncmp(base, walk_tree_ctx->match_path, baselen)
 30		|| strcmp(walk_tree_ctx->match_path + baselen, pathname))
 31		return READ_TREE_RECURSIVE;
 32	memmove(walk_tree_ctx->matched_sha1, sha1, 20);
 33	walk_tree_ctx->found_path = 1;
 34	return 0;
 35}
 36
 37int cgit_ref_path_exists(const char *path, const char *ref, int file_only)
 38{
 39	unsigned char sha1[20];
 40	unsigned long size;
 41	struct pathspec_item path_items = {
 42		.match = path,
 43		.len = strlen(path)
 44	};
 45	struct pathspec paths = {
 46		.nr = 1,
 47		.items = &path_items
 48	};
 49	struct walk_tree_context walk_tree_ctx = {
 50		.match_path = path,
 51		.matched_sha1 = sha1,
 52		.found_path = 0,
 53		.file_only = file_only
 54	};
 55
 56	if (get_sha1(ref, sha1))
 57		return 0;
 58	if (sha1_object_info(sha1, &size) != OBJ_COMMIT) 
 59		return 0;
 60	read_tree_recursive(lookup_commit_reference(sha1)->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
 61	return walk_tree_ctx.found_path;
 62}
 63
 64int cgit_print_file(char *path, const char *head, int file_only)
 65{
 66	unsigned char sha1[20];
 67	enum object_type type;
 68	char *buf;
 69	unsigned long size;
 70	struct commit *commit;
 71	struct pathspec_item path_items = {
 72		.match = path,
 73		.len = strlen(path)
 74	};
 75	struct pathspec paths = {
 76		.nr = 1,
 77		.items = &path_items
 78	};
 79	struct walk_tree_context walk_tree_ctx = {
 80		.match_path = path,
 81		.matched_sha1 = sha1,
 82		.found_path = 0,
 83		.file_only = file_only
 84	};
 85
 86	if (get_sha1(head, sha1))
 87		return -1;
 88	type = sha1_object_info(sha1, &size);
 89	if (type == OBJ_COMMIT && path) {
 90		commit = lookup_commit_reference(sha1);
 91		read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
 92		if (!walk_tree_ctx.found_path)
 93			return -1;
 94		type = sha1_object_info(sha1, &size);
 95	}
 96	if (type == OBJ_BAD)
 97		return -1;
 98	buf = read_sha1_file(sha1, &type, &size);
 99	if (!buf)
100		return -1;
101	buf[size] = '\0';
102	html_raw(buf, size);
103	return 0;
104}
105
106void cgit_print_blob(const char *hex, char *path, const char *head, int file_only)
107{
108	unsigned char sha1[20];
109	enum object_type type;
110	char *buf;
111	unsigned long size;
112	struct commit *commit;
113	struct pathspec_item path_items = {
114		.match = path,
115		.len = path ? strlen(path) : 0
116	};
117	struct pathspec paths = {
118		.nr = 1,
119		.items = &path_items
120	};
121	struct walk_tree_context walk_tree_ctx = {
122		.match_path = path,
123		.matched_sha1 = sha1,
124		.found_path = 0,
125		.file_only = file_only
126	};
127
128	if (hex) {
129		if (get_sha1_hex(hex, sha1)) {
130			cgit_print_error("Bad hex value: %s", hex);
131			return;
132		}
133	} else {
134		if (get_sha1(head, sha1)) {
135			cgit_print_error("Bad ref: %s", head);
136			return;
137		}
138	}
139
140	type = sha1_object_info(sha1, &size);
141
142	if ((!hex) && type == OBJ_COMMIT && path) {
143		commit = lookup_commit_reference(sha1);
144		read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
145		type = sha1_object_info(sha1,&size);
146	}
147
148	if (type == OBJ_BAD) {
149		cgit_print_error("Bad object name: %s", hex);
150		return;
151	}
152
153	buf = read_sha1_file(sha1, &type, &size);
154	if (!buf) {
155		cgit_print_error("Error reading object %s", hex);
156		return;
157	}
158
159	buf[size] = '\0';
160	ctx.page.mimetype = ctx.qry.mimetype;
161	if (!ctx.page.mimetype) {
162		if (buffer_is_binary(buf, size))
163			ctx.page.mimetype = "application/octet-stream";
164		else
165			ctx.page.mimetype = "text/plain";
166	}
167	ctx.page.filename = path;
168	cgit_print_http_headers(&ctx);
169	html_raw(buf, size);
170}