diff options
author | Junio C Hamano <gitster@pobox.com> | 2011-07-06 15:38:28 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2011-07-06 15:38:28 -0700 |
commit | 25d33546d474c7c28b72013c262fc23337cb3b21 (patch) | |
tree | 7551eb457a33329be6b3bb0fc11f76118a6e2c42 /builtin/clean.c | |
parent | 71ee7fd15457a0252c089420b5b66de266dcbd2f (diff) | |
parent | f696543dad6c7ba27b0c4fab167a5687263a9ba0 (diff) | |
download | git-25d33546d474c7c28b72013c262fc23337cb3b21.tar.gz |
Merge commit 'v1.7.6' into jc/checkout-reflog-fix
* commit 'v1.7.6': (3211 commits)
Git 1.7.6
completion: replace core.abbrevguard to core.abbrev
Git 1.7.6-rc3
Documentation: git diff --check respects core.whitespace
gitweb: 'pickaxe' and 'grep' features requires 'search' to be enabled
t7810: avoid unportable use of "echo"
plug a few coverity-spotted leaks
builtin/gc.c: add missing newline in message
tests: link shell libraries into valgrind directory
t/Makefile: pass test opts to valgrind target properly
sh-i18n--envsubst.c: do not #include getopt.h
Fix typo: existant->existent
Git 1.7.6-rc2
gitweb: do not misparse nonnumeric content tag files that contain a digit
Git 1.7.6-rc1
fetch: do not leak a refspec
t3703: skip more tests using colons in file names on Windows
gitweb: Fix usability of $prevent_xss
gitweb: Move "Requirements" up in gitweb/INSTALL
gitweb: Describe CSSMIN and JSMIN in gitweb/INSTALL
...
Diffstat (limited to 'builtin/clean.c')
-rw-r--r-- | builtin/clean.c | 191 |
1 files changed, 191 insertions, 0 deletions
diff --git a/builtin/clean.c b/builtin/clean.c new file mode 100644 index 0000000000..75697f7111 --- /dev/null +++ b/builtin/clean.c @@ -0,0 +1,191 @@ +/* + * "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" +#include "string-list.h" +#include "quote.h" + +static int force = -1; /* unset */ + +static const char *const builtin_clean_usage[] = { + "git clean [-d] [-f] [-n] [-q] [-e <pattern>] [-x | -X] [--] <paths>...", + NULL +}; + +static int git_clean_config(const char *var, const char *value, void *cb) +{ + if (!strcmp(var, "clean.requireforce")) + force = !git_config_bool(var, value); + return git_default_config(var, value, cb); +} + +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; +} + +int cmd_clean(int argc, const char **argv, const char *prefix) +{ + int i; + int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0; + int ignored_only = 0, config_set = 0, errors = 0; + int rm_flags = REMOVE_DIR_KEEP_NESTED_GIT; + struct strbuf directory = STRBUF_INIT; + struct dir_struct dir; + static const char **pathspec; + struct strbuf buf = STRBUF_INIT; + struct string_list exclude_list = STRING_LIST_INIT_NODUP; + const char *qname; + char *seen = NULL; + struct option options[] = { + OPT__QUIET(&quiet, "do not print names of files removed"), + OPT__DRY_RUN(&show_only, "dry run"), + OPT__FORCE(&force, "force"), + OPT_BOOLEAN('d', NULL, &remove_directories, + "remove whole directories"), + { OPTION_CALLBACK, 'e', "exclude", &exclude_list, "pattern", + "exclude <pattern>", PARSE_OPT_NONEG, exclude_cb }, + OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"), + OPT_BOOLEAN('X', NULL, &ignored_only, + "remove only ignored files"), + OPT_END() + }; + + git_config(git_clean_config, NULL); + if (force < 0) + force = 0; + else + config_set = 1; + + argc = parse_options(argc, argv, prefix, options, builtin_clean_usage, + 0); + + memset(&dir, 0, sizeof(dir)); + if (ignored_only) + dir.flags |= DIR_SHOW_IGNORED; + + if (ignored && ignored_only) + die(_("-x and -X cannot be used together")); + + 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")); + } + + if (force > 1) + rm_flags = 0; + + dir.flags |= DIR_SHOW_OTHER_DIRECTORIES; + + if (read_cache() < 0) + die(_("index file corrupt")); + + if (!ignored) + setup_standard_excludes(&dir); + + for (i = 0; i < exclude_list.nr; i++) + add_exclude(exclude_list.items[i].string, "", 0, dir.exclude_list); + + pathspec = get_pathspec(prefix, argv); + + fill_directory(&dir, pathspec); + + if (pathspec) + seen = xmalloc(argc > 0 ? argc : 1); + + for (i = 0; i < dir.nr; i++) { + struct dir_entry *ent = dir.entries[i]; + int len, pos; + int matches = 0; + 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 */ + } + + /* + * 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) { + memset(seen, 0, argc > 0 ? argc : 1); + matches = match_pathspec(pathspec, ent->name, len, + 0, seen); + } + + if (S_ISDIR(st.st_mode)) { + strbuf_addstr(&directory, ent->name); + qname = quote_path_relative(directory.buf, directory.len, &buf, prefix); + if (show_only && (remove_directories || + (matches == MATCHED_EXACTLY))) { + printf(_("Would remove %s\n"), qname); + } else if (remove_directories || + (matches == MATCHED_EXACTLY)) { + if (!quiet) + printf(_("Removing %s\n"), qname); + if (remove_dir_recursively(&directory, + rm_flags) != 0) { + warning(_("failed to remove %s"), qname); + errors++; + } + } else if (show_only) { + printf(_("Would not remove %s\n"), qname); + } else { + printf(_("Not removing %s\n"), qname); + } + strbuf_reset(&directory); + } else { + if (pathspec && !matches) + continue; + qname = quote_path_relative(ent->name, -1, &buf, prefix); + if (show_only) { + printf(_("Would remove %s\n"), qname); + continue; + } else if (!quiet) { + printf(_("Removing %s\n"), qname); + } + if (unlink(ent->name) != 0) { + warning(_("failed to remove %s"), qname); + errors++; + } + } + } + free(seen); + + strbuf_release(&directory); + string_list_clear(&exclude_list, 0); + return (errors != 0); +} |