1 /* ui-blob.c: show blob content
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
12 #include "ui-shared.h"
14 struct walk_tree_context {
15 const char *match_path;
16 struct object_id *matched_oid;
17 unsigned int found_path:1;
18 unsigned int file_only:1;
21 static int walk_tree(const unsigned char *sha1, struct strbuf *base,
22 const char *pathname, unsigned mode, int stage, void *cbdata)
24 struct walk_tree_context *walk_tree_ctx = cbdata;
26 if (walk_tree_ctx->file_only && !S_ISREG(mode))
27 return READ_TREE_RECURSIVE;
28 if (strncmp(base->buf, walk_tree_ctx->match_path, base->len)
29 || strcmp(walk_tree_ctx->match_path + base->len, pathname))
30 return READ_TREE_RECURSIVE;
31 hashcpy(walk_tree_ctx->matched_oid->hash, sha1);
32 walk_tree_ctx->found_path = 1;
36 int cgit_ref_path_exists(const char *path, const char *ref, int file_only)
40 struct pathspec_item path_items = {
41 .match = xstrdup(path),
44 struct pathspec paths = {
48 struct walk_tree_context walk_tree_ctx = {
52 .file_only = file_only
55 if (get_oid(ref, &oid))
57 if (sha1_object_info(oid.hash, &size) != OBJ_COMMIT)
59 read_tree_recursive(lookup_commit_reference(oid.hash)->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
62 free(path_items.match);
63 return walk_tree_ctx.found_path;
66 int cgit_print_file(char *path, const char *head, int file_only)
69 enum object_type type;
72 struct commit *commit;
73 struct pathspec_item path_items = {
77 struct pathspec paths = {
81 struct walk_tree_context walk_tree_ctx = {
85 .file_only = file_only
88 if (get_oid(head, &oid))
90 type = sha1_object_info(oid.hash, &size);
91 if (type == OBJ_COMMIT) {
92 commit = lookup_commit_reference(oid.hash);
93 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
94 if (!walk_tree_ctx.found_path)
96 type = sha1_object_info(oid.hash, &size);
100 buf = read_sha1_file(oid.hash, &type, &size);
109 void cgit_print_blob(const char *hex, char *path, const char *head, int file_only)
111 struct object_id oid;
112 enum object_type type;
115 struct commit *commit;
116 struct pathspec_item path_items = {
118 .len = path ? strlen(path) : 0
120 struct pathspec paths = {
124 struct walk_tree_context walk_tree_ctx = {
128 .file_only = file_only
132 if (get_oid_hex(hex, &oid)) {
133 cgit_print_error_page(400, "Bad request",
134 "Bad hex value: %s", hex);
138 if (get_oid(head, &oid)) {
139 cgit_print_error_page(404, "Not found",
140 "Bad ref: %s", head);
145 type = sha1_object_info(oid.hash, &size);
147 if ((!hex) && type == OBJ_COMMIT && path) {
148 commit = lookup_commit_reference(oid.hash);
149 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
150 type = sha1_object_info(oid.hash, &size);
153 if (type == OBJ_BAD) {
154 cgit_print_error_page(404, "Not found",
155 "Bad object name: %s", hex);
159 buf = read_sha1_file(oid.hash, &type, &size);
161 cgit_print_error_page(500, "Internal server error",
162 "Error reading object %s", hex);
167 if (buffer_is_binary(buf, size))
168 ctx.page.mimetype = "application/octet-stream";
170 ctx.page.mimetype = "text/plain";
171 ctx.page.filename = path;
173 html("X-Content-Type-Options: nosniff\n");
174 html("Content-Security-Policy: default-src 'none'\n");
175 cgit_print_http_headers();