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] [--rewrite] [--updateref] <refs>...";
20 static unsigned long default_reflog_expire
;
21 static unsigned long default_reflog_expire_unreachable
;
23 struct cmd_reflog_expire_cb
{
30 unsigned long expire_total
;
31 unsigned long expire_unreachable
;
35 struct expire_reflog_cb
{
38 struct commit
*ref_commit
;
39 struct cmd_reflog_expire_cb
*cmd
;
40 unsigned char last_kept_sha1
[20];
43 struct collected_reflog
{
44 unsigned char sha1
[20];
45 char reflog
[FLEX_ARRAY
];
47 struct collect_reflog_cb
{
48 struct collected_reflog
**e
;
53 #define INCOMPLETE (1u<<10)
54 #define STUDYING (1u<<11)
56 static int tree_is_complete(const unsigned char *sha1
)
58 struct tree_desc desc
;
59 struct name_entry entry
;
63 tree
= lookup_tree(sha1
);
66 if (tree
->object
.flags
& SEEN
)
68 if (tree
->object
.flags
& INCOMPLETE
)
72 enum object_type type
;
74 void *data
= read_sha1_file(sha1
, &type
, &size
);
76 tree
->object
.flags
|= INCOMPLETE
;
82 init_tree_desc(&desc
, tree
->buffer
, tree
->size
);
84 while (tree_entry(&desc
, &entry
)) {
85 if (!has_sha1_file(entry
.sha1
) ||
86 (S_ISDIR(entry
.mode
) && !tree_is_complete(entry
.sha1
))) {
87 tree
->object
.flags
|= INCOMPLETE
;
95 tree
->object
.flags
|= SEEN
;
99 static int commit_is_complete(struct commit
*commit
)
101 struct object_array study
;
102 struct object_array found
;
103 int is_incomplete
= 0;
107 if (commit
->object
.flags
& SEEN
)
109 if (commit
->object
.flags
& INCOMPLETE
)
112 * Find all commits that are reachable and are not marked as
113 * SEEN. Then make sure the trees and blobs contained are
114 * complete. After that, mark these commits also as SEEN.
115 * If some of the objects that are needed to complete this
116 * commit are missing, mark this commit as INCOMPLETE.
118 memset(&study
, 0, sizeof(study
));
119 memset(&found
, 0, sizeof(found
));
120 add_object_array(&commit
->object
, NULL
, &study
);
121 add_object_array(&commit
->object
, NULL
, &found
);
122 commit
->object
.flags
|= STUDYING
;
125 struct commit_list
*parent
;
127 c
= (struct commit
*)study
.objects
[--study
.nr
].item
;
128 if (!c
->object
.parsed
&& !parse_object(c
->object
.sha1
))
129 c
->object
.flags
|= INCOMPLETE
;
131 if (c
->object
.flags
& INCOMPLETE
) {
135 else if (c
->object
.flags
& SEEN
)
137 for (parent
= c
->parents
; parent
; parent
= parent
->next
) {
138 struct commit
*p
= parent
->item
;
139 if (p
->object
.flags
& STUDYING
)
141 p
->object
.flags
|= STUDYING
;
142 add_object_array(&p
->object
, NULL
, &study
);
143 add_object_array(&p
->object
, NULL
, &found
);
146 if (!is_incomplete
) {
148 * make sure all commits in "found" array have all the
151 for (i
= 0; i
< found
.nr
; i
++) {
153 (struct commit
*)found
.objects
[i
].item
;
154 if (!tree_is_complete(c
->tree
->object
.sha1
)) {
156 c
->object
.flags
|= INCOMPLETE
;
159 if (!is_incomplete
) {
160 /* mark all found commits as complete, iow SEEN */
161 for (i
= 0; i
< found
.nr
; i
++)
162 found
.objects
[i
].item
->flags
|= SEEN
;
165 /* clear flags from the objects we traversed */
166 for (i
= 0; i
< found
.nr
; i
++)
167 found
.objects
[i
].item
->flags
&= ~STUDYING
;
169 commit
->object
.flags
|= INCOMPLETE
;
172 * If we come here, we have (1) traversed the ancestry chain
173 * from the "commit" until we reach SEEN commits (which are
174 * known to be complete), and (2) made sure that the commits
175 * encountered during the above traversal refer to trees that
176 * are complete. Which means that we know *all* the commits
177 * we have seen during this process are complete.
179 for (i
= 0; i
< found
.nr
; i
++)
180 found
.objects
[i
].item
->flags
|= SEEN
;
182 /* free object arrays */
185 return !is_incomplete
;
188 static int keep_entry(struct commit
**it
, unsigned char *sha1
)
190 struct commit
*commit
;
192 if (is_null_sha1(sha1
))
194 commit
= lookup_commit_reference_gently(sha1
, 1);
199 * Make sure everything in this commit exists.
201 * We have walked all the objects reachable from the refs
202 * and cache earlier. The commits reachable by this commit
203 * must meet SEEN commits -- and then we should mark them as
206 if (!commit_is_complete(commit
))
212 static int expire_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
213 const char *email
, unsigned long timestamp
, int tz
,
214 const char *message
, void *cb_data
)
216 struct expire_reflog_cb
*cb
= cb_data
;
217 struct commit
*old
, *new;
219 if (timestamp
< cb
->cmd
->expire_total
)
222 if (cb
->cmd
->rewrite
)
223 osha1
= cb
->last_kept_sha1
;
226 if (cb
->cmd
->stalefix
&&
227 (!keep_entry(&old
, osha1
) || !keep_entry(&new, nsha1
)))
230 if (timestamp
< cb
->cmd
->expire_unreachable
) {
233 if (!old
&& !is_null_sha1(osha1
))
234 old
= lookup_commit_reference_gently(osha1
, 1);
235 if (!new && !is_null_sha1(nsha1
))
236 new = lookup_commit_reference_gently(nsha1
, 1);
237 if ((old
&& !in_merge_bases(old
, &cb
->ref_commit
, 1)) ||
238 (new && !in_merge_bases(new, &cb
->ref_commit
, 1)))
242 if (cb
->cmd
->recno
&& --(cb
->cmd
->recno
) == 0)
246 char sign
= (tz
< 0) ? '-' : '+';
247 int zone
= (tz
< 0) ? (-tz
) : tz
;
248 fprintf(cb
->newlog
, "%s %s %s %lu %c%04d\t%s",
249 sha1_to_hex(osha1
), sha1_to_hex(nsha1
),
250 email
, timestamp
, sign
, zone
,
252 hashcpy(cb
->last_kept_sha1
, nsha1
);
254 if (cb
->cmd
->verbose
)
255 printf("keep %s", message
);
258 if (!cb
->newlog
|| cb
->cmd
->verbose
)
259 printf("%sprune %s", cb
->newlog
? "" : "would ", message
);
263 static int expire_reflog(const char *ref
, const unsigned char *sha1
, int unused
, void *cb_data
)
265 struct cmd_reflog_expire_cb
*cmd
= cb_data
;
266 struct expire_reflog_cb cb
;
267 struct ref_lock
*lock
;
268 char *log_file
, *newlog_path
= NULL
;
271 memset(&cb
, 0, sizeof(cb
));
272 /* we take the lock for the ref itself to prevent it from
275 lock
= lock_any_ref_for_update(ref
, sha1
, 0);
277 return error("cannot lock ref '%s'", ref
);
278 log_file
= xstrdup(git_path("logs/%s", ref
));
279 if (!file_exists(log_file
))
282 newlog_path
= xstrdup(git_path("logs/%s.lock", ref
));
283 cb
.newlog
= fopen(newlog_path
, "w");
286 cb
.ref_commit
= lookup_commit_reference_gently(sha1
, 1);
289 for_each_reflog_ent(ref
, expire_reflog_ent
, &cb
);
292 if (fclose(cb
.newlog
)) {
293 status
|= error("%s: %s", strerror(errno
),
296 } else if (cmd
->updateref
&&
297 (write_in_full(lock
->lock_fd
,
298 sha1_to_hex(cb
.last_kept_sha1
), 40) != 40 ||
299 write_in_full(lock
->lock_fd
, "\n", 1) != 1 ||
300 close_ref(lock
) < 0)) {
301 status
|= error("Couldn't write %s",
304 } else if (rename(newlog_path
, log_file
)) {
305 status
|= error("cannot rename %s to %s",
306 newlog_path
, log_file
);
308 } else if (cmd
->updateref
&& commit_ref(lock
)) {
309 status
|= error("Couldn't set %s", lock
->ref_name
);
311 adjust_shared_perm(log_file
);
320 static int collect_reflog(const char *ref
, const unsigned char *sha1
, int unused
, void *cb_data
)
322 struct collected_reflog
*e
;
323 struct collect_reflog_cb
*cb
= cb_data
;
324 size_t namelen
= strlen(ref
);
326 e
= xmalloc(sizeof(*e
) + namelen
+ 1);
327 hashcpy(e
->sha1
, sha1
);
328 memcpy(e
->reflog
, ref
, namelen
+ 1);
329 ALLOC_GROW(cb
->e
, cb
->nr
+ 1, cb
->alloc
);
334 static int reflog_expire_config(const char *var
, const char *value
, void *cb
)
336 if (!strcmp(var
, "gc.reflogexpire")) {
338 config_error_nonbool(var
);
339 default_reflog_expire
= approxidate(value
);
342 if (!strcmp(var
, "gc.reflogexpireunreachable")) {
344 config_error_nonbool(var
);
345 default_reflog_expire_unreachable
= approxidate(value
);
348 return git_default_config(var
, value
, cb
);
351 static int cmd_reflog_expire(int argc
, const char **argv
, const char *prefix
)
353 struct cmd_reflog_expire_cb cb
;
354 unsigned long now
= time(NULL
);
355 int i
, status
, do_all
;
357 git_config(reflog_expire_config
, NULL
);
359 save_commit_buffer
= 0;
361 memset(&cb
, 0, sizeof(cb
));
363 if (!default_reflog_expire_unreachable
)
364 default_reflog_expire_unreachable
= now
- 30 * 24 * 3600;
365 if (!default_reflog_expire
)
366 default_reflog_expire
= now
- 90 * 24 * 3600;
367 cb
.expire_total
= default_reflog_expire
;
368 cb
.expire_unreachable
= default_reflog_expire_unreachable
;
371 * We can trust the commits and objects reachable from refs
372 * even in older repository. We cannot trust what's reachable
373 * from reflog if the repository was pruned with older git.
376 for (i
= 1; i
< argc
; i
++) {
377 const char *arg
= argv
[i
];
378 if (!strcmp(arg
, "--dry-run") || !strcmp(arg
, "-n"))
380 else if (!prefixcmp(arg
, "--expire="))
381 cb
.expire_total
= approxidate(arg
+ 9);
382 else if (!prefixcmp(arg
, "--expire-unreachable="))
383 cb
.expire_unreachable
= approxidate(arg
+ 21);
384 else if (!strcmp(arg
, "--stale-fix"))
386 else if (!strcmp(arg
, "--rewrite"))
388 else if (!strcmp(arg
, "--updateref"))
390 else if (!strcmp(arg
, "--all"))
392 else if (!strcmp(arg
, "--verbose"))
394 else if (!strcmp(arg
, "--")) {
398 else if (arg
[0] == '-')
399 usage(reflog_expire_usage
);
404 init_revisions(&cb
.revs
, prefix
);
406 printf("Marking reachable objects...");
407 mark_reachable_objects(&cb
.revs
, 0);
413 struct collect_reflog_cb collected
;
416 memset(&collected
, 0, sizeof(collected
));
417 for_each_reflog(collect_reflog
, &collected
);
418 for (i
= 0; i
< collected
.nr
; i
++) {
419 struct collected_reflog
*e
= collected
.e
[i
];
420 status
|= expire_reflog(e
->reflog
, e
->sha1
, 0, &cb
);
427 const char *ref
= argv
[i
++];
428 unsigned char sha1
[20];
429 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
430 status
|= error("%s points nowhere!", ref
);
433 status
|= expire_reflog(ref
, sha1
, 0, &cb
);
438 static int count_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
439 const char *email
, unsigned long timestamp
, int tz
,
440 const char *message
, void *cb_data
)
442 struct cmd_reflog_expire_cb
*cb
= cb_data
;
443 if (!cb
->expire_total
|| timestamp
< cb
->expire_total
)
448 static int cmd_reflog_delete(int argc
, const char **argv
, const char *prefix
)
450 struct cmd_reflog_expire_cb cb
;
453 memset(&cb
, 0, sizeof(cb
));
455 for (i
= 1; i
< argc
; i
++) {
456 const char *arg
= argv
[i
];
457 if (!strcmp(arg
, "--dry-run") || !strcmp(arg
, "-n"))
459 else if (!strcmp(arg
, "--rewrite"))
461 else if (!strcmp(arg
, "--updateref"))
463 else if (!strcmp(arg
, "--verbose"))
465 else if (!strcmp(arg
, "--")) {
469 else if (arg
[0] == '-')
470 usage(reflog_delete_usage
);
476 return error("Nothing to delete?");
478 for ( ; i
< argc
; i
++) {
479 const char *spec
= strstr(argv
[i
], "@{");
480 unsigned char sha1
[20];
485 status
|= error("Not a reflog: %s", argv
[i
]);
489 if (!dwim_ref(argv
[i
], spec
- argv
[i
], sha1
, &ref
)) {
490 status
|= error("%s points nowhere!", argv
[i
]);
494 recno
= strtoul(spec
+ 2, &ep
, 10);
497 for_each_reflog_ent(ref
, count_reflog_ent
, &cb
);
499 cb
.expire_total
= approxidate(spec
+ 2);
500 for_each_reflog_ent(ref
, count_reflog_ent
, &cb
);
504 status
|= expire_reflog(ref
, sha1
, 0, &cb
);
514 static const char reflog_usage
[] =
515 "git-reflog (expire | ...)";
517 int cmd_reflog(int argc
, const char **argv
, const char *prefix
)
519 /* With no command, we default to showing it. */
520 if (argc
< 2 || *argv
[1] == '-')
521 return cmd_log_reflog(argc
, argv
, prefix
);
523 if (!strcmp(argv
[1], "show"))
524 return cmd_log_reflog(argc
- 1, argv
+ 1, prefix
);
526 if (!strcmp(argv
[1], "expire"))
527 return cmd_reflog_expire(argc
- 1, argv
+ 1, prefix
);
529 if (!strcmp(argv
[1], "delete"))
530 return cmd_reflog_delete(argc
- 1, argv
+ 1, prefix
);
532 /* Not a recognized reflog command..*/