11 #include "cache-tree.h"
13 static const char prune_usage
[] = "git-prune [-n]";
15 static struct rev_info revs
;
17 static int prune_object(char *path
, const char *filename
, const unsigned char *sha1
)
20 printf("would prune %s/%s\n", path
, filename
);
23 unlink(mkpath("%s/%s", path
, filename
));
28 static int prune_dir(int i
, char *path
)
30 DIR *dir
= opendir(path
);
36 while ((de
= readdir(dir
)) != NULL
) {
38 unsigned char sha1
[20];
39 int len
= strlen(de
->d_name
);
43 if (de
->d_name
[1] != '.')
46 if (de
->d_name
[0] != '.')
50 sprintf(name
, "%02x", i
);
51 memcpy(name
+2, de
->d_name
, len
+1);
52 if (get_sha1_hex(name
, sha1
) < 0)
56 * Do we know about this object?
57 * It must have been reachable
59 if (lookup_object(sha1
))
62 prune_object(path
, de
->d_name
, sha1
);
65 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
71 static void prune_object_dir(const char *path
)
74 for (i
= 0; i
< 256; i
++) {
75 static char dir
[4096];
76 sprintf(dir
, "%s/%02x", path
, i
);
81 static void process_blob(struct blob
*blob
,
82 struct object_array
*p
,
83 struct name_path
*path
,
86 struct object
*obj
= &blob
->object
;
88 if (obj
->flags
& SEEN
)
91 /* Nothing to do, really .. The blob lookup was the important part */
94 static void process_tree(struct tree
*tree
,
95 struct object_array
*p
,
96 struct name_path
*path
,
99 struct object
*obj
= &tree
->object
;
100 struct tree_desc desc
;
101 struct name_entry entry
;
104 if (obj
->flags
& SEEN
)
107 if (parse_tree(tree
) < 0)
108 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
109 name
= xstrdup(name
);
110 add_object(obj
, p
, path
, name
);
113 me
.elem_len
= strlen(name
);
115 desc
.buf
= tree
->buffer
;
116 desc
.size
= tree
->size
;
118 while (tree_entry(&desc
, &entry
)) {
119 if (S_ISDIR(entry
.mode
))
120 process_tree(lookup_tree(entry
.sha1
), p
, &me
, entry
.path
);
122 process_blob(lookup_blob(entry
.sha1
), p
, &me
, entry
.path
);
128 static void process_tag(struct tag
*tag
, struct object_array
*p
, const char *name
)
130 struct object
*obj
= &tag
->object
;
133 if (obj
->flags
& SEEN
)
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
);
146 static void walk_commit_list(struct rev_info
*revs
)
149 struct commit
*commit
;
150 struct object_array objects
= { 0, 0, NULL
};
152 /* Walk all commits, process their trees */
153 while ((commit
= get_revision(revs
)) != NULL
)
154 process_tree(commit
->tree
, &objects
, NULL
, "");
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
== OBJ_TAG
) {
162 process_tag((struct tag
*) obj
, &objects
, name
);
165 if (obj
->type
== OBJ_TREE
) {
166 process_tree((struct tree
*)obj
, &objects
, NULL
, name
);
169 if (obj
->type
== OBJ_BLOB
) {
170 process_blob((struct blob
*)obj
, &objects
, NULL
, name
);
173 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
177 static int add_one_ref(const char *path
, const unsigned char *sha1
)
179 struct object
*object
= parse_object(sha1
);
181 die("bad object ref: %s:%s", path
, sha1_to_hex(sha1
));
182 add_pending_object(&revs
, object
, "");
186 static void add_one_tree(const unsigned char *sha1
)
188 struct tree
*tree
= lookup_tree(sha1
);
189 add_pending_object(&revs
, &tree
->object
, "");
192 static void add_cache_tree(struct cache_tree
*it
)
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
);
202 static void add_cache_refs(void)
207 for (i
= 0; i
< active_nr
; i
++) {
208 lookup_blob(active_cache
[i
]->sha1
);
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
216 if (active_cache_tree
)
217 add_cache_tree(active_cache_tree
);
220 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
224 for (i
= 1; i
< argc
; i
++) {
225 const char *arg
= argv
[i
];
226 if (!strcmp(arg
, "-n")) {
234 * Set up revision parsing, and mark us as being interested
235 * in all object types, not just commits.
237 init_revisions(&revs
, prefix
);
238 revs
.tag_objects
= 1;
239 revs
.blob_objects
= 1;
240 revs
.tree_objects
= 1;
242 /* Add all external refs */
243 for_each_ref(add_one_ref
);
245 /* Add all refs from the index file */
249 * Set up the revision walk - this will move all commits
250 * from the pending list to the commit walking list.
252 prepare_revision_walk(&revs
);
254 walk_commit_list(&revs
);
256 prune_object_dir(get_object_directory());
259 prune_packed_objects(show_only
);