15 static const char reflog_expire_usage
[] =
16 "git-reflog 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
;
30 struct expire_reflog_cb
{
33 struct commit
*ref_commit
;
34 struct cmd_reflog_expire_cb
*cmd
;
37 #define INCOMPLETE (1u<<10)
38 #define STUDYING (1u<<11)
40 static int tree_is_complete(const unsigned char *sha1
)
42 struct tree_desc desc
;
43 struct name_entry entry
;
47 tree
= lookup_tree(sha1
);
50 if (tree
->object
.flags
& SEEN
)
52 if (tree
->object
.flags
& INCOMPLETE
)
55 desc
.buf
= tree
->buffer
;
56 desc
.size
= tree
->size
;
59 void *data
= read_sha1_file(sha1
, type
, &desc
.size
);
61 tree
->object
.flags
|= INCOMPLETE
;
68 while (tree_entry(&desc
, &entry
)) {
69 if (!has_sha1_file(entry
.sha1
) ||
70 (S_ISDIR(entry
.mode
) && !tree_is_complete(entry
.sha1
))) {
71 tree
->object
.flags
|= INCOMPLETE
;
79 tree
->object
.flags
|= SEEN
;
83 static int commit_is_complete(struct commit
*commit
)
85 struct object_array study
;
86 struct object_array found
;
87 int is_incomplete
= 0;
91 if (commit
->object
.flags
& SEEN
)
93 if (commit
->object
.flags
& INCOMPLETE
)
96 * Find all commits that are reachable and are not marked as
97 * SEEN. Then make sure the trees and blobs contained are
98 * complete. After that, mark these commits also as SEEN.
99 * If some of the objects that are needed to complete this
100 * commit are missing, mark this commit as INCOMPLETE.
102 memset(&study
, 0, sizeof(study
));
103 memset(&found
, 0, sizeof(found
));
104 add_object_array(&commit
->object
, NULL
, &study
);
105 add_object_array(&commit
->object
, NULL
, &found
);
106 commit
->object
.flags
|= STUDYING
;
109 struct commit_list
*parent
;
111 c
= (struct commit
*)study
.objects
[--study
.nr
].item
;
112 if (!c
->object
.parsed
&& !parse_object(c
->object
.sha1
))
113 c
->object
.flags
|= INCOMPLETE
;
115 if (c
->object
.flags
& INCOMPLETE
) {
119 else if (c
->object
.flags
& SEEN
)
121 for (parent
= c
->parents
; parent
; parent
= parent
->next
) {
122 struct commit
*p
= parent
->item
;
123 if (p
->object
.flags
& STUDYING
)
125 p
->object
.flags
|= STUDYING
;
126 add_object_array(&p
->object
, NULL
, &study
);
127 add_object_array(&p
->object
, NULL
, &found
);
130 if (!is_incomplete
) {
132 * make sure all commits in "found" array have all the
135 for (i
= 0; i
< found
.nr
; i
++) {
137 (struct commit
*)found
.objects
[i
].item
;
138 if (!tree_is_complete(c
->tree
->object
.sha1
)) {
140 c
->object
.flags
|= INCOMPLETE
;
143 if (!is_incomplete
) {
144 /* mark all found commits as complete, iow SEEN */
145 for (i
= 0; i
< found
.nr
; i
++)
146 found
.objects
[i
].item
->flags
|= SEEN
;
149 /* clear flags from the objects we traversed */
150 for (i
= 0; i
< found
.nr
; i
++)
151 found
.objects
[i
].item
->flags
&= ~STUDYING
;
153 commit
->object
.flags
|= INCOMPLETE
;
156 * If we come here, we have (1) traversed the ancestry chain
157 * from the "commit" until we reach SEEN commits (which are
158 * known to be complete), and (2) made sure that the commits
159 * encountered during the above traversal refer to trees that
160 * are complete. Which means that we know *all* the commits
161 * we have seen during this process are complete.
163 for (i
= 0; i
< found
.nr
; i
++)
164 found
.objects
[i
].item
->flags
|= SEEN
;
166 /* free object arrays */
169 return !is_incomplete
;
172 static int keep_entry(struct commit
**it
, unsigned char *sha1
)
174 struct commit
*commit
;
176 if (is_null_sha1(sha1
))
178 commit
= lookup_commit_reference_gently(sha1
, 1);
183 * Make sure everything in this commit exists.
185 * We have walked all the objects reachable from the refs
186 * and cache earlier. The commits reachable by this commit
187 * must meet SEEN commits -- and then we should mark them as
190 if (!commit_is_complete(commit
))
196 static int expire_reflog_ent(unsigned char *osha1
, unsigned char *nsha1
,
197 const char *email
, unsigned long timestamp
, int tz
,
198 const char *message
, void *cb_data
)
200 struct expire_reflog_cb
*cb
= cb_data
;
201 struct commit
*old
, *new;
203 if (timestamp
< cb
->cmd
->expire_total
)
207 if (cb
->cmd
->stalefix
&&
208 (!keep_entry(&old
, osha1
) || !keep_entry(&new, nsha1
)))
211 if (timestamp
< cb
->cmd
->expire_unreachable
) {
214 if (!old
&& !is_null_sha1(osha1
))
215 old
= lookup_commit_reference_gently(osha1
, 1);
216 if (!new && !is_null_sha1(nsha1
))
217 new = lookup_commit_reference_gently(nsha1
, 1);
218 if ((old
&& !in_merge_bases(old
, cb
->ref_commit
)) ||
219 (new && !in_merge_bases(new, cb
->ref_commit
)))
224 char sign
= (tz
< 0) ? '-' : '+';
225 int zone
= (tz
< 0) ? (-tz
) : tz
;
226 fprintf(cb
->newlog
, "%s %s %s %lu %c%04d\t%s",
227 sha1_to_hex(osha1
), sha1_to_hex(nsha1
),
228 email
, timestamp
, sign
, zone
,
231 if (cb
->cmd
->verbose
)
232 printf("keep %s", message
);
235 if (!cb
->newlog
|| cb
->cmd
->verbose
)
236 printf("%sprune %s", cb
->newlog
? "" : "would ", message
);
240 static int expire_reflog(const char *ref
, const unsigned char *sha1
, int unused
, void *cb_data
)
242 struct cmd_reflog_expire_cb
*cmd
= cb_data
;
243 struct expire_reflog_cb cb
;
244 struct ref_lock
*lock
;
245 char *log_file
, *newlog_path
= NULL
;
248 if (strncmp(ref
, "refs/", 5))
249 return error("not a ref '%s'", ref
);
251 memset(&cb
, 0, sizeof(cb
));
252 /* we take the lock for the ref itself to prevent it from
255 lock
= lock_ref_sha1(ref
+ 5, sha1
);
257 return error("cannot lock ref '%s'", ref
);
258 log_file
= xstrdup(git_path("logs/%s", ref
));
259 if (!file_exists(log_file
))
262 newlog_path
= xstrdup(git_path("logs/%s.lock", ref
));
263 cb
.newlog
= fopen(newlog_path
, "w");
266 cb
.ref_commit
= lookup_commit_reference_gently(sha1
, 1);
269 for_each_reflog_ent(ref
, expire_reflog_ent
, &cb
);
272 if (fclose(cb
.newlog
))
273 status
|= error("%s: %s", strerror(errno
),
275 if (rename(newlog_path
, log_file
)) {
276 status
|= error("cannot rename %s to %s",
277 newlog_path
, log_file
);
287 static int reflog_expire_config(const char *var
, const char *value
)
289 if (!strcmp(var
, "gc.reflogexpire"))
290 default_reflog_expire
= approxidate(value
);
291 else if (!strcmp(var
, "gc.reflogexpireunreachable"))
292 default_reflog_expire_unreachable
= approxidate(value
);
294 return git_default_config(var
, value
);
298 static int cmd_reflog_expire(int argc
, const char **argv
, const char *prefix
)
300 struct cmd_reflog_expire_cb cb
;
301 unsigned long now
= time(NULL
);
302 int i
, status
, do_all
;
304 git_config(reflog_expire_config
);
306 save_commit_buffer
= 0;
308 memset(&cb
, 0, sizeof(cb
));
310 if (!default_reflog_expire_unreachable
)
311 default_reflog_expire_unreachable
= now
- 30 * 24 * 3600;
312 if (!default_reflog_expire
)
313 default_reflog_expire
= now
- 90 * 24 * 3600;
314 cb
.expire_total
= default_reflog_expire
;
315 cb
.expire_unreachable
= default_reflog_expire_unreachable
;
318 * We can trust the commits and objects reachable from refs
319 * even in older repository. We cannot trust what's reachable
320 * from reflog if the repository was pruned with older git.
323 for (i
= 1; i
< argc
; i
++) {
324 const char *arg
= argv
[i
];
325 if (!strcmp(arg
, "--dry-run") || !strcmp(arg
, "-n"))
327 else if (!strncmp(arg
, "--expire=", 9))
328 cb
.expire_total
= approxidate(arg
+ 9);
329 else if (!strncmp(arg
, "--expire-unreachable=", 21))
330 cb
.expire_unreachable
= approxidate(arg
+ 21);
331 else if (!strcmp(arg
, "--stale-fix"))
333 else if (!strcmp(arg
, "--all"))
335 else if (!strcmp(arg
, "--verbose"))
337 else if (!strcmp(arg
, "--")) {
341 else if (arg
[0] == '-')
342 usage(reflog_expire_usage
);
347 init_revisions(&cb
.revs
, prefix
);
349 printf("Marking reachable objects...");
350 mark_reachable_objects(&cb
.revs
, 0);
356 status
|= for_each_ref(expire_reflog
, &cb
);
358 const char *ref
= argv
[i
++];
359 unsigned char sha1
[20];
360 if (!resolve_ref(ref
, sha1
, 1, NULL
)) {
361 status
|= error("%s points nowhere!", ref
);
364 status
|= expire_reflog(ref
, sha1
, 0, &cb
);
373 static const char reflog_usage
[] =
374 "git-reflog (expire | ...)";
376 int cmd_reflog(int argc
, const char **argv
, const char *prefix
)
380 else if (!strcmp(argv
[1], "expire"))
381 return cmd_reflog_expire(argc
- 1, argv
+ 1, prefix
);