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 |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 4 | * Copyright (C) 2010 Jason A. Donenfeld <Jason@zx2c4.com> |
Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 5 | * |
| 6 | * Licensed under GNU General Public License v2 |
| 7 | * (see COPYING for full license text) |
| 8 | */ |
| 9 | |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 10 | #include "cgit.h" |
Lars Hjemli | b1f9b9c | 2008-02-23 22:45:33 +0100 | [diff] [blame] | 11 | #include "html.h" |
Lars Hjemli | a4d1ca1 | 2008-03-24 16:50:57 +0100 | [diff] [blame] | 12 | #include "ui-shared.h" |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 13 | |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 14 | struct walk_tree_context { |
| 15 | char *match_path; |
| 16 | unsigned char *matched_sha1; |
| 17 | int found_path; |
| 18 | }; |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 19 | |
Lukas Fleischer | 53bc747 | 2013-03-03 16:04:29 +0100 | [diff] [blame] | 20 | static int walk_tree(const unsigned char *sha1, const char *base, int baselen, |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 21 | const char *pathname, unsigned mode, int stage, void *cbdata) |
| 22 | { |
| 23 | struct walk_tree_context *walk_tree_ctx = cbdata; |
| 24 | |
| 25 | if (strncmp(base, walk_tree_ctx->match_path, baselen) |
| 26 | || strcmp(walk_tree_ctx->match_path + baselen, pathname)) |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 27 | return READ_TREE_RECURSIVE; |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 28 | memmove(walk_tree_ctx->matched_sha1, sha1, 20); |
| 29 | walk_tree_ctx->found_path = 1; |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | int cgit_print_file(char *path, const char *head) |
| 34 | { |
| 35 | unsigned char sha1[20]; |
| 36 | enum object_type type; |
| 37 | char *buf; |
| 38 | unsigned long size; |
| 39 | struct commit *commit; |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 40 | struct pathspec_item path_items = { |
| 41 | .match = path, |
| 42 | .len = strlen(path) |
| 43 | }; |
| 44 | struct pathspec paths = { |
| 45 | .nr = 1, |
| 46 | .items = &path_items |
| 47 | }; |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 48 | struct walk_tree_context walk_tree_ctx = { |
| 49 | .match_path = path, |
| 50 | .matched_sha1 = sha1, |
| 51 | .found_path = 0 |
| 52 | }; |
| 53 | |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 54 | if (get_sha1(head, sha1)) |
| 55 | return -1; |
| 56 | type = sha1_object_info(sha1, &size); |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 57 | if (type == OBJ_COMMIT && path) { |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 58 | commit = lookup_commit_reference(sha1); |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 59 | read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); |
| 60 | if (!walk_tree_ctx.found_path) |
Jason A. Donenfeld | 379e80e | 2010-08-04 00:45:42 +0200 | [diff] [blame] | 61 | return -1; |
| 62 | type = sha1_object_info(sha1, &size); |
| 63 | } |
| 64 | if (type == OBJ_BAD) |
| 65 | return -1; |
| 66 | buf = read_sha1_file(sha1, &type, &size); |
| 67 | if (!buf) |
| 68 | return -1; |
| 69 | buf[size] = '\0'; |
Mark Lodato | d187b98 | 2010-09-04 14:18:16 -0400 | [diff] [blame] | 70 | html_raw(buf, size); |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | void cgit_print_blob(const char *hex, char *path, const char *head) |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 75 | { |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 76 | unsigned char sha1[20]; |
| 77 | enum object_type type; |
Lars Hjemli | 596eb8f | 2009-07-25 11:51:19 +0200 | [diff] [blame] | 78 | char *buf; |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 79 | unsigned long size; |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 80 | struct commit *commit; |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 81 | struct pathspec_item path_items = { |
| 82 | .match = path, |
John Keeping | 1c32e00 | 2013-04-07 15:06:23 +0100 | [diff] [blame^] | 83 | .len = path ? strlen(path) : 0 |
John Keeping | c1633c6 | 2013-03-02 12:32:11 +0000 | [diff] [blame] | 84 | }; |
| 85 | struct pathspec paths = { |
| 86 | .nr = 1, |
| 87 | .items = &path_items |
| 88 | }; |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 89 | struct walk_tree_context walk_tree_ctx = { |
| 90 | .match_path = path, |
| 91 | .matched_sha1 = sha1, |
| 92 | }; |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 93 | |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 94 | if (hex) { |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 95 | if (get_sha1_hex(hex, sha1)) { |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 96 | cgit_print_error(fmt("Bad hex value: %s", hex)); |
| 97 | return; |
| 98 | } |
| 99 | } else { |
Lukas Fleischer | 53bc747 | 2013-03-03 16:04:29 +0100 | [diff] [blame] | 100 | if (get_sha1(head, sha1)) { |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 101 | cgit_print_error(fmt("Bad ref: %s", head)); |
| 102 | return; |
| 103 | } |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | type = sha1_object_info(sha1, &size); |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 107 | |
Jason A. Donenfeld | bdae1d8 | 2013-03-03 23:21:33 -0500 | [diff] [blame] | 108 | if ((!hex) && type == OBJ_COMMIT && path) { |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 109 | commit = lookup_commit_reference(sha1); |
Lukas Fleischer | 41f9c4e | 2013-03-03 17:27:46 +0100 | [diff] [blame] | 110 | read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx); |
Michael Krelin | 01d2dce | 2008-06-24 23:33:24 +0200 | [diff] [blame] | 111 | type = sha1_object_info(sha1,&size); |
| 112 | } |
| 113 | |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 114 | if (type == OBJ_BAD) { |
| 115 | cgit_print_error(fmt("Bad object name: %s", hex)); |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | buf = read_sha1_file(sha1, &type, &size); |
| 120 | if (!buf) { |
| 121 | cgit_print_error(fmt("Error reading object %s", hex)); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | buf[size] = '\0'; |
Michael Krelin | 42effc9 | 2008-06-24 23:42:32 +0200 | [diff] [blame] | 126 | ctx.page.mimetype = ctx.qry.mimetype; |
Lars Hjemli | 596eb8f | 2009-07-25 11:51:19 +0200 | [diff] [blame] | 127 | if (!ctx.page.mimetype) { |
| 128 | if (buffer_is_binary(buf, size)) |
| 129 | ctx.page.mimetype = "application/octet-stream"; |
| 130 | else |
| 131 | ctx.page.mimetype = "text/plain"; |
| 132 | } |
Lars Hjemli | f3c1a18 | 2008-03-24 00:51:19 +0100 | [diff] [blame] | 133 | ctx.page.filename = path; |
| 134 | cgit_print_http_headers(&ctx); |
Mark Lodato | d187b98 | 2010-09-04 14:18:16 -0400 | [diff] [blame] | 135 | html_raw(buf, size); |
Lars Hjemli | ca8eb8f | 2007-05-09 00:48:09 +0200 | [diff] [blame] | 136 | } |