blob: 9c99519d997276d9962e346da075a6f8b777ee07 [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;
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020017 int found_path:1;
18 int file_only:1;
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010019};
Michael Krelin01d2dce2008-06-24 23:33:24 +020020
Lukas Fleischer53bc7472013-03-03 16:04:29 +010021static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010022 const char *pathname, unsigned mode, int stage, void *cbdata)
23{
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;
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010028 if (strncmp(base, walk_tree_ctx->match_path, baselen)
29 || strcmp(walk_tree_ctx->match_path + baselen, 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;
57 if (sha1_object_info(sha1, &size) != OBJ_COMMIT)
58 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);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050088 if (type == OBJ_COMMIT && path) {
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 Keepinged5bd302013-04-06 11:23:52 +0100129 cgit_print_error("Bad hex value: %s", hex);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200130 return;
131 }
132 } else {
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100133 if (get_sha1(head, sha1)) {
John Keepinged5bd302013-04-06 11:23:52 +0100134 cgit_print_error("Bad ref: %s", head);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200135 return;
136 }
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200137 }
138
139 type = sha1_object_info(sha1, &size);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200140
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500141 if ((!hex) && type == OBJ_COMMIT && path) {
Michael Krelin01d2dce2008-06-24 23:33:24 +0200142 commit = lookup_commit_reference(sha1);
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +0100143 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200144 type = sha1_object_info(sha1,&size);
145 }
146
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200147 if (type == OBJ_BAD) {
John Keepinged5bd302013-04-06 11:23:52 +0100148 cgit_print_error("Bad object name: %s", hex);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200149 return;
150 }
151
152 buf = read_sha1_file(sha1, &type, &size);
153 if (!buf) {
John Keepinged5bd302013-04-06 11:23:52 +0100154 cgit_print_error("Error reading object %s", hex);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200155 return;
156 }
157
158 buf[size] = '\0';
Michael Krelin42effc92008-06-24 23:42:32 +0200159 ctx.page.mimetype = ctx.qry.mimetype;
Lars Hjemli596eb8f2009-07-25 11:51:19 +0200160 if (!ctx.page.mimetype) {
161 if (buffer_is_binary(buf, size))
162 ctx.page.mimetype = "application/octet-stream";
163 else
164 ctx.page.mimetype = "text/plain";
165 }
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100166 ctx.page.filename = path;
Lukas Fleischerf60ffa12014-01-15 21:53:15 +0100167 cgit_print_http_headers();
Mark Lodatod187b982010-09-04 14:18:16 -0400168 html_raw(buf, size);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200169}