1 /* ui-blob.c: show blob content
3 * Copyright (C) 2008 Lars Hjemli
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
11 #include "ui-shared.h"
13 static char *match_path;
14 static unsigned char *matched_sha1;
16 static int walk_tree(const unsigned char *sha1, const char *base,int baselen, const char *pathname, unsigned mode, int stage) {
17 if(strncmp(base,match_path,baselen)
18 || strcmp(match_path+baselen,pathname) )
19 return READ_TREE_RECURSIVE;
20 memmove(matched_sha1,sha1,20);
24 void cgit_print_blob(const char *hex, char *path, const char *head)
27 unsigned char sha1[20];
28 enum object_type type;
31 struct commit *commit;
32 const char *paths[] = {path, NULL};
35 if (get_sha1_hex(hex, sha1)){
36 cgit_print_error(fmt("Bad hex value: %s", hex));
40 if (get_sha1(head,sha1)) {
41 cgit_print_error(fmt("Bad ref: %s", head));
46 type = sha1_object_info(sha1, &size);
48 if((!hex) && type == OBJ_COMMIT && path) {
49 commit = lookup_commit_reference(sha1);
52 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree);
53 type = sha1_object_info(sha1,&size);
56 if (type == OBJ_BAD) {
57 cgit_print_error(fmt("Bad object name: %s", hex));
61 buf = read_sha1_file(sha1, &type, &size);
63 cgit_print_error(fmt("Error reading object %s", hex));
68 ctx.page.mimetype = ctx.qry.mimetype;
69 ctx.page.filename = path;
70 cgit_print_http_headers(&ctx);
71 write(htmlfd, buf, size);