@matheusbsbs / matheus-bagliano / commits / 113f10f22f
Fork 0

Make git-clean a builtin

This replaces git-clean.sh with builtin-clean.c, and moves git-clean.sh to the examples. This also introduces a change in behavior when removing directories explicitly specified as a path. For example currently: 1. When dir has only untracked files, these two behave differently: $ git clean -n dir $ git clean -n dir/ the former says "Would not remove dir/", while the latter would say "Would remove dir/untracked" for all paths under it, but not the directory itself. With -d, the former would stop refusing, however since the user explicitly asked to remove the directory the -d is no longer required. 2. When there are more parameters: $ git clean -n dir foo $ git clean -n dir/ foo both cases refuse to remove dir/ unless -d is specified. Once again since both cases requested to remove dir the -d is no longer required. Thanks to Johannes Schindelin for the conversion to using the parse-options API. Signed-off-by: Shawn Bohrer <shawn.bohrer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shawn Bohrer committed Nov 11, 2007 at 19:48 UTC 113f10f22f4b3b599e44e192e241e0bace9cc39e
5 files changed +158 -1
Makefile
+2 -1
@@ -213,7 +213,7 @@ BASIC_LDFLAGS =
213
214 SCRIPT_SH = \
215 git-bisect.sh git-checkout.sh \
216 - git-clean.sh git-clone.sh git-commit.sh \
216 + git-clone.sh git-commit.sh \
217 git-ls-remote.sh \
218 git-merge-one-file.sh git-mergetool.sh git-parse-remote.sh \
219 git-pull.sh git-rebase.sh git-rebase--interactive.sh \
@@ -330,6 +330,7 @@ BUILTIN_OBJS = \
330 builtin-check-attr.o \
331 builtin-checkout-index.o \
332 builtin-check-ref-format.o \
333 + builtin-clean.o \
334 builtin-commit-tree.o \
335 builtin-count-objects.o \
336 builtin-describe.o \
builtin-clean.c new
+154
@@ -0,0 +1,154 @@
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 +
14 +static int force;
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;
32 + int ignored_only = 0, baselen = 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()
47 + };
48 +
49 + git_config(git_clean_config);
50 + argc = parse_options(argc, argv, options, builtin_clean_usage, 0);
51 +
52 + memset(&dir, 0, sizeof(dir));
53 + if (ignored_only) {
54 + dir.show_ignored =1;
55 + dir.exclude_per_dir = ".gitignore";
56 + }
57 +
58 + if (ignored && ignored_only)
59 + die("-x and -X cannot be used together");
60 +
61 + if (!show_only && !force)
62 + die("clean.requireForce set and -n or -f not given; refusing to clean");
63 +
64 + dir.show_other_directories = 1;
65 +
66 + if (!ignored) {
67 + dir.exclude_per_dir = ".gitignore";
68 + if (!access(git_path("info/exclude"), F_OK)) {
69 + char *exclude_path = git_path("info/exclude");
70 + add_excludes_from_file(&dir, exclude_path);
71 + }
72 + }
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 +
89 + for (j = 0; j < dir.nr; ++j) {
90 + struct dir_entry *ent = dir.entries[j];
91 + int len, pos, specs;
92 + struct cache_entry *ce;
93 + struct stat st;
94 + char *seen;
95 +
96 + /*
97 + * Remove the '/' at the end that directory
98 + * walking adds for directory entries.
99 + */
100 + len = ent->len;
101 + if (len && ent->name[len-1] == '/')
102 + len--;
103 + pos = cache_name_pos(ent->name, len);
104 + if (0 <= pos)
105 + continue; /* exact match */
106 + pos = -pos - 1;
107 + if (pos < active_nr) {
108 + ce = active_cache[pos];
109 + if (ce_namelen(ce) == len &&
110 + !memcmp(ce->name, ent->name, len))
111 + continue; /* Yup, this one exists unmerged */
112 + }
113 +
114 + if (!lstat(ent->name, &st) && (S_ISDIR(st.st_mode))) {
115 + int matched_path = 0;
116 + strbuf_addstr(&directory, ent->name);
117 + if (pathspec) {
118 + for (specs =0; pathspec[specs]; ++specs)
119 + /* nothing */;
120 + seen = xcalloc(specs, 1);
121 + /* Check if directory was explictly passed as
122 + * pathspec. If so we want to remove it */
123 + if (match_pathspec(pathspec, ent->name, ent->len,
124 + baselen, seen))
125 + matched_path = 1;
126 + free(seen);
127 + }
128 + if (show_only && (remove_directories || matched_path)) {
129 + printf("Would remove %s\n", directory.buf);
130 + } else if (quiet && (remove_directories || matched_path)) {
131 + remove_dir_recursively(&directory, 0);
132 + } else if (remove_directories || matched_path) {
133 + printf("Removing %s\n", directory.buf);
134 + remove_dir_recursively(&directory, 0);
135 + } else if (show_only) {
136 + printf("Would not remove %s\n", directory.buf);
137 + } else {
138 + printf("Not removing %s\n", directory.buf);
139 + }
140 + strbuf_reset(&directory);
141 + } else {
142 + if (show_only) {
143 + printf("Would remove %s\n", ent->name);
144 + continue;
145 + } else if (!quiet) {
146 + printf("Removing %s\n", ent->name);
147 + }
148 + unlink(ent->name);
149 + }
150 + }
151 +
152 + strbuf_release(&directory);
153 + return 0;
154 +}
builtin.h
+1
@@ -24,6 +24,7 @@ extern int cmd_check_attr(int argc, const char **argv, const char *prefix);
24 extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);
25 extern int cmd_cherry(int argc, const char **argv, const char *prefix);
26 extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);
27 +extern int cmd_clean(int argc, const char **argv, const char *prefix);
28 extern int cmd_commit_tree(int argc, const char **argv, const char *prefix);
29 extern int cmd_count_objects(int argc, const char **argv, const char *prefix);
30 extern int cmd_describe(int argc, const char **argv, const char *prefix);
contrib/examples/git-clean.sh renamed
git.c
+1
@@ -293,6 +293,7 @@ static void handle_internal_command(int argc, const char **argv)
293 { "check-attr", cmd_check_attr, RUN_SETUP | NEED_WORK_TREE },
294 { "cherry", cmd_cherry, RUN_SETUP },
295 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
296 + { "clean", cmd_clean, RUN_SETUP | NEED_WORK_TREE },
297 { "commit-tree", cmd_commit_tree, RUN_SETUP },
298 { "config", cmd_config },
299 { "count-objects", cmd_count_objects, RUN_SETUP },