Skip to content

Commit 07de4eb

Browse files
jhancegitster
authored andcommitted
Add -e/--exclude to git-clean.
With the -e/--exclude option for git-clean, a user can specify files that they haven't yet told git about, but either need for a short amount of time or plan to tell git about them later. This allows one to still use git-clean while these files are around without losing data. Signed-off-by: Jared Hance <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c5212b8 commit 07de4eb

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

Documentation/git-clean.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-clean - Remove untracked files from the working tree
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git clean' [-d] [-f] [-n] [-q] [-x | -X] [--] <path>...
11+
'git clean' [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <path>...
1212

1313
DESCRIPTION
1414
-----------
@@ -45,6 +45,12 @@ OPTIONS
4545
Be quiet, only report errors, but not the files that are
4646
successfully removed.
4747

48+
-e <pattern>::
49+
--exclude=<pattern>::
50+
Specify special exceptions to not be cleaned. Each <pattern> is
51+
the same form as in $GIT_DIR/info/excludes and this option can be
52+
given multiple times.
53+
4854
-x::
4955
Don't use the ignore rules. This allows removing all untracked
5056
files, including build products. This can be used (possibly in

builtin/clean.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
#include "cache.h"
1111
#include "dir.h"
1212
#include "parse-options.h"
13+
#include "string-list.h"
1314
#include "quote.h"
1415

1516
static int force = -1; /* unset */
1617

1718
static const char *const builtin_clean_usage[] = {
18-
"git clean [-d] [-f] [-n] [-q] [-x | -X] [--] <paths>...",
19+
"git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...",
1920
NULL
2021
};
2122

@@ -26,6 +27,13 @@ static int git_clean_config(const char *var, const char *value, void *cb)
2627
return git_default_config(var, value, cb);
2728
}
2829

30+
static int exclude_cb(const struct option *opt, const char *arg, int unset)
31+
{
32+
struct string_list *exclude_list = opt->value;
33+
string_list_append(exclude_list, arg);
34+
return 0;
35+
}
36+
2937
int cmd_clean(int argc, const char **argv, const char *prefix)
3038
{
3139
int i;
@@ -36,6 +44,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
3644
struct dir_struct dir;
3745
static const char **pathspec;
3846
struct strbuf buf = STRBUF_INIT;
47+
struct string_list exclude_list = { NULL, 0, 0, 0 };
3948
const char *qname;
4049
char *seen = NULL;
4150
struct option options[] = {
@@ -44,6 +53,8 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
4453
OPT_BOOLEAN('f', "force", &force, "force"),
4554
OPT_BOOLEAN('d', NULL, &remove_directories,
4655
"remove whole directories"),
56+
{ OPTION_CALLBACK, 'e', "exclude", &exclude_list, "pattern",
57+
"exclude <pattern>", PARSE_OPT_NONEG, exclude_cb },
4758
OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"),
4859
OPT_BOOLEAN('X', NULL, &ignored_only,
4960
"remove only ignored files"),
@@ -81,6 +92,9 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
8192
if (!ignored)
8293
setup_standard_excludes(&dir);
8394

95+
for (i = 0; i < exclude_list.nr; i++)
96+
add_exclude(exclude_list.items[i].string, "", 0, dir.exclude_list);
97+
8498
pathspec = get_pathspec(prefix, argv);
8599

86100
fill_directory(&dir, pathspec);
@@ -167,5 +181,6 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
167181
free(seen);
168182

169183
strbuf_release(&directory);
184+
string_list_clear(&exclude_list, 0);
170185
return (errors != 0);
171186
}

0 commit comments

Comments
 (0)