| Petr Baudis | 7912c07 | 2005-04-13 09:02:34 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
| 6 | #include "cache.h" |
| Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 7 | #include "blob.h" |
| 8 | #include "tree.h" |
| Junio C Hamano | 22ddf71 | 2005-10-15 04:56:46 | [diff] [blame] | 9 | #include "quote.h" |
| Petr Baudis | 7912c07 | 2005-04-13 09:02:34 | [diff] [blame] | 10 | |
| Linus Torvalds | e99d59f | 2005-05-20 18:46:10 | [diff] [blame] | 11 | static int line_termination = '\n'; |
| Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 12 | #define LS_RECURSIVE 1 |
| 13 | #define LS_TREE_ONLY 2 |
| Linus Torvalds | 0f8f45c | 2005-12-01 18:35:51 | [diff] [blame] | 14 | #define LS_SHOW_TREES 4 |
| Junio C Hamano | c639a55 | 2005-12-01 22:54:00 | [diff] [blame] | 15 | #define LS_NAME_ONLY 8 |
| Linus Torvalds | e246637 | 2005-11-28 06:48:08 | [diff] [blame] | 16 | static int ls_options = 0; |
| 17 | const char **pathspec; |
| Junio C Hamano | a69dd58 | 2005-12-23 21:39:30 | [diff] [blame] | 18 | static int chomp_prefix = 0; |
| 19 | static const char *prefix; |
| Junio C Hamano | aa1c48d | 2005-04-15 15:37:05 | [diff] [blame] | 20 | |
| Petr Baudis | 4d1f119 | 2005-07-29 09:01:26 | [diff] [blame] | 21 | static const char ls_tree_usage[] = |
| Junio C Hamano | a69dd58 | 2005-12-23 21:39:30 | [diff] [blame] | 22 | "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] <tree-ish> [path...]"; |
| Linus Torvalds | 0f8f45c | 2005-12-01 18:35:51 | [diff] [blame] | 23 | |
| 24 | static int show_recursive(const char *base, int baselen, const char *pathname) |
| 25 | { |
| 26 | const char **s; |
| 27 | |
| 28 | if (ls_options & LS_RECURSIVE) |
| 29 | return 1; |
| 30 | |
| 31 | s = pathspec; |
| 32 | if (!s) |
| 33 | return 0; |
| 34 | |
| 35 | for (;;) { |
| 36 | const char *spec = *s++; |
| 37 | int len, speclen; |
| 38 | |
| 39 | if (!spec) |
| 40 | return 0; |
| 41 | if (strncmp(base, spec, baselen)) |
| 42 | continue; |
| 43 | len = strlen(pathname); |
| 44 | spec += baselen; |
| 45 | speclen = strlen(spec); |
| 46 | if (speclen <= len) |
| 47 | continue; |
| 48 | if (memcmp(pathname, spec, len)) |
| 49 | continue; |
| 50 | return 1; |
| 51 | } |
| 52 | } |
| Junio C Hamano | aa1c48d | 2005-04-15 15:37:05 | [diff] [blame] | 53 | |
| Junio C Hamano | a69dd58 | 2005-12-23 21:39:30 | [diff] [blame] | 54 | static int show_tree(unsigned char *sha1, const char *base, int baselen, |
| 55 | const char *pathname, unsigned mode, int stage) |
| Petr Baudis | 7912c07 | 2005-04-13 09:02:34 | [diff] [blame] | 56 | { |
| Linus Torvalds | 0f8f45c | 2005-12-01 18:35:51 | [diff] [blame] | 57 | int retval = 0; |
| Linus Torvalds | 3c5e846 | 2005-11-26 17:38:20 | [diff] [blame] | 58 | const char *type = "blob"; |
| Petr Baudis | 7912c07 | 2005-04-13 09:02:34 | [diff] [blame] | 59 | |
| Linus Torvalds | 3c5e846 | 2005-11-26 17:38:20 | [diff] [blame] | 60 | if (S_ISDIR(mode)) { |
| Linus Torvalds | 0f8f45c | 2005-12-01 18:35:51 | [diff] [blame] | 61 | if (show_recursive(base, baselen, pathname)) { |
| 62 | retval = READ_TREE_RECURSIVE; |
| 63 | if (!(ls_options & LS_SHOW_TREES)) |
| 64 | return retval; |
| Linus Torvalds | e246637 | 2005-11-28 06:48:08 | [diff] [blame] | 65 | } |
| Linus Torvalds | b45c569 | 2005-11-27 19:00:09 | [diff] [blame] | 66 | type = "tree"; |
| Linus Torvalds | 3c5e846 | 2005-11-26 17:38:20 | [diff] [blame] | 67 | } |
| Junio C Hamano | f598467 | 2005-12-01 21:15:20 | [diff] [blame] | 68 | else if (ls_options & LS_TREE_ONLY) |
| 69 | return 0; |
| Linus Torvalds | 3c5e846 | 2005-11-26 17:38:20 | [diff] [blame] | 70 | |
| Junio C Hamano | a69dd58 | 2005-12-23 21:39:30 | [diff] [blame] | 71 | if (chomp_prefix && |
| 72 | (baselen < chomp_prefix || memcmp(prefix, base, chomp_prefix))) |
| 73 | return 0; |
| 74 | |
| Junio C Hamano | c639a55 | 2005-12-01 22:54:00 | [diff] [blame] | 75 | if (!(ls_options & LS_NAME_ONLY)) |
| 76 | printf("%06o %s %s\t", mode, type, sha1_to_hex(sha1)); |
| Junio C Hamano | a69dd58 | 2005-12-23 21:39:30 | [diff] [blame] | 77 | write_name_quoted(base + chomp_prefix, baselen - chomp_prefix, |
| 78 | pathname, |
| 79 | line_termination, stdout); |
| Junio C Hamano | 32b5904 | 2005-11-28 10:30:04 | [diff] [blame] | 80 | putchar(line_termination); |
| Linus Torvalds | 0f8f45c | 2005-12-01 18:35:51 | [diff] [blame] | 81 | return retval; |
| Linus Torvalds | 3c5e846 | 2005-11-26 17:38:20 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | int main(int argc, const char **argv) |
| 85 | { |
| Linus Torvalds | 3c5e846 | 2005-11-26 17:38:20 | [diff] [blame] | 86 | unsigned char sha1[20]; |
| Daniel Barkalow | 521698b | 2006-01-26 06:13:36 | [diff] [blame] | 87 | struct tree *tree; |
| Linus Torvalds | 3c5e846 | 2005-11-26 17:38:20 | [diff] [blame] | 88 | |
| 89 | prefix = setup_git_directory(); |
| Junio C Hamano | a69dd58 | 2005-12-23 21:39:30 | [diff] [blame] | 90 | if (prefix && *prefix) |
| 91 | chomp_prefix = strlen(prefix); |
| Junio C Hamano | aa1c48d | 2005-04-15 15:37:05 | [diff] [blame] | 92 | while (1 < argc && argv[1][0] == '-') { |
| 93 | switch (argv[1][1]) { |
| 94 | case 'z': |
| 95 | line_termination = 0; |
| 96 | break; |
| 97 | case 'r': |
| Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 98 | ls_options |= LS_RECURSIVE; |
| 99 | break; |
| 100 | case 'd': |
| 101 | ls_options |= LS_TREE_ONLY; |
| Junio C Hamano | aa1c48d | 2005-04-15 15:37:05 | [diff] [blame] | 102 | break; |
| Linus Torvalds | 0f8f45c | 2005-12-01 18:35:51 | [diff] [blame] | 103 | case 't': |
| 104 | ls_options |= LS_SHOW_TREES; |
| 105 | break; |
| Junio C Hamano | c639a55 | 2005-12-01 22:54:00 | [diff] [blame] | 106 | case '-': |
| 107 | if (!strcmp(argv[1]+2, "name-only") || |
| 108 | !strcmp(argv[1]+2, "name-status")) { |
| 109 | ls_options |= LS_NAME_ONLY; |
| 110 | break; |
| 111 | } |
| Junio C Hamano | a69dd58 | 2005-12-23 21:39:30 | [diff] [blame] | 112 | if (!strcmp(argv[1]+2, "full-name")) { |
| 113 | chomp_prefix = 0; |
| 114 | break; |
| 115 | } |
| Junio C Hamano | c639a55 | 2005-12-01 22:54:00 | [diff] [blame] | 116 | /* otherwise fallthru */ |
| Junio C Hamano | aa1c48d | 2005-04-15 15:37:05 | [diff] [blame] | 117 | default: |
| Junio C Hamano | 0f2303f | 2005-04-16 20:57:39 | [diff] [blame] | 118 | usage(ls_tree_usage); |
| Junio C Hamano | aa1c48d | 2005-04-15 15:37:05 | [diff] [blame] | 119 | } |
| 120 | argc--; argv++; |
| 121 | } |
| Junio C Hamano | f598467 | 2005-12-01 21:15:20 | [diff] [blame] | 122 | /* -d -r should imply -t, but -d by itself should not have to. */ |
| 123 | if ( (LS_TREE_ONLY|LS_RECURSIVE) == |
| 124 | ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options)) |
| 125 | ls_options |= LS_SHOW_TREES; |
| Junio C Hamano | aa1c48d | 2005-04-15 15:37:05 | [diff] [blame] | 126 | |
| Jason McMullan | 6d3a507 | 2005-05-26 17:52:50 | [diff] [blame] | 127 | if (argc < 2) |
| Junio C Hamano | 0f2303f | 2005-04-16 20:57:39 | [diff] [blame] | 128 | usage(ls_tree_usage); |
| Linus Torvalds | 3c249c9 | 2005-05-01 23:36:56 | [diff] [blame] | 129 | if (get_sha1(argv[1], sha1) < 0) |
| Junio C Hamano | 0f2303f | 2005-04-16 20:57:39 | [diff] [blame] | 130 | usage(ls_tree_usage); |
| Junio C Hamano | 6af1f01 | 2005-05-28 07:05:38 | [diff] [blame] | 131 | |
| Linus Torvalds | e246637 | 2005-11-28 06:48:08 | [diff] [blame] | 132 | pathspec = get_pathspec(prefix, argv + 2); |
| Daniel Barkalow | 521698b | 2006-01-26 06:13:36 | [diff] [blame] | 133 | tree = parse_tree_indirect(sha1); |
| 134 | if (!tree) |
| Linus Torvalds | 3c5e846 | 2005-11-26 17:38:20 | [diff] [blame] | 135 | die("not a tree object"); |
| Daniel Barkalow | 521698b | 2006-01-26 06:13:36 | [diff] [blame] | 136 | read_tree_recursive(tree, "", 0, 0, pathspec, show_tree); |
| Linus Torvalds | 3c5e846 | 2005-11-26 17:38:20 | [diff] [blame] | 137 | |
| Petr Baudis | 7912c07 | 2005-04-13 09:02:34 | [diff] [blame] | 138 | return 0; |
| 139 | } |