15 static const char reflog_expire_usage
[] =
16 "git-reflog expire [--verbose] [--dry-run] [--fix-stale] [--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
;
30 struct expire_reflog_cb
{
33 struct commit
*ref_commit
;
34 struct cmd_reflog_expire_cb
*cmd
;
37 static int tree_is_complete(const unsigned char *sha1
)
39 struct tree_desc desc
;
43 buf
= read_sha1_file(sha1
, type
, &desc
.size
);
48 const unsigned char *elem
;
52 elem
= tree_entry_extract(&desc
, &name
, &mode
);
53 if (!has_sha1_file(elem
) ||
54 (S_ISDIR(mode
) && !tree_is_complete(elem
))) {
58 update_tree_entry(&desc
);
64 #define INCOMPLETE (1u<<10)
65 #define STUDYING (1u<<11)
67 static int commit_is_complete(struct commit
*commit
)
69 struct object_array study
;
70 struct object_array found
;
71 int is_incomplete
= 0;
75 if (commit
->object
.flags
& SEEN
)
77 if (commit
->object
.flags
& INCOMPLETE
)
80 * Find all commits that are reachable and are not marked as
81 * SEEN. Then make sure the trees and blobs contained are
82 * complete. After that, mark these commits also as SEEN.
83 * If some of the objects that are needed to complete this
84 * commit are missing, mark this commit as INCOMPLETE.
86 memset(&study
, 0, sizeof(study
));
87 memset(&found
, 0, sizeof(found
));
88 add_object_array(&commit
->object
, NULL
, &study
);
89 add_object_array(&commit
->object
, NULL
, &found
);
90 commit
->object
.flags
|= STUDYING
;
93 struct commit_list
*parent
;
95 c
= (struct commit
*)study
.objects
[--study
.nr
].item
;
96 if (!c
->object
.parsed
&& !parse_object(c
->object
.sha1
))
97 c
->object
.flags
|= INCOMPLETE
;
99 if (c
->object
.flags
& INCOMPLETE
) {
103 else if (c
->object
.flags
& SEEN
)
105 for (parent
= c
->parents
; parent
; parent
= parent
->next
) {
106 struct commit
*p
= parent
->item
;
107 if (p
->object
.flags
& STUDYING
)
109 p
->object
.flags
|= STUDYING
;
110 add_object_array(&p
->object
, NULL
, &study
);
111 add_object_array(&p
->object
, NULL
, &found
);
114 if (!is_incomplete
) {
115 /* make sure all commits in found have all the
118 for (i
= 0; !is_incomplete
&& i
< found
.nr
; i
++) {
120 (struct commit
*)found
.objects
[i
].item
;
121 if (!tree_is_complete(c
->tree
->object
.sha1
))
124 if (!is_incomplete
) {
125 /* mark all found commits as complete, iow SEEN */
126 for (i
= 0; i
< found
.nr
; i
++)
127 found
.objects
[i
].item
->flags
|= SEEN
;
130 /* clear flags from the objects we traversed */
131 for (i
= 0; i
< found
.nr
; i
++)
132 found
.objects
[i
].item
->flags
&= ~STUDYING
;
134 commit
->object
.flags
|= INCOMPLETE
;
135 /* free object arrays */
138 return !is_incomplete
;
141 static int keep_entry(struct commit
**it
, unsigned char *sha1
)
143 struct commit
*commit
;
146 if (is_null_sha1(sha1
))
148 commit
= lookup_commit_reference_gently(sha1
, 1);
153 * Make sure everything in this commit exists.
155 * We have walked all the objects reachable from the refs
156 * and cache earlier. The commits reachable by this commit
157 * must meet SEEN commits -- and then we should mark them as
160 if (!commit_is_complete(commit
))
166 static int expire_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
167 char *data
, void *cb_data
)
169 struct expire_reflog_cb
*cb
= cb_data
;
170 unsigned long timestamp
;
172 struct commit
*old
, *new;
174 cp
= strchr(data
, '>');
175 if (!cp
|| *++cp
!= ' ')
177 timestamp
= strtoul(cp
, &ep
, 10);
180 if (timestamp
< cb
->cmd
->expire_total
)
183 if (cb
->cmd
->stalefix
&&
184 (!keep_entry(&old
, osha1
) || !keep_entry(&new, nsha1
)))
187 if ((timestamp
< cb
->cmd
->expire_unreachable
) &&
189 (old
&& !in_merge_bases(old
, cb
->ref_commit
)) ||
190 (new && !in_merge_bases(new, cb
->ref_commit
))))
194 fprintf(cb
->newlog
, "%s %s %s",
195 sha1_to_hex(osha1
), sha1_to_hex(nsha1
), data
);
196 if (cb
->cmd
->verbose
)
197 printf("keep %s", data
);
200 if (!cb
->newlog
|| cb
->cmd
->verbose
)
201 printf("%sprune %s", cb
->newlog
? "" : "would ", data
);
205 static int expire_reflog(const char *ref
, const unsigned char *sha1
, int unused
, void *cb_data
)
207 struct cmd_reflog_expire_cb
*cmd
= cb_data
;
208 struct expire_reflog_cb cb
;
209 struct ref_lock
*lock
;
210 char *newlog_path
= NULL
;
213 if (strncmp(ref
, "refs/", 5))
214 return error("not a ref '%s'", ref
);
216 memset(&cb
, 0, sizeof(cb
));
217 /* we take the lock for the ref itself to prevent it from
220 lock
= lock_ref_sha1(ref
+ 5, sha1
);
222 return error("cannot lock ref '%s'", ref
);
223 if (!file_exists(lock
->log_file
))
226 newlog_path
= xstrdup(git_path("logs/%s.lock", ref
));
227 cb
.newlog
= fopen(newlog_path
, "w");
230 cb
.ref_commit
= lookup_commit_reference_gently(sha1
, 1);
233 "warning: ref '%s' does not point at a commit\n", ref
);
236 for_each_reflog_ent(ref
, expire_reflog_ent
, &cb
);
239 if (fclose(cb
.newlog
))
240 status
|= error("%s: %s", strerror(errno
),
242 if (rename(newlog_path
, lock
->log_file
)) {
243 status
|= error("cannot rename %s to %s",
244 newlog_path
, lock
->log_file
);
253 static int reflog_expire_config(const char *var
, const char *value
)
255 if (!strcmp(var
, "gc.reflogexpire"))
256 default_reflog_expire
= approxidate(value
);
257 else if (!strcmp(var
, "gc.reflogexpireunreachable"))
258 default_reflog_expire_unreachable
= approxidate(value
);
260 return git_default_config(var
, value
);
264 static int cmd_reflog_expire(int argc
, const char **argv
, const char *prefix
)
266 struct cmd_reflog_expire_cb cb
;
267 unsigned long now
= time(NULL
);
268 int i
, status
, do_all
;
270 git_config(reflog_expire_config
);
272 save_commit_buffer
= 0;
274 memset(&cb
, 0, sizeof(cb
));
276 if (!default_reflog_expire_unreachable
)
277 default_reflog_expire_unreachable
= now
- 30 * 24 * 3600;
278 if (!default_reflog_expire
)
279 default_reflog_expire
= now
- 90 * 24 * 3600;
280 cb
.expire_total
= default_reflog_expire
;
281 cb
.expire_unreachable
= default_reflog_expire_unreachable
;
284 * We can trust the commits and objects reachable from refs
285 * even in older repository. We cannot trust what's reachable
286 * from reflog if the repository was pruned with older git.
289 for (i
= 1; i
< argc
; i
++) {
290 const char *arg
= argv
[i
];
291 if (!strcmp(arg
, "--dry-run") || !strcmp(arg
, "-n"))
293 else if (!strncmp(arg
, "--expire=", 9))
294 cb
.expire_total
= approxidate(arg
+ 9);
295 else if (!strncmp(arg
, "--expire-unreachable=", 21))
296 cb
.expire_unreachable
= approxidate(arg
+ 21);
297 else if (!strcmp(arg
, "--stale-fix"))
299 else if (!strcmp(arg
, "--all"))
301 else if (!strcmp(arg
, "--verbose"))
303 else if (!strcmp(arg
, "--")) {
307 else if (arg
[0] == '-')
308 usage(reflog_expire_usage
);
313 init_revisions(&cb
.revs
, prefix
);
315 printf("Marking reachable objects...");
316 mark_reachable_objects(&cb
.revs
, 0);
322 status
|= for_each_ref(expire_reflog
, &cb
);
324 const char *ref
= argv
[i
++];
325 unsigned char sha1
[20];
326 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
327 status
|= error("%s points nowhere!", ref
);
330 status
|= expire_reflog(ref
, sha1
, 0, &cb
);
339 static const char reflog_usage
[] =
340 "git-reflog (expire | ...)";
342 int cmd_reflog(int argc
, const char **argv
, const char *prefix
)
346 else if (!strcmp(argv
[1], "expire"))
347 return cmd_reflog_expire(argc
- 1, argv
+ 1, prefix
);