Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 1 | /* ui-tree.c: functions for tree output |
| 2 | * |
| 3 | * Copyright (C) 2006 Lars Hjemli |
| 4 | * |
| 5 | * Licensed under GNU General Public License v2 |
| 6 | * (see COPYING for full license text) |
| 7 | */ |
| 8 | |
| 9 | #include "cgit.h" |
| 10 | |
| 11 | |
| 12 | static int print_entry(const unsigned char *sha1, const char *base, |
| 13 | int baselen, const char *pathname, unsigned int mode, |
| 14 | int stage) |
| 15 | { |
| 16 | char *name; |
| 17 | char type[20]; |
| 18 | unsigned long size; |
| 19 | |
| 20 | if (sha1_object_info(sha1, type, &size)) { |
| 21 | cgit_print_error(fmt("Bad object name: %s", |
| 22 | sha1_to_hex(sha1))); |
| 23 | return 0; |
| 24 | } |
| 25 | name = xstrdup(pathname); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 26 | html("<tr><td class='filemode'>"); |
| 27 | html_filemode(mode); |
| 28 | html("</td><td>"); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 29 | if (S_ISDIR(mode)) { |
| 30 | html("<div class='ls-dir'><a href='"); |
| 31 | html_attr(cgit_pageurl(cgit_query_repo, "tree", |
Lars Hjemli | 5cd2bf7 | 2007-01-12 00:46:17 +0100 | [diff] [blame] | 32 | fmt("id=%s&path=%s%s/", |
| 33 | sha1_to_hex(sha1), |
| 34 | cgit_query_path ? cgit_query_path : "", |
| 35 | pathname))); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 36 | } else { |
| 37 | html("<div class='ls-blob'><a href='"); |
| 38 | html_attr(cgit_pageurl(cgit_query_repo, "view", |
Lars Hjemli | 5cd2bf7 | 2007-01-12 00:46:17 +0100 | [diff] [blame] | 39 | fmt("id=%s&path=%s%s", sha1_to_hex(sha1), |
| 40 | cgit_query_path ? cgit_query_path : "", |
| 41 | pathname))); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 42 | } |
| 43 | html("'>"); |
| 44 | html_txt(name); |
| 45 | if (S_ISDIR(mode)) |
| 46 | html("/"); |
| 47 | html("</a></div></td>"); |
| 48 | htmlf("<td class='filesize'>%li</td>", size); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 49 | html("</tr>\n"); |
| 50 | free(name); |
| 51 | return 0; |
| 52 | } |
| 53 | |
Lars Hjemli | 5cd2bf7 | 2007-01-12 00:46:17 +0100 | [diff] [blame] | 54 | void cgit_print_tree(const char *hex, char *path) |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 55 | { |
| 56 | struct tree *tree; |
| 57 | unsigned char sha1[20]; |
| 58 | |
| 59 | if (get_sha1_hex(hex, sha1)) { |
| 60 | cgit_print_error(fmt("Invalid object id: %s", hex)); |
| 61 | return; |
| 62 | } |
| 63 | tree = parse_tree_indirect(sha1); |
| 64 | if (!tree) { |
| 65 | cgit_print_error(fmt("Not a tree object: %s", hex)); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | html("<h2>Tree content</h2>\n"); |
Lars Hjemli | 5cd2bf7 | 2007-01-12 00:46:17 +0100 | [diff] [blame] | 70 | html_txt(path); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 71 | html("<table class='list'>\n"); |
Lars Hjemli | 777faf7 | 2007-01-28 00:39:26 +0100 | [diff] [blame^] | 72 | html("<tr class='nohover'>"); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 73 | html("<th class='left'>Mode</th>"); |
| 74 | html("<th class='left'>Name</th>"); |
Lars Hjemli | 9a8f886 | 2006-12-16 00:19:56 +0100 | [diff] [blame] | 75 | html("<th class='right'>Size</th>"); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 76 | html("</tr>\n"); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 77 | read_tree_recursive(tree, "", 0, 1, NULL, print_entry); |
| 78 | html("</table>\n"); |
| 79 | } |