git-clean: fix off-by-one memory access when given no arguments
[git/git.git] / builtin-clean.c
CommitLineData
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 14static int force = -1; /* unset */
113f10f2
SB
15
16static const char *const builtin_clean_usage[] = {
17 "git-clean [-d] [-f] [-n] [-q] [-x | -X] [--] <paths>...",
18 NULL
19};
20
21static 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
28int 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;
95bf4bd4 37 int prefix_offset = 0;
d871c869 38 char *seen = NULL;
113f10f2
SB
39 struct option options[] = {
40 OPT__QUIET(&quiet),
41 OPT__DRY_RUN(&show_only),
42 OPT_BOOLEAN('f', NULL, &force, "force"),
43 OPT_BOOLEAN('d', NULL, &remove_directories,
44 "remove whole directories"),
45 OPT_BOOLEAN('x', NULL, &ignored, "remove ignored files, too"),
46 OPT_BOOLEAN('X', NULL, &ignored_only,
47 "remove only ignored files"),
48 OPT_END()
49 };
50
51 git_config(git_clean_config);
625db1b7
JH
52 if (force < 0)
53 force = 0;
54 else
55 config_set = 1;
56
113f10f2
SB
57 argc = parse_options(argc, argv, options, builtin_clean_usage, 0);
58
59 memset(&dir, 0, sizeof(dir));
1617adc7
SB
60 if (ignored_only)
61 dir.show_ignored = 1;
113f10f2
SB
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
1617adc7
SB
72 if (!ignored)
73 setup_standard_excludes(&dir);
113f10f2 74
95bf4bd4
JH
75 if (prefix)
76 prefix_offset = strlen(prefix);
113f10f2
SB
77 pathspec = get_pathspec(prefix, argv);
78 read_cache();
79
80 /*
81 * Calculate common prefix for the pathspec, and
82 * use that to optimize the directory walk
83 */
84 baselen = common_prefix(pathspec);
85 path = ".";
86 base = "";
87 if (baselen)
88 path = base = xmemdupz(*pathspec, baselen);
89 read_directory(&dir, path, base, baselen, pathspec);
90 strbuf_init(&directory, 0);
91
d871c869 92 if (pathspec)
a8db80c2 93 seen = xmalloc(argc > 0 ? argc : 1);
d871c869
JH
94
95 for (i = 0; i < dir.nr; i++) {
96 struct dir_entry *ent = dir.entries[i];
97 int len, pos, matches;
113f10f2
SB
98 struct cache_entry *ce;
99 struct stat st;
113f10f2
SB
100
101 /*
102 * Remove the '/' at the end that directory
103 * walking adds for directory entries.
104 */
105 len = ent->len;
106 if (len && ent->name[len-1] == '/')
107 len--;
108 pos = cache_name_pos(ent->name, len);
109 if (0 <= pos)
110 continue; /* exact match */
111 pos = -pos - 1;
112 if (pos < active_nr) {
113 ce = active_cache[pos];
114 if (ce_namelen(ce) == len &&
115 !memcmp(ce->name, ent->name, len))
116 continue; /* Yup, this one exists unmerged */
117 }
118
d871c869
JH
119 /*
120 * we might have removed this as part of earlier
121 * recursive directory removal, so lstat() here could
122 * fail with ENOENT.
123 */
124 if (lstat(ent->name, &st))
125 continue;
126
127 if (pathspec) {
a8db80c2 128 memset(seen, 0, argc > 0 ? argc : 1);
d871c869
JH
129 matches = match_pathspec(pathspec, ent->name, ent->len,
130 baselen, seen);
131 } else {
132 matches = 0;
133 }
134
135 if (S_ISDIR(st.st_mode)) {
113f10f2 136 strbuf_addstr(&directory, ent->name);
d871c869 137 if (show_only && (remove_directories || matches)) {
95bf4bd4
JH
138 printf("Would remove %s\n",
139 directory.buf + prefix_offset);
d871c869 140 } else if (quiet && (remove_directories || matches)) {
113f10f2 141 remove_dir_recursively(&directory, 0);
d871c869 142 } else if (remove_directories || matches) {
95bf4bd4
JH
143 printf("Removing %s\n",
144 directory.buf + prefix_offset);
113f10f2
SB
145 remove_dir_recursively(&directory, 0);
146 } else if (show_only) {
95bf4bd4
JH
147 printf("Would not remove %s\n",
148 directory.buf + prefix_offset);
113f10f2 149 } else {
95bf4bd4
JH
150 printf("Not removing %s\n",
151 directory.buf + prefix_offset);
113f10f2
SB
152 }
153 strbuf_reset(&directory);
154 } else {
d871c869
JH
155 if (pathspec && !matches)
156 continue;
113f10f2 157 if (show_only) {
95bf4bd4
JH
158 printf("Would remove %s\n",
159 ent->name + prefix_offset);
113f10f2
SB
160 continue;
161 } else if (!quiet) {
95bf4bd4
JH
162 printf("Removing %s\n",
163 ent->name + prefix_offset);
113f10f2
SB
164 }
165 unlink(ent->name);
166 }
167 }
d871c869 168 free(seen);
113f10f2
SB
169
170 strbuf_release(&directory);
171 return 0;
172}