]>
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); | |
25 | return 0; | |
26 | } | |
27 | ||
28 | int cmd_clean(int argc, const char **argv, const char *prefix) | |
29 | { | |
30 | int j; | |
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; | |
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() | |
47 | }; | |
48 | ||
49 | git_config(git_clean_config); | |
625db1b7 JH |
50 | if (force < 0) |
51 | force = 0; | |
52 | else | |
53 | config_set = 1; | |
54 | ||
113f10f2 SB |
55 | argc = parse_options(argc, argv, options, builtin_clean_usage, 0); |
56 | ||
57 | memset(&dir, 0, sizeof(dir)); | |
58 | if (ignored_only) { | |
59 | dir.show_ignored =1; | |
60 | dir.exclude_per_dir = ".gitignore"; | |
61 | } | |
62 | ||
63 | if (ignored && ignored_only) | |
64 | die("-x and -X cannot be used together"); | |
65 | ||
66 | if (!show_only && !force) | |
625db1b7 JH |
67 | die("clean.requireForce%s set and -n or -f not given; " |
68 | "refusing to clean", config_set ? "" : " not"); | |
113f10f2 SB |
69 | |
70 | dir.show_other_directories = 1; | |
71 | ||
72 | if (!ignored) { | |
73 | dir.exclude_per_dir = ".gitignore"; | |
74 | if (!access(git_path("info/exclude"), F_OK)) { | |
75 | char *exclude_path = git_path("info/exclude"); | |
76 | add_excludes_from_file(&dir, exclude_path); | |
77 | } | |
78 | } | |
79 | ||
80 | pathspec = get_pathspec(prefix, argv); | |
81 | read_cache(); | |
82 | ||
83 | /* | |
84 | * Calculate common prefix for the pathspec, and | |
85 | * use that to optimize the directory walk | |
86 | */ | |
87 | baselen = common_prefix(pathspec); | |
88 | path = "."; | |
89 | base = ""; | |
90 | if (baselen) | |
91 | path = base = xmemdupz(*pathspec, baselen); | |
92 | read_directory(&dir, path, base, baselen, pathspec); | |
93 | strbuf_init(&directory, 0); | |
94 | ||
95 | for (j = 0; j < dir.nr; ++j) { | |
96 | struct dir_entry *ent = dir.entries[j]; | |
97 | int len, pos, specs; | |
98 | struct cache_entry *ce; | |
99 | struct stat st; | |
100 | char *seen; | |
101 | ||
102 | /* | |
103 | * Remove the '/' at the end that directory | |
104 | * walking adds for directory entries. | |
105 | */ | |
106 | len = ent->len; | |
107 | if (len && ent->name[len-1] == '/') | |
108 | len--; | |
109 | pos = cache_name_pos(ent->name, len); | |
110 | if (0 <= pos) | |
111 | continue; /* exact match */ | |
112 | pos = -pos - 1; | |
113 | if (pos < active_nr) { | |
114 | ce = active_cache[pos]; | |
115 | if (ce_namelen(ce) == len && | |
116 | !memcmp(ce->name, ent->name, len)) | |
117 | continue; /* Yup, this one exists unmerged */ | |
118 | } | |
119 | ||
120 | if (!lstat(ent->name, &st) && (S_ISDIR(st.st_mode))) { | |
121 | int matched_path = 0; | |
122 | strbuf_addstr(&directory, ent->name); | |
123 | if (pathspec) { | |
124 | for (specs =0; pathspec[specs]; ++specs) | |
125 | /* nothing */; | |
126 | seen = xcalloc(specs, 1); | |
127 | /* Check if directory was explictly passed as | |
128 | * pathspec. If so we want to remove it */ | |
129 | if (match_pathspec(pathspec, ent->name, ent->len, | |
130 | baselen, seen)) | |
131 | matched_path = 1; | |
132 | free(seen); | |
133 | } | |
134 | if (show_only && (remove_directories || matched_path)) { | |
135 | printf("Would remove %s\n", directory.buf); | |
136 | } else if (quiet && (remove_directories || matched_path)) { | |
137 | remove_dir_recursively(&directory, 0); | |
138 | } else if (remove_directories || matched_path) { | |
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 { | |
148 | if (show_only) { | |
149 | printf("Would remove %s\n", ent->name); | |
150 | continue; | |
151 | } else if (!quiet) { | |
152 | printf("Removing %s\n", ent->name); | |
153 | } | |
154 | unlink(ent->name); | |
155 | } | |
156 | } | |
157 | ||
158 | strbuf_release(&directory); | |
159 | return 0; | |
160 | } |