+
Skip to content

clean: add config.exclude and --remove-excluded #1725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Documentation/config/clean.adoc
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
clean.requireForce::
A boolean to make git-clean refuse to delete files unless -f
is given. Defaults to true.

clean.exclude::
Additional exclude patterns that have higher priority than the standard
linkgit:gitignore[5] rules and will be honored in (almost) all cases,
even if the `-x` or `-X` options are given. These patterns are intended
to be used for user-specific "precious" files such as IDE configuration
that must not be removed even if a pristine build is desired. This list
has the same priority and semantics as the `-e` command line option.

The `--remove-excluded` command line option can be used to disregard
these exclude patterns (intentionally no short form).
22 changes: 15 additions & 7 deletions Documentation/git-clean.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,10 @@ OPTIONS
Be quiet, only report errors, but not the files that are
successfully removed.

-e <pattern>::
--exclude=<pattern>::
Use the given exclude pattern in addition to the standard ignore rules
(see linkgit:gitignore[5]).

-x::
Don't use the standard ignore rules (see linkgit:gitignore[5]), but
still use the ignore rules given with `-e` options from the command
line. This allows removing all untracked
still use the ignore rules given with the `-e` command line option or the
`clean.exclude` configuration variable. This allows removing all untracked
files, including build products. This can be used (possibly in
conjunction with 'git restore' or 'git reset') to create a pristine
working directory to test a clean build.
Expand All @@ -76,6 +71,19 @@ OPTIONS
Remove only files ignored by Git. This may be useful to rebuild
everything from scratch, but keep manually created files.

-e <pattern>::
--exclude=<pattern>::
Use the given exclude pattern in addition to the standard ignore rules
(see linkgit:gitignore[5]). Exclude patterns can also be configured
using the `clean.exclude` configuration variable. These patterns have
higher priority than the `-x` or `-X` options and will be honored
even in their presence.

--remove-excluded::
Disregard the additional exclude patterns provided by `-e` or the
configuration variable `clean.exclude`. This flag has the highest
priority and intentionally does not have a short form.

Interactive mode
----------------
When the command enters the interactive mode, it shows the
Expand Down
32 changes: 21 additions & 11 deletions builtin/clean.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
static int require_force = -1; /* unset */
static int interactive;
static struct string_list del_list = STRING_LIST_INIT_DUP;
static struct string_list config_exclude_list = STRING_LIST_INIT_DUP;
static unsigned int colopts;

static const char *const builtin_clean_usage[] = {
Expand Down Expand Up @@ -133,6 +134,11 @@ static int git_clean_config(const char *var, const char *value,
return 0;
}

if (!strcmp(var, "clean.exclude")) {
string_list_append(&config_exclude_list, value);
return 0;
}

if (git_color_config(var, value, cb) < 0)
return -1;

Expand Down Expand Up @@ -923,15 +929,15 @@ int cmd_clean(int argc,
struct repository *repo UNUSED)
{
int i, res;
int dry_run = 0, remove_directories = 0, quiet = 0, ignored = 0;
int dry_run = 0, remove_directories = 0, quiet = 0, remove_ignored = 0;
int ignored_only = 0, force = 0, errors = 0, gone = 1;
int remove_excluded = 0;
int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
struct strbuf abs_path = STRBUF_INIT;
struct dir_struct dir = DIR_INIT;
struct pathspec pathspec;
struct strbuf buf = STRBUF_INIT;
struct string_list exclude_list = STRING_LIST_INIT_NODUP;
struct pattern_list *pl;
struct string_list_item *item;
const char *qname;
struct option options[] = {
Expand All @@ -941,11 +947,13 @@ int cmd_clean(int argc,
OPT_BOOL('i', "interactive", &interactive, N_("interactive cleaning")),
OPT_BOOL('d', NULL, &remove_directories,
N_("remove whole directories")),
OPT_CALLBACK_F('e', "exclude", &exclude_list, N_("pattern"),
N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG, exclude_cb),
OPT_BOOL('x', NULL, &ignored, N_("remove ignored files, too")),
OPT_BOOL('x', NULL, &remove_ignored, N_("remove ignored files, too")),
OPT_BOOL('X', NULL, &ignored_only,
N_("remove only ignored files")),
OPT_CALLBACK_F('e', "exclude", &exclude_list, N_("pattern"),
N_("always exclude <pattern> from cleaning (overrides -x)"), PARSE_OPT_NONEG, exclude_cb),
OPT_BOOL(0, "remove-excluded", &remove_excluded,
N_("remove excluded files, too (overrides -e and clean.exclude)")),
OPT_END()
};

Expand All @@ -964,9 +972,9 @@ int cmd_clean(int argc,

dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;

if (ignored && ignored_only)
if (remove_ignored && ignored_only)
die(_("options '%s' and '%s' cannot be used together"), "-x", "-X");
if (!ignored)
if (!remove_ignored)
setup_standard_excludes(&dir);
if (ignored_only)
dir.flags |= DIR_SHOW_IGNORED;
Expand Down Expand Up @@ -996,7 +1004,7 @@ int cmd_clean(int argc,
* recursing into a directory which is itself ignored.
*/
dir.flags |= DIR_SHOW_IGNORED_TOO;
if (!ignored)
if (!remove_ignored)
dir.flags |= DIR_SHOW_IGNORED_TOO_MODE_MATCHING;

/*
Expand All @@ -1017,9 +1025,10 @@ int cmd_clean(int argc,
if (repo_read_index(the_repository) < 0)
die(_("index file corrupt"));

pl = add_pattern_list(&dir, EXC_CMDL, "--exclude option");
for (i = 0; i < exclude_list.nr; i++)
add_pattern(exclude_list.items[i].string, "", 0, pl, -(i+1));
if (!remove_excluded) {
add_patterns_from_string_list(&dir, EXC_CMDL, "--exclude option", &exclude_list);
add_patterns_from_string_list(&dir, EXC_CMDL, "clean.exclude", &config_exclude_list);
}

parse_pathspec(&pathspec, 0,
PATHSPEC_PREFER_CWD,
Expand Down Expand Up @@ -1094,6 +1103,7 @@ int cmd_clean(int argc,
strbuf_release(&buf);
string_list_clear(&del_list, 0);
string_list_clear(&exclude_list, 0);
string_list_clear(&config_exclude_list, 0);
clear_pathspec(&pathspec);
return (errors != 0);
}
15 changes: 15 additions & 0 deletions dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,21 @@ struct pattern_list *add_pattern_list(struct dir_struct *dir,
return pl;
}

/*
* Convenience function to convert a string_list into pattern_list.
*/
struct pattern_list *add_patterns_from_string_list(struct dir_struct *dir,
int group_type,
const char *src,
struct string_list *sl)
{
struct pattern_list *pl;
pl = add_pattern_list(dir, group_type, src);
for (int i = 0; i < sl->nr; i++)
add_pattern(sl->items[i].string, "", 0, pl, -(i+1));
return pl;
}

/*
* Used to set up core.excludesfile and .git/info/exclude lists.
*/
Expand Down
4 changes: 4 additions & 0 deletions dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ int hashmap_contains_parent(struct hashmap *map,
struct strbuf *buffer);
struct pattern_list *add_pattern_list(struct dir_struct *dir,
int group_type, const char *src);
struct pattern_list *add_patterns_from_string_list(struct dir_struct *dir,
int group_type,
const char *src,
struct string_list *lst);
int add_patterns_from_file_to_list(const char *fname, const char *base, int baselen,
struct pattern_list *pl, struct index_state *istate,
unsigned flags);
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载