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

[PATCH] git-status: Show empty directories

From
Leila Muhtasib <muhtasib@gmail.com>
Date
Jun 9, 2012, 19:40 UTC
Message-ID
<1339270806-65013-1-git-send-email-muhtasib@gmail.com>

git-status now lists empty directories under the untracked header. Before this modification, git status did not list empty directories. The header changed from 'Untracked files' to instead display 'Untracked files and directories'. A helpful reminder is also added after empty directories indicating they cannot be added/staged if they are empty. git status -u is unchanged, and will still only show untracked files just as before. As a result, no need for documentation change.

Empty dirs are work in progress. They result because of one of the following: user forgot to add files to dir, user forgot to clean up dir, user under impression dir is staged and will be committed or is already committed. Last item might occur as some users setup project and dir structures before adding files.

So this patch servers as a helpful reminder to users that they have an empty dir so they can act upon it. Plus it clarifies git behavior to the user that empty dirs can't be tracked.

Signed-off-by: Leila Muhtasib <muhtasib@gmail.com>
---

I ran into this issue myself where I thought my dir was already tracked, and when I googled and found other people were confused and asking the same question. Why don't empty dirs appear under git status when they aren't tracked? So I came up with this patch.

In my commit message, I compiled a list of arguments in favor of this patch. But I've also thought about arguments against, however I didn't think they were compelling enough to not have this patch. So I've also included my own rebuttal below :)

1) Direcories can't be tracked, so why show them under untracked?
Because it is a helpful reminder. See reasons in the commit message above for 
how it is helpful. This would make git more user friendly.
Plus untracked does not mean 'it can be tracked', it just means it's not currently 
tracked by git.
2) Empty directories should be ignored
If someone really wants to ignore something, they can put it in the .gitignore 
file. Otherwise, the benefits above out weight this.
 wt-status.c |   24 +++++++++++++++++++-----
 1 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/wt-status.c b/wt-status.c
index 9ffc535..81bf1aa 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -184,7 +184,12 @@ static void wt_status_print_other_header(struct wt_status *s,
 					 const char *how)
 {
 	const char *c = color(WT_STATUS_HEADER, s);
-	status_printf_ln(s, c, _("%s files:"), what);
+
+	if (s->show_untracked_files == SHOW_NORMAL_UNTRACKED_FILES)
+		status_printf_ln(s, c, _("%s files and directories:"), what);
+	else if (s->show_untracked_files == SHOW_ALL_UNTRACKED_FILES)
+		status_printf_ln(s, c, _("%s files:"), what);
+
 	if (!advice_status_hints)
 		return;
 	status_printf_ln(s, c, _("  (use \"git %s <file>...\" to include in what will be committed)"), how);
@@ -464,16 +469,25 @@ static void wt_status_collect_untracked(struct wt_status *s)
 		return;
 	memset(&dir, 0, sizeof(dir));
 	if (s->show_untracked_files != SHOW_ALL_UNTRACKED_FILES)
-		dir.flags |=
-			DIR_SHOW_OTHER_DIRECTORIES | DIR_HIDE_EMPTY_DIRECTORIES;
+	dir.flags |=
+	  DIR_SHOW_OTHER_DIRECTORIES;
 	setup_standard_excludes(&dir);
 
 	fill_directory(&dir, s->pathspec);
 	for (i = 0; i < dir.nr; i++) {
 		struct dir_entry *ent = dir.entries[i];
 		if (cache_name_is_other(ent->name, ent->len) &&
-		    match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL))
-			string_list_insert(&s->untracked, ent->name);
+		    match_pathspec(s->pathspec, ent->name, ent->len, 0, NULL)) {
+			if (is_empty_dir(ent->name)) {
+				struct strbuf buf_name = STRBUF_INIT;
+				strbuf_addstr(&buf_name, ent->name);
+				strbuf_addstr(&buf_name, " (empty directories cannot be added)");
+				string_list_insert(&s->untracked, buf_name.buf);
+				strbuf_release(&buf_name);
+			}
+			else
+				string_list_insert(&s->untracked, ent->name);
+		}
 		free(ent);
 	}
 
-- 
1.7.7.5 (Apple Git-26)
Next: konglu@minatec.inpg.fr
Message 1 of 17 in “git-status: Show empty directories”
  1. git-status: Show empty directoriesLeila Muhtasib, Jun 9, 2012
  2. konglu@minatec.inpg.frJun 9, 2012
  3. LeilaJun 9, 2012
  4. Thomas RastJun 9, 2012
  5. LeilaJun 9, 2012
  6. konglu@minatec.inpg.frJun 9, 2012
  7. Thomas RastJun 10, 2012
  8. konglu@minatec.inpg.frJun 10, 2012
  9. LeilaJun 10, 2012
  10. Junio C HamanoJun 11, 2012
  11. Junio C HamanoJun 10, 2012
  12. LeilaJun 10, 2012
  13. konglu@minatec.inpg.frJun 10, 2012
  14. LeilaJun 10, 2012
  15. Junio C HamanoJun 11, 2012
  16. LeilaJun 11, 2012
  17. LeilaJun 11, 2012

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.