blob: 7abe333ce99a3448373119c1a811341cb15f1d10 [file] [log] [blame]
Petr Baudis7912c072005-04-13 02:02:34 -07001/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
Junio C Hamano6af1f012005-05-28 00:05:38 -07007#include "blob.h"
8#include "tree.h"
Linus Torvaldsf35a6d32007-04-09 21:20:29 -07009#include "commit.h"
Junio C Hamano22ddf712005-10-14 21:56:46 -070010#include "quote.h"
Peter Eriksenaae01bd2006-05-23 14:15:30 +020011#include "builtin.h"
Petr Baudis7912c072005-04-13 02:02:34 -070012
Linus Torvaldse99d59f2005-05-20 11:46:10 -070013static int line_termination = '\n';
Junio C Hamano6af1f012005-05-28 00:05:38 -070014#define LS_RECURSIVE 1
15#define LS_TREE_ONLY 2
Linus Torvalds0f8f45c2005-12-01 10:35:51 -080016#define LS_SHOW_TREES 4
Junio C Hamanoc639a552005-12-01 14:54:00 -080017#define LS_NAME_ONLY 8
Jakub Narebskia5bbda82007-05-19 22:08:11 +020018#define LS_SHOW_SIZE 16
David Rientjes96f1e582006-08-15 10:23:48 -070019static int abbrev;
20static int ls_options;
Peter Eriksenaae01bd2006-05-23 14:15:30 +020021static const char **pathspec;
David Rientjes96f1e582006-08-15 10:23:48 -070022static int chomp_prefix;
Linus Torvaldsa633fca2006-07-28 22:44:25 -070023static const char *ls_tree_prefix;
Junio C Hamanoaa1c48d2005-04-15 08:37:05 -070024
Petr Baudis4d1f1192005-07-29 11:01:26 +020025static const char ls_tree_usage[] =
Jakub Narebskia5bbda82007-05-19 22:08:11 +020026 "git-ls-tree [-d] [-r] [-t] [-l] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]";
Linus Torvalds0f8f45c2005-12-01 10:35:51 -080027
28static int show_recursive(const char *base, int baselen, const char *pathname)
29{
30 const char **s;
31
32 if (ls_options & LS_RECURSIVE)
33 return 1;
34
35 s = pathspec;
36 if (!s)
37 return 0;
38
39 for (;;) {
40 const char *spec = *s++;
41 int len, speclen;
42
43 if (!spec)
44 return 0;
45 if (strncmp(base, spec, baselen))
46 continue;
47 len = strlen(pathname);
48 spec += baselen;
49 speclen = strlen(spec);
50 if (speclen <= len)
51 continue;
52 if (memcmp(pathname, spec, len))
53 continue;
54 return 1;
55 }
56}
Junio C Hamanoaa1c48d2005-04-15 08:37:05 -070057
Linus Torvalds097dc3d2006-05-28 15:13:53 -070058static int show_tree(const unsigned char *sha1, const char *base, int baselen,
Junio C Hamanoa69dd582005-12-23 13:39:30 -080059 const char *pathname, unsigned mode, int stage)
Petr Baudis7912c072005-04-13 02:02:34 -070060{
Linus Torvalds0f8f45c2005-12-01 10:35:51 -080061 int retval = 0;
Peter Eriksen8e440252006-04-02 14:44:09 +020062 const char *type = blob_type;
Jakub Narebskia5bbda82007-05-19 22:08:11 +020063 unsigned long size;
Petr Baudis7912c072005-04-13 02:02:34 -070064
Martin Waitz302b9282007-05-21 22:08:28 +020065 if (S_ISGITLINK(mode)) {
Linus Torvaldsf35a6d32007-04-09 21:20:29 -070066 /*
67 * Maybe we want to have some recursive version here?
68 *
69 * Something like:
70 *
71 if (show_subprojects(base, baselen, pathname)) {
72 if (fork()) {
73 chdir(base);
74 exec ls-tree;
75 }
76 waitpid();
77 }
78 *
79 * ..or similar..
80 */
81 type = commit_type;
82 } else if (S_ISDIR(mode)) {
Linus Torvalds0f8f45c2005-12-01 10:35:51 -080083 if (show_recursive(base, baselen, pathname)) {
84 retval = READ_TREE_RECURSIVE;
85 if (!(ls_options & LS_SHOW_TREES))
86 return retval;
Linus Torvaldse2466372005-11-27 22:48:08 -080087 }
Peter Eriksen8e440252006-04-02 14:44:09 +020088 type = tree_type;
Linus Torvalds3c5e8462005-11-26 09:38:20 -080089 }
Junio C Hamanof5984672005-12-01 13:15:20 -080090 else if (ls_options & LS_TREE_ONLY)
91 return 0;
Linus Torvalds3c5e8462005-11-26 09:38:20 -080092
Junio C Hamanoa69dd582005-12-23 13:39:30 -080093 if (chomp_prefix &&
Linus Torvaldsa633fca2006-07-28 22:44:25 -070094 (baselen < chomp_prefix || memcmp(ls_tree_prefix, base, chomp_prefix)))
Junio C Hamanoa69dd582005-12-23 13:39:30 -080095 return 0;
96
Jakub Narebskia5bbda82007-05-19 22:08:11 +020097 if (!(ls_options & LS_NAME_ONLY)) {
98 if (ls_options & LS_SHOW_SIZE) {
99 if (!strcmp(type, blob_type)) {
100 sha1_object_info(sha1, &size);
101 printf("%06o %s %s %7lu\t", mode, type,
102 abbrev ? find_unique_abbrev(sha1, abbrev)
103 : sha1_to_hex(sha1),
104 size);
105 } else
106 printf("%06o %s %s %7c\t", mode, type,
107 abbrev ? find_unique_abbrev(sha1, abbrev)
108 : sha1_to_hex(sha1),
109 '-');
110 } else
111 printf("%06o %s %s\t", mode, type,
112 abbrev ? find_unique_abbrev(sha1, abbrev)
113 : sha1_to_hex(sha1));
114 }
Pierre Habouzit663af342007-09-20 00:42:15 +0200115 write_name_quotedpfx(base + chomp_prefix, baselen - chomp_prefix,
116 pathname, stdout, line_termination);
Linus Torvalds0f8f45c2005-12-01 10:35:51 -0800117 return retval;
Linus Torvalds3c5e8462005-11-26 09:38:20 -0800118}
119
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700120int cmd_ls_tree(int argc, const char **argv, const char *prefix)
Linus Torvalds3c5e8462005-11-26 09:38:20 -0800121{
Linus Torvalds3c5e8462005-11-26 09:38:20 -0800122 unsigned char sha1[20];
Daniel Barkalow521698b2006-01-26 01:13:36 -0500123 struct tree *tree;
Linus Torvalds3c5e8462005-11-26 09:38:20 -0800124
Junio C Hamano84a9b582006-03-23 23:41:18 -0800125 git_config(git_default_config);
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700126 ls_tree_prefix = prefix;
Junio C Hamanoa69dd582005-12-23 13:39:30 -0800127 if (prefix && *prefix)
128 chomp_prefix = strlen(prefix);
Junio C Hamanoaa1c48d2005-04-15 08:37:05 -0700129 while (1 < argc && argv[1][0] == '-') {
130 switch (argv[1][1]) {
131 case 'z':
132 line_termination = 0;
133 break;
134 case 'r':
Junio C Hamano6af1f012005-05-28 00:05:38 -0700135 ls_options |= LS_RECURSIVE;
136 break;
137 case 'd':
138 ls_options |= LS_TREE_ONLY;
Junio C Hamanoaa1c48d2005-04-15 08:37:05 -0700139 break;
Linus Torvalds0f8f45c2005-12-01 10:35:51 -0800140 case 't':
141 ls_options |= LS_SHOW_TREES;
142 break;
Jakub Narebskia5bbda82007-05-19 22:08:11 +0200143 case 'l':
144 ls_options |= LS_SHOW_SIZE;
145 break;
Junio C Hamanoc639a552005-12-01 14:54:00 -0800146 case '-':
147 if (!strcmp(argv[1]+2, "name-only") ||
148 !strcmp(argv[1]+2, "name-status")) {
149 ls_options |= LS_NAME_ONLY;
150 break;
151 }
Jakub Narebskia5bbda82007-05-19 22:08:11 +0200152 if (!strcmp(argv[1]+2, "long")) {
153 ls_options |= LS_SHOW_SIZE;
154 break;
155 }
Junio C Hamanoa69dd582005-12-23 13:39:30 -0800156 if (!strcmp(argv[1]+2, "full-name")) {
157 chomp_prefix = 0;
158 break;
159 }
Junio C Hamano1968d772007-02-20 01:55:07 -0800160 if (!prefixcmp(argv[1]+2, "abbrev=")) {
Eric Wongcb85bfe2006-03-07 05:52:02 -0800161 abbrev = strtoul(argv[1]+9, NULL, 10);
162 if (abbrev && abbrev < MINIMUM_ABBREV)
163 abbrev = MINIMUM_ABBREV;
164 else if (abbrev > 40)
165 abbrev = 40;
166 break;
167 }
168 if (!strcmp(argv[1]+2, "abbrev")) {
169 abbrev = DEFAULT_ABBREV;
170 break;
171 }
Junio C Hamanoc639a552005-12-01 14:54:00 -0800172 /* otherwise fallthru */
Junio C Hamanoaa1c48d2005-04-15 08:37:05 -0700173 default:
Junio C Hamano0f2303f2005-04-16 13:57:39 -0700174 usage(ls_tree_usage);
Junio C Hamanoaa1c48d2005-04-15 08:37:05 -0700175 }
176 argc--; argv++;
177 }
Junio C Hamanof5984672005-12-01 13:15:20 -0800178 /* -d -r should imply -t, but -d by itself should not have to. */
179 if ( (LS_TREE_ONLY|LS_RECURSIVE) ==
180 ((LS_TREE_ONLY|LS_RECURSIVE) & ls_options))
181 ls_options |= LS_SHOW_TREES;
Junio C Hamanoaa1c48d2005-04-15 08:37:05 -0700182
Jason McMullan6d3a5072005-05-26 10:52:50 -0700183 if (argc < 2)
Junio C Hamano0f2303f2005-04-16 13:57:39 -0700184 usage(ls_tree_usage);
Dmitry V. Levin31fff302006-05-09 01:43:38 +0400185 if (get_sha1(argv[1], sha1))
186 die("Not a valid object name %s", argv[1]);
Junio C Hamano6af1f012005-05-28 00:05:38 -0700187
Linus Torvaldse2466372005-11-27 22:48:08 -0800188 pathspec = get_pathspec(prefix, argv + 2);
Daniel Barkalow521698b2006-01-26 01:13:36 -0500189 tree = parse_tree_indirect(sha1);
190 if (!tree)
Linus Torvalds3c5e8462005-11-26 09:38:20 -0800191 die("not a tree object");
Daniel Barkalow521698b2006-01-26 01:13:36 -0500192 read_tree_recursive(tree, "", 0, 0, pathspec, show_tree);
Linus Torvalds3c5e8462005-11-26 09:38:20 -0800193
Petr Baudis7912c072005-04-13 02:02:34 -0700194 return 0;
195}