blob: b4be139043519d590b4891963e35662e40ffdd6d [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. Donenfeld379e80e2010-08-04 00:45:42 +02004 * Copyright (C) 2010 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;
18 int found_path;
19};
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
26 if (strncmp(base, walk_tree_ctx->match_path, baselen)
27 || strcmp(walk_tree_ctx->match_path + baselen, pathname))
Michael Krelin01d2dce2008-06-24 23:33:24 +020028 return READ_TREE_RECURSIVE;
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010029 memmove(walk_tree_ctx->matched_sha1, sha1, 20);
30 walk_tree_ctx->found_path = 1;
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020031 return 0;
32}
33
Jason A. Donenfeldcd4c77d2013-05-25 16:32:37 +020034int cgit_ref_path_exists(const char *path, const char *ref)
35{
36 unsigned char sha1[20];
37 unsigned long size;
38 struct pathspec_item path_items = {
39 .match = path,
40 .len = strlen(path)
41 };
42 struct pathspec paths = {
43 .nr = 1,
44 .items = &path_items
45 };
46 struct walk_tree_context walk_tree_ctx = {
47 .match_path = path,
48 .matched_sha1 = sha1,
49 .found_path = 0
50 };
51
52 if (get_sha1(ref, sha1))
53 return 0;
54 if (sha1_object_info(sha1, &size) != OBJ_COMMIT)
55 return 0;
56 read_tree_recursive(lookup_commit_reference(sha1)->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
57 return walk_tree_ctx.found_path;
58}
59
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020060int cgit_print_file(char *path, const char *head)
61{
62 unsigned char sha1[20];
63 enum object_type type;
64 char *buf;
65 unsigned long size;
66 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +000067 struct pathspec_item path_items = {
68 .match = path,
69 .len = strlen(path)
70 };
71 struct pathspec paths = {
72 .nr = 1,
73 .items = &path_items
74 };
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010075 struct walk_tree_context walk_tree_ctx = {
76 .match_path = path,
77 .matched_sha1 = sha1,
78 .found_path = 0
79 };
80
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020081 if (get_sha1(head, sha1))
82 return -1;
83 type = sha1_object_info(sha1, &size);
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -050084 if (type == OBJ_COMMIT && path) {
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020085 commit = lookup_commit_reference(sha1);
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +010086 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
87 if (!walk_tree_ctx.found_path)
Jason A. Donenfeld379e80e2010-08-04 00:45:42 +020088 return -1;
89 type = sha1_object_info(sha1, &size);
90 }
91 if (type == OBJ_BAD)
92 return -1;
93 buf = read_sha1_file(sha1, &type, &size);
94 if (!buf)
95 return -1;
96 buf[size] = '\0';
Mark Lodatod187b982010-09-04 14:18:16 -040097 html_raw(buf, size);
Michael Krelin01d2dce2008-06-24 23:33:24 +020098 return 0;
99}
100
101void cgit_print_blob(const char *hex, char *path, const char *head)
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200102{
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200103 unsigned char sha1[20];
104 enum object_type type;
Lars Hjemli596eb8f2009-07-25 11:51:19 +0200105 char *buf;
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200106 unsigned long size;
Michael Krelin01d2dce2008-06-24 23:33:24 +0200107 struct commit *commit;
John Keepingc1633c62013-03-02 12:32:11 +0000108 struct pathspec_item path_items = {
109 .match = path,
John Keeping1c32e002013-04-07 15:06:23 +0100110 .len = path ? strlen(path) : 0
John Keepingc1633c62013-03-02 12:32:11 +0000111 };
112 struct pathspec paths = {
113 .nr = 1,
114 .items = &path_items
115 };
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +0100116 struct walk_tree_context walk_tree_ctx = {
117 .match_path = path,
118 .matched_sha1 = sha1,
119 };
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200120
Michael Krelin01d2dce2008-06-24 23:33:24 +0200121 if (hex) {
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500122 if (get_sha1_hex(hex, sha1)) {
John Keepinged5bd302013-04-06 11:23:52 +0100123 cgit_print_error("Bad hex value: %s", hex);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200124 return;
125 }
126 } else {
Lukas Fleischer53bc7472013-03-03 16:04:29 +0100127 if (get_sha1(head, sha1)) {
John Keepinged5bd302013-04-06 11:23:52 +0100128 cgit_print_error("Bad ref: %s", head);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200129 return;
130 }
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200131 }
132
133 type = sha1_object_info(sha1, &size);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200134
Jason A. Donenfeldbdae1d82013-03-03 23:21:33 -0500135 if ((!hex) && type == OBJ_COMMIT && path) {
Michael Krelin01d2dce2008-06-24 23:33:24 +0200136 commit = lookup_commit_reference(sha1);
Lukas Fleischer41f9c4e2013-03-03 17:27:46 +0100137 read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
Michael Krelin01d2dce2008-06-24 23:33:24 +0200138 type = sha1_object_info(sha1,&size);
139 }
140
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200141 if (type == OBJ_BAD) {
John Keepinged5bd302013-04-06 11:23:52 +0100142 cgit_print_error("Bad object name: %s", hex);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200143 return;
144 }
145
146 buf = read_sha1_file(sha1, &type, &size);
147 if (!buf) {
John Keepinged5bd302013-04-06 11:23:52 +0100148 cgit_print_error("Error reading object %s", hex);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200149 return;
150 }
151
152 buf[size] = '\0';
Michael Krelin42effc92008-06-24 23:42:32 +0200153 ctx.page.mimetype = ctx.qry.mimetype;
Lars Hjemli596eb8f2009-07-25 11:51:19 +0200154 if (!ctx.page.mimetype) {
155 if (buffer_is_binary(buf, size))
156 ctx.page.mimetype = "application/octet-stream";
157 else
158 ctx.page.mimetype = "text/plain";
159 }
Lars Hjemlif3c1a182008-03-24 00:51:19 +0100160 ctx.page.filename = path;
161 cgit_print_http_headers(&ctx);
Mark Lodatod187b982010-09-04 14:18:16 -0400162 html_raw(buf, size);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +0200163}