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