{"thread":{"id":"30242","subject":"[PATCH] clean: add --null option","startedAt":"2012-04-14T17:55:58Z","lastAt":"2012-04-14T19:38:35Z","messageCount":2,"participants":["Ross Lagerwall","Clemens Buchacher"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"189268","messageId":"1334426158-13607-1-git-send-email-rosslagerwall@gmail.com","threadId":"30242","inReplyTo":null,"subject":"[PATCH] clean: add --null option","fromName":"Ross Lagerwall","fromEmail":"rosslagerwall@gmail.com","sentAt":"2012-04-14T17:55:58Z","receivedAt":"2012-04-14T17:55:58Z","isPatch":true,"sender":{"key":"rosslagerwall@gmail.com","avatar":"https://avatars.githubusercontent.com/u/878055?v=4"},"body":"Add a --null (-z) option to git-clean which prints out the files\nand directories separated with a NUL character rather than '\\n'.\nThis makes it useful in conjunction with xargs -0.\n\nSigned-off-by: Ross Lagerwall <rosslagerwall@gmail.com>\n---\n Documentation/git-clean.txt |    7 ++++++-\n builtin/clean.c             |   33 +++++++++++++++++++++++++--------\n 2 files changed, 31 insertions(+), 9 deletions(-)\n\ndiff --git a/Documentation/git-clean.txt b/Documentation/git-clean.txt\nindex 79fb984..0150484 100644\n--- a/Documentation/git-clean.txt\n+++ b/Documentation/git-clean.txt\n@@ -8,7 +8,8 @@ git-clean - Remove untracked files from the working tree\n SYNOPSIS\n --------\n [verse]\n-'git clean' [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>...\n+'git clean' [-d] [-f] [-n] [-z] [-q] [-e <pattern>] [-x | -X]\n+\t    [--] <path>...\n \n DESCRIPTION\n -----------\n@@ -40,6 +41,10 @@ OPTIONS\n --dry-run::\n \tDon't actually remove anything, just show what would be done.\n \n+-z::\n+--null::\n+\tSeparate paths with the NUL character.\n+\n -q::\n --quiet::\n \tBe quiet, only report errors, but not the files that are\ndiff --git a/builtin/clean.c b/builtin/clean.c\nindex 0c7b3d0..b2947e3 100644\n--- a/builtin/clean.c\n+++ b/builtin/clean.c\n@@ -16,7 +16,7 @@\n static int force = -1; /* unset */\n \n static const char *const builtin_clean_usage[] = {\n-\t\"git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...\",\n+\t\"git clean [-d] [-f] [-n] [-z] [-q] [-e <pattern>] [-x | -X] [--] <paths>...\",\n \tNULL\n };\n \n@@ -34,11 +34,20 @@ static int exclude_cb(const struct option *opt, const char *arg, int unset)\n \treturn 0;\n }\n \n+static void print_path(const char *msg, const char *name, int use_nul)\n+{\n+\tif (use_nul) {\n+\t\tfputs(name, stdout);\n+\t\tputchar('\\0');\n+\t} else\n+\t\tprintf(msg, name);\n+}\n+\n int cmd_clean(int argc, const char **argv, const char *prefix)\n {\n \tint i;\n \tint show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;\n-\tint ignored_only = 0, config_set = 0, errors = 0;\n+\tint ignored_only = 0, config_set = 0, errors = 0, use_nul = 0;\n \tint rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;\n \tstruct strbuf directory = STRBUF_INIT;\n \tstruct dir_struct dir;\n@@ -50,6 +59,8 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \tstruct option options[] = {\n \t\tOPT__QUIET(&quiet, \"do not print names of files removed\"),\n \t\tOPT__DRY_RUN(&show_only, \"dry run\"),\n+\t\tOPT_BOOLEAN('z', \"null\", &use_nul,\n+\t\t\t    \"paths are separated with NUL character\"),\n \t\tOPT__FORCE(&force, \"force\"),\n \t\tOPT_BOOLEAN('d', NULL, &remove_directories,\n \t\t\t\t\"remove whole directories\"),\n@@ -152,20 +163,25 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \t\t\tqname = quote_path_relative(directory.buf, directory.len, &buf, prefix);\n \t\t\tif (show_only && (remove_directories ||\n \t\t\t    (matches == MATCHED_EXACTLY))) {\n-\t\t\t\tprintf(_(\"Would remove %s\\n\"), qname);\n+\t\t\t\tprint_path(_(\"Would remove %s\\n\"),\n+\t\t\t\t\t   qname, use_nul);\n \t\t\t} else if (remove_directories ||\n \t\t\t\t   (matches == MATCHED_EXACTLY)) {\n \t\t\t\tif (!quiet)\n-\t\t\t\t\tprintf(_(\"Removing %s\\n\"), qname);\n+\t\t\t\t\tprint_path(_(\"Removing %s\\n\"), qname,\n+\t\t\t\t\t\t   use_nul);\n \t\t\t\tif (remove_dir_recursively(&directory,\n \t\t\t\t\t\t\t   rm_flags) != 0) {\n \t\t\t\t\twarning(_(\"failed to remove %s\"), qname);\n \t\t\t\t\terrors++;\n \t\t\t\t}\n \t\t\t} else if (show_only) {\n-\t\t\t\tprintf(_(\"Would not remove %s\\n\"), qname);\n+\t\t\t\tif (!use_nul)\n+\t\t\t\t\tprintf(_(\"Would not remove %s\\n\"),\n+\t\t\t\t\t       qname);\n \t\t\t} else {\n-\t\t\t\tprintf(_(\"Not removing %s\\n\"), qname);\n+\t\t\t\tif (!use_nul)\n+\t\t\t\t\tprintf(_(\"Not removing %s\\n\"), qname);\n \t\t\t}\n \t\t\tstrbuf_reset(&directory);\n \t\t} else {\n@@ -173,10 +189,11 @@ int cmd_clean(int argc, const char **argv, const char *prefix)\n \t\t\t\tcontinue;\n \t\t\tqname = quote_path_relative(ent->name, -1, &buf, prefix);\n \t\t\tif (show_only) {\n-\t\t\t\tprintf(_(\"Would remove %s\\n\"), qname);\n+\t\t\t\tprint_path(_(\"Would remove %s\\n\"), qname,\n+\t\t\t\t\t   use_nul);\n \t\t\t\tcontinue;\n \t\t\t} else if (!quiet) {\n-\t\t\t\tprintf(_(\"Removing %s\\n\"), qname);\n+\t\t\t\tprint_path(_(\"Removing %s\\n\"), qname, use_nul);\n \t\t\t}\n \t\t\tif (unlink(ent->name) != 0) {\n \t\t\t\twarning(_(\"failed to remove %s\"), qname);\n-- \n1.7.10\n"},{"id":"189281","messageId":"20120414193833.GA29999@ecki","threadId":"30242","inReplyTo":"1334426158-13607-1-git-send-email-rosslagerwall@gmail.com","subject":"Re: [PATCH] clean: add --null option","fromName":"Clemens Buchacher","fromEmail":"drizzd@aon.at","sentAt":"2012-04-14T19:38:35Z","receivedAt":"2012-04-14T19:38:35Z","isPatch":true,"sender":{"key":"drizzd@gmx.net","avatar":"https://avatars.githubusercontent.com/u/59082?v=4"},"body":"On Sat, Apr 14, 2012 at 07:55:58PM +0200, Ross Lagerwall wrote:\n>\n> Add a --null (-z) option to git-clean which prints out the files\n> and directories separated with a NUL character rather than '\\n'.\n> This makes it useful in conjunction with xargs -0.\n\nThanks. Looks sensible to me.\n\n> -\t\t\t\t\tprintf(_(\"Removing %s\\n\"), qname);\n> +\t\t\t\t\tprint_path(_(\"Removing %s\\n\"), qname,\n> +\t\t\t\t\t\t   use_nul);\n\nAlthough exceeding the 80 column boundary, I think this would be more\nreadable without line break. If we want to stay within 80 columns, I\ndon't think you can use 5 levels of indentation at the same time.\n\nYou could also make use_nul a static global, although it probably does\nnot improve the situation much.\n"}]}