Skip to contentSkip to conversation

Second attempt at making git-clean a builtin

RFC In review Shawn Bohrer 10 participants 30 messages Jun 15, 2016 Kbuild

lore
Share & tools
lore
#0 lore raw

I've taken all of the comments I received from my previous attempt see:

http://marc.info/?l=git&m=119181975419521&w=2

With these new changes in place my new git-clean passes all of the original tests as well as the new tests I've added. While looking at how git-ls-files walks the tree there were some things that didn't quite understand, or thought might be unnecessary so there may be some things I missed. For example I'm still not quite sure what verify_pathspec() does.

I did however notice what I would call a bug in the behavior of git-ls-files and therefore the current git-clean.sh. With the current git-clean if you have two directories that contain only untracked files, for example docs/ and examples/ running:

git clean docs/ examples/

will not remove either directory. Instead you must use the -d parameter. To me this makes sense, however if you run:

git clean docs/

it will remove the docs directory without using the -d parameter. My patch is at least consistent in that it requires the -d in both cases.

#1 lore raw

PATCH 1/1 Add more tests for git-clean

---
 t/t7300-clean.sh |  109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 109 insertions(+), 0 deletions(-)
t/t7300-clean.sh +109 -0
index 8697213..d74c11c 100755
@@ -39,6 +39,97 @@ test_expect_success 'git-clean' '
 
 '
 
+test_expect_success 'git-clean src/' '
+
+	mkdir -p build docs &&
+	touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+	git-clean src/ &&
+	test -f Makefile &&
+	test -f README &&
+	test -f src/part1.c &&
+	test -f src/part2.c &&
+	test -f a.out &&
+	test ! -f src/part3.c &&
+	test -f docs/manual.txt &&
+	test -f obj.o &&
+	test -f build/lib.so
+
+'
+
+test_expect_success 'git-clean src/ src/' '
+
+	mkdir -p build docs &&
+	touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+	git-clean src/ src/ &&
+	test -f Makefile &&
+	test -f README &&
+	test -f src/part1.c &&
+	test -f src/part2.c &&
+	test -f a.out &&
+	test ! -f src/part3.c &&
+	test -f docs/manual.txt &&
+	test -f obj.o &&
+	test -f build/lib.so
+
+'
+
+test_expect_success 'git-clean with prefix' '
+
+	mkdir -p build docs &&
+	touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+	cd src/ &&
+	git-clean &&
+	cd - &&
+	test -f Makefile &&
+	test -f README &&
+	test -f src/part1.c &&
+	test -f src/part2.c &&
+	test -f a.out &&
+	test ! -f src/part3.c &&
+	test -f docs/manual.txt &&
+	test -f obj.o &&
+	test -f build/lib.so
+
+'
+test_expect_success 'git-clean -d with prefix and path' '
+
+	mkdir -p build docs src/feature &&
+	touch a.out src/part3.c src/feature/file.c docs/manual.txt obj.o build/lib.so &&
+	cd src/ &&
+	git-clean -d feature/ &&
+	cd - &&
+	test -f Makefile &&
+	test -f README &&
+	test -f src/part1.c &&
+	test -f src/part2.c &&
+	test -f a.out &&
+	test -f src/part3.c &&
+	test ! -f src/feature/file.c &&
+	test -f docs/manual.txt &&
+	test -f obj.o &&
+	test -f build/lib.so
+
+'
+
+test_expect_success 'git-clean symbolic link' '
+
+	mkdir -p build docs &&
+	touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+	ln -s docs/manual.txt src/part4.c
+	git-clean &&
+	test -f Makefile &&
+	test -f README &&
+	test -f src/part1.c &&
+	test -f src/part2.c &&
+	test ! -f a.out &&
+	test ! -f src/part3.c &&
+	test ! -f src/part4.c &&
+	test -f docs/manual.txt &&
+	test -f obj.o &&
+	test -f build/lib.so
+
+'
+
 test_expect_success 'git-clean -n' '
 
 	mkdir -p build docs &&
@@ -73,6 +164,24 @@ test_expect_success 'git-clean -d' '
 
 '
 
+test_expect_success 'git-clean -d src/ examples/' '
+
+	mkdir -p build docs examples &&
+	touch a.out src/part3.c docs/manual.txt obj.o build/lib.so examples/1.c &&
+	git-clean -d src/ examples/ &&
+	test -f Makefile &&
+	test -f README &&
+	test -f src/part1.c &&
+	test -f src/part2.c &&
+	test -f a.out &&
+	test ! -f src/part3.c &&
+	test ! -f examples/1.c &&
+	test -f docs/manual.txt &&
+	test -f obj.o &&
+	test -f build/lib.so
+
+'
+
 test_expect_success 'git-clean -x' '
 
 	mkdir -p build docs &&
Signature · 1 line
1.5.3.GIT
#2 lore raw

PATCH 1/1 Make git-clean a builtin

This replaces git-clean.sh with builtin-clean.c, and moves git-clean.sh to the examples.

---
 Makefile                                      |    3 +-
 builtin-clean.c                               |  157 +++++++++++++++++++++++++
 builtin.h                                     |    1 +
 git-clean.sh => contrib/examples/git-clean.sh |    0 
 git.c                                         |    1 +
 5 files changed, 161 insertions(+), 1 deletions(-)
 create mode 100644 builtin-clean.c
 rename git-clean.sh => contrib/examples/git-clean.sh (100%)
Makefile +2 -1
index 3ec1876..fad49b2 100644
@@ -209,7 +209,7 @@ BASIC_LDFLAGS =
 
 SCRIPT_SH = \
 	git-bisect.sh git-checkout.sh \
-	git-clean.sh git-clone.sh git-commit.sh \
+	git-clone.sh git-commit.sh \
 	git-ls-remote.sh \
 	git-merge-one-file.sh git-mergetool.sh git-parse-remote.sh \
 	git-pull.sh git-rebase.sh git-rebase--interactive.sh \
@@ -326,6 +326,7 @@ BUILTIN_OBJS = \
 	builtin-check-attr.o \
 	builtin-checkout-index.o \
 	builtin-check-ref-format.o \
+	builtin-clean.o \
 	builtin-commit-tree.o \
 	builtin-count-objects.o \
 	builtin-describe.o \
builtin-clean.c new file +157 -0

157 lines not on this page · raw message

builtin.h +1 -0
index 2335c01..0cbd685 100644
@@ -24,6 +24,7 @@ extern int cmd_check_attr(int argc, const char **argv, const char *prefix);
 extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);
+extern int cmd_clean(int argc, const char **argv, const char *prefix);
 extern int cmd_commit_tree(int argc, const char **argv, const char *prefix);
 extern int cmd_count_objects(int argc, const char **argv, const char *prefix);
 extern int cmd_describe(int argc, const char **argv, const char *prefix);
contrib/examples/git-clean.sh renamed from git-clean.sh +0 -0
similarity index 100%
rename from git-clean.sh
rename to contrib/examples/git-clean.sh
git.c +1 -0
index 19a2172..30b7c22 100644
@@ -298,6 +298,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "check-attr", cmd_check_attr, RUN_SETUP | NEED_WORK_TREE },
 		{ "cherry", cmd_cherry, RUN_SETUP },
 		{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
+		{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
 		{ "commit-tree", cmd_commit_tree, RUN_SETUP },
 		{ "config", cmd_config },
 		{ "count-objects", cmd_count_objects, RUN_SETUP },
Signature · 1 line
1.5.3.GIT
#3 lore raw

Re: [PATCH] Make git-clean a builtin

Quoted 27 lines · Shawn Bohrer
builtin-clean.c new file +27 -0
+	for (i = 1; i < argc; i++) {
+		const char *arg = argv[i];
+
+		if (arg[0] != '-')
+			break;
+		if (!strcmp(arg, "--")) {
+			i++;
+			break;
+		}
+		if (!strcmp(arg, "-n")) {
+			show_only = 1;
+			disabled = 0;
+			continue;
+		}
+		if (!strcmp(arg, "-f")) {
+			disabled = 0;
+			continue;
+		}
+		if (!strcmp(arg, "-d")) {
+			remove_directories = 1;
+			continue;
+		}
+		if (!strcmp(arg, "-q")) {
+			quiet = 1;
+			continue;
+		}
+		if (!strcmp(arg, "-x")) {

builtin-clean.c line 80

Quoted 10 lines
builtin-clean.c new file +10 -0
+			ignored = 1;
+			continue;
+		}
+		if (!strcmp(arg, "-X")) {
+			ignored_only = 1;
+			dir.show_ignored =1;
+			dir.exclude_per_dir = ".gitignore";
+			continue;
+		}
+		usage(builtin_clean_usage);

Please, parse-options.c is now in next, please use it.

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#4 lore raw

[PATCH 3/2] Use parse-options in builtin-clean

---

	On Sun, 4 Nov 2007, Pierre Habouzit wrote:

	> On Sun, Nov 04, 2007 at 07:02:21PM +0000, Shawn Bohrer wrote:
	> 
	> > +	for (i = 1; i < argc; i++) {
	> > +		const char *arg = argv[i];
	> > [...]
	> 
	>   Please, parse-options.c is now in next, please use it.

	Something like this?
 builtin-clean.c |   71 ++++++++++++++++++++----------------------------------
 1 files changed, 26 insertions(+), 45 deletions(-)
builtin-clean.c +26 -45
index 4141eb4..d6fc2ad 100644
@@ -9,81 +9,62 @@
 #include "builtin.h"
 #include "cache.h"
 #include "dir.h"
+#include "parse-options.h"
 
-static int disabled = 1;
+static int force = 0;
 static int show_only = 0;
 static int remove_directories = 0;
 static int quiet = 0;
 static int ignored = 0;
 static int ignored_only = 0;
 
-static const char builtin_clean_usage[] =
-"git-clean [-d] [-f] [-n] [-q] [-x | -X] [--] <paths>...";
+static const char *const builtin_clean_usage[] = {
+	"git-clean [-d] [-f] [-n] [-q] [-x | -X] [--] <paths>...",
+	NULL
+};
 
 static int git_clean_config(const char *var, const char *value)
 {
 	if (!strcmp(var, "clean.requireforce")) {
-		disabled = git_config_bool(var, value);
+		force = !git_config_bool(var, value);
 	}
 	return 0;
 }
 
 int cmd_clean(int argc, const char **argv, const char *prefix)
 {
-	int i, j;
+	int j;
 	struct strbuf directory;
 	struct dir_struct dir;
 	const char *path = ".";
 	const char *base = "";
 	int baselen = 0;
 	static const char **pathspec;
+	struct option options[] = {
+		OPT__QUIET(&quiet),
+		OPT__DRY_RUN(&show_only),
+		OPT_BOOLEAN('f', NULL, &force, "force"),
+		OPT_BOOLEAN('d', NULL, &remove_directories,
+				"remove whole directories"),
+		OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"),
+		OPT_BOOLEAN('X', NULL, &ignored_only,
+				"remove only ignored files"),
+		OPT_END()
+	};
 
-	memset(&dir, 0, sizeof(dir));
 	git_config(git_clean_config);
+	argc = parse_options(argc, argv, options, builtin_clean_usage, 0);
 
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
-
-		if (arg[0] != '-')
-			break;
-		if (!strcmp(arg, "--")) {
-			i++;
-			break;
-		}
-		if (!strcmp(arg, "-n")) {
-			show_only = 1;
-			disabled = 0;
-			continue;
-		}
-		if (!strcmp(arg, "-f")) {
-			disabled = 0;
-			continue;
-		}
-		if (!strcmp(arg, "-d")) {
-			remove_directories = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-q")) {
-			quiet = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-x")) {
-			ignored = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-X")) {
-			ignored_only = 1;
-			dir.show_ignored =1;
-			dir.exclude_per_dir = ".gitignore";
-			continue;
-		}
-		usage(builtin_clean_usage);
+	memset(&dir, 0, sizeof(dir));
+	if (ignored_only) {
+		dir.show_ignored =1;
+		dir.exclude_per_dir = ".gitignore";
 	}
 
 	if (ignored && ignored_only)
 		die("-x and -X cannot be used together");
 
-	if (disabled)
+	if (!show_only && !force)
 		die("clean.requireForce set and -n or -f not given; refusing to clean");
 
 	dir.show_other_directories = 1;
@@ -96,7 +77,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 		}
 	}
 
-	pathspec = get_pathspec(prefix, argv + i);
+	pathspec = get_pathspec(prefix, argv);
 	read_cache();
 	read_directory(&dir, path, base, baselen, pathspec);
 	strbuf_init(&directory, 0);
Signature · 1 line
1.5.3.5.1549.g91a3
#5 lore raw

Re: [PATCH 3/2] Use parse-options in builtin-clean

Quoted 4 lines · Johannes Schindelin

---

Quoted 11 lines
	On Sun, 4 Nov 2007, Pierre Habouzit wrote:

	> On Sun, Nov 04, 2007 at 07:02:21PM +0000, Shawn Bohrer wrote:
	> 
	> > +	for (i = 1; i < argc; i++) {
	> > +		const char *arg = argv[i];
	> > [...]
	> 
	>   Please, parse-options.c is now in next, please use it.

	Something like this?

something like this works for me :)

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#6 lore raw

Re: [PATCH] Make git-clean a builtin

In reply to Shawn Bohrer
Quoted 14 lines · Shawn Bohrer

This replaces git-clean.sh with builtin-clean.c, and moves git-clean.sh to the examples.

---

builtin-clean.c new file +3 -0
new file mode 100644
index 0000000..4141eb4
@@ -0,0 +1,157 @@
+/*
+ * "git clean" builtin command
+ *

builtin-clean.c line 13

Quoted 10 lines
builtin-clean.c new file +10 -0
+ * Copyright (C) 2007 Shawn Bohrer
+ *
+ * Based on git-clean.sh by Pavel Roskin
+ */
+
+#include "builtin.h"
+#include "cache.h"
+#include "dir.h"
+
+static int disabled = 1;

This means we are committed to make clean.requireForce default to true, which is fine by me. I need to warn the users about this early.

builtin-clean.c line 18

Quoted 5 lines
builtin-clean.c new file +5 -0
+static int show_only = 0;
+static int remove_directories = 0;
+static int quiet = 0;
+static int ignored = 0;
+static int ignored_only = 0;

Please do not explicitly initialize static variables to zero.

#7 lore raw

Re: [PATCH] Make git-clean a builtin

2007/11/5, Junio C Hamano <[email protected]>:

Quoted 9 lines

builtin-clean.c line 18

Quoted 5 lines · Shawn Bohrer
builtin-clean.c new file +5 -0
+static int show_only = 0;
+static int remove_directories = 0;
+static int quiet = 0;
+static int ignored = 0;
+static int ignored_only = 0;

Please do not explicitly initialize static variables to zero.

Is it really needed to declare those variables outside of a function in this case? This scheme makes difficult reusing the code from other builtins, rewriting it for libification, calling it many times, or even understand if they were declared that way with a purpose or not. I just don't know why they are that way in this case, is there a reason for it?

#8 lore raw

Re: [PATCH] Make git-clean a builtin

Quoted 11 lines · Carlos Rica

2007/11/5, Junio C Hamano <[email protected]>:

Quoted 9 lines

builtin-clean.c line 18

Quoted 5 lines · Shawn Bohrer
builtin-clean.c new file +5 -0
+static int show_only = 0;
+static int remove_directories = 0;
+static int quiet = 0;
+static int ignored = 0;
+static int ignored_only = 0;

Please do not explicitly initialize static variables to zero.

Quoted 2 lines

Is it really needed to declare those variables outside of a function in this case?

I do not think so --- I suspect that this is a simple cut & paste from the standalone ls-files implementation.

#9 lore raw

Re: [PATCH] Make git-clean a builtin

In reply to Junio C Hamano
Quoted 7 lines · Junio C Hamano

Shawn Bohrer <[email protected]> writes:
[...]

Quoted 1 line

+static int disabled = 1;

This means we are committed to make clean.requireForce default to true, which is fine by me. I need to warn the users about this early.

Actually I don't care either way, but in my last rebase on next this change was already made to git-clean.sh so I adjusted accordingly.

Quoted 7 lines

builtin-clean.c line 18

Quoted 5 lines
builtin-clean.c new file +5 -0
+static int show_only = 0;
+static int remove_directories = 0;
+static int quiet = 0;
+static int ignored = 0;
+static int ignored_only = 0;

Please do not explicitly initialize static variables to zero.

I realize that static variables will be automatically initialized to zero so this is unnecessary, but is there some technical reason not to initialize explicitly? If the answer is simply a style preference that is fine, I'm just here to learn.

Of course as already pointed out these don't actually need to be static in the first place so I'll simply move them into cmd_clean(). This does lead me to another question though. Now that Dscho has converted my patch to use parse-options, what is the best way to update my patch while still giving credit to Dscho?

#10 lore raw

Re: [PATCH] Make git-clean a builtin

Quoted 11 lines · Shawn Bohrer
Quoted 7 lines · Junio C Hamano

Shawn Bohrer <[email protected]> writes:
[...]

Quoted 1 line

+static int disabled = 1;

This means we are committed to make clean.requireForce default to true, which is fine by me. I need to warn the users about this early.

Actually I don't care either way, but in my last rebase on next this change was already made to git-clean.sh so I adjusted accordingly.

Oh, that was not a question to you, but a note to me.

Quoted 8 lines
Quoted 7 lines

builtin-clean.c line 18

Quoted 5 lines
builtin-clean.c new file +5 -0
+static int show_only = 0;
+static int remove_directories = 0;
+static int quiet = 0;
+static int ignored = 0;
+static int ignored_only = 0;

Please do not explicitly initialize static variables to zero.

Quoted 4 lines

I realize that static variables will be automatically initialized to zero so this is unnecessary, but is there some technical reason not to initialize explicitly? If the answer is simply a style preference that is fine, I'm just here to learn.

Both readability and style have to do much with this.

The style has a historical background which is a slight technical merit. It results in a smaller executable file, as C compilers traditionally placed file-scope static variables that are not explicitly initialized in the BSS section, instead of explicitly storing N-bytes of zero as the the initial data in it (although I do not see a reason for compilers not to do the same for variables explicitly initialized to zero. In fact, I think modern gcc produces the same allocation with or without "= 0" initialization).

Quoted 5 lines

Of course as already pointed out these don't actually need to be static in the first place so I'll simply move them into cmd_clean(). This does lead me to another question though. Now that Dscho has converted my patch to use parse-options, what is the best way to update my patch while still giving credit to Dscho?

Please send a rewritten replacement version as a single patch that is cleanly applicable to 'next', and mention people whose input helped you in polishing the patch in the proposed commit log message.

#11 lore raw

PATCH 1/1 Make git-clean a builtin

This replaces git-clean.sh with builtin-clean.c, and moves git-clean.sh to the examples.

This also introduces a change in behavior where the -d parameter is required to remove an entire directory of untracked files even when the directory is passed as a path. For example:

git clean dir/

Now requires

git clean -d dir/

if 'dir' only contains untracked files. This is consistent with the old behavior when two or more paths were specified.

Thanks to Johannes Schindelin for the conversion to using the parse-options API.

---
 Makefile                                      |    3 +-
 builtin-clean.c                               |  134 +++++++++++++++++++++++++
 builtin.h                                     |    1 +
 git-clean.sh => contrib/examples/git-clean.sh |    0 
 git.c                                         |    1 +
 5 files changed, 138 insertions(+), 1 deletions(-)
 create mode 100644 builtin-clean.c
 rename git-clean.sh => contrib/examples/git-clean.sh (100%)
Makefile +2 -1
index c427fee..932ff08 100644
@@ -209,7 +209,7 @@ BASIC_LDFLAGS =
 
 SCRIPT_SH = \
 	git-bisect.sh git-checkout.sh \
-	git-clean.sh git-clone.sh git-commit.sh \
+	git-clone.sh git-commit.sh \
 	git-merge-one-file.sh git-mergetool.sh git-parse-remote.sh \
 	git-pull.sh git-rebase.sh git-rebase--interactive.sh \
 	git-repack.sh git-request-pull.sh \
@@ -325,6 +325,7 @@ BUILTIN_OBJS = \
 	builtin-check-attr.o \
 	builtin-checkout-index.o \
 	builtin-check-ref-format.o \
+	builtin-clean.o \
 	builtin-commit-tree.o \
 	builtin-count-objects.o \
 	builtin-describe.o \
builtin-clean.c new file +134 -0

134 lines not on this page · raw message

builtin.h +1 -0
index 525107f..5476a92 100644
@@ -24,6 +24,7 @@ extern int cmd_check_attr(int argc, const char **argv, const char *prefix);
 extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);
+extern int cmd_clean(int argc, const char **argv, const char *prefix);
 extern int cmd_commit_tree(int argc, const char **argv, const char *prefix);
 extern int cmd_count_objects(int argc, const char **argv, const char *prefix);
 extern int cmd_describe(int argc, const char **argv, const char *prefix);
contrib/examples/git-clean.sh renamed from git-clean.sh +0 -0
similarity index 100%
rename from git-clean.sh
rename to contrib/examples/git-clean.sh
git.c +1 -0
index 204a6f7..3fa8e4d 100644
@@ -293,6 +293,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "check-attr", cmd_check_attr, RUN_SETUP | NEED_WORK_TREE },
 		{ "cherry", cmd_cherry, RUN_SETUP },
 		{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
+		{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
 		{ "commit-tree", cmd_commit_tree, RUN_SETUP },
 		{ "config", cmd_config },
 		{ "count-objects", cmd_count_objects, RUN_SETUP },
Signature · 1 line
1.5.3.GIT
#12 lore raw

Re: [PATCH] Make git-clean a builtin

Quoted 6 lines · Shawn Bohrer

This replaces git-clean.sh with builtin-clean.c, and moves git-clean.sh to the examples.

This also introduces a change in behavior where the -d parameter is required to remove an entire directory of untracked files even when the directory is passed as a path.

The updated behaviour may be better, but this description at the first read makes one wonder if it is describing a regression as if it is a feature.

Quoted 4 lines

... For example ...
...
if 'dir' only contains untracked files. This is consistent with the old behavior when two or more paths were specified.

I think what you fixed are two inconsistencies in the original implementation. If you spelled out the existing inconsistency and described what your implementation does differently, the proposal would start looking like a real improvement, like this:

1. When dir has only untracked files, these two behave differently:

$ git clean -n dir
$ git clean -n dir/

the former says "Would not remove dir/", while the latter would say "Would remove dir/untracked" for all paths under it.

With -d, the former would stop refusing, but the difference in reporting is still there. The latter lists all paths under the directory.

2. When there are more parameters, the latter behave differently:

$ git clean -n dir/ foo

refuses to remove dir/. This is inconsistent.

My reimplementation changes the behaviour by always requiring the -d option with or without the trailing slash.

Having said that, I do not particularly agree with the way the new implementation resolves the existing inconsistencies.

Wouldn't it be better to remove "dir" when the user explicitly told you to clean "dir", with or without the trailing slash? That's what the user asked you to do, isn't it?

#13 lore raw

Re: [PATCH] Make git-clean a builtin

Quoted 7 lines · Junio C Hamano

Having said that, I do not particularly agree with the way the new implementation resolves the existing inconsistencies.

Wouldn't it be better to remove "dir" when the user explicitly told you to clean "dir", with or without the trailing slash? That's what the user asked you to do, isn't it?

Yes I suppose I agree. Of course I need to spend some more time staring at the code to figure out how to do so. Perhaps I can figure out what is causing the original inconsistency in git-ls-files while I'm at it.

#14 lore raw

PATCH 1/1 Make git-clean a builtin

This replaces git-clean.sh with builtin-clean.c, and moves git-clean.sh to the examples.

This also introduces a change in behavior when removing directories explicitly specified as a path. For example currently:

1. When dir has only untracked files, these two behave differently:

    $ git clean -n dir
    $ git clean -n dir/

the former says "Would not remove dir/", while the latter would say "Would remove dir/untracked" for all paths under it, but not the directory itself.

With -d, the former would stop refusing, however since the user explicitly asked to remove the directory the -d is no longer required.

2. When there are more parameters:

    $ git clean -n dir foo
    $ git clean -n dir/ foo

both cases refuse to remove dir/ unless -d is specified. Once again since both cases requested to remove dir the -d is no longer required.

Thanks to Johannes Schindelin for the conversion to using the parse-options API.

---

Quoted 5 lines · Shawn Bohrer
Quoted 4 lines · Junio C Hamano

Having said that, I do not particularly agree with the way the new implementation resolves the existing inconsistencies.

Quoted 7 lines
Quoted 3 lines

Wouldn't it be better to remove "dir" when the user explicitly told you to clean "dir", with or without the trailing slash? That's what the user asked you to do, isn't it?

Yes I suppose I agree. Of course I need to spend some more time staring at the code to figure out how to do so. Perhaps I can figure out what is causing the original inconsistency in git-ls-files while I'm at it.

OK, I failed to fix the inconsistency between a single and multiple paths so I'm not sure if this patch is much better. The problem appears to be that when dir.show_others_directories is set directories that only contain untracked files are not recursed, which is fine with a single path because you can adjust the path, base and baselen to read_directory(). With multiple paths you need to find a common prefix to all of the paths before calling read_directory() which causes the inconsistency.

If anyone as a suggestion on how to fix this I'm all for it, but even with the inconsistency we are no worse than we were with git-clean.sh

 Makefile                                      |    3 +-
 builtin-clean.c                               |  154 +++++++++++++++++++++++++
 builtin.h                                     |    1 +
 git-clean.sh => contrib/examples/git-clean.sh |    0 
 git.c                                         |    1 +
 5 files changed, 158 insertions(+), 1 deletions(-)
 create mode 100644 builtin-clean.c
 rename git-clean.sh => contrib/examples/git-clean.sh (100%)
Makefile +2 -1
index a2fcdb8..ef7420d 100644
@@ -213,7 +213,7 @@ BASIC_LDFLAGS =
 
 SCRIPT_SH = \
 	git-bisect.sh git-checkout.sh \
-	git-clean.sh git-clone.sh git-commit.sh \
+	git-clone.sh git-commit.sh \
 	git-merge-one-file.sh git-mergetool.sh git-parse-remote.sh \
 	git-pull.sh git-rebase.sh git-rebase--interactive.sh \
 	git-repack.sh git-request-pull.sh \
@@ -329,6 +329,7 @@ BUILTIN_OBJS = \
 	builtin-check-attr.o \
 	builtin-checkout-index.o \
 	builtin-check-ref-format.o \
+	builtin-clean.o \
 	builtin-commit-tree.o \
 	builtin-count-objects.o \
 	builtin-describe.o \
builtin-clean.c new file +154 -0

154 lines not on this page · raw message

builtin.h +1 -0
index 525107f..5476a92 100644
@@ -24,6 +24,7 @@ extern int cmd_check_attr(int argc, const char **argv, const char *prefix);
 extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry(int argc, const char **argv, const char *prefix);
 extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);
+extern int cmd_clean(int argc, const char **argv, const char *prefix);
 extern int cmd_commit_tree(int argc, const char **argv, const char *prefix);
 extern int cmd_count_objects(int argc, const char **argv, const char *prefix);
 extern int cmd_describe(int argc, const char **argv, const char *prefix);
contrib/examples/git-clean.sh renamed from git-clean.sh +0 -0
similarity index 100%
rename from git-clean.sh
rename to contrib/examples/git-clean.sh
git.c +1 -0
index 204a6f7..3fa8e4d 100644
@@ -293,6 +293,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "check-attr", cmd_check_attr, RUN_SETUP | NEED_WORK_TREE },
 		{ "cherry", cmd_cherry, RUN_SETUP },
 		{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
+		{ "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
 		{ "commit-tree", cmd_commit_tree, RUN_SETUP },
 		{ "config", cmd_config },
 		{ "count-objects", cmd_count_objects, RUN_SETUP },
Signature · 1 line
1.5.3.GIT
#16 lore raw

Re: [PATCH] Make git-clean a builtin

Quoted 8 lines · On Wednesday, November 7, 2007 at 11:10:45 (+0000) Johannes Schindelin writes:

Hi,

you still have quite a number of instances where you wrap just one line into curly brackets:

	if (bla) {
		[just one line]
	}

I've always found this a thoughtful practice. It helps ensure nobody writes:

       if (bla)
           just_one_line();
           /* perhaps a comment, other stuff ... */
           just_another_line();

which I've seen happen countless times. It also is nice for others who come along and extend the branch from just one line to multiple ones, as the brackets are already in place.

Why do you find it objectionable?

Bill

#17 lore raw

Re: [PATCH] Make git-clean a builtin

Quoted 17 lines · Bill Lear
Quoted 8 lines · On Wednesday, November 7, 2007 at 11:10:45 (+0000) Johannes Schindelin writes:

Hi,

you still have quite a number of instances where you wrap just one line into curly brackets:

	if (bla) {
		[just one line]
	}

I've always found this a thoughtful practice. It helps ensure nobody writes:

       if (bla)
           just_one_line();
           /* perhaps a comment, other stuff ... */
           just_another_line();
Quoted 5 lines

which I've seen happen countless times. It also is nice for others who come along and extend the branch from just one line to multiple ones, as the brackets are already in place.

Why do you find it objectionable?

I _totally_ agree with Bill.

jdl

#18 lore raw

Re: [PATCH] Make git-clean a builtin

In reply to Bill Lear

Hi,

Quoted 9 lines · Bill Lear
Quoted 6 lines · On Wednesday, November 7, 2007 at 11:10:45 (+0000) Johannes Schindelin writes:

you still have quite a number of instances where you wrap just one line into curly brackets:

	if (bla) {
		[just one line]
	}
Quoted 7 lines

I've always found this a thoughtful practice. It helps ensure nobody writes:

       if (bla)
           just_one_line();
           /* perhaps a comment, other stuff ... */
           just_another_line();

But if there is only one line and you fail to add curly brackets when adding a second line, well, uhm, then I cannot help you with anything.

BTW I was talking about _one_ line, not a line and another one with a comment.

Quoted 2 lines

It also is nice for others who come along and extend the branch from just one line to multiple ones, as the brackets are already in place.

The fact is: these lines will stay single lines most likely for eternity.

Quoted 1 line

Why do you find it objectionable?

It distracts. It's ugly. It's unnecessary.

Ciao,
Dscho

#19 lore raw

Re: [PATCH] Make git-clean a builtin

In reply to Bill Lear
Quoted 6 lines · Bill Lear

I've always found this a thoughtful practice. It helps ensure nobody writes:

       if (bla)
           just_one_line();
           /* perhaps a comment, other stuff ... */
           just_another_line();

It also simplify patches for cases like

if (bla) {
just_one_line();
+ another_added_line();
}

instead of

- if (bla)
+ if (bla) {
just_one_line();
+ another_added_line();
+ }

But it seems people here prefer not putting the braces in this case.

Signature · 1 line
Matthieu
#20 lore raw

Re: [PATCH] Make git-clean a builtin

In reply to Bill Lear
Quoted 1 line · Bill Lear

Why do you find it objectionable?

It bloats the code, and makes it less readable.

[My conjecture is that the latter happens because braces are so visually striking that they attract the eye; for _long_ blocks, this property of braces helps, because it makes it easier to find them amongst the rest of the code, but for _short_ blocks, it hurts, because it draws the eye away from the actual code, and emphasizes structure at a time when structure doesn't need emphasizing (because it's utterly obvious already with such short blocks).]

-Miles

Signature · 1 line
Run away!  Run away!
#21 lore raw

Re: [PATCH] Make git-clean a builtin

In reply to Johannes Schindelin
Quoted 7 lines · Johannes Schindelin

you still have quite a number of instances where you wrap just one line into curly brackets:

	if (bla) {
		[just one line]
	}

Crap. OK I count one instance unless you count:

	if (foo) {
		one_line();
	} else if (bar) {
		one_line();
		two_lines();
	} else {
		something_else();
	}

Now I suppose I can get rid of the curly braces here as well but I personally find that strange and ugly. So is there an official guideline on if else statements?

Of course I'll fix the other one I missed and send a new patch.

#22 lore raw

Re: [PATCH] Make git-clean a builtin

Hi,

Quoted 11 lines · Shawn Bohrer
Quoted 7 lines · Johannes Schindelin

you still have quite a number of instances where you wrap just one line into curly brackets:

	if (bla) {
		[just one line]
	}

Crap. OK I count one instance unless you count:

Quoted 8 lines
	if (foo) {
		one_line();
	} else if (bar) {
		one_line();
		two_lines();
	} else {
		something_else();
	}

I do count them. Personally, I find it highly distracting and ugly. Besides, we have the convention of putting the "}" not into the same line as "else". (See keyword "uncuddling" in the list archives.)

While it may be true that some parts of the code follow these rules less strictly, it does not mean that we should introduce more of that kind.

BTW there are plenty of examples in the existing code which illustrate our implicit coding conventions.

Quoted 3 lines

Now I suppose I can get rid of the curly braces here as well but I personally find that strange and ugly. So is there an official guideline on if else statements?

Not yet ;-) I can add it to the tentative v3 of Documentation/CodingStyle or CodingConventions or however the list would like to name it.

Ciao,
Dscho

#23 lore raw

Re: [PATCH] Make git-clean a builtin

Quoted 3 lines · Johannes Schindelin

I do count them. Personally, I find it highly distracting and ugly. Besides, we have the convention of putting the "}" not into the same line as "else". (See keyword "uncuddling" in the list archives.)

I was under the impression that Git followed the kernel coding standards, which seem to want "cuddled" else statements:

136 Note that the closing brace is empty on a line of its own, _except_ in 137 the cases where it is followed by a continuation of the same statement, 138 ie a "while" in a do-statement or an "else" in an if-statement, like 139 this:
140
141 do {
142 body of do-loop
143 } while (condition);
144
145 and
146
147 if (x == y) {
148 ..
149 } else if (x > y) {
150 ...
151 } else {
152 ....
153 }
154
155 Rationale: K&R.

Searching the MARC list archives for "uncuddling" only yields the message I am replying to.

In addition, the kernel style seems to want braces for all branches of a conditional if any branch needs it:

163 Do not unnecessarily use braces where a single statement will do. 164
165 if (condition)
166 action();
167
168 This does not apply if one branch of a conditional statement is a single 169 statement. Use braces in both branches.
170
171 if (condition) {
172 do_this();
173 do_that();
174 } else {
175 otherwise();
176 }

This makes sense (to me), as at most you're only adding one extra line for the final closing brace, and it makes the whole conditional look more "balanced", IMHO.

But regardless, whatever the actual style for Git should be followed. Life's too short for arguments about coding style (even if divergence from K&R brace style is just plain wrong. :)

-bcd

#24 lore raw

Re: [PATCH] Make git-clean a builtin

Quoted 7 lines · [email protected] (Brian Downing) writes:

This makes sense (to me), as at most you're only adding one extra line for the final closing brace, and it makes the whole conditional look more "balanced", IMHO.

But regardless, whatever the actual style for Git should be followed. Life's too short for arguments about coding style (even if divergence from K&R brace style is just plain wrong. :)

Ok. We do not have any particularly strong technical reason to deviate from the kernel style. Let's follow that.

#25 lore raw

Re: [PATCH] Add more tests for git-clean

In reply to Shawn Bohrer

t/t7300-clean.sh test_expect_success 'git-clean' ' · line 82

Quoted 7 lines · Shawn Bohrer
t/t7300-clean.sh +7 -0
+test_expect_success 'git-clean with prefix' '
+
+	mkdir -p build docs &&
+	touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+	cd src/ &&
+	git-clean &&
+	cd - &&

This is wrong for two reasons.

- Is "cd -" portable?

- What happens when git-clean fails? This test fails, and then it goes on to the next test without cd'ing back.

#26 lore raw

Re: [PATCH] Add more tests for git-clean

Quoted 12 lines · Junio C Hamano

t/t7300-clean.sh test_expect_success 'git-clean' ' · line 82

Quoted 7 lines · Shawn Bohrer
t/t7300-clean.sh +7 -0
+test_expect_success 'git-clean with prefix' '
+
+	mkdir -p build docs &&
+	touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+	cd src/ &&
+	git-clean &&
+	cd - &&

This is wrong for two reasons.

Quoted 1 line

- Is "cd -" portable?

this is POSIX:

8910 − When a hyphen is used as the operand, this shall be equivalent to the command:
8911 cd "$OLDPWD" && pwd
8912 which changes to the previous working directory and then writes its name.

Meaning that cd $OLDPWD should work, and won't print $OLDPWD.

Signature · 3 lines
·O·  Pierre Habouzit
··O                                                [email protected]
OOO                                                http://www.madism.org
#27 lore raw

Re: [PATCH] Add more tests for git-clean

Quoted 13 lines · Pierre Habouzit
Quoted 12 lines · Junio C Hamano

t/t7300-clean.sh test_expect_success 'git-clean' ' · line 82

Quoted 7 lines · Shawn Bohrer
t/t7300-clean.sh +7 -0
+test_expect_success 'git-clean with prefix' '
+
+	mkdir -p build docs &&
+	touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+	cd src/ &&
+	git-clean &&
+	cd - &&

This is wrong for two reasons.

Quoted 3 lines
Quoted 1 line

- Is "cd -" portable?

this is POSIX:

That actually doesn't matter. What the real world shells do matters more.

In addition, "cd -" is a nice shorthand for interactive use but it is a bad discipline to use it in a script anyway.

	...
	( cd src && git-clean ) &&
	...

would be the best way to write this.

#28 lore raw

PATCH 1/1 Add more tests for git-clean

---
 t/t7300-clean.sh |  105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 105 insertions(+), 0 deletions(-)
Quoted 5 lines · Junio C Hamano
      ...
      ( cd src && git-clean ) &&
      ...

would be the best way to write this.

Agreed here is an updated patch that does this.

t/t7300-clean.sh +105 -0
index 8697213..25d3102 100755
@@ -39,6 +39,93 @@ test_expect_success 'git-clean' '
 
 '
 
+test_expect_success 'git-clean src/' '
+
+	mkdir -p build docs &&
+	touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+	git-clean src/ &&
+	test -f Makefile &&
+	test -f README &&
+	test -f src/part1.c &&
+	test -f src/part2.c &&
+	test -f a.out &&
+	test ! -f src/part3.c &&
+	test -f docs/manual.txt &&
+	test -f obj.o &&
+	test -f build/lib.so
+
+'
+
+test_expect_success 'git-clean src/ src/' '
+
+	mkdir -p build docs &&
+	touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+	git-clean src/ src/ &&
+	test -f Makefile &&
+	test -f README &&
+	test -f src/part1.c &&
+	test -f src/part2.c &&
+	test -f a.out &&
+	test ! -f src/part3.c &&
+	test -f docs/manual.txt &&
+	test -f obj.o &&
+	test -f build/lib.so
+
+'
+
+test_expect_success 'git-clean with prefix' '
+
+	mkdir -p build docs &&
+	touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+	(cd src/ && git-clean) &&
+	test -f Makefile &&
+	test -f README &&
+	test -f src/part1.c &&
+	test -f src/part2.c &&
+	test -f a.out &&
+	test ! -f src/part3.c &&
+	test -f docs/manual.txt &&
+	test -f obj.o &&
+	test -f build/lib.so
+
+'
+test_expect_success 'git-clean -d with prefix and path' '
+
+	mkdir -p build docs src/feature &&
+	touch a.out src/part3.c src/feature/file.c docs/manual.txt obj.o build/lib.so &&
+	(cd src/ && git-clean -d feature/) &&
+	test -f Makefile &&
+	test -f README &&
+	test -f src/part1.c &&
+	test -f src/part2.c &&
+	test -f a.out &&
+	test -f src/part3.c &&
+	test ! -f src/feature/file.c &&
+	test -f docs/manual.txt &&
+	test -f obj.o &&
+	test -f build/lib.so
+
+'
+
+test_expect_success 'git-clean symbolic link' '
+
+	mkdir -p build docs &&
+	touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+	ln -s docs/manual.txt src/part4.c
+	git-clean &&
+	test -f Makefile &&
+	test -f README &&
+	test -f src/part1.c &&
+	test -f src/part2.c &&
+	test ! -f a.out &&
+	test ! -f src/part3.c &&
+	test ! -f src/part4.c &&
+	test -f docs/manual.txt &&
+	test -f obj.o &&
+	test -f build/lib.so
+
+'
+
 test_expect_success 'git-clean -n' '
 
 	mkdir -p build docs &&
@@ -73,6 +160,24 @@ test_expect_success 'git-clean -d' '
 
 '
 
+test_expect_success 'git-clean -d src/ examples/' '
+
+	mkdir -p build docs examples &&
+	touch a.out src/part3.c docs/manual.txt obj.o build/lib.so examples/1.c &&
+	git-clean -d src/ examples/ &&
+	test -f Makefile &&
+	test -f README &&
+	test -f src/part1.c &&
+	test -f src/part2.c &&
+	test -f a.out &&
+	test ! -f src/part3.c &&
+	test ! -f examples/1.c &&
+	test -f docs/manual.txt &&
+	test -f obj.o &&
+	test -f build/lib.so
+
+'
+
 test_expect_success 'git-clean -x' '
 
 	mkdir -p build docs &&
Signature · 1 line
1.5.3.GIT
#29 lore raw

Re: [PATCH] Add more tests for git-clean

In reply to Junio C Hamano

Hi,

Quoted 12 lines · Junio C Hamano

t/t7300-clean.sh test_expect_success 'git-clean' ' · line 82

Quoted 7 lines · Shawn Bohrer
t/t7300-clean.sh +7 -0
+test_expect_success 'git-clean with prefix' '
+
+	mkdir -p build docs &&
+	touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+	cd src/ &&
+	git-clean &&
+	cd - &&

This is wrong for two reasons.

Quoted 4 lines

- Is "cd -" portable?

- What happens when git-clean fails? This test fails, and then it goes on to the next test without cd'ing back.

So it should be

	(cd src/ && git clean) &&

right? (Note that I also removed the dash, since it will be a builtin after the next commit.)

Ciao,
Dscho