blob: d3c3a102669fe9a56dba80d4c511f39ab8300189 [file] [log] [blame]
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +01001/* ui-blob.c: show blob content
2 *
Lukas Fleischerf7f26f82014-01-08 15:10:49 +01003 * Copyright (C) 2006-2014 cgit Development Team <cgit@lists.zx2c4.com>
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +01004 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lars Hjemlica8eb8f2007-05-09 00:48:09 +02009#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010010#include "ui-blob.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010011#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010012#include "ui-shared.h"
Lars Hjemlica8eb8f2007-05-09 00:48:09 +020013
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010014struct walk_tree_context {
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020015 const char *match_path;
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010016 unsigned char *matched_sha1;
John Keeping12d3d4c2015-03-08 16:32:17 +000017 unsigned int found_path:1;
18 unsigned int file_only:1;
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010019};
Michael Krelin01d2dce2008-06-24 23:33:24 +020020
Christian Hesse7358f632015-02-07 14:18:28 +010021static int walk_tree(const unsigned char *sha1, struct strbuf *base,
22 const char *pathname, unsigned mode, int stage, void *cbdata)
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010023{
24 struct walk_tree_context *walk_tree_ctx = cbdata;
25
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020026 if (walk_tree_ctx->file_only && !S_ISREG(mode))
27 return READ_TREE_RECURSIVE;
Christian Hesse7358f632015-02-07 14:18:28 +010028 if (strncmp(base->buf, walk_tree_ctx->match_path, base->len)
29 || strcmp(walk_tree_ctx->match_path + base->len, pathname))
Michael Krelin01d2dce2008-06-24 23:33:24 +020030 return READ_TREE_RECURSIVE;
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010031 memmove(walk_tree_ctx->matched_sha1, sha1, 20);
32 walk_tree_ctx->found_path = 1;
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020033 return 0;
34}
35
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020036int cgit_ref_path_exists(const char *path, const char *ref, int file_only)
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020037{
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020038 unsigned char sha1[20];
39 unsigned long size;
40 struct pathspec_item path_items = {
41 .match = 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_sha1 = sha1,
51 .found_path = 0,
52 .file_only = file_only
53 };
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020054
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020055 if (get_sha1(ref, sha1))
56 return 0;
Christian Hesseb4312822014-04-17 11:55:46 +020057 if (sha1_object_info(sha1, &size) != OBJ_COMMIT)
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020058 return 0;
59 read_tree_recursive(lookup_commit_reference(sha1)->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
60 return walk_tree_ctx.found_path;
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020061}
62
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020063int cgit_print_file(char *path, const char *head, int file_only)
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020064{
65 unsigned char sha1[20];
66 enum object_type type;
67 char *buf;
68 unsigned long size;
69 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +000070 struct pathspec_item path_items = {
71 .match = path,
72 .len = strlen(path)
73 };
74 struct pathspec paths = {
75 .nr = 1,
76 .items = &path_items
77 };
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010078 struct walk_tree_context walk_tree_ctx = {
79 .match_path = path,
80 .matched_sha1 = sha1,
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020081 .found_path = 0,
82 .file_only = file_only
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010083 };
84
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020085 if (get_sha1(head, sha1))
86 return -1;
87 type = sha1_object_info(sha1, &size);
John Keeping509488d2015-10-08 23:23:58 +010088 if (type == OBJ_COMMIT) {
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020089 commit = lookup_commit_reference(sha1);
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010090 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
91 if (!walk_tree_ctx.found_path)
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020092 return -1;
93 type = sha1_object_info(sha1, &size);
94 }
95 if (type == OBJ_BAD)
96 return -1;
97 buf = read_sha1_file(sha1, &type, &size);
98 if (!buf)
99 return -1;
100 buf[size] = '\0';
Mark Lodatod187b982010-09-04 14:18:16 -0400101 html_raw(buf, size);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200102 return 0;
103}
104
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200105void cgit_print_blob(const char *hex, char *path, const char *head, int file_only)
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200106{
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200107 unsigned char sha1[20];
108 enum object_type type;
Lars Hjemli596eb8f2009-07-25 11:51:19 +0200109 char *buf;
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200110 unsigned long size;
Michael Krelin01d2dce2008-06-24 23:33:24 +0200111 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +0000112 struct pathspec_item path_items = {
113 .match = path,
John Keeping1c32e002013-04-07 15:06:23 +0100114 .len = path ? strlen(path) : 0
John Keepingc1633c62013-03-02 12:32:11 +0000115 };
116 struct pathspec paths = {
117 .nr = 1,
118 .items = &path_items
119 };
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +0100120 struct walk_tree_context walk_tree_ctx = {
121 .match_path = path,
122 .matched_sha1 = sha1,
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200123 .found_path = 0,
124 .file_only = file_only
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +0100125 };
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200126
Michael Krelin01d2dce2008-06-24 23:33:24 +0200127 if (hex) {
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500128 if (get_sha1_hex(hex, sha1)) {
John Keeping9a062112015-08-14 12:47:06 +0100129 cgit_print_error_page(400, "Bad request",
130 "Bad hex value: %s", hex);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200131 return;
132 }
133 } else {
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100134 if (get_sha1(head, sha1)) {
John Keeping9a062112015-08-14 12:47:06 +0100135 cgit_print_error_page(404, "Not found",
136 "Bad ref: %s", head);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200137 return;
138 }
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200139 }
140
141 type = sha1_object_info(sha1, &size);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200142
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500143 if ((!hex) && type == OBJ_COMMIT && path) {
Michael Krelin01d2dce2008-06-24 23:33:24 +0200144 commit = lookup_commit_reference(sha1);
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +0100145 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200146 type = sha1_object_info(sha1,&size);
147 }
148
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200149 if (type == OBJ_BAD) {
John Keeping9a062112015-08-14 12:47:06 +0100150 cgit_print_error_page(404, "Not found",
151 "Bad object name: %s", hex);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200152 return;
153 }
154
155 buf = read_sha1_file(sha1, &type, &size);
156 if (!buf) {
John Keeping9a062112015-08-14 12:47:06 +0100157 cgit_print_error_page(500, "Internal server error",
158 "Error reading object %s", hex);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200159 return;
160 }
161
162 buf[size] = '\0';
Michael Krelin42effc92008-06-24 23:42:32 +0200163 ctx.page.mimetype = ctx.qry.mimetype;
Lars Hjemli596eb8f2009-07-25 11:51:19 +0200164 if (!ctx.page.mimetype) {
165 if (buffer_is_binary(buf, size))
166 ctx.page.mimetype = "application/octet-stream";
167 else
168 ctx.page.mimetype = "text/plain";
169 }
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100170 ctx.page.filename = path;
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100171 cgit_print_http_headers();
Mark Lodatod187b982010-09-04 14:18:16 -0400172 html_raw(buf, size);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200173}