blob: eb14a754c8e9995b3def375b1988544a54d7197f [file] [log] [blame]
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +01001/* ui-blob.c: show blob content
2 *
3 * Copyright (C) 2008 Lars Hjemli
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +02004 * Copyright (C) 2010-2013 Jason A. Donenfeld <Jason@zx2c4.com>
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +01005 *
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
8 */
9
Lars Hjemlica8eb8f2007-05-09 00:48:09 +020010#include "cgit.h"
John Keeping8f208792013-04-06 11:37:59 +010011#include "ui-blob.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010012#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010013#include "ui-shared.h"
Lars Hjemlica8eb8f2007-05-09 00:48:09 +020014
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010015struct walk_tree_context {
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020016 const char *match_path;
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010017 unsigned char *matched_sha1;
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020018 int found_path:1;
19 int file_only:1;
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010020};
Michael Krelin01d2dce2008-06-24 23:33:24 +020021
Lukas Fleischer53bc7472013-03-03 16:04:29 +010022static int walk_tree(const unsigned char *sha1, const char *base, int baselen,
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010023 const char *pathname, unsigned mode, int stage, void *cbdata)
24{
25 struct walk_tree_context *walk_tree_ctx = cbdata;
26
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020027 if (walk_tree_ctx->file_only && !S_ISREG(mode))
28 return READ_TREE_RECURSIVE;
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010029 if (strncmp(base, walk_tree_ctx->match_path, baselen)
30 || strcmp(walk_tree_ctx->match_path + baselen, pathname))
Michael Krelin01d2dce2008-06-24 23:33:24 +020031 return READ_TREE_RECURSIVE;
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010032 memmove(walk_tree_ctx->matched_sha1, sha1, 20);
33 walk_tree_ctx->found_path = 1;
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020034 return 0;
35}
36
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020037int cgit_ref_path_exists(const char *path, const char *ref, int file_only)
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020038{
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020039 unsigned char sha1[20];
40 unsigned long size;
41 struct pathspec_item path_items = {
42 .match = path,
43 .len = strlen(path)
44 };
45 struct pathspec paths = {
46 .nr = 1,
47 .items = &path_items
48 };
49 struct walk_tree_context walk_tree_ctx = {
50 .match_path = path,
51 .matched_sha1 = sha1,
52 .found_path = 0,
53 .file_only = file_only
54 };
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020055
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020056 if (get_sha1(ref, sha1))
57 return 0;
58 if (sha1_object_info(sha1, &size) != OBJ_COMMIT)
59 return 0;
60 read_tree_recursive(lookup_commit_reference(sha1)->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
61 return walk_tree_ctx.found_path;
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020062}
63
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020064int cgit_print_file(char *path, const char *head, int file_only)
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020065{
66 unsigned char sha1[20];
67 enum object_type type;
68 char *buf;
69 unsigned long size;
70 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +000071 struct pathspec_item path_items = {
72 .match = path,
73 .len = strlen(path)
74 };
75 struct pathspec paths = {
76 .nr = 1,
77 .items = &path_items
78 };
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010079 struct walk_tree_context walk_tree_ctx = {
80 .match_path = path,
81 .matched_sha1 = sha1,
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +020082 .found_path = 0,
83 .file_only = file_only
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010084 };
85
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020086 if (get_sha1(head, sha1))
87 return -1;
88 type = sha1_object_info(sha1, &size);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050089 if (type == OBJ_COMMIT && path) {
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020090 commit = lookup_commit_reference(sha1);
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010091 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
92 if (!walk_tree_ctx.found_path)
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020093 return -1;
94 type = sha1_object_info(sha1, &size);
95 }
96 if (type == OBJ_BAD)
97 return -1;
98 buf = read_sha1_file(sha1, &type, &size);
99 if (!buf)
100 return -1;
101 buf[size] = '\0';
Mark Lodatod187b982010-09-04 14:18:16 -0400102 html_raw(buf, size);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200103 return 0;
104}
105
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200106void cgit_print_blob(const char *hex, char *path, const char *head, int file_only)
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200107{
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200108 unsigned char sha1[20];
109 enum object_type type;
Lars Hjemli596eb8f2009-07-25 11:51:19 +0200110 char *buf;
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200111 unsigned long size;
Michael Krelin01d2dce2008-06-24 23:33:24 +0200112 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +0000113 struct pathspec_item path_items = {
114 .match = path,
John Keeping1c32e002013-04-07 15:06:23 +0100115 .len = path ? strlen(path) : 0
John Keepingc1633c62013-03-02 12:32:11 +0000116 };
117 struct pathspec paths = {
118 .nr = 1,
119 .items = &path_items
120 };
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +0100121 struct walk_tree_context walk_tree_ctx = {
122 .match_path = path,
123 .matched_sha1 = sha1,
Jason A. Donenfelddcbc0432013-05-26 15:20:02 +0200124 .found_path = 0,
125 .file_only = file_only
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +0100126 };
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200127
Michael Krelin01d2dce2008-06-24 23:33:24 +0200128 if (hex) {
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500129 if (get_sha1_hex(hex, sha1)) {
John Keepinged5bd302013-04-06 11:23:52 +0100130 cgit_print_error("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 Keepinged5bd302013-04-06 11:23:52 +0100135 cgit_print_error("Bad ref: %s", head);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200136 return;
137 }
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200138 }
139
140 type = sha1_object_info(sha1, &size);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200141
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500142 if ((!hex) && type == OBJ_COMMIT && path) {
Michael Krelin01d2dce2008-06-24 23:33:24 +0200143 commit = lookup_commit_reference(sha1);
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +0100144 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200145 type = sha1_object_info(sha1,&size);
146 }
147
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200148 if (type == OBJ_BAD) {
John Keepinged5bd302013-04-06 11:23:52 +0100149 cgit_print_error("Bad object name: %s", hex);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200150 return;
151 }
152
153 buf = read_sha1_file(sha1, &type, &size);
154 if (!buf) {
John Keepinged5bd302013-04-06 11:23:52 +0100155 cgit_print_error("Error reading object %s", hex);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200156 return;
157 }
158
159 buf[size] = '\0';
Michael Krelin42effc92008-06-24 23:42:32 +0200160 ctx.page.mimetype = ctx.qry.mimetype;
Lars Hjemli596eb8f2009-07-25 11:51:19 +0200161 if (!ctx.page.mimetype) {
162 if (buffer_is_binary(buf, size))
163 ctx.page.mimetype = "application/octet-stream";
164 else
165 ctx.page.mimetype = "text/plain";
166 }
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100167 ctx.page.filename = path;
168 cgit_print_http_headers(&ctx);
Mark Lodatod187b982010-09-04 14:18:16 -0400169 html_raw(buf, size);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200170}