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 struct object_id
*oid
, struct strbuf
*base
,
22 const char *pathname
, unsigned mode
, 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 oidcpy(walk_tree_ctx
->matched_oid
, oid
);
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 (oid_object_info(the_repository
, &oid
, &size
) != OBJ_COMMIT
)
59 read_tree(the_repository
,
60 repo_get_commit_tree(the_repository
, lookup_commit_reference(the_repository
, &oid
)),
61 &paths
, walk_tree
, &walk_tree_ctx
);
64 free(path_items
.match
);
65 return walk_tree_ctx
.found_path
;
68 int cgit_print_file(char *path
, const char *head
, int file_only
)
71 enum object_type type
;
74 struct commit
*commit
;
75 struct pathspec_item path_items
= {
79 struct pathspec paths
= {
83 struct walk_tree_context walk_tree_ctx
= {
87 .file_only
= file_only
90 if (get_oid(head
, &oid
))
92 type
= oid_object_info(the_repository
, &oid
, &size
);
93 if (type
== OBJ_COMMIT
) {
94 commit
= lookup_commit_reference(the_repository
, &oid
);
95 read_tree(the_repository
, repo_get_commit_tree(the_repository
, commit
),
96 &paths
, walk_tree
, &walk_tree_ctx
);
97 if (!walk_tree_ctx
.found_path
)
99 type
= oid_object_info(the_repository
, &oid
, &size
);
103 buf
= read_object_file(&oid
, &type
, &size
);
112 void cgit_print_blob(const char *hex
, char *path
, const char *head
, int file_only
)
114 struct object_id oid
;
115 enum object_type type
;
118 struct commit
*commit
;
119 struct pathspec_item path_items
= {
121 .len
= path
? strlen(path
) : 0
123 struct pathspec paths
= {
127 struct walk_tree_context walk_tree_ctx
= {
131 .file_only
= file_only
135 if (get_oid_hex(hex
, &oid
)) {
136 cgit_print_error_page(400, "Bad request",
137 "Bad hex value: %s", hex
);
141 if (get_oid(head
, &oid
)) {
142 cgit_print_error_page(404, "Not found",
143 "Bad ref: %s", head
);
148 type
= oid_object_info(the_repository
, &oid
, &size
);
150 if ((!hex
) && type
== OBJ_COMMIT
&& path
) {
151 commit
= lookup_commit_reference(the_repository
, &oid
);
152 read_tree(the_repository
, repo_get_commit_tree(the_repository
, commit
),
153 &paths
, walk_tree
, &walk_tree_ctx
);
154 type
= oid_object_info(the_repository
, &oid
, &size
);
157 if (type
== OBJ_BAD
) {
158 cgit_print_error_page(404, "Not found",
159 "Bad object name: %s", hex
);
163 buf
= read_object_file(&oid
, &type
, &size
);
165 cgit_print_error_page(500, "Internal server error",
166 "Error reading object %s", hex
);
171 if (buffer_is_binary(buf
, size
))
172 ctx
.page
.mimetype
= "application/octet-stream";
174 ctx
.page
.mimetype
= "text/plain";
175 ctx
.page
.filename
= path
;
177 html("X-Content-Type-Options: nosniff\n");
178 html("Content-Security-Policy: default-src 'none'\n");
179 cgit_print_http_headers();