Subject: [PATCH 14/16] ls: immitate UNIX ls output style

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 9 Feb 2011 19:24:42 +0700

Two points:

 - When input is a mixed of directories and files, files will be
   grouped and displayed first. Directory content will follow.

 - GNU ls when examining a directory will output files relative to
   that directory.

Index does not have directories. There nearest thing to that is
pathspecs that expand to more than one item. So in Git the rules become:

 - Multiple match pathspecs is printed with a header, the pathspec,
   then the content (its expansion). If there is only one pathspec, no
   header will be printed.

 - The common directory prefix of all files from a pathspec will be
   stripped (not yet implemented)

 - One-match pathspecs will be grouped and printed out first like
   the result of a virtual multiple match pathspec.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 Mixing multiple entry types (others and cached) will result in
 duplicates and unsorted output. The command is a porcelain now,
 perhaps we should sort/uniq output?

 one_match_pathspec() needs better impl.

 builtin/ls-files.c |  101 +++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 96 insertions(+), 5 deletions(-)

diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index 87ee728..22e0c87 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -676,10 +676,46 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 	return 0;
 }
 
+static int one_match_pathspec(const char *pathspec)
+{
+	struct stat st;
+	int len = strlen(pathspec);
+
+	if (show_cached) {
+		char *new_path;
+		int pos = index_name_pos(&the_index, pathspec, len);
+		if (pos >= 0)
+			return 1;
+
+		new_path = xmalloc(len+2);
+		memcpy(new_path, pathspec, len);
+		new_path[len++] = '/';
+		new_path[len] = 0;
+		pos = index_name_pos(&the_index, pathspec, len);
+		if (pos >= 0)
+			die("BUG: Wait there are directories in index??");
+		pos = -pos-1;
+
+		/* dir match */
+		if (!strncmp(the_index.cache[pos]->name, new_path, len)) {
+			free(new_path);
+			return 0;
+		}
+		free(new_path);
+	}
+
+	if (!stat(pathspec, &st) && !S_ISDIR(st.st_mode))
+		return 1;
+
+	return 0;
+}
+
 int cmd_ls(int argc, const char **argv, const char *cmd_prefix)
 {
+	int show_pathspec;
+	const char **pathspecs, **one_matches = NULL;
 	struct dir_struct dir;
-	int recursive = 0;
+	int recursive = 0, src, dst, len;
 	struct option builtin_ls_files_options[] = {
 		OPT_BOOLEAN('c', "cached", &show_cached,
 			"show cached files in the output (default)"),
@@ -720,20 +756,75 @@ int cmd_ls(int argc, const char **argv, const char *cmd_prefix)
 	if (dir.flags & DIR_SHOW_IGNORED || show_others)
 		setup_standard_excludes(&dir);
 
-	pathspec = get_pathspec(prefix, argv);
+	pathspecs = get_pathspec(prefix, argv);
 
 	if (show_modified)
-		refresh_index(&the_index, REFRESH_QUIET, pathspec, NULL, NULL);
+		refresh_index(&the_index, REFRESH_QUIET, pathspecs, NULL, NULL);
+
+	if (!pathspecs) {
+		static const char *spec[2];
+		spec[0] = ".";
+		spec[1] = NULL;
+		pathspecs = get_pathspec(prefix, spec);
+	}
 
 	/* With no flags, we default to showing the cached files */
 	if (!(show_stage | show_deleted | show_others | show_unmerged |
 	      show_killed | show_modified | show_resolve_undo))
 		show_cached = 1;
 
+	/*
+	 * Group one-match pathspecs together. Shell expansion may
+	 * turn foo* to fooa foob/ and fooc. List fooa and fooc, leave
+	 * foob for later.
+	 */
+	len = src = dst = 0;
+	while (pathspecs[src]) {
+		if (one_match_pathspec(pathspecs[src])) {
+			len++;
+			one_matches = xrealloc(one_matches, sizeof(*one_matches) * (len+1));
+			one_matches[len - 1] = pathspecs[src];
+			src++;
+			continue;
+		}
+		if (src != dst)
+			pathspecs[dst] = pathspecs[src];
+		src++;
+		dst++;
+	}
+	pathspecs[dst] = NULL;
+
 	if (!recursive)
 		depth_limit = 1;
 
-	show_files(&dir);
-	display_columns(&output, column_mode, term_columns(), 2, NULL);
+	if (len) {
+		one_matches[len] = NULL;
+		pathspec = one_matches;
+		show_files(&dir);
+		display_columns(&output, column_mode, term_columns(), 2, NULL);
+		string_list_clear(&output, 0);
+		if (dst)
+			printf("\n");
+	}
+
+	if (!pathspecs[0])
+		return 0;
+
+	show_pathspec = dst > 1 || len;
+	pathspec = xmalloc(sizeof(*pathspec)*2);
+	pathspec[1] = NULL;
+	while (*pathspecs) {
+		pathspec[0] = pathspecs[0];
+		pathspecs++;
+
+		show_files(&dir);
+		if (output.nr && show_pathspec)
+			printf("%s:\n", pathspec[0]);
+
+		display_columns(&output, column_mode, term_columns(), 2, NULL);
+		string_list_clear(&output, 0);
+		if (show_pathspec && pathspecs[0])
+			printf("\n");
+	}
 	return 0;
 }
-- 
1.7.2.2