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

[PATCH 2/2] Don't clean any untracked submodule's .git dir by default in git-clean

From
JHJason Holden <jason.k.holden@gmail.com>
Date
Jun 30, 2009, 02:10 UTC
Message-ID
<1246327845-22718-3-git-send-email-jason.k.holden@gmail.com>
In-Reply-To
<1246327845-22718-2-git-send-email-jason.k.holden@gmail.com>

Git-clean is not safe when the submodules are not tracked in mainline. If we run git-clean on the mainline branch, when we have a submodule that only exists on a local branch, the entire .git directory of the untracked submodule will get deleted, possibly losing any un-pushed local changes to the submodule.

This change doesn't delete any untracked submodule's .git directories during the recursive-delete (unless forced with the -m option to git-clean), so that the submodule history can be restored w/ the proper git commands.

# Example illustrating problem: # Clone mainline project git clone git://github.com/thoughtbot/paperclip.git cd paperclip/

# Add a submodule not tracked by mainline git checkout -b test_submodule_clean git submodule add git://github.com/technoweenie/attachment_fu.git attachement_fu git commit -m "add submodule"

# Make a modification to the submodule. Note that we haven't pushed the change cd attachement_fu/ git checkout -b mod_readme_in_submodule vi README git add README git commit -m "Small change in submodule"

# Go back to mainline's master branch and do a clean cd .. git checkout master git clean -f -d

# Our change to the submodule, that was never pushed, is now gone forever # because all the history stored in the submodule's .git direct is deleted. # There is no recovering from this. # This breaks the "git must be safe" rule, as we've lost potentially a lot of # changes to any submodule projects that didn't get pushed yet. Solve # this issue by not deleting any .git directories we come across during a # git-clean, unless the "-m" option is passed to git-clean.

Signed-off-by: Jason Holden <jason.k.holden@gmail.com>
---
 Documentation/git-clean.txt |    6 +++++-
 builtin-clean.c             |   15 +++++++++++++--
 2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/Documentation/git-clean.txt b/Documentation/git-clean.txt
index be894af..04a5a65 100644
--- a/Documentation/git-clean.txt
+++ b/Documentation/git-clean.txt
@@ -8,7 +8,7 @@ git-clean - Remove untracked files from the working tree
 SYNOPSIS
 --------
 [verse]
-'git clean' [-d] [-f] [-n] [-q] [-x | -X] [--] <path>...
+'git clean' [-d] [-f] [-n] [-q] [-m] [-x | -X] [--] <path>...
 
 DESCRIPTION
 -----------
@@ -41,6 +41,10 @@ OPTIONS
 	Be quiet, only report errors, but not the files that are
 	successfully removed.
 
+-m::
+	Clean any .git directories that may be left-over, untracked
+	submodules.
+
 -x::
 	Don't use the ignore rules.  This allows removing all untracked
 	files, including build products.  This can be used (possibly in
diff --git a/builtin-clean.c b/builtin-clean.c
index cd82407..60d78dc 100644
--- a/builtin-clean.c
+++ b/builtin-clean.c
@@ -15,7 +15,7 @@
 static int force = -1; /* unset */
 
 static const char *const builtin_clean_usage[] = {
-	"git clean [-d] [-f] [-n] [-q] [-x | -X] [--] <paths>...",
+	"git clean [-d] [-f] [-n] [-q] [-m] [-x | -X] [--] <paths>...",
 	NULL
 };
 
@@ -31,6 +31,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 	int i;
 	int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
 	int ignored_only = 0, baselen = 0, config_set = 0, errors = 0;
+	int rm_untracked_submodules = 0;
 	struct strbuf directory = STRBUF_INIT;
 	struct dir_struct dir;
 	const char *path, *base;
@@ -44,6 +45,8 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 		OPT_BOOLEAN('f', NULL, &force, "force"),
 		OPT_BOOLEAN('d', NULL, &remove_directories,
 				"remove whole directories"),
+		OPT_BOOLEAN('m', NULL, &rm_untracked_submodules,
+				"remove untracked submodules"),
 		OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"),
 		OPT_BOOLEAN('X', NULL, &ignored_only,
 				"remove only ignored files"),
@@ -59,6 +62,14 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
 			     0);
 
+
+	int keep_dot_git = 0;
+	if (rm_untracked_submodules == 0)
+		keep_dot_git = 1;
+	else
+		printf("Any untracked .git directories will be deleted (abandoned submodules)\n");
+
+
 	memset(&dir, 0, sizeof(dir));
 	if (ignored_only)
 		dir.flags |= DIR_SHOW_IGNORED;
@@ -141,7 +152,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 				   (matches == MATCHED_EXACTLY)) {
 				if (!quiet)
 					printf("Removing %s\n", qname);
-				if (remove_dir_recursively(&directory, 0, 0) != 0) {
+				if (remove_dir_recursively(&directory, 0, keep_dot_git) != 0) {
 					warning("failed to remove '%s'", qname);
 					errors++;
 				}
-- 
1.6.3.2.207.ga8208
Previous: Jason HoldenNext: Paolo Bonzini
Message 3 of 10 in “Don't delete untracked submodule's .git dirs by default”
  1. 0/2 Don't delete untracked submodule's .git dirs by defaultJason Holden, Jun 30, 2009
  2. 1/2 Add option to not delete a .git directory in remove_dir_recursively()Jason Holden, Jun 30, 2009
  3. 2/2 Don't clean any untracked submodule's .git dir by default in git-cleanJason Holden, Jun 30, 2009
  4. Paolo BonziniJun 30, 2009
  5. Johannes SixtJun 30, 2009
  6. Junio C HamanoJun 30, 2009
  7. Junio C HamanoJun 30, 2009
  8. Jason HoldenJul 1, 2009
  9. Junio C HamanoJul 1, 2009
  10. Johannes SixtJun 30, 2009

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.