Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "builtin.h" |
| 3 | #include "commit.h" |
| 4 | #include "refs.h" |
| 5 | #include "dir.h" |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 6 | #include "tree-walk.h" |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 7 | #include "diff.h" |
| 8 | #include "revision.h" |
| 9 | #include "reachable.h" |
| 10 | |
| 11 | /* |
| 12 | * reflog expire |
| 13 | */ |
| 14 | |
| 15 | static const char reflog_expire_usage[] = |
Linus Torvalds | cf39f54 | 2007-02-08 09:51:56 -0800 | [diff] [blame] | 16 | "git-reflog (show|expire) [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>..."; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 17 | |
Junio C Hamano | 4aec56d | 2006-12-27 01:47:57 -0800 | [diff] [blame] | 18 | static unsigned long default_reflog_expire; |
| 19 | static unsigned long default_reflog_expire_unreachable; |
| 20 | |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 21 | struct cmd_reflog_expire_cb { |
| 22 | struct rev_info revs; |
| 23 | int dry_run; |
| 24 | int stalefix; |
| 25 | int verbose; |
| 26 | unsigned long expire_total; |
| 27 | unsigned long expire_unreachable; |
| 28 | }; |
| 29 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 30 | struct expire_reflog_cb { |
| 31 | FILE *newlog; |
| 32 | const char *ref; |
| 33 | struct commit *ref_commit; |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 34 | struct cmd_reflog_expire_cb *cmd; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 35 | }; |
| 36 | |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 37 | #define INCOMPLETE (1u<<10) |
| 38 | #define STUDYING (1u<<11) |
| 39 | |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 40 | static int tree_is_complete(const unsigned char *sha1) |
| 41 | { |
| 42 | struct tree_desc desc; |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 43 | struct name_entry entry; |
| 44 | int complete; |
| 45 | struct tree *tree; |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 46 | |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 47 | tree = lookup_tree(sha1); |
| 48 | if (!tree) |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 49 | return 0; |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 50 | if (tree->object.flags & SEEN) |
| 51 | return 1; |
| 52 | if (tree->object.flags & INCOMPLETE) |
| 53 | return 0; |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 54 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 55 | if (!tree->buffer) { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 56 | enum object_type type; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 57 | unsigned long size; |
| 58 | void *data = read_sha1_file(sha1, &type, &size); |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 59 | if (!data) { |
| 60 | tree->object.flags |= INCOMPLETE; |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 61 | return 0; |
| 62 | } |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 63 | tree->buffer = data; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 64 | tree->size = size; |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 65 | } |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 66 | init_tree_desc(&desc, tree->buffer, tree->size); |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 67 | complete = 1; |
| 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; |
| 72 | complete = 0; |
| 73 | } |
| 74 | } |
| 75 | free(tree->buffer); |
| 76 | tree->buffer = NULL; |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 77 | |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 78 | if (complete) |
| 79 | tree->object.flags |= SEEN; |
| 80 | return complete; |
| 81 | } |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 82 | |
| 83 | static int commit_is_complete(struct commit *commit) |
| 84 | { |
| 85 | struct object_array study; |
| 86 | struct object_array found; |
| 87 | int is_incomplete = 0; |
| 88 | int i; |
| 89 | |
| 90 | /* early return */ |
| 91 | if (commit->object.flags & SEEN) |
| 92 | return 1; |
| 93 | if (commit->object.flags & INCOMPLETE) |
| 94 | return 0; |
| 95 | /* |
| 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. |
| 101 | */ |
| 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; |
| 107 | while (study.nr) { |
| 108 | struct commit *c; |
| 109 | struct commit_list *parent; |
| 110 | |
| 111 | c = (struct commit *)study.objects[--study.nr].item; |
| 112 | if (!c->object.parsed && !parse_object(c->object.sha1)) |
| 113 | c->object.flags |= INCOMPLETE; |
| 114 | |
| 115 | if (c->object.flags & INCOMPLETE) { |
| 116 | is_incomplete = 1; |
| 117 | break; |
| 118 | } |
| 119 | else if (c->object.flags & SEEN) |
| 120 | continue; |
| 121 | for (parent = c->parents; parent; parent = parent->next) { |
| 122 | struct commit *p = parent->item; |
| 123 | if (p->object.flags & STUDYING) |
| 124 | continue; |
| 125 | p->object.flags |= STUDYING; |
| 126 | add_object_array(&p->object, NULL, &study); |
| 127 | add_object_array(&p->object, NULL, &found); |
| 128 | } |
| 129 | } |
| 130 | if (!is_incomplete) { |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 131 | /* |
| 132 | * make sure all commits in "found" array have all the |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 133 | * necessary objects. |
| 134 | */ |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 135 | for (i = 0; i < found.nr; i++) { |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 136 | struct commit *c = |
| 137 | (struct commit *)found.objects[i].item; |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 138 | if (!tree_is_complete(c->tree->object.sha1)) { |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 139 | is_incomplete = 1; |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 140 | c->object.flags |= INCOMPLETE; |
| 141 | } |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 142 | } |
| 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; |
| 147 | } |
| 148 | } |
| 149 | /* clear flags from the objects we traversed */ |
| 150 | for (i = 0; i < found.nr; i++) |
| 151 | found.objects[i].item->flags &= ~STUDYING; |
| 152 | if (is_incomplete) |
| 153 | commit->object.flags |= INCOMPLETE; |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 154 | else { |
| 155 | /* |
| 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. |
| 162 | */ |
| 163 | for (i = 0; i < found.nr; i++) |
| 164 | found.objects[i].item->flags |= SEEN; |
| 165 | } |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 166 | /* free object arrays */ |
| 167 | free(study.objects); |
| 168 | free(found.objects); |
| 169 | return !is_incomplete; |
| 170 | } |
| 171 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 172 | static int keep_entry(struct commit **it, unsigned char *sha1) |
| 173 | { |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 174 | struct commit *commit; |
| 175 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 176 | if (is_null_sha1(sha1)) |
| 177 | return 1; |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 178 | commit = lookup_commit_reference_gently(sha1, 1); |
| 179 | if (!commit) |
| 180 | return 0; |
| 181 | |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 182 | /* |
| 183 | * Make sure everything in this commit exists. |
| 184 | * |
| 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 |
| 188 | * SEEN as well. |
| 189 | */ |
| 190 | if (!commit_is_complete(commit)) |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 191 | return 0; |
| 192 | *it = commit; |
| 193 | return 1; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1, |
Johannes Schindelin | 883d60f | 2007-01-08 01:59:54 +0100 | [diff] [blame] | 197 | const char *email, unsigned long timestamp, int tz, |
| 198 | const char *message, void *cb_data) |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 199 | { |
| 200 | struct expire_reflog_cb *cb = cb_data; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 201 | struct commit *old, *new; |
| 202 | |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 203 | if (timestamp < cb->cmd->expire_total) |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 204 | goto prune; |
| 205 | |
Junio C Hamano | 9bbaa6c | 2007-01-11 19:56:43 -0800 | [diff] [blame] | 206 | old = new = NULL; |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 207 | if (cb->cmd->stalefix && |
| 208 | (!keep_entry(&old, osha1) || !keep_entry(&new, nsha1))) |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 209 | goto prune; |
| 210 | |
Junio C Hamano | 9bbaa6c | 2007-01-11 19:56:43 -0800 | [diff] [blame] | 211 | if (timestamp < cb->cmd->expire_unreachable) { |
| 212 | if (!cb->ref_commit) |
| 213 | goto prune; |
| 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); |
Junio C Hamano | 4a164d4 | 2007-02-13 16:50:32 -0800 | [diff] [blame] | 218 | if ((old && !in_merge_bases(old, &cb->ref_commit, 1)) || |
| 219 | (new && !in_merge_bases(new, &cb->ref_commit, 1))) |
Junio C Hamano | 9bbaa6c | 2007-01-11 19:56:43 -0800 | [diff] [blame] | 220 | goto prune; |
| 221 | } |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 222 | |
Johannes Schindelin | 883d60f | 2007-01-08 01:59:54 +0100 | [diff] [blame] | 223 | if (cb->newlog) { |
| 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, |
| 229 | message); |
| 230 | } |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 231 | if (cb->cmd->verbose) |
Johannes Schindelin | 883d60f | 2007-01-08 01:59:54 +0100 | [diff] [blame] | 232 | printf("keep %s", message); |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 233 | return 0; |
| 234 | prune: |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 235 | if (!cb->newlog || cb->cmd->verbose) |
Johannes Schindelin | 883d60f | 2007-01-08 01:59:54 +0100 | [diff] [blame] | 236 | printf("%sprune %s", cb->newlog ? "" : "would ", message); |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 240 | static int expire_reflog(const char *ref, const unsigned char *sha1, int unused, void *cb_data) |
| 241 | { |
| 242 | struct cmd_reflog_expire_cb *cmd = cb_data; |
| 243 | struct expire_reflog_cb cb; |
| 244 | struct ref_lock *lock; |
Nicolas Pitre | 9a13f0b | 2007-01-26 17:26:05 -0500 | [diff] [blame] | 245 | char *log_file, *newlog_path = NULL; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 246 | int status = 0; |
| 247 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 248 | memset(&cb, 0, sizeof(cb)); |
| 249 | /* we take the lock for the ref itself to prevent it from |
| 250 | * getting updated. |
| 251 | */ |
Sven Verdoolaege | 68db31c | 2007-05-09 12:33:20 +0200 | [diff] [blame] | 252 | lock = lock_any_ref_for_update(ref, sha1, 0); |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 253 | if (!lock) |
| 254 | return error("cannot lock ref '%s'", ref); |
Nicolas Pitre | 9a13f0b | 2007-01-26 17:26:05 -0500 | [diff] [blame] | 255 | log_file = xstrdup(git_path("logs/%s", ref)); |
| 256 | if (!file_exists(log_file)) |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 257 | goto finish; |
| 258 | if (!cmd->dry_run) { |
| 259 | newlog_path = xstrdup(git_path("logs/%s.lock", ref)); |
| 260 | cb.newlog = fopen(newlog_path, "w"); |
| 261 | } |
| 262 | |
| 263 | cb.ref_commit = lookup_commit_reference_gently(sha1, 1); |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 264 | cb.ref = ref; |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 265 | cb.cmd = cmd; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 266 | for_each_reflog_ent(ref, expire_reflog_ent, &cb); |
| 267 | finish: |
| 268 | if (cb.newlog) { |
| 269 | if (fclose(cb.newlog)) |
| 270 | status |= error("%s: %s", strerror(errno), |
| 271 | newlog_path); |
Nicolas Pitre | 9a13f0b | 2007-01-26 17:26:05 -0500 | [diff] [blame] | 272 | if (rename(newlog_path, log_file)) { |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 273 | status |= error("cannot rename %s to %s", |
Nicolas Pitre | 9a13f0b | 2007-01-26 17:26:05 -0500 | [diff] [blame] | 274 | newlog_path, log_file); |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 275 | unlink(newlog_path); |
| 276 | } |
| 277 | } |
| 278 | free(newlog_path); |
Nicolas Pitre | 9a13f0b | 2007-01-26 17:26:05 -0500 | [diff] [blame] | 279 | free(log_file); |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 280 | unlock_ref(lock); |
| 281 | return status; |
| 282 | } |
| 283 | |
Junio C Hamano | 4aec56d | 2006-12-27 01:47:57 -0800 | [diff] [blame] | 284 | static int reflog_expire_config(const char *var, const char *value) |
| 285 | { |
| 286 | if (!strcmp(var, "gc.reflogexpire")) |
| 287 | default_reflog_expire = approxidate(value); |
| 288 | else if (!strcmp(var, "gc.reflogexpireunreachable")) |
| 289 | default_reflog_expire_unreachable = approxidate(value); |
| 290 | else |
| 291 | return git_default_config(var, value); |
| 292 | return 0; |
| 293 | } |
| 294 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 295 | static int cmd_reflog_expire(int argc, const char **argv, const char *prefix) |
| 296 | { |
| 297 | struct cmd_reflog_expire_cb cb; |
| 298 | unsigned long now = time(NULL); |
| 299 | int i, status, do_all; |
| 300 | |
Junio C Hamano | 4aec56d | 2006-12-27 01:47:57 -0800 | [diff] [blame] | 301 | git_config(reflog_expire_config); |
| 302 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 303 | save_commit_buffer = 0; |
| 304 | do_all = status = 0; |
| 305 | memset(&cb, 0, sizeof(cb)); |
Junio C Hamano | 4aec56d | 2006-12-27 01:47:57 -0800 | [diff] [blame] | 306 | |
| 307 | if (!default_reflog_expire_unreachable) |
| 308 | default_reflog_expire_unreachable = now - 30 * 24 * 3600; |
| 309 | if (!default_reflog_expire) |
| 310 | default_reflog_expire = now - 90 * 24 * 3600; |
| 311 | cb.expire_total = default_reflog_expire; |
| 312 | cb.expire_unreachable = default_reflog_expire_unreachable; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 313 | |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 314 | /* |
| 315 | * We can trust the commits and objects reachable from refs |
| 316 | * even in older repository. We cannot trust what's reachable |
| 317 | * from reflog if the repository was pruned with older git. |
| 318 | */ |
| 319 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 320 | for (i = 1; i < argc; i++) { |
| 321 | const char *arg = argv[i]; |
| 322 | if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n")) |
| 323 | cb.dry_run = 1; |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 324 | else if (!prefixcmp(arg, "--expire=")) |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 325 | cb.expire_total = approxidate(arg + 9); |
Junio C Hamano | cc44c76 | 2007-02-20 01:53:29 -0800 | [diff] [blame] | 326 | else if (!prefixcmp(arg, "--expire-unreachable=")) |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 327 | cb.expire_unreachable = approxidate(arg + 21); |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 328 | else if (!strcmp(arg, "--stale-fix")) |
| 329 | cb.stalefix = 1; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 330 | else if (!strcmp(arg, "--all")) |
| 331 | do_all = 1; |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 332 | else if (!strcmp(arg, "--verbose")) |
| 333 | cb.verbose = 1; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 334 | else if (!strcmp(arg, "--")) { |
| 335 | i++; |
| 336 | break; |
| 337 | } |
| 338 | else if (arg[0] == '-') |
| 339 | usage(reflog_expire_usage); |
| 340 | else |
| 341 | break; |
| 342 | } |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 343 | if (cb.stalefix) { |
| 344 | init_revisions(&cb.revs, prefix); |
| 345 | if (cb.verbose) |
| 346 | printf("Marking reachable objects..."); |
| 347 | mark_reachable_objects(&cb.revs, 0); |
| 348 | if (cb.verbose) |
| 349 | putchar('\n'); |
| 350 | } |
| 351 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 352 | if (do_all) |
Nicolas Pitre | eb8381c | 2007-02-03 13:25:43 -0500 | [diff] [blame] | 353 | status |= for_each_reflog(expire_reflog, &cb); |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 354 | while (i < argc) { |
| 355 | const char *ref = argv[i++]; |
| 356 | unsigned char sha1[20]; |
| 357 | if (!resolve_ref(ref, sha1, 1, NULL)) { |
| 358 | status |= error("%s points nowhere!", ref); |
| 359 | continue; |
| 360 | } |
| 361 | status |= expire_reflog(ref, sha1, 0, &cb); |
| 362 | } |
| 363 | return status; |
| 364 | } |
| 365 | |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 366 | /* |
| 367 | * main "reflog" |
| 368 | */ |
| 369 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 370 | static const char reflog_usage[] = |
| 371 | "git-reflog (expire | ...)"; |
| 372 | |
| 373 | int cmd_reflog(int argc, const char **argv, const char *prefix) |
| 374 | { |
Linus Torvalds | cf39f54 | 2007-02-08 09:51:56 -0800 | [diff] [blame] | 375 | /* With no command, we default to showing it. */ |
| 376 | if (argc < 2 || *argv[1] == '-') |
| 377 | return cmd_log_reflog(argc, argv, prefix); |
| 378 | |
| 379 | if (!strcmp(argv[1], "show")) |
| 380 | return cmd_log_reflog(argc - 1, argv + 1, prefix); |
| 381 | |
| 382 | if (!strcmp(argv[1], "expire")) |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 383 | return cmd_reflog_expire(argc - 1, argv + 1, prefix); |
Linus Torvalds | cf39f54 | 2007-02-08 09:51:56 -0800 | [diff] [blame] | 384 | |
| 385 | /* Not a recognized reflog command..*/ |
| 386 | usage(reflog_usage); |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 387 | } |