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; |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame^] | 17 | enum object_type type; |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 18 | unsigned long size; |
| 19 | |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame^] | 20 | name = xstrdup(pathname); |
| 21 | type = sha1_object_info(sha1, &size); |
| 22 | if (type == OBJ_BAD) { |
| 23 | htmlf("<tr><td colspan='3'>Bad object: %s %s</td></tr>", |
| 24 | name, |
| 25 | sha1_to_hex(sha1)); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 26 | return 0; |
| 27 | } |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 28 | html("<tr><td class='filemode'>"); |
| 29 | html_filemode(mode); |
| 30 | html("</td><td>"); |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame^] | 31 | if (S_ISDIRLNK(mode)) { |
| 32 | htmlf("<div class='ls-dirlnk'>%s => submodule</div>", name); |
| 33 | } else if (S_ISDIR(mode)) { |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 34 | html("<div class='ls-dir'><a href='"); |
| 35 | html_attr(cgit_pageurl(cgit_query_repo, "tree", |
Lars Hjemli | 5cd2bf7 | 2007-01-12 00:46:17 +0100 | [diff] [blame] | 36 | fmt("id=%s&path=%s%s/", |
| 37 | sha1_to_hex(sha1), |
| 38 | cgit_query_path ? cgit_query_path : "", |
| 39 | pathname))); |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame^] | 40 | htmlf("'>%s</a></div>", name); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 41 | } else { |
| 42 | html("<div class='ls-blob'><a href='"); |
| 43 | html_attr(cgit_pageurl(cgit_query_repo, "view", |
Lars Hjemli | 5cd2bf7 | 2007-01-12 00:46:17 +0100 | [diff] [blame] | 44 | fmt("id=%s&path=%s%s", sha1_to_hex(sha1), |
| 45 | cgit_query_path ? cgit_query_path : "", |
| 46 | pathname))); |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame^] | 47 | htmlf("'>%s</a></div>", name); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 48 | } |
Lars Hjemli | 61c3ca9 | 2007-05-08 22:40:59 +0200 | [diff] [blame^] | 49 | html("</div></td>"); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 50 | htmlf("<td class='filesize'>%li</td>", size); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 51 | html("</tr>\n"); |
| 52 | free(name); |
| 53 | return 0; |
| 54 | } |
| 55 | |
Lars Hjemli | 5cd2bf7 | 2007-01-12 00:46:17 +0100 | [diff] [blame] | 56 | void cgit_print_tree(const char *hex, char *path) |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 57 | { |
| 58 | struct tree *tree; |
| 59 | unsigned char sha1[20]; |
| 60 | |
| 61 | if (get_sha1_hex(hex, sha1)) { |
| 62 | cgit_print_error(fmt("Invalid object id: %s", hex)); |
| 63 | return; |
| 64 | } |
| 65 | tree = parse_tree_indirect(sha1); |
| 66 | if (!tree) { |
| 67 | cgit_print_error(fmt("Not a tree object: %s", hex)); |
| 68 | return; |
| 69 | } |
| 70 | |
Lars Hjemli | 5cd2bf7 | 2007-01-12 00:46:17 +0100 | [diff] [blame] | 71 | html_txt(path); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 72 | html("<table class='list'>\n"); |
Lars Hjemli | 777faf7 | 2007-01-28 00:39:26 +0100 | [diff] [blame] | 73 | html("<tr class='nohover'>"); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 74 | html("<th class='left'>Mode</th>"); |
| 75 | html("<th class='left'>Name</th>"); |
Lars Hjemli | 9a8f886 | 2006-12-16 00:19:56 +0100 | [diff] [blame] | 76 | html("<th class='right'>Size</th>"); |
Lars Hjemli | a530428 | 2006-12-17 23:55:53 +0100 | [diff] [blame] | 77 | html("</tr>\n"); |
Lars Hjemli | 06fe0c2 | 2006-12-13 00:13:27 +0100 | [diff] [blame] | 78 | read_tree_recursive(tree, "", 0, 1, NULL, print_entry); |
| 79 | html("</table>\n"); |
| 80 | } |