* [PATCH] clean: add --null option
@ 2012-04-14 17:55 Ross Lagerwall
2012-04-14 19:38 ` Clemens Buchacher
0 siblings, 1 reply; 2+ messages in thread
From: Ross Lagerwall @ 2012-04-14 17:55 UTC (permalink / raw)
To: git; +Cc: 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>
---
Documentation/git-clean.txt | 7 ++++++-
builtin/clean.c | 33 +++++++++++++++++++++++++--------
2 files changed, 31 insertions(+), 9 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..b2947e3 100644
--- a/builtin/clean.c
+++ b/builtin/clean.c
@@ -16,7 +16,7 @@
static int force = -1; /* unset */
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,11 +34,20 @@ 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, int use_nul)
+{
+ 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;
int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
- int ignored_only = 0, config_set = 0, errors = 0;
+ int ignored_only = 0, config_set = 0, errors = 0, use_nul = 0;
int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
struct strbuf directory = STRBUF_INIT;
struct dir_struct dir;
@@ -50,6 +59,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 +163,25 @@ 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, use_nul);
} else if (remove_directories ||
(matches == MATCHED_EXACTLY)) {
if (!quiet)
- printf(_("Removing %s\n"), qname);
+ print_path(_("Removing %s\n"), qname,
+ use_nul);
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 +189,11 @@ 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,
+ use_nul);
continue;
} else if (!quiet) {
- printf(_("Removing %s\n"), qname);
+ print_path(_("Removing %s\n"), qname, use_nul);
}
if (unlink(ent->name) != 0) {
warning(_("failed to remove %s"), qname);
--
1.7.10
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] clean: add --null option
2012-04-14 17:55 [PATCH] clean: add --null option Ross Lagerwall
@ 2012-04-14 19:38 ` Clemens Buchacher
0 siblings, 0 replies; 2+ messages in thread
From: Clemens Buchacher @ 2012-04-14 19:38 UTC (permalink / raw)
To: Ross Lagerwall; +Cc: git
On Sat, Apr 14, 2012 at 07:55:58PM +0200, Ross Lagerwall wrote:
>
> 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.
Thanks. Looks sensible to me.
> - printf(_("Removing %s\n"), qname);
> + print_path(_("Removing %s\n"), qname,
> + use_nul);
Although exceeding the 80 column boundary, I think this would be more
readable without line break. If we want to stay within 80 columns, I
don't think you can use 5 levels of indentation at the same time.
You could also make use_nul a static global, although it probably does
not improve the situation much.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2012-04-14 19:39 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-04-14 17:55 [PATCH] clean: add --null option Ross Lagerwall
2012-04-14 19:38 ` Clemens Buchacher
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).