]> git.cameronkatri.com Git - cgit.git/blob - ui-blob.c
git: update to v2.32.0
[cgit.git] / ui-blob.c
1 /* ui-blob.c: show blob content
2 *
3 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
9 #include "cgit.h"
10 #include "ui-blob.h"
11 #include "html.h"
12 #include "ui-shared.h"
13
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;
19 };
20
21 static int walk_tree(const struct object_id *oid, struct strbuf *base,
22 const char *pathname, unsigned mode, void *cbdata)
23 {
24 struct walk_tree_context *walk_tree_ctx = cbdata;
25
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;
33 return 0;
34 }
35
36 int cgit_ref_path_exists(const char *path, const char *ref, int file_only)
37 {
38 struct object_id oid;
39 unsigned long size;
40 struct pathspec_item path_items = {
41 .match = xstrdup(path),
42 .len = strlen(path)
43 };
44 struct pathspec paths = {
45 .nr = 1,
46 .items = &path_items
47 };
48 struct walk_tree_context walk_tree_ctx = {
49 .match_path = path,
50 .matched_oid = &oid,
51 .found_path = 0,
52 .file_only = file_only
53 };
54
55 if (get_oid(ref, &oid))
56 goto done;
57 if (oid_object_info(the_repository, &oid, &size) != OBJ_COMMIT)
58 goto done;
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);
62
63 done:
64 free(path_items.match);
65 return walk_tree_ctx.found_path;
66 }
67
68 int cgit_print_file(char *path, const char *head, int file_only)
69 {
70 struct object_id oid;
71 enum object_type type;
72 char *buf;
73 unsigned long size;
74 struct commit *commit;
75 struct pathspec_item path_items = {
76 .match = path,
77 .len = strlen(path)
78 };
79 struct pathspec paths = {
80 .nr = 1,
81 .items = &path_items
82 };
83 struct walk_tree_context walk_tree_ctx = {
84 .match_path = path,
85 .matched_oid = &oid,
86 .found_path = 0,
87 .file_only = file_only
88 };
89
90 if (get_oid(head, &oid))
91 return -1;
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)
98 return -1;
99 type = oid_object_info(the_repository, &oid, &size);
100 }
101 if (type == OBJ_BAD)
102 return -1;
103 buf = read_object_file(&oid, &type, &size);
104 if (!buf)
105 return -1;
106 buf[size] = '\0';
107 html_raw(buf, size);
108 free(buf);
109 return 0;
110 }
111
112 void cgit_print_blob(const char *hex, char *path, const char *head, int file_only)
113 {
114 struct object_id oid;
115 enum object_type type;
116 char *buf;
117 unsigned long size;
118 struct commit *commit;
119 struct pathspec_item path_items = {
120 .match = path,
121 .len = path ? strlen(path) : 0
122 };
123 struct pathspec paths = {
124 .nr = 1,
125 .items = &path_items
126 };
127 struct walk_tree_context walk_tree_ctx = {
128 .match_path = path,
129 .matched_oid = &oid,
130 .found_path = 0,
131 .file_only = file_only
132 };
133
134 if (hex) {
135 if (get_oid_hex(hex, &oid)) {
136 cgit_print_error_page(400, "Bad request",
137 "Bad hex value: %s", hex);
138 return;
139 }
140 } else {
141 if (get_oid(head, &oid)) {
142 cgit_print_error_page(404, "Not found",
143 "Bad ref: %s", head);
144 return;
145 }
146 }
147
148 type = oid_object_info(the_repository, &oid, &size);
149
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);
155 }
156
157 if (type == OBJ_BAD) {
158 cgit_print_error_page(404, "Not found",
159 "Bad object name: %s", hex);
160 return;
161 }
162
163 buf = read_object_file(&oid, &type, &size);
164 if (!buf) {
165 cgit_print_error_page(500, "Internal server error",
166 "Error reading object %s", hex);
167 return;
168 }
169
170 buf[size] = '\0';
171 if (buffer_is_binary(buf, size))
172 ctx.page.mimetype = "application/octet-stream";
173 else
174 ctx.page.mimetype = "text/plain";
175 ctx.page.filename = path;
176
177 html("X-Content-Type-Options: nosniff\n");
178 html("Content-Security-Policy: default-src 'none'\n");
179 cgit_print_http_headers();
180 html_raw(buf, size);
181 free(buf);
182 }