2007-11-12 02:48:47 +01: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 21:35:56 +02:00
|
|
|
#include "string-list.h"
|
2008-03-07 02:13:17 +01:00
|
|
|
#include "quote.h"
|
2007-11-12 02:48:47 +01:00
|
|
|
|
2007-11-13 06:13:05 +01:00
|
|
|
static int force = -1; /* unset */
|
2007-11-12 02:48:47 +01:00
|
|
|
|
|
|
|
static const char *const builtin_clean_usage[] = {
|
2010-07-20 21:35:56 +02:00
|
|
|
"git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...",
|
2007-11-12 02:48:47 +01:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2008-05-14 19:46:53 +02:00
|
|
|
static int git_clean_config(const char *var, const char *value, void *cb)
|
2007-11-12 02:48:47 +01:00
|
|
|
{
|
|
|
|
if (!strcmp(var, "clean.requireforce"))
|
|
|
|
force = !git_config_bool(var, value);
|
2008-05-14 19:46:53 +02:00
|
|
|
return git_default_config(var, value, cb);
|
2007-11-12 02:48:47 +01:00
|
|
|
}
|
|
|
|
|
2010-07-20 21:35:56 +02: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 02:48:47 +01:00
|
|
|
int cmd_clean(int argc, const char **argv, const char *prefix)
|
|
|
|
{
|
2007-12-05 08:55:41 +01:00
|
|
|
int i;
|
2007-11-12 02:48:47 +01:00
|
|
|
int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
|
2010-11-15 07:42:44 +01:00
|
|
|
int ignored_only = 0, config_set = 0, errors = 0;
|
2009-07-01 00:33:45 +02:00
|
|
|
int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT;
|
2008-10-09 21:12:12 +02:00
|
|
|
struct strbuf directory = STRBUF_INIT;
|
2007-11-12 02:48:47 +01:00
|
|
|
struct dir_struct dir;
|
|
|
|
static const char **pathspec;
|
2008-10-09 21:12:12 +02:00
|
|
|
struct strbuf buf = STRBUF_INIT;
|
2010-09-07 01:32:55 +02:00
|
|
|
struct string_list exclude_list = STRING_LIST_INIT_NODUP;
|
2008-03-07 02:13:17 +01:00
|
|
|
const char *qname;
|
2007-12-05 08:55:41 +01:00
|
|
|
char *seen = NULL;
|
2007-11-12 02:48:47 +01:00
|
|
|
struct option options[] = {
|
2010-11-08 20:54:48 +01:00
|
|
|
OPT__QUIET(&quiet, "do not print names of files removed"),
|
2010-11-08 18:58:51 +01:00
|
|
|
OPT__DRY_RUN(&show_only, "dry run"),
|
2010-11-08 19:01:54 +01:00
|
|
|
OPT__FORCE(&force, "force"),
|
2007-11-12 02:48:47 +01:00
|
|
|
OPT_BOOLEAN('d', NULL, &remove_directories,
|
|
|
|
"remove whole directories"),
|
2010-07-20 21:35:56 +02:00
|
|
|
{ OPTION_CALLBACK, 'e', "exclude", &exclude_list, "pattern",
|
2011-08-25 20:29:57 +02:00
|
|
|
"add <pattern> to ignore rules", PARSE_OPT_NONEG, exclude_cb },
|
2007-11-12 02:48:47 +01:00
|
|
|
OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"),
|
|
|
|
OPT_BOOLEAN('X', NULL, &ignored_only,
|
|
|
|
"remove only ignored files"),
|
|
|
|
OPT_END()
|
|
|
|
};
|
|
|
|
|
2008-05-14 19:46:53 +02:00
|
|
|
git_config(git_clean_config, NULL);
|
2007-11-13 06:13:05 +01:00
|
|
|
if (force < 0)
|
|
|
|
force = 0;
|
|
|
|
else
|
|
|
|
config_set = 1;
|
|
|
|
|
2009-05-23 20:53:12 +02:00
|
|
|
argc = parse_options(argc, argv, prefix, options, builtin_clean_usage,
|
|
|
|
0);
|
2007-11-12 02:48:47 +01:00
|
|
|
|
|
|
|
memset(&dir, 0, sizeof(dir));
|
2007-11-15 06:00:54 +01:00
|
|
|
if (ignored_only)
|
2009-02-16 13:20:25 +01:00
|
|
|
dir.flags |= DIR_SHOW_IGNORED;
|
2007-11-12 02:48:47 +01:00
|
|
|
|
|
|
|
if (ignored && ignored_only)
|
2011-02-23 00:42:21 +01:00
|
|
|
die(_("-x and -X cannot be used together"));
|
2007-11-12 02:48:47 +01:00
|
|
|
|
2011-02-23 00:42:22 +01: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 02:48:47 +01:00
|
|
|
|
2009-07-01 00:33:45 +02:00
|
|
|
if (force > 1)
|
|
|
|
rm_flags = 0;
|
|
|
|
|
2009-02-16 13:20:25 +01:00
|
|
|
dir.flags |= DIR_SHOW_OTHER_DIRECTORIES;
|
2007-11-12 02:48:47 +01:00
|
|
|
|
2009-08-20 15:47:01 +02:00
|
|
|
if (read_cache() < 0)
|
2011-02-23 00:42:21 +01:00
|
|
|
die(_("index file corrupt"));
|
2009-08-20 15:47:01 +02:00
|
|
|
|
2007-11-15 06:00:54 +01:00
|
|
|
if (!ignored)
|
|
|
|
setup_standard_excludes(&dir);
|
2007-11-12 02:48:47 +01:00
|
|
|
|
2010-07-20 21:35:56 +02:00
|
|
|
for (i = 0; i < exclude_list.nr; i++)
|
2011-08-25 20:29:57 +02:00
|
|
|
add_exclude(exclude_list.items[i].string, "", 0,
|
|
|
|
&dir.exclude_list[EXC_CMDL]);
|
2010-07-20 21:35:56 +02:00
|
|
|
|
2007-11-12 02:48:47 +01:00
|
|
|
pathspec = get_pathspec(prefix, argv);
|
|
|
|
|
2009-05-14 22:22:36 +02:00
|
|
|
fill_directory(&dir, pathspec);
|
2007-11-12 02:48:47 +01:00
|
|
|
|
2007-12-05 08:55:41 +01:00
|
|
|
if (pathspec)
|
2008-01-12 10:04:32 +01:00
|
|
|
seen = xmalloc(argc > 0 ? argc : 1);
|
2007-12-05 08:55:41 +01:00
|
|
|
|
|
|
|
for (i = 0; i < dir.nr; i++) {
|
|
|
|
struct dir_entry *ent = dir.entries[i];
|
2008-04-15 05:14:09 +02:00
|
|
|
int len, pos;
|
|
|
|
int matches = 0;
|
2007-11-12 02:48:47 +01: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 08:55:41 +01: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 10:04:32 +01:00
|
|
|
memset(seen, 0, argc > 0 ? argc : 1);
|
2008-04-15 05:14:09 +02:00
|
|
|
matches = match_pathspec(pathspec, ent->name, len,
|
2010-11-15 07:42:44 +01:00
|
|
|
0, seen);
|
2007-12-05 08:55:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2007-11-12 02:48:47 +01:00
|
|
|
strbuf_addstr(&directory, ent->name);
|
2008-03-07 02:13:17 +01:00
|
|
|
qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
|
2008-04-15 05:14:09 +02:00
|
|
|
if (show_only && (remove_directories ||
|
|
|
|
(matches == MATCHED_EXACTLY))) {
|
2011-02-23 00:42:21 +01:00
|
|
|
printf(_("Would remove %s\n"), qname);
|
2008-04-15 05:14:09 +02:00
|
|
|
} else if (remove_directories ||
|
|
|
|
(matches == MATCHED_EXACTLY)) {
|
2008-02-21 02:44:46 +01:00
|
|
|
if (!quiet)
|
2011-02-23 00:42:21 +01:00
|
|
|
printf(_("Removing %s\n"), qname);
|
2009-07-01 00:33:45 +02:00
|
|
|
if (remove_dir_recursively(&directory,
|
|
|
|
rm_flags) != 0) {
|
2011-02-23 00:42:21 +01:00
|
|
|
warning(_("failed to remove %s"), qname);
|
2008-02-21 02:44:46 +01:00
|
|
|
errors++;
|
|
|
|
}
|
2007-11-12 02:48:47 +01:00
|
|
|
} else if (show_only) {
|
2011-02-23 00:42:21 +01:00
|
|
|
printf(_("Would not remove %s\n"), qname);
|
2007-11-12 02:48:47 +01:00
|
|
|
} else {
|
2011-02-23 00:42:21 +01:00
|
|
|
printf(_("Not removing %s\n"), qname);
|
2007-11-12 02:48:47 +01:00
|
|
|
}
|
|
|
|
strbuf_reset(&directory);
|
|
|
|
} else {
|
2007-12-05 08:55:41 +01:00
|
|
|
if (pathspec && !matches)
|
|
|
|
continue;
|
2008-03-07 02:13:17 +01:00
|
|
|
qname = quote_path_relative(ent->name, -1, &buf, prefix);
|
2007-11-12 02:48:47 +01:00
|
|
|
if (show_only) {
|
2011-02-23 00:42:21 +01:00
|
|
|
printf(_("Would remove %s\n"), qname);
|
2007-11-12 02:48:47 +01:00
|
|
|
continue;
|
|
|
|
} else if (!quiet) {
|
2011-02-23 00:42:21 +01:00
|
|
|
printf(_("Removing %s\n"), qname);
|
2007-11-12 02:48:47 +01:00
|
|
|
}
|
2008-02-21 02:44:46 +01:00
|
|
|
if (unlink(ent->name) != 0) {
|
2011-02-23 00:42:21 +01:00
|
|
|
warning(_("failed to remove %s"), qname);
|
2008-02-21 02:44:46 +01:00
|
|
|
errors++;
|
|
|
|
}
|
2007-11-12 02:48:47 +01:00
|
|
|
}
|
|
|
|
}
|
2007-12-05 08:55:41 +01:00
|
|
|
free(seen);
|
2007-11-12 02:48:47 +01:00
|
|
|
|
|
|
|
strbuf_release(&directory);
|
2010-07-20 21:35:56 +02:00
|
|
|
string_list_clear(&exclude_list, 0);
|
2008-02-21 02:44:46 +01:00
|
|
|
return (errors != 0);
|
2007-11-12 02:48:47 +01:00
|
|
|
}
|