blob: 3cda03d37992e987a3000613b01d6d7ac325db18 [file] [log] [blame]
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +01001/* ui-blob.c: show blob content
2 *
3 * Copyright (C) 2008 Lars Hjemli
4 *
5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text)
7 */
8
Lars Hjemlica8eb8f2007-05-09 00:48:09 +02009#include "cgit.h"
Lars Hjemlib1f9b9c2008-02-23 22:45:33 +010010#include "html.h"
Lars Hjemlia4d1ca12008-03-24 16:50:57 +010011#include "ui-shared.h"
Lars Hjemlica8eb8f2007-05-09 00:48:09 +020012
Michael Krelin01d2dce2008-06-24 23:33:24 +020013static char *match_path;
14static unsigned char *matched_sha1;
15
Lars Hjemli566f92b2008-07-21 10:10:48 +020016static int walk_tree(const unsigned char *sha1, const char *base,int baselen,
17 const char *pathname, unsigned mode, int stage, void *cbdata) {
Michael Krelin01d2dce2008-06-24 23:33:24 +020018 if(strncmp(base,match_path,baselen)
19 || strcmp(match_path+baselen,pathname) )
20 return READ_TREE_RECURSIVE;
21 memmove(matched_sha1,sha1,20);
22 return 0;
23}
24
25void cgit_print_blob(const char *hex, char *path, const char *head)
Lars Hjemlica8eb8f2007-05-09 00:48:09 +020026{
27
28 unsigned char sha1[20];
29 enum object_type type;
30 unsigned char *buf;
31 unsigned long size;
Michael Krelin01d2dce2008-06-24 23:33:24 +020032 struct commit *commit;
33 const char *paths[] = {path, NULL};
Lars Hjemlica8eb8f2007-05-09 00:48:09 +020034
Michael Krelin01d2dce2008-06-24 23:33:24 +020035 if (hex) {
36 if (get_sha1_hex(hex, sha1)){
37 cgit_print_error(fmt("Bad hex value: %s", hex));
38 return;
39 }
40 } else {
41 if (get_sha1(head,sha1)) {
42 cgit_print_error(fmt("Bad ref: %s", head));
43 return;
44 }
Lars Hjemlica8eb8f2007-05-09 00:48:09 +020045 }
46
47 type = sha1_object_info(sha1, &size);
Michael Krelin01d2dce2008-06-24 23:33:24 +020048
49 if((!hex) && type == OBJ_COMMIT && path) {
50 commit = lookup_commit_reference(sha1);
51 match_path = path;
52 matched_sha1 = sha1;
Lars Hjemli566f92b2008-07-21 10:10:48 +020053 read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL);
Michael Krelin01d2dce2008-06-24 23:33:24 +020054 type = sha1_object_info(sha1,&size);
55 }
56
Lars Hjemlica8eb8f2007-05-09 00:48:09 +020057 if (type == OBJ_BAD) {
58 cgit_print_error(fmt("Bad object name: %s", hex));
59 return;
60 }
61
62 buf = read_sha1_file(sha1, &type, &size);
63 if (!buf) {
64 cgit_print_error(fmt("Error reading object %s", hex));
65 return;
66 }
67
68 buf[size] = '\0';
Michael Krelin42effc92008-06-24 23:42:32 +020069 ctx.page.mimetype = ctx.qry.mimetype;
Lars Hjemlif3c1a182008-03-24 00:51:19 +010070 ctx.page.filename = path;
71 cgit_print_http_headers(&ctx);
Lars Hjemlica8eb8f2007-05-09 00:48:09 +020072 write(htmlfd, buf, size);
73}