]>
Commit | Line | Data |
---|---|---|
113f10f2 SB |
1 | /* |
2 | * "git clean" builtin command | |
3 | * | |
4 | * Copyright (C) 2007 Shawn Bohrer | |
5 | * | |
6 | * Based on git-clean.sh by Pavel Roskin | |
7 | */ | |
8 | ||
9 | #include "builtin.h" | |
10 | #include "cache.h" | |
11 | #include "dir.h" | |
12 | #include "parse-options.h" | |
13 | ||
625db1b7 | 14 | static int force = -1; /* unset */ |
113f10f2 SB |
15 | |
16 | static const char *const builtin_clean_usage[] = { | |
17 | "git-clean [-d] [-f] [-n] [-q] [-x | -X] [--] <paths>...", | |
18 | NULL | |
19 | }; | |
20 | ||
21 | static int git_clean_config(const char *var, const char *value) | |
22 | { | |
23 | if (!strcmp(var, "clean.requireforce")) | |
24 | force = !git_config_bool(var, value); | |
1617adc7 | 25 | return git_default_config(var, value); |
113f10f2 SB |
26 | } |
27 | ||
28 | int cmd_clean(int argc, const char **argv, const char *prefix) | |
29 | { | |
d871c869 | 30 | int i; |
113f10f2 | 31 | int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0; |
625db1b7 | 32 | int ignored_only = 0, baselen = 0, config_set = 0; |
113f10f2 SB |
33 | struct strbuf directory; |
34 | struct dir_struct dir; | |
35 | const char *path, *base; | |
36 | static const char **pathspec; | |
d871c869 | 37 | char *seen = NULL; |
113f10f2 SB |
38 | struct option options[] = { |
39 | OPT__QUIET(&quiet), | |
40 | OPT__DRY_RUN(&show_only), | |
41 | OPT_BOOLEAN('f', NULL, &force, "force"), | |
42 | OPT_BOOLEAN('d', NULL, &remove_directories, | |
43 | "remove whole directories"), | |
44 | OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"), | |
45 | OPT_BOOLEAN('X', NULL, &ignored_only, | |
46 | "remove only ignored files"), | |
47 | OPT_END() | |
48 | }; | |
49 | ||
50 | git_config(git_clean_config); | |
625db1b7 JH |
51 | if (force < 0) |
52 | force = 0; | |
53 | else | |
54 | config_set = 1; | |
55 | ||
113f10f2 SB |
56 | argc = parse_options(argc, argv, options, builtin_clean_usage, 0); |
57 | ||
58 | memset(&dir, 0, sizeof(dir)); | |
1617adc7 SB |
59 | if (ignored_only) |
60 | dir.show_ignored = 1; | |
113f10f2 SB |
61 | |
62 | if (ignored && ignored_only) | |
63 | die("-x and -X cannot be used together"); | |
64 | ||
65 | if (!show_only && !force) | |
625db1b7 JH |
66 | die("clean.requireForce%s set and -n or -f not given; " |
67 | "refusing to clean", config_set ? "" : " not"); | |
113f10f2 SB |
68 | |
69 | dir.show_other_directories = 1; | |
70 | ||
1617adc7 SB |
71 | if (!ignored) |
72 | setup_standard_excludes(&dir); | |
113f10f2 SB |
73 | |
74 | pathspec = get_pathspec(prefix, argv); | |
75 | read_cache(); | |
76 | ||
77 | /* | |
78 | * Calculate common prefix for the pathspec, and | |
79 | * use that to optimize the directory walk | |
80 | */ | |
81 | baselen = common_prefix(pathspec); | |
82 | path = "."; | |
83 | base = ""; | |
84 | if (baselen) | |
85 | path = base = xmemdupz(*pathspec, baselen); | |
86 | read_directory(&dir, path, base, baselen, pathspec); | |
87 | strbuf_init(&directory, 0); | |
88 | ||
d871c869 JH |
89 | if (pathspec) |
90 | seen = xmalloc(argc); | |
91 | ||
92 | for (i = 0; i < dir.nr; i++) { | |
93 | struct dir_entry *ent = dir.entries[i]; | |
94 | int len, pos, matches; | |
113f10f2 SB |
95 | struct cache_entry *ce; |
96 | struct stat st; | |
113f10f2 SB |
97 | |
98 | /* | |
99 | * Remove the '/' at the end that directory | |
100 | * walking adds for directory entries. | |
101 | */ | |
102 | len = ent->len; | |
103 | if (len && ent->name[len-1] == '/') | |
104 | len--; | |
105 | pos = cache_name_pos(ent->name, len); | |
106 | if (0 <= pos) | |
107 | continue; /* exact match */ | |
108 | pos = -pos - 1; | |
109 | if (pos < active_nr) { | |
110 | ce = active_cache[pos]; | |
111 | if (ce_namelen(ce) == len && | |
112 | !memcmp(ce->name, ent->name, len)) | |
113 | continue; /* Yup, this one exists unmerged */ | |
114 | } | |
115 | ||
d871c869 JH |
116 | /* |
117 | * we might have removed this as part of earlier | |
118 | * recursive directory removal, so lstat() here could | |
119 | * fail with ENOENT. | |
120 | */ | |
121 | if (lstat(ent->name, &st)) | |
122 | continue; | |
123 | ||
124 | if (pathspec) { | |
125 | memset(seen, 0, argc); | |
126 | matches = match_pathspec(pathspec, ent->name, ent->len, | |
127 | baselen, seen); | |
128 | } else { | |
129 | matches = 0; | |
130 | } | |
131 | ||
132 | if (S_ISDIR(st.st_mode)) { | |
113f10f2 | 133 | strbuf_addstr(&directory, ent->name); |
d871c869 | 134 | if (show_only && (remove_directories || matches)) { |
113f10f2 | 135 | printf("Would remove %s\n", directory.buf); |
d871c869 | 136 | } else if (quiet && (remove_directories || matches)) { |
113f10f2 | 137 | remove_dir_recursively(&directory, 0); |
d871c869 | 138 | } else if (remove_directories || matches) { |
113f10f2 SB |
139 | printf("Removing %s\n", directory.buf); |
140 | remove_dir_recursively(&directory, 0); | |
141 | } else if (show_only) { | |
142 | printf("Would not remove %s\n", directory.buf); | |
143 | } else { | |
144 | printf("Not removing %s\n", directory.buf); | |
145 | } | |
146 | strbuf_reset(&directory); | |
147 | } else { | |
d871c869 JH |
148 | if (pathspec && !matches) |
149 | continue; | |
113f10f2 SB |
150 | if (show_only) { |
151 | printf("Would remove %s\n", ent->name); | |
152 | continue; | |
153 | } else if (!quiet) { | |
154 | printf("Removing %s\n", ent->name); | |
155 | } | |
156 | unlink(ent->name); | |
157 | } | |
158 | } | |
d871c869 | 159 | free(seen); |
113f10f2 SB |
160 | |
161 | strbuf_release(&directory); | |
162 | return 0; | |
163 | } |