15 static const char reflog_expire_usage
[] =
16 "git-reflog (show|expire) [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
17 static const char reflog_delete_usage
[] =
18 "git-reflog delete [--verbose] [--dry-run] <refs>...";
20 static unsigned long default_reflog_expire
;
21 static unsigned long default_reflog_expire_unreachable
;
23 struct cmd_reflog_expire_cb
{
28 unsigned long expire_total
;
29 unsigned long expire_unreachable
;
33 struct expire_reflog_cb
{
36 struct commit
*ref_commit
;
37 struct cmd_reflog_expire_cb
*cmd
;
40 struct collected_reflog
{
41 unsigned char sha1
[20];
42 char reflog
[FLEX_ARRAY
];
44 struct collect_reflog_cb
{
45 struct collected_reflog
**e
;
50 #define INCOMPLETE (1u<<10)
51 #define STUDYING (1u<<11)
53 static int tree_is_complete(const unsigned char *sha1
)
55 struct tree_desc desc
;
56 struct name_entry entry
;
60 tree
= lookup_tree(sha1
);
63 if (tree
->object
.flags
& SEEN
)
65 if (tree
->object
.flags
& INCOMPLETE
)
69 enum object_type type
;
71 void *data
= read_sha1_file(sha1
, &type
, &size
);
73 tree
->object
.flags
|= INCOMPLETE
;
79 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
81 while (tree_entry(&desc
, &entry
)) {
82 if (!has_sha1_file(entry
.sha1
) ||
83 (S_ISDIR(entry
.mode
) && !tree_is_complete(entry
.sha1
))) {
84 tree
->object
.flags
|= INCOMPLETE
;
92 tree
->object
.flags
|= SEEN
;
96 static int commit_is_complete(struct commit
*commit
)
98 struct object_array study
;
99 struct object_array found
;
100 int is_incomplete
= 0;
104 if (commit
->object
.flags
& SEEN
)
106 if (commit
->object
.flags
& INCOMPLETE
)
109 * Find all commits that are reachable and are not marked as
110 * SEEN. Then make sure the trees and blobs contained are
111 * complete. After that, mark these commits also as SEEN.
112 * If some of the objects that are needed to complete this
113 * commit are missing, mark this commit as INCOMPLETE.
115 memset(&study
, 0, sizeof(study
));
116 memset(&found
, 0, sizeof(found
));
117 add_object_array(&commit
->object
, NULL
, &study
);
118 add_object_array(&commit
->object
, NULL
, &found
);
119 commit
->object
.flags
|= STUDYING
;
122 struct commit_list
*parent
;
124 c
= (struct commit
*)study
.objects
[--study
.nr
].item
;
125 if (!c
->object
.parsed
&& !parse_object(c
->object
.sha1
))
126 c
->object
.flags
|= INCOMPLETE
;
128 if (c
->object
.flags
& INCOMPLETE
) {
132 else if (c
->object
.flags
& SEEN
)
134 for (parent
= c
->parents
; parent
; parent
= parent
->next
) {
135 struct commit
*p
= parent
->item
;
136 if (p
->object
.flags
& STUDYING
)
138 p
->object
.flags
|= STUDYING
;
139 add_object_array(&p
->object
, NULL
, &study
);
140 add_object_array(&p
->object
, NULL
, &found
);
143 if (!is_incomplete
) {
145 * make sure all commits in "found" array have all the
148 for (i
= 0; i
< found
.nr
; i
++) {
150 (struct commit
*)found
.objects
[i
].item
;
151 if (!tree_is_complete(c
->tree
->object
.sha1
)) {
153 c
->object
.flags
|= INCOMPLETE
;
156 if (!is_incomplete
) {
157 /* mark all found commits as complete, iow SEEN */
158 for (i
= 0; i
< found
.nr
; i
++)
159 found
.objects
[i
].item
->flags
|= SEEN
;
162 /* clear flags from the objects we traversed */
163 for (i
= 0; i
< found
.nr
; i
++)
164 found
.objects
[i
].item
->flags
&= ~STUDYING
;
166 commit
->object
.flags
|= INCOMPLETE
;
169 * If we come here, we have (1) traversed the ancestry chain
170 * from the "commit" until we reach SEEN commits (which are
171 * known to be complete), and (2) made sure that the commits
172 * encountered during the above traversal refer to trees that
173 * are complete. Which means that we know *all* the commits
174 * we have seen during this process are complete.
176 for (i
= 0; i
< found
.nr
; i
++)
177 found
.objects
[i
].item
->flags
|= SEEN
;
179 /* free object arrays */
182 return !is_incomplete
;
185 static int keep_entry(struct commit
**it
, unsigned char *sha1
)
187 struct commit
*commit
;
189 if (is_null_sha1(sha1
))
191 commit
= lookup_commit_reference_gently(sha1
, 1);
196 * Make sure everything in this commit exists.
198 * We have walked all the objects reachable from the refs
199 * and cache earlier. The commits reachable by this commit
200 * must meet SEEN commits -- and then we should mark them as
203 if (!commit_is_complete(commit
))
209 static int expire_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
210 const char *email
, unsigned long timestamp
, int tz
,
211 const char *message
, void *cb_data
)
213 struct expire_reflog_cb
*cb
= cb_data
;
214 struct commit
*old
, *new;
216 if (timestamp
< cb
->cmd
->expire_total
)
220 if (cb
->cmd
->stalefix
&&
221 (!keep_entry(&old
, osha1
) || !keep_entry(&new, nsha1
)))
224 if (timestamp
< cb
->cmd
->expire_unreachable
) {
227 if (!old
&& !is_null_sha1(osha1
))
228 old
= lookup_commit_reference_gently(osha1
, 1);
229 if (!new && !is_null_sha1(nsha1
))
230 new = lookup_commit_reference_gently(nsha1
, 1);
231 if ((old
&& !in_merge_bases(old
, &cb
->ref_commit
, 1)) ||
232 (new && !in_merge_bases(new, &cb
->ref_commit
, 1)))
236 if (cb
->cmd
->recno
&& --(cb
->cmd
->recno
) == 0)
240 char sign
= (tz
< 0) ? '-' : '+';
241 int zone
= (tz
< 0) ? (-tz
) : tz
;
242 fprintf(cb
->newlog
, "%s %s %s %lu %c%04d\t%s",
243 sha1_to_hex(osha1
), sha1_to_hex(nsha1
),
244 email
, timestamp
, sign
, zone
,
247 if (cb
->cmd
->verbose
)
248 printf("keep %s", message
);
251 if (!cb
->newlog
|| cb
->cmd
->verbose
)
252 printf("%sprune %s", cb
->newlog
? "" : "would ", message
);
256 static int expire_reflog(const char *ref
, const unsigned char *sha1
, int unused
, void *cb_data
)
258 struct cmd_reflog_expire_cb
*cmd
= cb_data
;
259 struct expire_reflog_cb cb
;
260 struct ref_lock
*lock
;
261 char *log_file
, *newlog_path
= NULL
;
264 memset(&cb
, 0, sizeof(cb
));
265 /* we take the lock for the ref itself to prevent it from
268 lock
= lock_any_ref_for_update(ref
, sha1
, 0);
270 return error("cannot lock ref '%s'", ref
);
271 log_file
= xstrdup(git_path("logs/%s", ref
));
272 if (!file_exists(log_file
))
275 newlog_path
= xstrdup(git_path("logs/%s.lock", ref
));
276 cb
.newlog
= fopen(newlog_path
, "w");
279 cb
.ref_commit
= lookup_commit_reference_gently(sha1
, 1);
282 for_each_reflog_ent(ref
, expire_reflog_ent
, &cb
);
285 if (fclose(cb
.newlog
)) {
286 status
|= error("%s: %s", strerror(errno
),
289 } else if (rename(newlog_path
, log_file
)) {
290 status
|= error("cannot rename %s to %s",
291 newlog_path
, log_file
);
301 static int collect_reflog(const char *ref
, const unsigned char *sha1
, int unused
, void *cb_data
)
303 struct collected_reflog
*e
;
304 struct collect_reflog_cb
*cb
= cb_data
;
305 size_t namelen
= strlen(ref
);
307 e
= xmalloc(sizeof(*e
) + namelen
+ 1);
308 hashcpy(e
->sha1
, sha1
);
309 memcpy(e
->reflog
, ref
, namelen
+ 1);
310 ALLOC_GROW(cb
->e
, cb
->nr
+ 1, cb
->alloc
);
315 static int reflog_expire_config(const char *var
, const char *value
)
317 if (!strcmp(var
, "gc.reflogexpire")) {
319 config_error_nonbool(var
);
320 default_reflog_expire
= approxidate(value
);
323 if (!strcmp(var
, "gc.reflogexpireunreachable")) {
325 config_error_nonbool(var
);
326 default_reflog_expire_unreachable
= approxidate(value
);
329 return git_default_config(var
, value
);
332 static int cmd_reflog_expire(int argc
, const char **argv
, const char *prefix
)
334 struct cmd_reflog_expire_cb cb
;
335 unsigned long now
= time(NULL
);
336 int i
, status
, do_all
;
338 git_config(reflog_expire_config
);
340 save_commit_buffer
= 0;
342 memset(&cb
, 0, sizeof(cb
));
344 if (!default_reflog_expire_unreachable
)
345 default_reflog_expire_unreachable
= now
- 30 * 24 * 3600;
346 if (!default_reflog_expire
)
347 default_reflog_expire
= now
- 90 * 24 * 3600;
348 cb
.expire_total
= default_reflog_expire
;
349 cb
.expire_unreachable
= default_reflog_expire_unreachable
;
352 * We can trust the commits and objects reachable from refs
353 * even in older repository. We cannot trust what's reachable
354 * from reflog if the repository was pruned with older git.
357 for (i
= 1; i
< argc
; i
++) {
358 const char *arg
= argv
[i
];
359 if (!strcmp(arg
, "--dry-run") || !strcmp(arg
, "-n"))
361 else if (!prefixcmp(arg
, "--expire="))
362 cb
.expire_total
= approxidate(arg
+ 9);
363 else if (!prefixcmp(arg
, "--expire-unreachable="))
364 cb
.expire_unreachable
= approxidate(arg
+ 21);
365 else if (!strcmp(arg
, "--stale-fix"))
367 else if (!strcmp(arg
, "--all"))
369 else if (!strcmp(arg
, "--verbose"))
371 else if (!strcmp(arg
, "--")) {
375 else if (arg
[0] == '-')
376 usage(reflog_expire_usage
);
381 init_revisions(&cb
.revs
, prefix
);
383 printf("Marking reachable objects...");
384 mark_reachable_objects(&cb
.revs
, 0);
390 struct collect_reflog_cb collected
;
393 memset(&collected
, 0, sizeof(collected
));
394 for_each_reflog(collect_reflog
, &collected
);
395 for (i
= 0; i
< collected
.nr
; i
++) {
396 struct collected_reflog
*e
= collected
.e
[i
];
397 status
|= expire_reflog(e
->reflog
, e
->sha1
, 0, &cb
);
404 const char *ref
= argv
[i
++];
405 unsigned char sha1
[20];
406 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
407 status
|= error("%s points nowhere!", ref
);
410 status
|= expire_reflog(ref
, sha1
, 0, &cb
);
415 static int count_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
416 const char *email
, unsigned long timestamp
, int tz
,
417 const char *message
, void *cb_data
)
419 struct cmd_reflog_expire_cb
*cb
= cb_data
;
420 if (!cb
->expire_total
|| timestamp
< cb
->expire_total
)
425 static int cmd_reflog_delete(int argc
, const char **argv
, const char *prefix
)
427 struct cmd_reflog_expire_cb cb
;
430 memset(&cb
, 0, sizeof(cb
));
432 for (i
= 1; i
< argc
; i
++) {
433 const char *arg
= argv
[i
];
434 if (!strcmp(arg
, "--dry-run") || !strcmp(arg
, "-n"))
436 else if (!strcmp(arg
, "--verbose"))
438 else if (!strcmp(arg
, "--")) {
442 else if (arg
[0] == '-')
443 usage(reflog_delete_usage
);
449 return error("Nothing to delete?");
451 for ( ; i
< argc
; i
++) {
452 const char *spec
= strstr(argv
[i
], "@{");
453 unsigned char sha1
[20];
458 status
|= error("Not a reflog: %s", argv
[i
]);
462 if (!dwim_ref(argv
[i
], spec
- argv
[i
], sha1
, &ref
)) {
463 status
|= error("%s points nowhere!", argv
[i
]);
467 recno
= strtoul(spec
+ 2, &ep
, 10);
470 for_each_reflog_ent(ref
, count_reflog_ent
, &cb
);
472 cb
.expire_total
= approxidate(spec
+ 2);
473 for_each_reflog_ent(ref
, count_reflog_ent
, &cb
);
477 status
|= expire_reflog(ref
, sha1
, 0, &cb
);
487 static const char reflog_usage
[] =
488 "git-reflog (expire | ...)";
490 int cmd_reflog(int argc
, const char **argv
, const char *prefix
)
492 /* With no command, we default to showing it. */
493 if (argc
< 2 || *argv
[1] == '-')
494 return cmd_log_reflog(argc
, argv
, prefix
);
496 if (!strcmp(argv
[1], "show"))
497 return cmd_log_reflog(argc
- 1, argv
+ 1, prefix
);
499 if (!strcmp(argv
[1], "expire"))
500 return cmd_reflog_expire(argc
- 1, argv
+ 1, prefix
);
502 if (!strcmp(argv
[1], "delete"))
503 return cmd_reflog_delete(argc
- 1, argv
+ 1, prefix
);
505 /* Not a recognized reflog command..*/