2007-11-12 04:48:47 +03:00
|
|
|
/*
|
|
|
|
* "git clean" builtin command
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Shawn Bohrer
|
|
|
|
*
|
|
|
|
* Based on git-clean.sh by Pavel Roskin
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "cache.h"
|
|
|
|
#include "dir.h"
|
|
|
|
#include "parse-options.h"
|
2010-07-20 23:35:56 +04:00
|
|
|
#include "string-list.h"
|
2008-03-07 04:13:17 +03:00
|
|
|
#include "quote.h"
|
2007-11-12 04:48:47 +03:00
|
|
|
|
2007-11-13 08:13:05 +03:00
|
|
|
static int force = -1; /* unset */
|
2007-11-12 04:48:47 +03:00
|
|
|
|
|
|
|
static const char *const builtin_clean_usage[] = {
|
2012-08-20 16:32:01 +04:00
|
|
|
N_("git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>..."),
|
2007-11-12 04:48:47 +03:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2008-05-14 21:46:53 +04:00
|
|
|
static int git_clean_config(const char *var, const char *value, void *cb)
|
2007-11-12 04:48:47 +03:00
|
|
|
{
|
|
|
|
if (!strcmp(var, "clean.requireforce"))
|
|
|
|
force = !git_config_bool(var, value);
|
2008-05-14 21:46:53 +04:00
|
|
|
return git_default_config(var, value, cb);
|
2007-11-12 04:48:47 +03:00
|
|
|
}
|
|
|
|
|
2010-07-20 23:35:56 +04:00
|
|
|
static int exclude_cb(const struct option *opt, const char *arg, int unset)
|
|
|
|
{
|
|
|
|
struct string_list *exclude_list = opt->value;
|
|
|
|
string_list_append(exclude_list, arg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-11-12 04:48:47 +03:00
|
|
|
int cmd_clean(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
2007-12-05 10:55:41 +03:00
|
|
|
int i;
|
2007-11-12 04:48:47 +03:00
|
|
|
int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
|
2010-11-15 09:42:44 +03:00
|
|
|
int ignored_only = 0, config_set = 0, errors = 0;
|
2009-07-01 02:33:45 +04:00
|
|
|
int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
|
2008-10-09 23:12:12 +04:00
|
|
|
struct strbuf directory = STRBUF_INIT;
|
2007-11-12 04:48:47 +03:00
|
|
|
struct dir_struct dir;
|
|
|
|
static const char **pathspec;
|
2008-10-09 23:12:12 +04:00
|
|
|
struct strbuf buf = STRBUF_INIT;
|
2010-09-07 03:32:55 +04:00
|
|
|
struct string_list exclude_list = STRING_LIST_INIT_NODUP;
|
2008-03-07 04:13:17 +03:00
|
|
|
const char *qname;
|
2007-12-05 10:55:41 +03:00
|
|
|
char *seen = NULL;
|
2007-11-12 04:48:47 +03:00
|
|
|
struct option options[] = {
|
2012-08-20 16:32:01 +04:00
|
|
|
OPT__QUIET(&quiet, N_("do not print names of files removed")),
|
|
|
|
OPT__DRY_RUN(&show_only, N_("dry run")),
|
|
|
|
OPT__FORCE(&force, N_("force")),
|
2007-11-12 04:48:47 +03:00
|
|
|
OPT_BOOLEAN('d', NULL, &remove_directories,
|
2012-08-20 16:32:01 +04:00
|
|
|
N_("remove whole directories")),
|
|
|
|
{ OPTION_CALLBACK, 'e', "exclude", &exclude_list, N_("pattern"),
|
|
|
|
N_("add <pattern> to ignore rules"), PARSE_OPT_NONEG, exclude_cb },
|
|
|
|
OPT_BOOLEAN('x', NULL, &ignored, N_("remove ignored files, too")),
|
2007-11-12 04:48:47 +03:00
|
|
|
OPT_BOOLEAN('X', NULL, &ignored_only,
|
2012-08-20 16:32:01 +04:00
|
|
|
N_("remove only ignored files")),
|
2007-11-12 04:48:47 +03:00
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
2008-05-14 21:46:53 +04:00
|
|
|
git_config(git_clean_config, NULL);
|
2007-11-13 08:13:05 +03:00
|
|
|
if (force < 0)
|
|
|
|
force = 0;
|
|
|
|
else
|
|
|
|
config_set = 1;
|
|
|
|
|
2009-05-23 22:53:12 +04:00
|
|
|
argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
|
|
|
|
0);
|
2007-11-12 04:48:47 +03:00
|
|
|
|
|
|
|
memset(&dir, 0, sizeof(dir));
|
2007-11-15 08:00:54 +03:00
|
|
|
if (ignored_only)
|
2009-02-16 15:20:25 +03:00
|
|
|
dir.flags |= DIR_SHOW_IGNORED;
|
2007-11-12 04:48:47 +03:00
|
|
|
|
|
|
|
if (ignored && ignored_only)
|
2011-02-23 02:42:21 +03:00
|
|
|
die(_("-x and -X cannot be used together"));
|
2007-11-12 04:48:47 +03:00
|
|
|
|
2011-02-23 02:42:22 +03:00
|
|
|
if (!show_only && !force) {
|
|
|
|
if (config_set)
|
|
|
|
die(_("clean.requireForce set to true and neither -n nor -f given; "
|
|
|
|
"refusing to clean"));
|
|
|
|
else
|
|
|
|
die(_("clean.requireForce defaults to true and neither -n nor -f given; "
|
|
|
|
"refusing to clean"));
|
|
|
|
}
|
2007-11-12 04:48:47 +03:00
|
|
|
|
2009-07-01 02:33:45 +04:00
|
|
|
if (force > 1)
|
|
|
|
rm_flags = 0;
|
|
|
|
|
2009-02-16 15:20:25 +03:00
|
|
|
dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
|
2007-11-12 04:48:47 +03:00
|
|
|
|
2009-08-20 17:47:01 +04:00
|
|
|
if (read_cache() < 0)
|
2011-02-23 02:42:21 +03:00
|
|
|
die(_("index file corrupt"));
|
2009-08-20 17:47:01 +04:00
|
|
|
|
2007-11-15 08:00:54 +03:00
|
|
|
if (!ignored)
|
|
|
|
setup_standard_excludes(&dir);
|
2007-11-12 04:48:47 +03:00
|
|
|
|
2010-07-20 23:35:56 +04:00
|
|
|
for (i = 0; i < exclude_list.nr; i++)
|
2011-08-25 22:29:57 +04:00
|
|
|
add_exclude(exclude_list.items[i].string, "", 0,
|
|
|
|
&dir.exclude_list[EXC_CMDL]);
|
2010-07-20 23:35:56 +04:00
|
|
|
|
2007-11-12 04:48:47 +03:00
|
|
|
pathspec = get_pathspec(prefix, argv);
|
|
|
|
|
2009-05-15 00:22:36 +04:00
|
|
|
fill_directory(&dir, pathspec);
|
2007-11-12 04:48:47 +03:00
|
|
|
|
2007-12-05 10:55:41 +03:00
|
|
|
if (pathspec)
|
2008-01-12 12:04:32 +03:00
|
|
|
seen = xmalloc(argc > 0 ? argc : 1);
|
2007-12-05 10:55:41 +03:00
|
|
|
|
|
|
|
for (i = 0; i < dir.nr; i++) {
|
|
|
|
struct dir_entry *ent = dir.entries[i];
|
2008-04-15 07:14:09 +04:00
|
|
|
int len, pos;
|
|
|
|
int matches = 0;
|
2007-11-12 04:48:47 +03:00
|
|
|
struct cache_entry *ce;
|
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove the '/' at the end that directory
|
|
|
|
* walking adds for directory entries.
|
|
|
|
*/
|
|
|
|
len = ent->len;
|
|
|
|
if (len && ent->name[len-1] == '/')
|
|
|
|
len--;
|
|
|
|
pos = cache_name_pos(ent->name, len);
|
|
|
|
if (0 <= pos)
|
|
|
|
continue; /* exact match */
|
|
|
|
pos = -pos - 1;
|
|
|
|
if (pos < active_nr) {
|
|
|
|
ce = active_cache[pos];
|
|
|
|
if (ce_namelen(ce) == len &&
|
|
|
|
!memcmp(ce->name, ent->name, len))
|
|
|
|
continue; /* Yup, this one exists unmerged */
|
|
|
|
}
|
|
|
|
|
2007-12-05 10:55:41 +03:00
|
|
|
/*
|
|
|
|
* we might have removed this as part of earlier
|
|
|
|
* recursive directory removal, so lstat() here could
|
|
|
|
* fail with ENOENT.
|
|
|
|
*/
|
|
|
|
if (lstat(ent->name, &st))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (pathspec) {
|
2008-01-12 12:04:32 +03:00
|
|
|
memset(seen, 0, argc > 0 ? argc : 1);
|
2008-04-15 07:14:09 +04:00
|
|
|
matches = match_pathspec(pathspec, ent->name, len,
|
2010-11-15 09:42:44 +03:00
|
|
|
0, seen);
|
2007-12-05 10:55:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2007-11-12 04:48:47 +03:00
|
|
|
strbuf_addstr(&directory, ent->name);
|
2008-03-07 04:13:17 +03:00
|
|
|
qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
|
2008-04-15 07:14:09 +04:00
|
|
|
if (show_only && (remove_directories ||
|
|
|
|
(matches == MATCHED_EXACTLY))) {
|
2011-02-23 02:42:21 +03:00
|
|
|
printf(_("Would remove %s\n"), qname);
|
2008-04-15 07:14:09 +04:00
|
|
|
} else if (remove_directories ||
|
|
|
|
(matches == MATCHED_EXACTLY)) {
|
2008-02-21 04:44:46 +03:00
|
|
|
if (!quiet)
|
2011-02-23 02:42:21 +03:00
|
|
|
printf(_("Removing %s\n"), qname);
|
2009-07-01 02:33:45 +04:00
|
|
|
if (remove_dir_recursively(&directory,
|
|
|
|
rm_flags) != 0) {
|
2011-02-23 02:42:21 +03:00
|
|
|
warning(_("failed to remove %s"), qname);
|
2008-02-21 04:44:46 +03:00
|
|
|
errors++;
|
|
|
|
}
|
2007-11-12 04:48:47 +03:00
|
|
|
} else if (show_only) {
|
2011-02-23 02:42:21 +03:00
|
|
|
printf(_("Would not remove %s\n"), qname);
|
2007-11-12 04:48:47 +03:00
|
|
|
} else {
|
2011-02-23 02:42:21 +03:00
|
|
|
printf(_("Not removing %s\n"), qname);
|
2007-11-12 04:48:47 +03:00
|
|
|
}
|
|
|
|
strbuf_reset(&directory);
|
|
|
|
} else {
|
2007-12-05 10:55:41 +03:00
|
|
|
if (pathspec && !matches)
|
|
|
|
continue;
|
2008-03-07 04:13:17 +03:00
|
|
|
qname = quote_path_relative(ent->name, -1, &buf, prefix);
|
2007-11-12 04:48:47 +03:00
|
|
|
if (show_only) {
|
2011-02-23 02:42:21 +03:00
|
|
|
printf(_("Would remove %s\n"), qname);
|
2007-11-12 04:48:47 +03:00
|
|
|
continue;
|
|
|
|
} else if (!quiet) {
|
2011-02-23 02:42:21 +03:00
|
|
|
printf(_("Removing %s\n"), qname);
|
2007-11-12 04:48:47 +03:00
|
|
|
}
|
2008-02-21 04:44:46 +03:00
|
|
|
if (unlink(ent->name) != 0) {
|
2011-02-23 02:42:21 +03:00
|
|
|
warning(_("failed to remove %s"), qname);
|
2008-02-21 04:44:46 +03:00
|
|
|
errors++;
|
|
|
|
}
|
2007-11-12 04:48:47 +03:00
|
|
|
}
|
|
|
|
}
|
2007-12-05 10:55:41 +03:00
|
|
|
free(seen);
|
2007-11-12 04:48:47 +03:00
|
|
|
|
|
|
|
strbuf_release(&directory);
|
2010-07-20 23:35:56 +04:00
|
|
|
string_list_clear(&exclude_list, 0);
|
2008-02-21 04:44:46 +03:00
|
|
|
return (errors != 0);
|
2007-11-12 04:48:47 +03:00
|
|
|
}
|