1 /* ui-blob.c: show blob content
3 * Copyright (C) 2008 Lars Hjemli
4 * Copyright (C) 2010-2013 Jason A. Donenfeld <Jason@zx2c4.com>
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
13 #include "ui-shared.h"
15 struct walk_tree_context
{
16 const char *match_path
;
17 unsigned char *matched_sha1
;
22 static int walk_tree(const unsigned char *sha1
, const char *base
, int baselen
,
23 const char *pathname
, unsigned mode
, int stage
, void *cbdata
)
25 struct walk_tree_context
*walk_tree_ctx
= cbdata
;
27 if (walk_tree_ctx
->file_only
&& !S_ISREG(mode
))
28 return READ_TREE_RECURSIVE
;
29 if (strncmp(base
, walk_tree_ctx
->match_path
, baselen
)
30 || strcmp(walk_tree_ctx
->match_path
+ baselen
, pathname
))
31 return READ_TREE_RECURSIVE
;
32 memmove(walk_tree_ctx
->matched_sha1
, sha1
, 20);
33 walk_tree_ctx
->found_path
= 1;
37 int cgit_ref_path_exists(const char *path
, const char *ref
, int file_only
)
39 unsigned char sha1
[20];
41 struct pathspec_item path_items
= {
45 struct pathspec paths
= {
49 struct walk_tree_context walk_tree_ctx
= {
53 .file_only
= file_only
56 if (get_sha1(ref
, sha1
))
58 if (sha1_object_info(sha1
, &size
) != OBJ_COMMIT
)
60 read_tree_recursive(lookup_commit_reference(sha1
)->tree
, "", 0, 0, &paths
, walk_tree
, &walk_tree_ctx
);
61 return walk_tree_ctx
.found_path
;
64 int cgit_print_file(char *path
, const char *head
, int file_only
)
66 unsigned char sha1
[20];
67 enum object_type type
;
70 struct commit
*commit
;
71 struct pathspec_item path_items
= {
75 struct pathspec paths
= {
79 struct walk_tree_context walk_tree_ctx
= {
83 .file_only
= file_only
86 if (get_sha1(head
, sha1
))
88 type
= sha1_object_info(sha1
, &size
);
89 if (type
== OBJ_COMMIT
&& path
) {
90 commit
= lookup_commit_reference(sha1
);
91 read_tree_recursive(commit
->tree
, "", 0, 0, &paths
, walk_tree
, &walk_tree_ctx
);
92 if (!walk_tree_ctx
.found_path
)
94 type
= sha1_object_info(sha1
, &size
);
98 buf
= read_sha1_file(sha1
, &type
, &size
);
106 void cgit_print_blob(const char *hex
, char *path
, const char *head
, int file_only
)
108 unsigned char sha1
[20];
109 enum object_type type
;
112 struct commit
*commit
;
113 struct pathspec_item path_items
= {
115 .len
= path
? strlen(path
) : 0
117 struct pathspec paths
= {
121 struct walk_tree_context walk_tree_ctx
= {
123 .matched_sha1
= sha1
,
125 .file_only
= file_only
129 if (get_sha1_hex(hex
, sha1
)) {
130 cgit_print_error("Bad hex value: %s", hex
);
134 if (get_sha1(head
, sha1
)) {
135 cgit_print_error("Bad ref: %s", head
);
140 type
= sha1_object_info(sha1
, &size
);
142 if ((!hex
) && type
== OBJ_COMMIT
&& path
) {
143 commit
= lookup_commit_reference(sha1
);
144 read_tree_recursive(commit
->tree
, "", 0, 0, &paths
, walk_tree
, &walk_tree_ctx
);
145 type
= sha1_object_info(sha1
,&size
);
148 if (type
== OBJ_BAD
) {
149 cgit_print_error("Bad object name: %s", hex
);
153 buf
= read_sha1_file(sha1
, &type
, &size
);
155 cgit_print_error("Error reading object %s", hex
);
160 ctx
.page
.mimetype
= ctx
.qry
.mimetype
;
161 if (!ctx
.page
.mimetype
) {
162 if (buffer_is_binary(buf
, size
))
163 ctx
.page
.mimetype
= "application/octet-stream";
165 ctx
.page
.mimetype
= "text/plain";
167 ctx
.page
.filename
= path
;
168 cgit_print_http_headers(&ctx
);