11 #include "cache-tree.h"
13 static const char prune_usage
[] = "git-prune [-n]";
16 static int prune_object(char *path
, const char *filename
, const unsigned char *sha1
)
22 if (sha1_object_info(sha1
, buf
, NULL
))
26 printf("%s %s\n", sha1_to_hex(sha1
), type
);
29 unlink(mkpath("%s/%s", path
, filename
));
34 static int prune_dir(int i
, char *path
)
36 DIR *dir
= opendir(path
);
42 while ((de
= readdir(dir
)) != NULL
) {
44 unsigned char sha1
[20];
45 int len
= strlen(de
->d_name
);
49 if (de
->d_name
[1] != '.')
52 if (de
->d_name
[0] != '.')
56 sprintf(name
, "%02x", i
);
57 memcpy(name
+2, de
->d_name
, len
+1);
58 if (get_sha1_hex(name
, sha1
) < 0)
62 * Do we know about this object?
63 * It must have been reachable
65 if (lookup_object(sha1
))
68 prune_object(path
, de
->d_name
, sha1
);
71 fprintf(stderr
, "bad sha1 file: %s/%s\n", path
, de
->d_name
);
77 static void prune_object_dir(const char *path
)
80 for (i
= 0; i
< 256; i
++) {
81 static char dir
[4096];
82 sprintf(dir
, "%s/%02x", path
, i
);
87 static void process_blob(struct blob
*blob
,
88 struct object_array
*p
,
89 struct name_path
*path
,
92 struct object
*obj
= &blob
->object
;
94 if (obj
->flags
& SEEN
)
97 /* Nothing to do, really .. The blob lookup was the important part */
100 static void process_tree(struct tree
*tree
,
101 struct object_array
*p
,
102 struct name_path
*path
,
105 struct object
*obj
= &tree
->object
;
106 struct tree_desc desc
;
107 struct name_entry entry
;
110 if (obj
->flags
& SEEN
)
113 if (parse_tree(tree
) < 0)
114 die("bad tree object %s", sha1_to_hex(obj
->sha1
));
115 name
= xstrdup(name
);
116 add_object(obj
, p
, path
, name
);
119 me
.elem_len
= strlen(name
);
121 desc
.buf
= tree
->buffer
;
122 desc
.size
= tree
->size
;
124 while (tree_entry(&desc
, &entry
)) {
125 if (S_ISDIR(entry
.mode
))
126 process_tree(lookup_tree(entry
.sha1
), p
, &me
, entry
.path
);
128 process_blob(lookup_blob(entry
.sha1
), p
, &me
, entry
.path
);
134 static void process_tag(struct tag
*tag
, struct object_array
*p
, const char *name
)
136 struct object
*obj
= &tag
->object
;
139 if (obj
->flags
& SEEN
)
147 if (parse_tag(tag
) < 0)
148 die("bad tag object %s", sha1_to_hex(obj
->sha1
));
149 add_object(tag
->tagged
, p
, NULL
, name
);
152 static void walk_commit_list(struct rev_info
*revs
)
155 struct commit
*commit
;
156 struct object_array objects
= { 0, 0, NULL
};
158 /* Walk all commits, process their trees */
159 while ((commit
= get_revision(revs
)) != NULL
)
160 process_tree(commit
->tree
, &objects
, NULL
, "");
162 /* Then walk all the pending objects, recursively processing them too */
163 for (i
= 0; i
< revs
->pending
.nr
; i
++) {
164 struct object_array_entry
*pending
= revs
->pending
.objects
+ i
;
165 struct object
*obj
= pending
->item
;
166 const char *name
= pending
->name
;
167 if (obj
->type
== OBJ_TAG
) {
168 process_tag((struct tag
*) obj
, &objects
, name
);
171 if (obj
->type
== OBJ_TREE
) {
172 process_tree((struct tree
*)obj
, &objects
, NULL
, name
);
175 if (obj
->type
== OBJ_BLOB
) {
176 process_blob((struct blob
*)obj
, &objects
, NULL
, name
);
179 die("unknown pending object %s (%s)", sha1_to_hex(obj
->sha1
), name
);
183 static int add_one_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
, char *datail
, void *cb_data
)
185 struct object
*object
;
186 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
188 object
= parse_object(osha1
);
190 add_pending_object(revs
, object
, "");
191 object
= parse_object(nsha1
);
193 add_pending_object(revs
, object
, "");
197 static int add_one_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
199 struct object
*object
= parse_object(sha1
);
200 struct rev_info
*revs
= (struct rev_info
*)cb_data
;
203 die("bad object ref: %s:%s", path
, sha1_to_hex(sha1
));
204 add_pending_object(revs
, object
, "");
209 static int add_one_reflog(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
211 for_each_reflog_ent(path
, add_one_reflog_ent
, cb_data
);
215 static void add_one_tree(const unsigned char *sha1
, struct rev_info
*revs
)
217 struct tree
*tree
= lookup_tree(sha1
);
218 add_pending_object(revs
, &tree
->object
, "");
221 static void add_cache_tree(struct cache_tree
*it
, struct rev_info
*revs
)
225 if (it
->entry_count
>= 0)
226 add_one_tree(it
->sha1
, revs
);
227 for (i
= 0; i
< it
->subtree_nr
; i
++)
228 add_cache_tree(it
->down
[i
]->cache_tree
, revs
);
231 static void add_cache_refs(struct rev_info
*revs
)
236 for (i
= 0; i
< active_nr
; i
++) {
237 lookup_blob(active_cache
[i
]->sha1
);
239 * We could add the blobs to the pending list, but quite
240 * frankly, we don't care. Once we've looked them up, and
241 * added them as objects, we've really done everything
242 * there is to do for a blob
245 if (active_cache_tree
)
246 add_cache_tree(active_cache_tree
, revs
);
249 int cmd_prune(int argc
, const char **argv
, const char *prefix
)
252 struct rev_info revs
;
254 for (i
= 1; i
< argc
; i
++) {
255 const char *arg
= argv
[i
];
256 if (!strcmp(arg
, "-n")) {
263 save_commit_buffer
= 0;
266 * Set up revision parsing, and mark us as being interested
267 * in all object types, not just commits.
269 init_revisions(&revs
, prefix
);
270 revs
.tag_objects
= 1;
271 revs
.blob_objects
= 1;
272 revs
.tree_objects
= 1;
274 /* Add all external refs */
275 for_each_ref(add_one_ref
, &revs
);
277 /* Add all refs from the index file */
278 add_cache_refs(&revs
);
280 /* Add all reflog info from refs */
281 for_each_ref(add_one_reflog
, &revs
);
284 * Set up the revision walk - this will move all commits
285 * from the pending list to the commit walking list.
287 prepare_revision_walk(&revs
);
289 walk_commit_list(&revs
);
291 prune_object_dir(get_object_directory());
294 prune_packed_objects(show_only
);