ls-files.c 10.2 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * This merges the file listing in the directory cache index
 * with the actual working directory list, and shows different
 * combinations of the two.
 *
 * Copyright (C) Linus Torvalds, 2005
 */
#include <dirent.h>
9
#include <fnmatch.h>
10 11 12 13 14 15 16

#include "cache.h"

static int show_deleted = 0;
static int show_cached = 0;
static int show_others = 0;
static int show_ignored = 0;
17
static int show_stage = 0;
18
static int show_unmerged = 0;
19
static int show_killed = 0;
20
static int line_terminator = '\n';
21

22 23 24 25
static const char *tag_cached = "";
static const char *tag_unmerged = "";
static const char *tag_removed = "";
static const char *tag_other = "";
26
static const char *tag_killed = "";
27

28
static char *exclude_per_dir = NULL;
29 30
static int nr_excludes;
static int excludes_alloc;
31 32 33 34 35
static struct exclude {
	const char *pattern;
	const char *base;
	int baselen;
} **excludes;
36

37
static void add_exclude(const char *string, const char *base, int baselen)
38
{
39 40 41 42 43
	struct exclude *x = xmalloc(sizeof (*x));

	x->pattern = string;
	x->base = base;
	x->baselen = baselen;
44 45 46 47
	if (nr_excludes == excludes_alloc) {
		excludes_alloc = alloc_nr(excludes_alloc);
		excludes = realloc(excludes, excludes_alloc*sizeof(char *));
	}
48
	excludes[nr_excludes++] = x;
49 50
}

51 52
static int add_excludes_from_file_1(const char *fname,
				    const char *base, int baselen)
53 54 55 56 57 58 59 60 61 62 63 64 65 66
{
	int fd, i;
	long size;
	char *buf, *entry;

	fd = open(fname, O_RDONLY);
	if (fd < 0)
		goto err;
	size = lseek(fd, 0, SEEK_END);
	if (size < 0)
		goto err;
	lseek(fd, 0, SEEK_SET);
	if (size == 0) {
		close(fd);
67
		return 0;
68 69 70 71 72 73 74 75 76
	}
	buf = xmalloc(size);
	if (read(fd, buf, size) != size)
		goto err;
	close(fd);

	entry = buf;
	for (i = 0; i < size; i++) {
		if (buf[i] == '\n') {
77
			if (entry != buf + i && entry[0] != '#') {
78
				buf[i] = 0;
79
				add_exclude(entry, base, baselen);
80 81 82 83
			}
			entry = buf + i + 1;
		}
	}
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	return 0;

 err:
	if (0 <= fd)
		close(fd);
	return -1;
}

static void add_excludes_from_file(const char *fname)
{
	if (add_excludes_from_file_1(fname, "", 0) < 0)
		die("cannot use %s as an exclude file", fname);
}

static int push_exclude_per_directory(const char *base, int baselen)
{
	char exclude_file[PATH_MAX];
	int current_nr = nr_excludes;

	if (exclude_per_dir) {
		memcpy(exclude_file, base, baselen);
		strcpy(exclude_file + baselen, exclude_per_dir);
		add_excludes_from_file_1(exclude_file, base, baselen);
	}
	return current_nr;
}
110

111 112 113 114
static void pop_exclude_per_directory(int stk)
{
	while (stk < nr_excludes)
		free(excludes[--nr_excludes]);
115 116 117 118 119
}

static int excluded(const char *pathname)
{
	int i;
120

121
	if (nr_excludes) {
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
		int pathlen = strlen(pathname);

		for (i = 0; i < nr_excludes; i++) {
			struct exclude *x = excludes[i];
			const char *exclude = x->pattern;
			int to_exclude = 1;

			if (*exclude == '!') {
				to_exclude = 0;
				exclude++;
			}

			if (!strchr(exclude, '/')) {
				/* match basename */
				const char *basename = strrchr(pathname, '/');
				basename = (basename) ? basename+1 : pathname;
				if (fnmatch(exclude, basename, 0) == 0)
					return to_exclude;
			}
			else {
				/* match with FNM_PATHNAME:
				 * exclude has base (baselen long) inplicitly
				 * in front of it.
				 */
				int baselen = x->baselen;
				if (*exclude == '/')
					exclude++;

				if (pathlen < baselen ||
				    (baselen && pathname[baselen-1] != '/') ||
				    strncmp(pathname, x->base, baselen))
				    continue;

				if (fnmatch(exclude, pathname+baselen,
					    FNM_PATHNAME) == 0)
					return to_exclude;
			}
		}
160 161 162 163
	}
	return 0;
}

164 165 166 167 168 169
struct nond_on_fs {
	int len;
	char name[0];
};

static struct nond_on_fs **dir;
170 171 172 173 174
static int nr_dir;
static int dir_alloc;

static void add_name(const char *pathname, int len)
{
175
	struct nond_on_fs *ent;
176 177 178 179 180 181

	if (cache_name_pos(pathname, len) >= 0)
		return;

	if (nr_dir == dir_alloc) {
		dir_alloc = alloc_nr(dir_alloc);
182
		dir = xrealloc(dir, dir_alloc*sizeof(ent));
183
	}
184 185 186 187
	ent = xmalloc(sizeof(*ent) + len + 1);
	ent->len = len;
	memcpy(ent->name, pathname, len);
	dir[nr_dir++] = ent;
188 189 190 191
}

/*
 * Read a directory tree. We currently ignore anything but
192 193 194
 * directories, regular files and symlinks. That's because git
 * doesn't handle them at all yet. Maybe that will change some
 * day.
195
 *
196
 * Also, we ignore the name ".git" (even if it is not a directory).
I
Ingo Molnar 已提交
197
 * That likely will not change.
198 199 200 201 202 203
 */
static void read_directory(const char *path, const char *base, int baselen)
{
	DIR *dir = opendir(path);

	if (dir) {
204
		int exclude_stk;
205 206 207 208
		struct dirent *de;
		char fullname[MAXPATHLEN + 1];
		memcpy(fullname, base, baselen);

209 210
		exclude_stk = push_exclude_per_directory(base, baselen);

211 212 213
		while ((de = readdir(dir)) != NULL) {
			int len;

214 215 216 217
			if ((de->d_name[0] == '.') &&
			    (de->d_name[1] == 0 ||
			     !strcmp(de->d_name + 1, ".") ||
			     !strcmp(de->d_name + 1, "git")))
218 219 220
				continue;
			len = strlen(de->d_name);
			memcpy(fullname + baselen, de->d_name, len+1);
221 222
			if (excluded(fullname) != show_ignored)
				continue;
223

224
			switch (DTYPE(de)) {
225 226 227 228 229 230
			struct stat st;
			default:
				continue;
			case DT_UNKNOWN:
				if (lstat(fullname, &st))
					continue;
231
				if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))
232 233 234 235 236 237
					break;
				if (!S_ISDIR(st.st_mode))
					continue;
				/* fallthrough */
			case DT_DIR:
				memcpy(fullname + baselen + len, "/", 2);
238 239
				read_directory(fullname, fullname,
					       baselen + len + 1);
240 241
				continue;
			case DT_REG:
242
			case DT_LNK:
243 244 245 246 247
				break;
			}
			add_name(fullname, baselen + len);
		}
		closedir(dir);
248 249

		pop_exclude_per_directory(exclude_stk);
250 251 252 253 254
	}
}

static int cmp_name(const void *p1, const void *p2)
{
255 256 257 258 259 260
	const struct nond_on_fs *e1 = *(const struct nond_on_fs **)p1;
	const struct nond_on_fs *e2 = *(const struct nond_on_fs **)p2;

	return cache_name_compare(e1->name, e1->len,
				  e2->name, e2->len);
}
261

L
Linus Torvalds 已提交
262
static void show_killed_files(void)
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
{
	int i;
	for (i = 0; i < nr_dir; i++) {
		struct nond_on_fs *ent = dir[i];
		char *cp, *sp;
		int pos, len, killed = 0;

		for (cp = ent->name; cp - ent->name < ent->len; cp = sp + 1) {
			sp = strchr(cp, '/');
			if (!sp) {
				/* If ent->name is prefix of an entry in the
				 * cache, it will be killed.
				 */
				pos = cache_name_pos(ent->name, ent->len);
				if (0 <= pos)
					die("bug in show-killed-files");
				pos = -pos - 1;
				while (pos < active_nr &&
				       ce_stage(active_cache[pos]))
					pos++; /* skip unmerged */
				if (active_nr <= pos)
					break;
				/* pos points at a name immediately after
				 * ent->name in the cache.  Does it expect
				 * ent->name to be a directory?
				 */
				len = ce_namelen(active_cache[pos]);
				if ((ent->len < len) &&
				    !strncmp(active_cache[pos]->name,
					     ent->name, ent->len) &&
				    active_cache[pos]->name[ent->len] == '/')
					killed = 1;
				break;
			}
			if (0 <= cache_name_pos(ent->name, sp - ent->name)) {
				/* If any of the leading directories in
				 * ent->name is registered in the cache,
				 * ent->name will be killed.
				 */
				killed = 1;
				break;
			}
		}
		if (killed)
			printf("%s%.*s%c", tag_killed,
			       dir[i]->len, dir[i]->name,
			       line_terminator);
	}
311 312 313 314 315 316 317
}

static void show_files(void)
{
	int i;

	/* For cached/deleted files we don't need to even do the readdir */
318
	if (show_others || show_killed) {
319
		read_directory(".", "", 0);
320 321 322 323 324 325 326 327
		qsort(dir, nr_dir, sizeof(struct nond_on_fs *), cmp_name);
		if (show_others)
			for (i = 0; i < nr_dir; i++)
				printf("%s%.*s%c", tag_other,
				       dir[i]->len, dir[i]->name,
				       line_terminator);
		if (show_killed)
			show_killed_files();
328
	}
329
	if (show_cached | show_stage) {
330 331
		for (i = 0; i < active_nr; i++) {
			struct cache_entry *ce = active_cache[i];
332 333
			if (excluded(ce->name) != show_ignored)
				continue;
334 335
			if (show_unmerged && !ce_stage(ce))
				continue;
336
			if (!show_stage)
337 338 339 340
				printf("%s%s%c",
				       ce_stage(ce) ? tag_unmerged :
				       tag_cached,
				       ce->name, line_terminator);
341
			else
342
				printf("%s%06o %s %d\t%s%c",
343 344
				       ce_stage(ce) ? tag_unmerged :
				       tag_cached,
345 346 347 348
				       ntohl(ce->ce_mode),
				       sha1_to_hex(ce->sha1),
				       ce_stage(ce),
				       ce->name, line_terminator); 
349 350 351 352 353 354
		}
	}
	if (show_deleted) {
		for (i = 0; i < active_nr; i++) {
			struct cache_entry *ce = active_cache[i];
			struct stat st;
355 356
			if (excluded(ce->name) != show_ignored)
				continue;
357
			if (!lstat(ce->name, &st))
358
				continue;
359 360
			printf("%s%s%c", tag_removed, ce->name,
			       line_terminator);
361 362 363 364
		}
	}
}

365
static const char *ls_files_usage =
366
	"git-ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged|killed])* "
367 368 369
	"[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] "
	"[ --exclude-per-directory=<filename> ]";
;
370

371 372 373 374 375 376 377
int main(int argc, char **argv)
{
	int i;

	for (i = 1; i < argc; i++) {
		char *arg = argv[i];

378 379
		if (!strcmp(arg, "-z")) {
			line_terminator = 0;
380 381 382 383 384
		} else if (!strcmp(arg, "-t")) {
			tag_cached = "H ";
			tag_unmerged = "M ";
			tag_removed = "R ";
			tag_other = "? ";
385
			tag_killed = "K ";
386
		} else if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
387
			show_cached = 1;
388
		} else if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
389
			show_deleted = 1;
390
		} else if (!strcmp(arg, "-o") || !strcmp(arg, "--others")) {
391
			show_others = 1;
392
		} else if (!strcmp(arg, "-i") || !strcmp(arg, "--ignored")) {
393
			show_ignored = 1;
394
		} else if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
395
			show_stage = 1;
396 397
		} else if (!strcmp(arg, "-k") || !strcmp(arg, "--killed")) {
			show_killed = 1;
398
		} else if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
399 400 401
			/* There's no point in showing unmerged unless
			 * you also show the stage information.
			 */
402 403
			show_stage = 1;
			show_unmerged = 1;
404
		} else if (!strcmp(arg, "-x") && i+1 < argc) {
405
			add_exclude(argv[++i], "", 0);
406
		} else if (!strncmp(arg, "--exclude=", 10)) {
407
			add_exclude(arg+10, "", 0);
408
		} else if (!strcmp(arg, "-X") && i+1 < argc) {
409
			add_excludes_from_file(argv[++i]);
410
		} else if (!strncmp(arg, "--exclude-from=", 15)) {
411
			add_excludes_from_file(arg+15);
412 413
		} else if (!strncmp(arg, "--exclude-per-directory=", 24)) {
			exclude_per_dir = arg + 24;
414
		} else
415
			usage(ls_files_usage);
416 417 418
	}

	if (show_ignored && !nr_excludes) {
419 420
		fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
			argv[0]);
421
		exit(1);
422 423 424
	}

	/* With no flags, we default to showing the cached files */
425
	if (!(show_stage | show_deleted | show_others | show_unmerged | show_killed))
426 427 428 429 430 431
		show_cached = 1;

	read_cache();
	show_files();
	return 0;
}