]> gitweb.ps.run Git - ps-cgit/blob - ui-blob.c
git: update to v2.28.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 #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, int stage, 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_recursive(the_repository,
60                             repo_get_commit_tree(the_repository, lookup_commit_reference(the_repository, &oid)),
61                             "", 0, 0, &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_recursive(the_repository,
96                                     repo_get_commit_tree(the_repository, commit),
97                                     "", 0, 0, &paths, walk_tree,
98                                     &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 = read_object_file(&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 (get_oid(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_recursive(the_repository,
155                                     repo_get_commit_tree(the_repository, commit),
156                                     "", 0, 0, &paths, walk_tree,
157                                     &walk_tree_ctx);
158                 type = oid_object_info(the_repository, &oid, &size);
159         }
160
161         if (type == OBJ_BAD) {
162                 cgit_print_error_page(404, "Not found",
163                                 "Bad object name: %s", hex);
164                 return;
165         }
166
167         buf = read_object_file(&oid, &type, &size);
168         if (!buf) {
169                 cgit_print_error_page(500, "Internal server error",
170                                 "Error reading object %s", hex);
171                 return;
172         }
173
174         buf[size] = '\0';
175         if (buffer_is_binary(buf, size))
176                 ctx.page.mimetype = "application/octet-stream";
177         else
178                 ctx.page.mimetype = "text/plain";
179         ctx.page.filename = path;
180
181         html("X-Content-Type-Options: nosniff\n");
182         html("Content-Security-Policy: default-src 'none'\n");
183         cgit_print_http_headers();
184         html_raw(buf, size);
185         free(buf);
186 }