blob: fefec3010c434219234e7832051ada9ed2e52335 [file] [log] [blame]
Shawn Bohrer113f10f2007-11-11 19:48:47 -06001/*
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"
Dmitry Potapov1fb32892008-03-07 04:13:17 +030013#include "quote.h"
Shawn Bohrer113f10f2007-11-11 19:48:47 -060014
Junio C Hamano625db1b2007-11-12 21:13:05 -080015static int force = -1; /* unset */
Shawn Bohrer113f10f2007-11-11 19:48:47 -060016
17static const char *const builtin_clean_usage[] = {
18 "git-clean [-d] [-f] [-n] [-q] [-x | -X] [--] <paths>...",
19 NULL
20};
21
22static int git_clean_config(const char *var, const char *value)
23{
24 if (!strcmp(var, "clean.requireforce"))
25 force = !git_config_bool(var, value);
Shawn Bohrer1617adc2007-11-14 23:00:54 -060026 return git_default_config(var, value);
Shawn Bohrer113f10f2007-11-11 19:48:47 -060027}
28
29int cmd_clean(int argc, const char **argv, const char *prefix)
30{
Junio C Hamanod871c862007-12-04 23:55:41 -080031 int i;
Shawn Bohrer113f10f2007-11-11 19:48:47 -060032 int show_only = 0, remove_directories = 0, quiet = 0, ignored = 0;
Miklos Vajnaaa9c83c2008-02-21 02:44:46 +010033 int ignored_only = 0, baselen = 0, config_set = 0, errors = 0;
Shawn Bohrer113f10f2007-11-11 19:48:47 -060034 struct strbuf directory;
35 struct dir_struct dir;
36 const char *path, *base;
37 static const char **pathspec;
Dmitry Potapov1fb32892008-03-07 04:13:17 +030038 struct strbuf buf;
39 const char *qname;
Junio C Hamanod871c862007-12-04 23:55:41 -080040 char *seen = NULL;
Shawn Bohrer113f10f2007-11-11 19:48:47 -060041 struct option options[] = {
42 OPT__QUIET(&quiet),
43 OPT__DRY_RUN(&show_only),
44 OPT_BOOLEAN('f', NULL, &force, "force"),
45 OPT_BOOLEAN('d', NULL, &remove_directories,
46 "remove whole directories"),
47 OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"),
48 OPT_BOOLEAN('X', NULL, &ignored_only,
49 "remove only ignored files"),
50 OPT_END()
51 };
52
53 git_config(git_clean_config);
Junio C Hamano625db1b2007-11-12 21:13:05 -080054 if (force < 0)
55 force = 0;
56 else
57 config_set = 1;
58
Shawn Bohrer113f10f2007-11-11 19:48:47 -060059 argc = parse_options(argc, argv, options, builtin_clean_usage, 0);
60
Dmitry Potapov1fb32892008-03-07 04:13:17 +030061 strbuf_init(&buf, 0);
Shawn Bohrer113f10f2007-11-11 19:48:47 -060062 memset(&dir, 0, sizeof(dir));
Shawn Bohrer1617adc2007-11-14 23:00:54 -060063 if (ignored_only)
64 dir.show_ignored = 1;
Shawn Bohrer113f10f2007-11-11 19:48:47 -060065
66 if (ignored && ignored_only)
67 die("-x and -X cannot be used together");
68
69 if (!show_only && !force)
Junio C Hamano625db1b2007-11-12 21:13:05 -080070 die("clean.requireForce%s set and -n or -f not given; "
71 "refusing to clean", config_set ? "" : " not");
Shawn Bohrer113f10f2007-11-11 19:48:47 -060072
73 dir.show_other_directories = 1;
74
Shawn Bohrer1617adc2007-11-14 23:00:54 -060075 if (!ignored)
76 setup_standard_excludes(&dir);
Shawn Bohrer113f10f2007-11-11 19:48:47 -060077
78 pathspec = get_pathspec(prefix, argv);
79 read_cache();
80
81 /*
82 * Calculate common prefix for the pathspec, and
83 * use that to optimize the directory walk
84 */
85 baselen = common_prefix(pathspec);
86 path = ".";
87 base = "";
88 if (baselen)
89 path = base = xmemdupz(*pathspec, baselen);
90 read_directory(&dir, path, base, baselen, pathspec);
91 strbuf_init(&directory, 0);
92
Junio C Hamanod871c862007-12-04 23:55:41 -080093 if (pathspec)
Jeff Kinga8db80c2008-01-12 04:04:32 -050094 seen = xmalloc(argc > 0 ? argc : 1);
Junio C Hamanod871c862007-12-04 23:55:41 -080095
96 for (i = 0; i < dir.nr; i++) {
97 struct dir_entry *ent = dir.entries[i];
98 int len, pos, matches;
Shawn Bohrer113f10f2007-11-11 19:48:47 -060099 struct cache_entry *ce;
100 struct stat st;
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600101
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
Junio C Hamanod871c862007-12-04 23:55:41 -0800120 /*
121 * we might have removed this as part of earlier
122 * recursive directory removal, so lstat() here could
123 * fail with ENOENT.
124 */
125 if (lstat(ent->name, &st))
126 continue;
127
128 if (pathspec) {
Jeff Kinga8db80c2008-01-12 04:04:32 -0500129 memset(seen, 0, argc > 0 ? argc : 1);
Junio C Hamanod871c862007-12-04 23:55:41 -0800130 matches = match_pathspec(pathspec, ent->name, ent->len,
131 baselen, seen);
132 } else {
133 matches = 0;
134 }
135
136 if (S_ISDIR(st.st_mode)) {
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600137 strbuf_addstr(&directory, ent->name);
Dmitry Potapov1fb32892008-03-07 04:13:17 +0300138 qname = quote_path_relative(directory.buf, directory.len, &buf, prefix);
Junio C Hamanod871c862007-12-04 23:55:41 -0800139 if (show_only && (remove_directories || matches)) {
Dmitry Potapov1fb32892008-03-07 04:13:17 +0300140 printf("Would remove %s\n", qname);
Junio C Hamanod871c862007-12-04 23:55:41 -0800141 } else if (remove_directories || matches) {
Miklos Vajnaaa9c83c2008-02-21 02:44:46 +0100142 if (!quiet)
Dmitry Potapov1fb32892008-03-07 04:13:17 +0300143 printf("Removing %s\n", qname);
Miklos Vajnaaa9c83c2008-02-21 02:44:46 +0100144 if (remove_dir_recursively(&directory, 0) != 0) {
Dmitry Potapov1fb32892008-03-07 04:13:17 +0300145 warning("failed to remove '%s'", qname);
Miklos Vajnaaa9c83c2008-02-21 02:44:46 +0100146 errors++;
147 }
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600148 } else if (show_only) {
Dmitry Potapov1fb32892008-03-07 04:13:17 +0300149 printf("Would not remove %s\n", qname);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600150 } else {
Dmitry Potapov1fb32892008-03-07 04:13:17 +0300151 printf("Not removing %s\n", qname);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600152 }
153 strbuf_reset(&directory);
154 } else {
Junio C Hamanod871c862007-12-04 23:55:41 -0800155 if (pathspec && !matches)
156 continue;
Dmitry Potapov1fb32892008-03-07 04:13:17 +0300157 qname = quote_path_relative(ent->name, -1, &buf, prefix);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600158 if (show_only) {
Dmitry Potapov1fb32892008-03-07 04:13:17 +0300159 printf("Would remove %s\n", qname);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600160 continue;
161 } else if (!quiet) {
Dmitry Potapov1fb32892008-03-07 04:13:17 +0300162 printf("Removing %s\n", qname);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600163 }
Miklos Vajnaaa9c83c2008-02-21 02:44:46 +0100164 if (unlink(ent->name) != 0) {
Dmitry Potapov1fb32892008-03-07 04:13:17 +0300165 warning("failed to remove '%s'", qname);
Miklos Vajnaaa9c83c2008-02-21 02:44:46 +0100166 errors++;
167 }
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600168 }
169 }
Junio C Hamanod871c862007-12-04 23:55:41 -0800170 free(seen);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600171
172 strbuf_release(&directory);
Miklos Vajnaaa9c83c2008-02-21 02:44:46 +0100173 return (errors != 0);
Shawn Bohrer113f10f2007-11-11 19:48:47 -0600174}