Include diff options in the git-log manpage
[git/platforms.git] / builtin-clean.c
blob56ae4eb9bb4efad179f7d664880457535bd6048f
1 /*
2 * "git clean" builtin command
4 * Copyright (C) 2007 Shawn Bohrer
6 * Based on git-clean.sh by Pavel Roskin
7 */
9 #include "builtin.h"
10 #include "cache.h"
11 #include "dir.h"
12 #include "parse-options.h"
14 static int force = -1; /* unset */
16 static const char *const builtin_clean_usage[] = {
17 "git-clean [-d] [-f] [-n] [-q] [-x | -X] [--] <paths>...",
18 NULL
21 static int git_clean_config(const char *var, const char *value)
23 if (!strcmp(var, "clean.requireforce"))
24 force = !git_config_bool(var, value);
25 return git_default_config(var, value);
28 int cmd_clean(int argc, const char **argv, const char *prefix)
30 int j;
31 int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
32 int ignored_only = 0, baselen = 0, config_set = 0;
33 struct strbuf directory;
34 struct dir_struct dir;
35 const char *path, *base;
36 static const char **pathspec;
37 struct option options[] = {
38 OPT__QUIET(&quiet),
39 OPT__DRY_RUN(&show_only),
40 OPT_BOOLEAN('f', NULL, &force, "force"),
41 OPT_BOOLEAN('d', NULL, &remove_directories,
42 "remove whole directories"),
43 OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"),
44 OPT_BOOLEAN('X', NULL, &ignored_only,
45 "remove only ignored files"),
46 OPT_END()
49 git_config(git_clean_config);
50 if (force < 0)
51 force = 0;
52 else
53 config_set = 1;
55 argc = parse_options(argc, argv, options, builtin_clean_usage, 0);
57 memset(&dir, 0, sizeof(dir));
58 if (ignored_only)
59 dir.show_ignored = 1;
61 if (ignored && ignored_only)
62 die("-x and -X cannot be used together");
64 if (!show_only && !force)
65 die("clean.requireForce%s set and -n or -f not given; "
66 "refusing to clean", config_set ? "" : " not");
68 dir.show_other_directories = 1;
70 if (!ignored)
71 setup_standard_excludes(&dir);
73 pathspec = get_pathspec(prefix, argv);
74 read_cache();
77 * Calculate common prefix for the pathspec, and
78 * use that to optimize the directory walk
80 baselen = common_prefix(pathspec);
81 path = ".";
82 base = "";
83 if (baselen)
84 path = base = xmemdupz(*pathspec, baselen);
85 read_directory(&dir, path, base, baselen, pathspec);
86 strbuf_init(&directory, 0);
88 for (j = 0; j < dir.nr; ++j) {
89 struct dir_entry *ent = dir.entries[j];
90 int len, pos, specs;
91 struct cache_entry *ce;
92 struct stat st;
93 char *seen;
96 * Remove the '/' at the end that directory
97 * walking adds for directory entries.
99 len = ent->len;
100 if (len && ent->name[len-1] == '/')
101 len--;
102 pos = cache_name_pos(ent->name, len);
103 if (0 <= pos)
104 continue; /* exact match */
105 pos = -pos - 1;
106 if (pos < active_nr) {
107 ce = active_cache[pos];
108 if (ce_namelen(ce) == len &&
109 !memcmp(ce->name, ent->name, len))
110 continue; /* Yup, this one exists unmerged */
113 if (!lstat(ent->name, &st) && (S_ISDIR(st.st_mode))) {
114 int matched_path = 0;
115 strbuf_addstr(&directory, ent->name);
116 if (pathspec) {
117 for (specs =0; pathspec[specs]; ++specs)
118 /* nothing */;
119 seen = xcalloc(specs, 1);
120 /* Check if directory was explictly passed as
121 * pathspec. If so we want to remove it */
122 if (match_pathspec(pathspec, ent->name, ent->len,
123 baselen, seen))
124 matched_path = 1;
125 free(seen);
127 if (show_only && (remove_directories || matched_path)) {
128 printf("Would remove %s\n", directory.buf);
129 } else if (quiet && (remove_directories || matched_path)) {
130 remove_dir_recursively(&directory, 0);
131 } else if (remove_directories || matched_path) {
132 printf("Removing %s\n", directory.buf);
133 remove_dir_recursively(&directory, 0);
134 } else if (show_only) {
135 printf("Would not remove %s\n", directory.buf);
136 } else {
137 printf("Not removing %s\n", directory.buf);
139 strbuf_reset(&directory);
140 } else {
141 if (show_only) {
142 printf("Would remove %s\n", ent->name);
143 continue;
144 } else if (!quiet) {
145 printf("Removing %s\n", ent->name);
147 unlink(ent->name);
151 strbuf_release(&directory);
152 return 0;