Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 1 | /* 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 Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 9 | #include "cgit.h" |
Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 10 | #include "html.h" |
Lars Hjemli | a4d1ca1 | 2008-03-24 16:50:57 +0100 | [diff] [blame] | 11 | #include "ui-shared.h" |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 12 | |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 13 | static char *match_path; |
| 14 | static unsigned char *matched_sha1; |
| 15 | |
Lars Hjemli | 566f92b | 2008-07-21 10:10:48 +0200 | [diff] [blame^] | 16 | static int walk_tree(const unsigned char *sha1, const char *base,int baselen, |
| 17 | const char *pathname, unsigned mode, int stage, void *cbdata) { |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 18 | 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 | |
| 25 | void cgit_print_blob(const char *hex, char *path, const char *head) |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 26 | { |
| 27 | |
| 28 | unsigned char sha1[20]; |
| 29 | enum object_type type; |
| 30 | unsigned char *buf; |
| 31 | unsigned long size; |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 32 | struct commit *commit; |
| 33 | const char *paths[] = {path, NULL}; |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 34 | |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 35 | 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 Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | type = sha1_object_info(sha1, &size); |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 48 | |
| 49 | if((!hex) && type == OBJ_COMMIT && path) { |
| 50 | commit = lookup_commit_reference(sha1); |
| 51 | match_path = path; |
| 52 | matched_sha1 = sha1; |
Lars Hjemli | 566f92b | 2008-07-21 10:10:48 +0200 | [diff] [blame^] | 53 | read_tree_recursive(commit->tree, NULL, 0, 0, paths, walk_tree, NULL); |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 54 | type = sha1_object_info(sha1,&size); |
| 55 | } |
| 56 | |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 57 | 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 Krelin | 42effc9 | 2008-06-24 23:42:32 +0200 | [diff] [blame] | 69 | ctx.page.mimetype = ctx.qry.mimetype; |
Lars Hjemli | f3c1a18 | 2008-03-24 00:51:19 +0100 | [diff] [blame] | 70 | ctx.page.filename = path; |
| 71 | cgit_print_http_headers(&ctx); |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 72 | write(htmlfd, buf, size); |
| 73 | } |