15 static const char reflog_expire_usage
[] =
16 "git-reflog (show|expire) [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
18 static unsigned long default_reflog_expire
;
19 static unsigned long default_reflog_expire_unreachable
;
21 struct cmd_reflog_expire_cb
{
26 unsigned long expire_total
;
27 unsigned long expire_unreachable
;
31 struct expire_reflog_cb
{
34 struct commit
*ref_commit
;
35 struct cmd_reflog_expire_cb
*cmd
;
38 struct collected_reflog
{
39 unsigned char sha1
[20];
40 char reflog
[FLEX_ARRAY
];
42 struct collect_reflog_cb
{
43 struct collected_reflog
**e
;
48 #define INCOMPLETE (1u<<10)
49 #define STUDYING (1u<<11)
51 static int tree_is_complete(const unsigned char *sha1
)
53 struct tree_desc desc
;
54 struct name_entry entry
;
58 tree
= lookup_tree(sha1
);
61 if (tree
->object
.flags
& SEEN
)
63 if (tree
->object
.flags
& INCOMPLETE
)
67 enum object_type type
;
69 void *data
= read_sha1_file(sha1
, &type
, &size
);
71 tree
->object
.flags
|= INCOMPLETE
;
77 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
79 while (tree_entry(&desc
, &entry
)) {
80 if (!has_sha1_file(entry
.sha1
) ||
81 (S_ISDIR(entry
.mode
) && !tree_is_complete(entry
.sha1
))) {
82 tree
->object
.flags
|= INCOMPLETE
;
90 tree
->object
.flags
|= SEEN
;
94 static int commit_is_complete(struct commit
*commit
)
96 struct object_array study
;
97 struct object_array found
;
98 int is_incomplete
= 0;
102 if (commit
->object
.flags
& SEEN
)
104 if (commit
->object
.flags
& INCOMPLETE
)
107 * Find all commits that are reachable and are not marked as
108 * SEEN. Then make sure the trees and blobs contained are
109 * complete. After that, mark these commits also as SEEN.
110 * If some of the objects that are needed to complete this
111 * commit are missing, mark this commit as INCOMPLETE.
113 memset(&study
, 0, sizeof(study
));
114 memset(&found
, 0, sizeof(found
));
115 add_object_array(&commit
->object
, NULL
, &study
);
116 add_object_array(&commit
->object
, NULL
, &found
);
117 commit
->object
.flags
|= STUDYING
;
120 struct commit_list
*parent
;
122 c
= (struct commit
*)study
.objects
[--study
.nr
].item
;
123 if (!c
->object
.parsed
&& !parse_object(c
->object
.sha1
))
124 c
->object
.flags
|= INCOMPLETE
;
126 if (c
->object
.flags
& INCOMPLETE
) {
130 else if (c
->object
.flags
& SEEN
)
132 for (parent
= c
->parents
; parent
; parent
= parent
->next
) {
133 struct commit
*p
= parent
->item
;
134 if (p
->object
.flags
& STUDYING
)
136 p
->object
.flags
|= STUDYING
;
137 add_object_array(&p
->object
, NULL
, &study
);
138 add_object_array(&p
->object
, NULL
, &found
);
141 if (!is_incomplete
) {
143 * make sure all commits in "found" array have all the
146 for (i
= 0; i
< found
.nr
; i
++) {
148 (struct commit
*)found
.objects
[i
].item
;
149 if (!tree_is_complete(c
->tree
->object
.sha1
)) {
151 c
->object
.flags
|= INCOMPLETE
;
154 if (!is_incomplete
) {
155 /* mark all found commits as complete, iow SEEN */
156 for (i
= 0; i
< found
.nr
; i
++)
157 found
.objects
[i
].item
->flags
|= SEEN
;
160 /* clear flags from the objects we traversed */
161 for (i
= 0; i
< found
.nr
; i
++)
162 found
.objects
[i
].item
->flags
&= ~STUDYING
;
164 commit
->object
.flags
|= INCOMPLETE
;
167 * If we come here, we have (1) traversed the ancestry chain
168 * from the "commit" until we reach SEEN commits (which are
169 * known to be complete), and (2) made sure that the commits
170 * encountered during the above traversal refer to trees that
171 * are complete. Which means that we know *all* the commits
172 * we have seen during this process are complete.
174 for (i
= 0; i
< found
.nr
; i
++)
175 found
.objects
[i
].item
->flags
|= SEEN
;
177 /* free object arrays */
180 return !is_incomplete
;
183 static int keep_entry(struct commit
**it
, unsigned char *sha1
)
185 struct commit
*commit
;
187 if (is_null_sha1(sha1
))
189 commit
= lookup_commit_reference_gently(sha1
, 1);
194 * Make sure everything in this commit exists.
196 * We have walked all the objects reachable from the refs
197 * and cache earlier. The commits reachable by this commit
198 * must meet SEEN commits -- and then we should mark them as
201 if (!commit_is_complete(commit
))
207 static int expire_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
208 const char *email
, unsigned long timestamp
, int tz
,
209 const char *message
, void *cb_data
)
211 struct expire_reflog_cb
*cb
= cb_data
;
212 struct commit
*old
, *new;
214 if (timestamp
< cb
->cmd
->expire_total
)
218 if (cb
->cmd
->stalefix
&&
219 (!keep_entry(&old
, osha1
) || !keep_entry(&new, nsha1
)))
222 if (timestamp
< cb
->cmd
->expire_unreachable
) {
225 if (!old
&& !is_null_sha1(osha1
))
226 old
= lookup_commit_reference_gently(osha1
, 1);
227 if (!new && !is_null_sha1(nsha1
))
228 new = lookup_commit_reference_gently(nsha1
, 1);
229 if ((old
&& !in_merge_bases(old
, &cb
->ref_commit
, 1)) ||
230 (new && !in_merge_bases(new, &cb
->ref_commit
, 1)))
234 if (cb
->cmd
->recno
&& --(cb
->cmd
->recno
) == 0)
238 char sign
= (tz
< 0) ? '-' : '+';
239 int zone
= (tz
< 0) ? (-tz
) : tz
;
240 fprintf(cb
->newlog
, "%s %s %s %lu %c%04d\t%s",
241 sha1_to_hex(osha1
), sha1_to_hex(nsha1
),
242 email
, timestamp
, sign
, zone
,
245 if (cb
->cmd
->verbose
)
246 printf("keep %s", message
);
249 if (!cb
->newlog
|| cb
->cmd
->verbose
)
250 printf("%sprune %s", cb
->newlog
? "" : "would ", message
);
254 static int expire_reflog(const char *ref
, const unsigned char *sha1
, int unused
, void *cb_data
)
256 struct cmd_reflog_expire_cb
*cmd
= cb_data
;
257 struct expire_reflog_cb cb
;
258 struct ref_lock
*lock
;
259 char *log_file
, *newlog_path
= NULL
;
262 memset(&cb
, 0, sizeof(cb
));
263 /* we take the lock for the ref itself to prevent it from
266 lock
= lock_any_ref_for_update(ref
, sha1
, 0);
268 return error("cannot lock ref '%s'", ref
);
269 log_file
= xstrdup(git_path("logs/%s", ref
));
270 if (!file_exists(log_file
))
273 newlog_path
= xstrdup(git_path("logs/%s.lock", ref
));
274 cb
.newlog
= fopen(newlog_path
, "w");
277 cb
.ref_commit
= lookup_commit_reference_gently(sha1
, 1);
280 for_each_reflog_ent(ref
, expire_reflog_ent
, &cb
);
283 if (fclose(cb
.newlog
)) {
284 status
|= error("%s: %s", strerror(errno
),
287 } else if (rename(newlog_path
, log_file
)) {
288 status
|= error("cannot rename %s to %s",
289 newlog_path
, log_file
);
299 static int collect_reflog(const char *ref
, const unsigned char *sha1
, int unused
, void *cb_data
)
301 struct collected_reflog
*e
;
302 struct collect_reflog_cb
*cb
= cb_data
;
303 size_t namelen
= strlen(ref
);
305 e
= xmalloc(sizeof(*e
) + namelen
+ 1);
306 hashcpy(e
->sha1
, sha1
);
307 memcpy(e
->reflog
, ref
, namelen
+ 1);
308 ALLOC_GROW(cb
->e
, cb
->nr
+ 1, cb
->alloc
);
313 static int reflog_expire_config(const char *var
, const char *value
)
315 if (!strcmp(var
, "gc.reflogexpire")) {
317 config_error_nonbool(var
);
318 default_reflog_expire
= approxidate(value
);
321 if (!strcmp(var
, "gc.reflogexpireunreachable")) {
323 config_error_nonbool(var
);
324 default_reflog_expire_unreachable
= approxidate(value
);
327 return git_default_config(var
, value
);
330 static int cmd_reflog_expire(int argc
, const char **argv
, const char *prefix
)
332 struct cmd_reflog_expire_cb cb
;
333 unsigned long now
= time(NULL
);
334 int i
, status
, do_all
;
336 git_config(reflog_expire_config
);
338 save_commit_buffer
= 0;
340 memset(&cb
, 0, sizeof(cb
));
342 if (!default_reflog_expire_unreachable
)
343 default_reflog_expire_unreachable
= now
- 30 * 24 * 3600;
344 if (!default_reflog_expire
)
345 default_reflog_expire
= now
- 90 * 24 * 3600;
346 cb
.expire_total
= default_reflog_expire
;
347 cb
.expire_unreachable
= default_reflog_expire_unreachable
;
350 * We can trust the commits and objects reachable from refs
351 * even in older repository. We cannot trust what's reachable
352 * from reflog if the repository was pruned with older git.
355 for (i
= 1; i
< argc
; i
++) {
356 const char *arg
= argv
[i
];
357 if (!strcmp(arg
, "--dry-run") || !strcmp(arg
, "-n"))
359 else if (!prefixcmp(arg
, "--expire="))
360 cb
.expire_total
= approxidate(arg
+ 9);
361 else if (!prefixcmp(arg
, "--expire-unreachable="))
362 cb
.expire_unreachable
= approxidate(arg
+ 21);
363 else if (!strcmp(arg
, "--stale-fix"))
365 else if (!strcmp(arg
, "--all"))
367 else if (!strcmp(arg
, "--verbose"))
369 else if (!strcmp(arg
, "--")) {
373 else if (arg
[0] == '-')
374 usage(reflog_expire_usage
);
379 init_revisions(&cb
.revs
, prefix
);
381 printf("Marking reachable objects...");
382 mark_reachable_objects(&cb
.revs
, 0);
388 struct collect_reflog_cb collected
;
391 memset(&collected
, 0, sizeof(collected
));
392 for_each_reflog(collect_reflog
, &collected
);
393 for (i
= 0; i
< collected
.nr
; i
++) {
394 struct collected_reflog
*e
= collected
.e
[i
];
395 status
|= expire_reflog(e
->reflog
, e
->sha1
, 0, &cb
);
402 const char *ref
= argv
[i
++];
403 unsigned char sha1
[20];
404 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
405 status
|= error("%s points nowhere!", ref
);
408 status
|= expire_reflog(ref
, sha1
, 0, &cb
);
413 static int count_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
414 const char *email
, unsigned long timestamp
, int tz
,
415 const char *message
, void *cb_data
)
417 struct cmd_reflog_expire_cb
*cb
= cb_data
;
418 if (!cb
->expire_total
|| timestamp
< cb
->expire_total
)
423 static int cmd_reflog_delete(int argc
, const char **argv
, const char *prefix
)
425 struct cmd_reflog_expire_cb cb
;
429 return error("Nothing to delete?");
431 memset(&cb
, 0, sizeof(cb
));
433 for (i
= 1; i
< argc
; i
++) {
434 const char *spec
= strstr(argv
[i
], "@{");
435 unsigned char sha1
[20];
440 status
|= error("Not a reflog: %s", argv
[i
]);
444 if (!dwim_ref(argv
[i
], spec
- argv
[i
], sha1
, &ref
)) {
445 status
|= error("%s points nowhere!", argv
[i
]);
449 recno
= strtoul(spec
+ 2, &ep
, 10);
452 for_each_reflog_ent(ref
, count_reflog_ent
, &cb
);
454 cb
.expire_total
= approxidate(spec
+ 2);
455 for_each_reflog_ent(ref
, count_reflog_ent
, &cb
);
459 status
|= expire_reflog(ref
, sha1
, 0, &cb
);
469 static const char reflog_usage
[] =
470 "git-reflog (expire | ...)";
472 int cmd_reflog(int argc
, const char **argv
, const char *prefix
)
474 /* With no command, we default to showing it. */
475 if (argc
< 2 || *argv
[1] == '-')
476 return cmd_log_reflog(argc
, argv
, prefix
);
478 if (!strcmp(argv
[1], "show"))
479 return cmd_log_reflog(argc
- 1, argv
+ 1, prefix
);
481 if (!strcmp(argv
[1], "expire"))
482 return cmd_reflog_expire(argc
- 1, argv
+ 1, prefix
);
484 if (!strcmp(argv
[1], "delete"))
485 return cmd_reflog_delete(argc
- 1, argv
+ 1, prefix
);
487 /* Not a recognized reflog command..*/