builtin "git prune"
This actually removes the objects to be pruned, unless you specify "-n" (at which point it will just tell you which files it would prune). This doesn't do the pack-file pruning that the shell-script used to do, but if somebody really wants to, they could add it easily enough. I wonder how useful it is, though, considering that "git repack -a -d" is just a lot more efficient and generates a better end result. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Linus Torvalds committed
Jul 6, 2006 at 10:16 UTC
ba84a797e76c27932e0b317c7ce54925e81093f7
5 files changed
+266
-48
Makefile
+3
-3
@@ -120,7 +120,7 @@ SCRIPT_SH = \
120
git-fetch.sh \
121
git-ls-remote.sh \
122
git-merge-one-file.sh git-parse-remote.sh \
123
- git-prune.sh git-pull.sh git-rebase.sh \
123
+ git-pull.sh git-rebase.sh \
124
git-repack.sh git-request-pull.sh git-reset.sh \
125
git-resolve.sh git-revert.sh git-sh-setup.sh \
126
git-tag.sh git-verify-tag.sh \
@@ -174,7 +174,7 @@ BUILT_INS = git-log$X git-whatchanged$X git-show$X git-update-ref$X \
174
git-read-tree$X git-commit-tree$X git-write-tree$X \
175
git-apply$X git-show-branch$X git-diff-files$X git-update-index$X \
176
git-diff-index$X git-diff-stages$X git-diff-tree$X git-cat-file$X \
177
- git-fmt-merge-msg$X
177
+ git-fmt-merge-msg$X git-prune$X
178
179
# what 'all' will build and 'install' will install, in gitexecdir
180
ALL_PROGRAMS = $(PROGRAMS) $(SIMPLE_PROGRAMS) $(SCRIPTS)
@@ -230,7 +230,7 @@ BUILTIN_OBJS = \
230
builtin-apply.o builtin-show-branch.o builtin-diff-files.o \
231
builtin-diff-index.o builtin-diff-stages.o builtin-diff-tree.o \
232
builtin-cat-file.o builtin-mailsplit.o builtin-stripspace.o \
233
- builtin-update-ref.o builtin-fmt-merge-msg.o
233
+ builtin-update-ref.o builtin-fmt-merge-msg.o builtin-prune.o
234
235
GITLIBS = $(LIB_FILE) $(XDIFF_LIB)
236
LIBS = $(GITLIBS) -lz
builtin-prune.c
new
+259
@@ -0,0 +1,259 @@
1
+#include "cache.h"
2
+#include "refs.h"
3
+#include "tag.h"
4
+#include "commit.h"
5
+#include "tree.h"
6
+#include "blob.h"
7
+#include "tree-walk.h"
8
+#include "diff.h"
9
+#include "revision.h"
10
+#include "builtin.h"
11
+#include "cache-tree.h"
12
+
13
+static const char prune_usage[] = "git prune [-n]";
14
+static int show_only = 0;
15
+static struct rev_info revs;
16
+
17
+static int prune_object(char *path, const char *filename, const unsigned char *sha1)
18
+{
19
+ if (show_only) {
20
+ printf("would prune %s/%s\n", path, filename);
21
+ return 0;
22
+ }
23
+ unlink(mkpath("%s/%s", path, filename));
24
+ rmdir(path);
25
+ return 0;
26
+}
27
+
28
+static int prune_dir(int i, char *path)
29
+{
30
+ DIR *dir = opendir(path);
31
+ struct dirent *de;
32
+
33
+ if (!dir)
34
+ return 0;
35
+
36
+ while ((de = readdir(dir)) != NULL) {
37
+ char name[100];
38
+ unsigned char sha1[20];
39
+ int len = strlen(de->d_name);
40
+
41
+ switch (len) {
42
+ case 2:
43
+ if (de->d_name[1] != '.')
44
+ break;
45
+ case 1:
46
+ if (de->d_name[0] != '.')
47
+ break;
48
+ continue;
49
+ case 38:
50
+ sprintf(name, "%02x", i);
51
+ memcpy(name+2, de->d_name, len+1);
52
+ if (get_sha1_hex(name, sha1) < 0)
53
+ break;
54
+
55
+ /*
56
+ * Do we know about this object?
57
+ * It must have been reachable
58
+ */
59
+ if (lookup_object(sha1))
60
+ continue;
61
+
62
+ prune_object(path, de->d_name, sha1);
63
+ continue;
64
+ }
65
+ fprintf(stderr, "bad sha1 file: %s/%s\n", path, de->d_name);
66
+ }
67
+ closedir(dir);
68
+ return 0;
69
+}
70
+
71
+static void prune_object_dir(const char *path)
72
+{
73
+ int i;
74
+ for (i = 0; i < 256; i++) {
75
+ static char dir[4096];
76
+ sprintf(dir, "%s/%02x", path, i);
77
+ prune_dir(i, dir);
78
+ }
79
+}
80
+
81
+static void process_blob(struct blob *blob,
82
+ struct object_array *p,
83
+ struct name_path *path,
84
+ const char *name)
85
+{
86
+ struct object *obj = &blob->object;
87
+
88
+ if (obj->flags & SEEN)
89
+ return;
90
+ obj->flags |= SEEN;
91
+ /* Nothing to do, really .. The blob lookup was the important part */
92
+}
93
+
94
+static void process_tree(struct tree *tree,
95
+ struct object_array *p,
96
+ struct name_path *path,
97
+ const char *name)
98
+{
99
+ struct object *obj = &tree->object;
100
+ struct tree_desc desc;
101
+ struct name_entry entry;
102
+ struct name_path me;
103
+
104
+ if (obj->flags & SEEN)
105
+ return;
106
+ obj->flags |= SEEN;
107
+ if (parse_tree(tree) < 0)
108
+ die("bad tree object %s", sha1_to_hex(obj->sha1));
109
+ name = strdup(name);
110
+ add_object(obj, p, path, name);
111
+ me.up = path;
112
+ me.elem = name;
113
+ me.elem_len = strlen(name);
114
+
115
+ desc.buf = tree->buffer;
116
+ desc.size = tree->size;
117
+
118
+ while (tree_entry(&desc, &entry)) {
119
+ if (S_ISDIR(entry.mode))
120
+ process_tree(lookup_tree(entry.sha1), p, &me, entry.path);
121
+ else
122
+ process_blob(lookup_blob(entry.sha1), p, &me, entry.path);
123
+ }
124
+ free(tree->buffer);
125
+ tree->buffer = NULL;
126
+}
127
+
128
+static void process_tag(struct tag *tag, struct object_array *p, const char *name)
129
+{
130
+ struct object *obj = &tag->object;
131
+ struct name_path me;
132
+
133
+ if (obj->flags & SEEN)
134
+ return;
135
+ obj->flags |= SEEN;
136
+
137
+ me.up = NULL;
138
+ me.elem = "tag:/";
139
+ me.elem_len = 5;
140
+
141
+ if (parse_tag(tag) < 0)
142
+ die("bad tag object %s", sha1_to_hex(obj->sha1));
143
+ add_object(tag->tagged, p, NULL, name);
144
+}
145
+
146
+static void walk_commit_list(struct rev_info *revs)
147
+{
148
+ int i;
149
+ struct commit *commit;
150
+ struct object_array objects = { 0, 0, NULL };
151
+
152
+ /* Walk all commits, process their trees */
153
+ while ((commit = get_revision(revs)) != NULL)
154
+ process_tree(commit->tree, &objects, NULL, "");
155
+
156
+ /* Then walk all the pending objects, recursively processing them too */
157
+ for (i = 0; i < revs->pending.nr; i++) {
158
+ struct object_array_entry *pending = revs->pending.objects + i;
159
+ struct object *obj = pending->item;
160
+ const char *name = pending->name;
161
+ if (obj->type == TYPE_TAG) {
162
+ process_tag((struct tag *) obj, &objects, name);
163
+ continue;
164
+ }
165
+ if (obj->type == TYPE_TREE) {
166
+ process_tree((struct tree *)obj, &objects, NULL, name);
167
+ continue;
168
+ }
169
+ if (obj->type == TYPE_BLOB) {
170
+ process_blob((struct blob *)obj, &objects, NULL, name);
171
+ continue;
172
+ }
173
+ die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name);
174
+ }
175
+}
176
+
177
+static int add_one_ref(const char *path, const unsigned char *sha1)
178
+{
179
+ struct object *object = parse_object(sha1);
180
+ if (!object)
181
+ die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
182
+ add_pending_object(&revs, object, "");
183
+ return 0;
184
+}
185
+
186
+static void add_one_tree(const unsigned char *sha1)
187
+{
188
+ struct tree *tree = lookup_tree(sha1);
189
+ add_pending_object(&revs, &tree->object, "");
190
+}
191
+
192
+static void add_cache_tree(struct cache_tree *it)
193
+{
194
+ int i;
195
+
196
+ if (it->entry_count >= 0)
197
+ add_one_tree(it->sha1);
198
+ for (i = 0; i < it->subtree_nr; i++)
199
+ add_cache_tree(it->down[i]->cache_tree);
200
+}
201
+
202
+static void add_cache_refs(void)
203
+{
204
+ int i;
205
+
206
+ read_cache();
207
+ for (i = 0; i < active_nr; i++) {
208
+ lookup_blob(active_cache[i]->sha1);
209
+ /*
210
+ * We could add the blobs to the pending list, but quite
211
+ * frankly, we don't care. Once we've looked them up, and
212
+ * added them as objects, we've really done everything
213
+ * there is to do for a blob
214
+ */
215
+ }
216
+ if (active_cache_tree)
217
+ add_cache_tree(active_cache_tree);
218
+}
219
+
220
+int cmd_prune(int argc, const char **argv, char **envp)
221
+{
222
+ int i;
223
+
224
+ for (i = 1; i < argc; i++) {
225
+ const char *arg = argv[i];
226
+ if (!strcmp(arg, "-n")) {
227
+ show_only = 1;
228
+ continue;
229
+ }
230
+ usage(prune_usage);
231
+ }
232
+
233
+ /*
234
+ * Set up revision parsing, and mark us as being interested
235
+ * in all object types, not just commits.
236
+ */
237
+ init_revisions(&revs);
238
+ revs.tag_objects = 1;
239
+ revs.blob_objects = 1;
240
+ revs.tree_objects = 1;
241
+
242
+ /* Add all external refs */
243
+ for_each_ref(add_one_ref);
244
+
245
+ /* Add all refs from the index file */
246
+ add_cache_refs();
247
+
248
+ /*
249
+ * Set up the revision walk - this will move all commits
250
+ * from the pending list to the commit walking list.
251
+ */
252
+ prepare_revision_walk(&revs);
253
+
254
+ walk_commit_list(&revs);
255
+
256
+ prune_object_dir(get_object_directory());
257
+
258
+ return 0;
259
+}
builtin.h
+2
@@ -25,6 +25,8 @@ extern int cmd_diff(int argc, const char **argv, char **envp);
25
extern int cmd_format_patch(int argc, const char **argv, char **envp);
26
extern int cmd_count_objects(int argc, const char **argv, char **envp);
27
28
+extern int cmd_prune(int argc, const char **argv, char **envp);
29
+
30
extern int cmd_push(int argc, const char **argv, char **envp);
31
extern int cmd_grep(int argc, const char **argv, char **envp);
32
extern int cmd_rm(int argc, const char **argv, char **envp);
git-prune.sh
deleted
-44
@@ -1,44 +0,0 @@
1
-#!/bin/sh
2
-
3
-USAGE='[-n] [--] [<head>...]'
4
-. git-sh-setup
5
-
6
-dryrun=
7
-echo=
8
-while case "$#" in 0) break ;; esac
9
-do
10
- case "$1" in
11
- -n) dryrun=-n echo=echo ;;
12
- --) break ;;
13
- -*) usage ;;
14
- *) break ;;
15
- esac
16
- shift;
17
-done
18
-
19
-sync
20
-case "$#" in
21
-0) git-fsck-objects --full --cache --unreachable ;;
22
-*) git-fsck-objects --full --cache --unreachable $(git-rev-parse --all) "$@" ;;
23
-esac |
24
-
25
-sed -ne '/unreachable /{
26
- s/unreachable [^ ][^ ]* //
27
- s|\(..\)|\1/|p
28
-}' | {
29
- cd "$GIT_OBJECT_DIRECTORY" || exit
30
- xargs $echo rm -f
31
- rmdir 2>/dev/null [0-9a-f][0-9a-f]
32
-}
33
-
34
-git-prune-packed $dryrun
35
-
36
-if redundant=$(git-pack-redundant --all 2>/dev/null) && test "" != "$redundant"
37
-then
38
- if test "" = "$dryrun"
39
- then
40
- echo "$redundant" | xargs rm -f
41
- else
42
- echo rm -f "$redundant"
43
- fi
44
-fi
git.c
+2
-1
@@ -188,7 +188,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
188
{ "stripspace", cmd_stripspace },
189
{ "update-index", cmd_update_index },
190
{ "update-ref", cmd_update_ref },
191
- { "fmt-merge-msg", cmd_fmt_merge_msg }
191
+ { "fmt-merge-msg", cmd_fmt_merge_msg },
192
+ { "prune", cmd_prune },
193
};
194
int i;
195