🌐 AI搜索 & 代理 主页
blob: d005643ee08e03be2309c02e72522c6a81e8a1c6 [file] [log] [blame]
Petr Baudis7912c072005-04-13 09:02:341/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
Junio C Hamano6af1f012005-05-28 07:05:387#include "blob.h"
8#include "tree.h"
Junio C Hamano22ddf712005-10-15 04:56:469#include "quote.h"
Petr Baudis7912c072005-04-13 09:02:3410
Linus Torvaldse99d59f2005-05-20 18:46:1011static int line_termination = '\n';
Junio C Hamano6af1f012005-05-28 07:05:3812#define LS_RECURSIVE 1
13#define LS_TREE_ONLY 2
Linus Torvalds0f8f45c2005-12-01 18:35:5114#define LS_SHOW_TREES 4
Junio C Hamanoc639a552005-12-01 22:54:0015#define LS_NAME_ONLY 8
Linus Torvaldse2466372005-11-28 06:48:0816static int ls_options = 0;
17const char **pathspec;
Junio C Hamanoa69dd582005-12-23 21:39:3018static int chomp_prefix = 0;
19static const char *prefix;
Junio C Hamanoaa1c48d2005-04-15 15:37:0520
Petr Baudis4d1f1192005-07-29 09:01:2621static const char ls_tree_usage[] =
Junio C Hamanoa69dd582005-12-23 21:39:3022 "git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] <tree-ish> [path...]";
Linus Torvalds0f8f45c2005-12-01 18:35:5123
24static 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 Hamanoaa1c48d2005-04-15 15:37:0553
Junio C Hamanoa69dd582005-12-23 21:39:3054static int show_tree(unsigned char *sha1, const char *base, int baselen,
55 const char *pathname, unsigned mode, int stage)
Petr Baudis7912c072005-04-13 09:02:3456{
Linus Torvalds0f8f45c2005-12-01 18:35:5157 int retval = 0;
Linus Torvalds3c5e8462005-11-26 17:38:2058 const char *type = "blob";
Petr Baudis7912c072005-04-13 09:02:3459
Linus Torvalds3c5e8462005-11-26 17:38:2060 if (S_ISDIR(mode)) {
Linus Torvalds0f8f45c2005-12-01 18:35:5161 if (show_recursive(base, baselen, pathname)) {
62 retval = READ_TREE_RECURSIVE;
63 if (!(ls_options & LS_SHOW_TREES))
64 return retval;
Linus Torvaldse2466372005-11-28 06:48:0865 }
Linus Torvaldsb45c5692005-11-27 19:00:0966 type = "tree";
Linus Torvalds3c5e8462005-11-26 17:38:2067 }
Junio C Hamanof5984672005-12-01 21:15:2068 else if (ls_options & LS_TREE_ONLY)
69 return 0;
Linus Torvalds3c5e8462005-11-26 17:38:2070
Junio C Hamanoa69dd582005-12-23 21:39:3071 if (chomp_prefix &&
72 (baselen < chomp_prefix || memcmp(prefix, base, chomp_prefix)))
73 return 0;
74
Junio C Hamanoc639a552005-12-01 22:54:0075 if (!(ls_options & LS_NAME_ONLY))
76 printf("%06o %s %s\t", mode, type, sha1_to_hex(sha1));
Junio C Hamanoa69dd582005-12-23 21:39:3077 write_name_quoted(base + chomp_prefix, baselen - chomp_prefix,
78 pathname,
79 line_termination, stdout);
Junio C Hamano32b59042005-11-28 10:30:0480 putchar(line_termination);
Linus Torvalds0f8f45c2005-12-01 18:35:5181 return retval;
Linus Torvalds3c5e8462005-11-26 17:38:2082}
83
84int main(int argc, const char **argv)
85{
Linus Torvalds3c5e8462005-11-26 17:38:2086 unsigned char sha1[20];
Daniel Barkalow521698b2006-01-26 06:13:3687 struct tree *tree;
Linus Torvalds3c5e8462005-11-26 17:38:2088
89 prefix = setup_git_directory();
Junio C Hamanoa69dd582005-12-23 21:39:3090 if (prefix && *prefix)
91 chomp_prefix = strlen(prefix);
Junio C Hamanoaa1c48d2005-04-15 15:37:0592 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 Hamano6af1f012005-05-28 07:05:3898 ls_options |= LS_RECURSIVE;
99 break;
100 case 'd':
101 ls_options |= LS_TREE_ONLY;
Junio C Hamanoaa1c48d2005-04-15 15:37:05102 break;
Linus Torvalds0f8f45c2005-12-01 18:35:51103 case 't':
104 ls_options |= LS_SHOW_TREES;
105 break;
Junio C Hamanoc639a552005-12-01 22:54:00106 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 Hamanoa69dd582005-12-23 21:39:30112 if (!strcmp(argv[1]+2, "full-name")) {
113 chomp_prefix = 0;
114 break;
115 }
Junio C Hamanoc639a552005-12-01 22:54:00116 /* otherwise fallthru */
Junio C Hamanoaa1c48d2005-04-15 15:37:05117 default:
Junio C Hamano0f2303f2005-04-16 20:57:39118 usage(ls_tree_usage);
Junio C Hamanoaa1c48d2005-04-15 15:37:05119 }
120 argc--; argv++;
121 }
Junio C Hamanof5984672005-12-01 21:15:20122 /* -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 Hamanoaa1c48d2005-04-15 15:37:05126
Jason McMullan6d3a5072005-05-26 17:52:50127 if (argc < 2)
Junio C Hamano0f2303f2005-04-16 20:57:39128 usage(ls_tree_usage);
Linus Torvalds3c249c92005-05-01 23:36:56129 if (get_sha1(argv[1], sha1) < 0)
Junio C Hamano0f2303f2005-04-16 20:57:39130 usage(ls_tree_usage);
Junio C Hamano6af1f012005-05-28 07:05:38131
Linus Torvaldse2466372005-11-28 06:48:08132 pathspec = get_pathspec(prefix, argv + 2);
Daniel Barkalow521698b2006-01-26 06:13:36133 tree = parse_tree_indirect(sha1);
134 if (!tree)
Linus Torvalds3c5e8462005-11-26 17:38:20135 die("not a tree object");
Daniel Barkalow521698b2006-01-26 06:13:36136 read_tree_recursive(tree, "", 0, 0, pathspec, show_tree);
Linus Torvalds3c5e8462005-11-26 17:38:20137
Petr Baudis7912c072005-04-13 09:02:34138 return 0;
139}