git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 18/21] list-files: add -F/--classify

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Jan 25, 2015, 12:37 UTC
Message-ID
<1422189476-7518-19-git-send-email-pclouds@gmail.com>
In-Reply-To
<1422189476-7518-1-git-send-email-pclouds@gmail.com>

This appends an indicator after the file name if it's executable, a directory and so on, like in GNU ls. In fact append_indicator() is a rewrite from get_type_indicator() in coreutils.git commit 7326d1f1a67edf21947ae98194f98c38b6e9e527.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/git-list-files.txt |  6 ++++++
 builtin/ls-files.c               | 31 +++++++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
diff --git a/Documentation/git-list-files.txt b/Documentation/git-list-files.txt
index 9d4c127..475c6da 100644
--- a/Documentation/git-list-files.txt
+++ b/Documentation/git-list-files.txt
@@ -52,6 +52,12 @@ OPTIONS
 	multiple file selections. See linkgit::git-ls-files[1] option
 	`-t` for more information.
 
+-F::
+--classify::
+	Append indicator (one of `*/=>@|`, which is executable,
+	directory, socket, Solaris door, symlink, or fifo
+	respectively) to entries.
+
 -R::
 --recursive::
 	Equivalent of `--max-depth=-1` (infinite recursion).
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index 29b5c2e..bbeb4ce 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -29,6 +29,7 @@ static int show_killed;
 static int show_valid_bit;
 static int show_tag;
 static int show_dirs;
+static int show_indicator;
 static int line_terminator = '\n';
 static int debug_mode;
 static int use_color;
@@ -77,6 +78,28 @@ static void write_name(struct strbuf *sb, const char *name)
 		quote_path_relative(name, real_prefix, sb);
 }
 
+static void append_indicator(struct strbuf *sb, mode_t mode)
+{
+	char c = 0;
+	if (S_ISREG(mode)) {
+		if (mode & (S_IXUSR | S_IXGRP | S_IXOTH))
+			c = '*';
+	} else if (S_ISDIR(mode))
+		c = '/';
+	else if (S_ISLNK(mode))
+		c = '@';
+	else if (S_ISFIFO(mode))
+		c = '|';
+	else if (S_ISSOCK(mode))
+		c = '=';
+#ifdef S_ISDOOR
+	else if (S_ISDOOR(mode))
+		c = '>';
+#endif
+	if (c)
+		strbuf_addch(sb, c);
+}
+
 static void strbuf_fputs(struct strbuf *sb, const char *full_name, FILE *fp)
 {
 	if (column_active(colopts) || porcelain) {
@@ -99,6 +122,8 @@ static void write_dir_entry(struct strbuf *sb, const struct dir_entry *ent)
 		color_filename(sb, ent->name, quoted.buf, st.st_mode, 1);
 	else
 		strbuf_addbuf(sb, &quoted);
+	if (show_indicator && st.st_mode)
+		append_indicator(sb, st.st_mode);
 	strbuf_addch(sb, line_terminator);
 	strbuf_release(&quoted);
 }
@@ -202,6 +227,8 @@ static int show_as_directory(const struct cache_entry *ce)
 		}
 		if (show_tag)
 			strbuf_insert(&sb2, 0, tag_cached, strlen(tag_cached));
+		if (show_indicator)
+			append_indicator(&sb2, S_IFDIR);
 		strbuf_fputs(&sb2, strbuf_detach(&sb, NULL), NULL);
 		strbuf_release(&sb2);
 		return 1;
@@ -218,6 +245,8 @@ static void write_ce_name(struct strbuf *sb, const struct cache_entry *ce)
 		color_filename(sb, ce->name, quoted.buf, ce->ce_mode, 1);
 	else
 		strbuf_addbuf(sb, &quoted);
+	if (show_indicator)
+		append_indicator(sb, ce->ce_mode);
 	strbuf_addch(sb, line_terminator);
 	strbuf_release(&quoted);
 }
@@ -706,6 +735,8 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 			DIR_SHOW_IGNORED),
 		OPT_BOOL('u', "unmerged", &show_unmerged,
 			N_("show unmerged files")),
+		OPT_BOOL('F', "classify", &show_indicator,
+			 N_("append indicator (one of */=>@|) to entries")),
 		OPT__COLOR(&use_color, N_("show color")),
 		OPT_COLUMN(0, "column", &colopts, N_("show files in columns")),
 		OPT_SET_INT('1', NULL, &colopts,
-- 
2.2.0.84.ge9c7a8a
Previous: Junio C HamanoNext: Nguyễn Thái Ngọc Duy
Message 22 of 30 in “nd/list-files updates”
  1. 00/21 nd/list-files updatesNguyễn Thái Ngọc Duy, Jan 25, 2015
  2. 01/21 ls_colors.c: add $LS_COLORS parsing codeNguyễn Thái Ngọc Duy, Jan 25, 2015
  3. 02/21 ls_colors.c: parse color.ls.* from config fileNguyễn Thái Ngọc Duy, Jan 25, 2015
  4. 03/21 ls_colors.c: add a function to color a file nameNguyễn Thái Ngọc Duy, Jan 25, 2015
  5. 04/21 ls_colors.c: highlight submodules like directoriesNguyễn Thái Ngọc Duy, Jan 25, 2015
  6. 05/21 ls-files: buffer full item in strbuf before printingNguyễn Thái Ngọc Duy, Jan 25, 2015
  7. 06/21 ls-files: add --color to highlight file namesNguyễn Thái Ngọc Duy, Jan 25, 2015
  8. 07/21 ls-files: add --columnNguyễn Thái Ngọc Duy, Jan 25, 2015
  9. 08/21 ls-files: support --max-depthNguyễn Thái Ngọc Duy, Jan 25, 2015
  10. 09/21 list-files: a user friendly version of ls-files and moreNguyễn Thái Ngọc Duy, Jan 25, 2015
  11. Junio C HamanoJan 27, 2015
  12. 10/21 list-files: make alias 'ls' default to 'list-files'Nguyễn Thái Ngọc Duy, Jan 25, 2015
  13. 11/21 list-files: -u does not imply showing stagesNguyễn Thái Ngọc Duy, Jan 25, 2015
  14. 12/21 list-files: add -R/--recursive short for --max-depth=-1Nguyễn Thái Ngọc Duy, Jan 25, 2015
  15. 13/21 list-files: add -1 short for --no-columnNguyễn Thái Ngọc Duy, Jan 25, 2015
  16. 14/21 list-files: add -t backNguyễn Thái Ngọc Duy, Jan 25, 2015
  17. 15/21 list-files: sort output and remove duplicatesNguyễn Thái Ngọc Duy, Jan 25, 2015
  18. 16/21 list-files: do not show duplicate cached entriesNguyễn Thái Ngọc Duy, Jan 25, 2015
  19. 17/21 list-files: show directories as well as filesNguyễn Thái Ngọc Duy, Jan 25, 2015
  20. Eric SunshineJan 25, 2015
  21. Junio C HamanoJan 27, 2015
  22. 18/21 list-files: add -F/--classifyNguyễn Thái Ngọc Duy, Jan 25, 2015
  23. 19/21 list-files -F: show submodules with the new indicator '&'Nguyễn Thái Ngọc Duy, Jan 25, 2015
  24. 20/21 list-files: -M aka diff-cachedNguyễn Thái Ngọc Duy, Jan 25, 2015
  25. 21/21 t3080: tests for git-list-filesNguyễn Thái Ngọc Duy, Jan 25, 2015
  26. Eric SunshineJan 25, 2015
  27. Michael BlumeJan 28, 2015
  28. Duy NguyenJan 28, 2015
  29. Michael BlumeJan 28, 2015
  30. Junio C HamanoJan 28, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.