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)
9 #define USE_THE_REPOSITORY_VARIABLE
14 #include "ui-shared.h"
16 struct walk_tree_context {
17 const char *match_path;
18 struct object_id *matched_oid;
19 unsigned int found_path:1;
20 unsigned int file_only:1;
23 static int walk_tree(const struct object_id *oid, struct strbuf *base,
24 const char *pathname, unsigned mode, void *cbdata)
26 struct walk_tree_context *walk_tree_ctx = cbdata;
28 if (walk_tree_ctx->file_only && !S_ISREG(mode))
29 return READ_TREE_RECURSIVE;
30 if (strncmp(base->buf, walk_tree_ctx->match_path, base->len)
31 || strcmp(walk_tree_ctx->match_path + base->len, pathname))
32 return READ_TREE_RECURSIVE;
33 oidcpy(walk_tree_ctx->matched_oid, oid);
34 walk_tree_ctx->found_path = 1;
38 int cgit_ref_path_exists(const char *path, const char *ref, int file_only)
42 struct pathspec_item path_items = {
43 .match = xstrdup(path),
46 struct pathspec paths = {
50 struct walk_tree_context walk_tree_ctx = {
54 .file_only = file_only
57 if (repo_get_oid(the_repository, ref, &oid))
59 if (oid_object_info(the_repository, &oid, &size) != OBJ_COMMIT)
61 read_tree(the_repository,
62 repo_get_commit_tree(the_repository, lookup_commit_reference(the_repository, &oid)),
63 &paths, walk_tree, &walk_tree_ctx);
66 free(path_items.match);
67 return walk_tree_ctx.found_path;
70 int cgit_print_file(char *path, const char *head, int file_only)
73 enum object_type type;
76 struct commit *commit;
77 struct pathspec_item path_items = {
81 struct pathspec paths = {
85 struct walk_tree_context walk_tree_ctx = {
89 .file_only = file_only
92 if (repo_get_oid(the_repository, head, &oid))
94 type = oid_object_info(the_repository, &oid, &size);
95 if (type == OBJ_COMMIT) {
96 commit = lookup_commit_reference(the_repository, &oid);
97 read_tree(the_repository, repo_get_commit_tree(the_repository, commit),
98 &paths, walk_tree, &walk_tree_ctx);
99 if (!walk_tree_ctx.found_path)
101 type = oid_object_info(the_repository, &oid, &size);
105 buf = repo_read_object_file(the_repository, &oid, &type, &size);
114 void cgit_print_blob(const char *hex, char *path, const char *head, int file_only)
116 struct object_id oid;
117 enum object_type type;
120 struct commit *commit;
121 struct pathspec_item path_items = {
123 .len = path ? strlen(path) : 0
125 struct pathspec paths = {
129 struct walk_tree_context walk_tree_ctx = {
133 .file_only = file_only
137 if (get_oid_hex(hex, &oid)) {
138 cgit_print_error_page(400, "Bad request",
139 "Bad hex value: %s", hex);
143 if (repo_get_oid(the_repository, head, &oid)) {
144 cgit_print_error_page(404, "Not found",
145 "Bad ref: %s", head);
150 type = oid_object_info(the_repository, &oid, &size);
152 if ((!hex) && type == OBJ_COMMIT && path) {
153 commit = lookup_commit_reference(the_repository, &oid);
154 read_tree(the_repository, repo_get_commit_tree(the_repository, commit),
155 &paths, walk_tree, &walk_tree_ctx);
156 type = oid_object_info(the_repository, &oid, &size);
159 if (type == OBJ_BAD) {
160 cgit_print_error_page(404, "Not found",
161 "Bad object name: %s", hex);
165 buf = repo_read_object_file(the_repository, &oid, &type, &size);
167 cgit_print_error_page(500, "Internal server error",
168 "Error reading object %s", hex);
173 if (buffer_is_binary(buf, size))
174 ctx.page.mimetype = "application/octet-stream";
176 ctx.page.mimetype = "text/plain";
177 ctx.page.filename = path;
179 html("X-Content-Type-Options: nosniff\n");
180 html("Content-Security-Policy: default-src 'none'\n");
181 cgit_print_http_headers();