All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] clean: add --null option
@ 2012-04-15  9:09 Ross Lagerwall
  2012-04-16 20:14 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Ross Lagerwall @ 2012-04-15  9:09 UTC (permalink / raw)
  To: git; +Cc: Clemens Buchacher, Ross Lagerwall

Add a --null (-z) option to git-clean which prints out the files
and directories separated with a NUL character rather than '\n'.
This makes it useful in conjunction with xargs -0.

Signed-off-by: Ross Lagerwall <rosslagerwall@gmail.com>
---
This updated patch converts use_nul to a static variable and reformats
some lines.

 Documentation/git-clean.txt |    7 ++++++-
 builtin/clean.c             |   28 +++++++++++++++++++++-------
 2 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/Documentation/git-clean.txt b/Documentation/git-clean.txt
index 79fb984..0150484 100644
--- a/Documentation/git-clean.txt
+++ b/Documentation/git-clean.txt
@@ -8,7 +8,8 @@ git-clean - Remove untracked files from the working tree
 SYNOPSIS
 --------
 [verse]
-'git clean' [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>...
+'git clean' [-d] [-f] [-n] [-z] [-q] [-e <pattern>] [-x | -X]
+	    [--] <path>...
 
 DESCRIPTION
 -----------
@@ -40,6 +41,10 @@ OPTIONS
 --dry-run::
 	Don't actually remove anything, just show what would be done.
 
+-z::
+--null::
+	Separate paths with the NUL character.
+
 -q::
 --quiet::
 	Be quiet, only report errors, but not the files that are
diff --git a/builtin/clean.c b/builtin/clean.c
index 0c7b3d0..9ce01d1 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -14,9 +14,10 @@
 #include "quote.h"
 
 static int force = -1; /* unset */
+static int use_nul;
 
 static const char *const builtin_clean_usage[] = {
-	"git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...",
+	"git clean [-d] [-f] [-n] [-z] [-q] [-e <pattern>] [-x | -X] [--] <paths>...",
 	NULL
 };
 
@@ -34,6 +35,15 @@ static int exclude_cb(const struct option *opt, const char *arg, int unset)
 	return 0;
 }
 
+static void print_path(const char *msg, const char *name)
+{
+	if (use_nul) {
+		fputs(name, stdout);
+		putchar('\0');
+	} else
+		printf(msg, name);
+}
+
 int cmd_clean(int argc, const char **argv, const char *prefix)
 {
 	int i;
@@ -50,6 +60,8 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 	struct option options[] = {
 		OPT__QUIET(&quiet, "do not print names of files removed"),
 		OPT__DRY_RUN(&show_only, "dry run"),
+		OPT_BOOLEAN('z', "null", &use_nul,
+			    "paths are separated with NUL character"),
 		OPT__FORCE(&force, "force"),
 		OPT_BOOLEAN('d', NULL, &remove_directories,
 				"remove whole directories"),
@@ -152,20 +164,22 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 			qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
 			if (show_only && (remove_directories ||
 			    (matches == MATCHED_EXACTLY))) {
-				printf(_("Would remove %s\n"), qname);
+				print_path(_("Would remove %s\n"), qname);
 			} else if (remove_directories ||
 				   (matches == MATCHED_EXACTLY)) {
 				if (!quiet)
-					printf(_("Removing %s\n"), qname);
+					print_path(_("Removing %s\n"), qname);
 				if (remove_dir_recursively(&directory,
 							   rm_flags) != 0) {
 					warning(_("failed to remove %s"), qname);
 					errors++;
 				}
 			} else if (show_only) {
-				printf(_("Would not remove %s\n"), qname);
+				if (!use_nul)
+					printf(_("Would not remove %s\n"), qname);
 			} else {
-				printf(_("Not removing %s\n"), qname);
+				if (!use_nul)
+					printf(_("Not removing %s\n"), qname);
 			}
 			strbuf_reset(&directory);
 		} else {
@@ -173,10 +187,10 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 				continue;
 			qname = quote_path_relative(ent->name, -1, &buf, prefix);
 			if (show_only) {
-				printf(_("Would remove %s\n"), qname);
+				print_path(_("Would remove %s\n"), qname);
 				continue;
 			} else if (!quiet) {
-				printf(_("Removing %s\n"), qname);
+				print_path(_("Removing %s\n"), qname);
 			}
 			if (unlink(ent->name) != 0) {
 				warning(_("failed to remove %s"), qname);
-- 
1.7.10

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] clean: add --null option
  2012-04-15  9:09 [PATCH v2] clean: add --null option Ross Lagerwall
@ 2012-04-16 20:14 ` Junio C Hamano
  2012-04-18 16:59   ` Ross Lagerwall
  0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2012-04-16 20:14 UTC (permalink / raw)
  To: Ross Lagerwall; +Cc: git, Clemens Buchacher

Ross Lagerwall <rosslagerwall@gmail.com> writes:

> Add a --null (-z) option to git-clean which prints out the files
> and directories separated with a NUL character rather than '\n'.
> This makes it useful in conjunction with xargs -0.

Hrm, I do not like this at all.

The parameters to print_path() are all _("l10n-ready string"). Isn't it a
clue already that they are not meant for machine consumption?  Why are we
moving to cast the output from an end-user facing Porcelain command in
stone?

I suspect that older days "git clean" was a scripted Porcelain around a
plumbing (perhaps ls-files?---I didn't bother digging its history as I do
not care too deeply about "git clean" which I do not use myself).  If we
have piled so many features into "git clean" after the rewrite-to-C to
make it impossible to express what "git clean" _would_ do in terms of
"ls-files", perhaps that is what needs to be fixed, so that we can express
"git clean --some-set-of-options" as:

    git ls-files -z --set-of-options-equivalent-to-what-is-given-to-clean |
    xargs -0 rm -fr

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] clean: add --null option
  2012-04-16 20:14 ` Junio C Hamano
@ 2012-04-18 16:59   ` Ross Lagerwall
  0 siblings, 0 replies; 3+ messages in thread
From: Ross Lagerwall @ 2012-04-18 16:59 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Clemens Buchacher

On 04/16/2012 10:14 PM, Junio C Hamano wrote:
> I suspect that older days "git clean" was a scripted Porcelain around a
> plumbing (perhaps ls-files?---I didn't bother digging its history as I do
> not care too deeply about "git clean" which I do not use myself).

Do you just use ls-files directly?

>  If we
> have piled so many features into "git clean" after the rewrite-to-C to
> make it impossible to express what "git clean" _would_ do in terms of
> "ls-files", perhaps that is what needs to be fixed, so that we can express
> "git clean --some-set-of-options" as:
> 
>     git ls-files -z --set-of-options-equivalent-to-what-is-given-to-clean |
>     xargs -0 rm -fr
> 

I agree that a --null option to git-clean is unnecessary if git-ls-files
is used. It seems like they can be made pretty much equivalent except
that git-ls-files doesn't seem to have a way of listing "only files" or
"only directories"?

-- 
Ross Lagerwall

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-04-18 16:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-15  9:09 [PATCH v2] clean: add --null option Ross Lagerwall
2012-04-16 20:14 ` Junio C Hamano
2012-04-18 16:59   ` Ross Lagerwall

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.