all repos — cgit @ 3cb5d86dc68bab4883bf5a7cbc90f3e266237355

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 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	char *match_path;
 17	unsigned char *matched_sha1;
 18	int found_path;
 19};
 20
 21static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
 22	const char *pathname, unsigned mode, int stage, void *cbdata)
 23{
 24	struct walk_tree_context *walk_tree_ctx = cbdata;
 25
 26	if (strncmp(base, walk_tree_ctx->match_path, baselen)
 27		|| strcmp(walk_tree_ctx->match_path + baselen, pathname))
 28		return READ_TREE_RECURSIVE;
 29	memmove(walk_tree_ctx->matched_sha1, sha1, 20);
 30	walk_tree_ctx->found_path = 1;
 31	return 0;
 32}
 33
 34int cgit_print_file(char *path, const char *head)
 35{
 36	unsigned char sha1[20];
 37	enum object_type type;
 38	char *buf;
 39	unsigned long size;
 40	struct commit *commit;
 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	};
 54
 55	if (get_sha1(head, sha1))
 56		return -1;
 57	type = sha1_object_info(sha1, &size);
 58	if (type == OBJ_COMMIT && path) {
 59		commit = lookup_commit_reference(sha1);
 60		read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
 61		if (!walk_tree_ctx.found_path)
 62			return -1;
 63		type = sha1_object_info(sha1, &size);
 64	}
 65	if (type == OBJ_BAD)
 66		return -1;
 67	buf = read_sha1_file(sha1, &type, &size);
 68	if (!buf)
 69		return -1;
 70	buf[size] = '\0';
 71	html_raw(buf, size);
 72	return 0;
 73}
 74
 75void cgit_print_blob(const char *hex, char *path, const char *head)
 76{
 77	unsigned char sha1[20];
 78	enum object_type type;
 79	char *buf;
 80	unsigned long size;
 81	struct commit *commit;
 82	struct pathspec_item path_items = {
 83		.match = path,
 84		.len = path ? strlen(path) : 0
 85	};
 86	struct pathspec paths = {
 87		.nr = 1,
 88		.items = &path_items
 89	};
 90	struct walk_tree_context walk_tree_ctx = {
 91		.match_path = path,
 92		.matched_sha1 = sha1,
 93	};
 94
 95	if (hex) {
 96		if (get_sha1_hex(hex, sha1)) {
 97			cgit_print_error("Bad hex value: %s", hex);
 98			return;
 99		}
100	} else {
101		if (get_sha1(head, sha1)) {
102			cgit_print_error("Bad ref: %s", head);
103			return;
104		}
105	}
106
107	type = sha1_object_info(sha1, &size);
108
109	if ((!hex) && type == OBJ_COMMIT && path) {
110		commit = lookup_commit_reference(sha1);
111		read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
112		type = sha1_object_info(sha1,&size);
113	}
114
115	if (type == OBJ_BAD) {
116		cgit_print_error("Bad object name: %s", hex);
117		return;
118	}
119
120	buf = read_sha1_file(sha1, &type, &size);
121	if (!buf) {
122		cgit_print_error("Error reading object %s", hex);
123		return;
124	}
125
126	buf[size] = '\0';
127	ctx.page.mimetype = ctx.qry.mimetype;
128	if (!ctx.page.mimetype) {
129		if (buffer_is_binary(buf, size))
130			ctx.page.mimetype = "application/octet-stream";
131		else
132			ctx.page.mimetype = "text/plain";
133	}
134	ctx.page.filename = path;
135	cgit_print_http_headers(&ctx);
136	html_raw(buf, size);
137}