1 /* ui-blob.c: show blob content
3 * Copyright (C) 2008 Lars Hjemli
4 * Copyright (C) 2010 Jason A. Donenfeld <Jason@zx2c4.com>
6 * Licensed under GNU General Public License v2
7 * (see COPYING for full license text)
12 #include "ui-shared.h"
14 struct walk_tree_context
{
16 unsigned char *matched_sha1
;
20 static int walk_tree(const unsigned char *sha1
, const char *base
, int baselen
,
21 const char *pathname
, unsigned mode
, int stage
, void *cbdata
)
23 struct walk_tree_context
*walk_tree_ctx
= cbdata
;
25 if (strncmp(base
, walk_tree_ctx
->match_path
, baselen
)
26 || strcmp(walk_tree_ctx
->match_path
+ baselen
, pathname
))
27 return READ_TREE_RECURSIVE
;
28 memmove(walk_tree_ctx
->matched_sha1
, sha1
, 20);
29 walk_tree_ctx
->found_path
= 1;
33 int cgit_print_file(char *path
, const char *head
)
35 unsigned char sha1
[20];
36 enum object_type type
;
39 struct commit
*commit
;
40 struct pathspec_item path_items
= {
44 struct pathspec paths
= {
48 struct walk_tree_context walk_tree_ctx
= {
54 if (get_sha1(head
, sha1
))
56 type
= sha1_object_info(sha1
, &size
);
57 if (type
== OBJ_COMMIT
&& path
) {
58 commit
= lookup_commit_reference(sha1
);
59 read_tree_recursive(commit
->tree
, "", 0, 0, &paths
, walk_tree
, &walk_tree_ctx
);
60 if (!walk_tree_ctx
.found_path
)
62 type
= sha1_object_info(sha1
, &size
);
66 buf
= read_sha1_file(sha1
, &type
, &size
);
74 void cgit_print_blob(const char *hex
, char *path
, const char *head
)
76 unsigned char sha1
[20];
77 enum object_type type
;
80 struct commit
*commit
;
81 struct pathspec_item path_items
= {
85 struct pathspec paths
= {
89 struct walk_tree_context walk_tree_ctx
= {
95 if (get_sha1_hex(hex
, sha1
)) {
96 cgit_print_error(fmt("Bad hex value: %s", hex
));
100 if (get_sha1(head
, sha1
)) {
101 cgit_print_error(fmt("Bad ref: %s", head
));
106 type
= sha1_object_info(sha1
, &size
);
108 if ((!hex
) && type
== OBJ_COMMIT
&& path
) {
109 commit
= lookup_commit_reference(sha1
);
110 read_tree_recursive(commit
->tree
, "", 0, 0, &paths
, walk_tree
, &walk_tree_ctx
);
111 type
= sha1_object_info(sha1
,&size
);
114 if (type
== OBJ_BAD
) {
115 cgit_print_error(fmt("Bad object name: %s", hex
));
119 buf
= read_sha1_file(sha1
, &type
, &size
);
121 cgit_print_error(fmt("Error reading object %s", hex
));
126 ctx
.page
.mimetype
= ctx
.qry
.mimetype
;
127 if (!ctx
.page
.mimetype
) {
128 if (buffer_is_binary(buf
, size
))
129 ctx
.page
.mimetype
= "application/octet-stream";
131 ctx
.page
.mimetype
= "text/plain";
133 ctx
.page
.filename
= path
;
134 cgit_print_http_headers(&ctx
);