Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 1 | #include "builtin.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 3 | #include "lockfile.h" |
Stefan Beller | cbd53a2 | 2018-05-15 16:42:15 -0700 | [diff] [blame] | 4 | #include "object-store.h" |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 5 | #include "repository.h" |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 6 | #include "commit.h" |
| 7 | #include "refs.h" |
| 8 | #include "dir.h" |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 9 | #include "tree-walk.h" |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 10 | #include "diff.h" |
| 11 | #include "revision.h" |
| 12 | #include "reachable.h" |
Nguyễn Thái Ngọc Duy | c9ef0d9 | 2018-10-21 10:08:59 +0200 | [diff] [blame] | 13 | #include "worktree.h" |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 14 | |
Michael Haggerty | fe2a181 | 2015-03-03 12:43:15 +0100 | [diff] [blame] | 15 | /* NEEDSWORK: switch to using parse_options */ |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 16 | static const char reflog_expire_usage[] = |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 17 | N_("git reflog expire [--expire=<time>] " |
| 18 | "[--expire-unreachable=<time>] " |
| 19 | "[--rewrite] [--updateref] [--stale-fix] [--dry-run | -n] " |
| 20 | "[--verbose] [--all] <refs>..."); |
Brandon Casey | 3c386aa | 2008-02-22 15:08:59 -0600 | [diff] [blame] | 21 | static const char reflog_delete_usage[] = |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 22 | N_("git reflog delete [--rewrite] [--updateref] " |
| 23 | "[--dry-run | -n] [--verbose] <refs>..."); |
David Turner | afcb2e7 | 2015-07-21 17:04:53 -0400 | [diff] [blame] | 24 | static const char reflog_exists_usage[] = |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 25 | N_("git reflog exists <ref>"); |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 26 | |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 27 | static timestamp_t default_reflog_expire; |
| 28 | static timestamp_t default_reflog_expire_unreachable; |
Junio C Hamano | 4aec56d | 2006-12-27 01:47:57 -0800 | [diff] [blame] | 29 | |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 30 | struct cmd_reflog_expire_cb { |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 31 | int stalefix; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 32 | timestamp_t expire_total; |
| 33 | timestamp_t expire_unreachable; |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 34 | int recno; |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 35 | }; |
| 36 | |
Michael Haggerty | ea7b4f6 | 2014-12-12 09:56:52 +0100 | [diff] [blame] | 37 | struct expire_reflog_policy_cb { |
Junio C Hamano | 03cb91b | 2010-04-09 13:20:02 -0700 | [diff] [blame] | 38 | enum { |
| 39 | UE_NORMAL, |
| 40 | UE_ALWAYS, |
| 41 | UE_HEAD |
| 42 | } unreachable_expire_kind; |
Junio C Hamano | b4ca1db | 2010-04-07 11:09:12 -0700 | [diff] [blame] | 43 | struct commit_list *mark_list; |
| 44 | unsigned long mark_limit; |
Michael Haggerty | b729eff | 2014-12-12 09:56:58 +0100 | [diff] [blame] | 45 | struct cmd_reflog_expire_cb cmd; |
Michael Haggerty | c48a163 | 2014-12-12 09:56:48 +0100 | [diff] [blame] | 46 | struct commit *tip_commit; |
| 47 | struct commit_list *tips; |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 48 | unsigned int dry_run:1; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 49 | }; |
| 50 | |
Ævar Arnfjörð Bjarmason | f2919ba | 2021-12-22 05:06:42 +0100 | [diff] [blame] | 51 | struct worktree_reflogs { |
| 52 | struct worktree *worktree; |
| 53 | struct string_list reflogs; |
Junio C Hamano | bda3a31 | 2008-01-25 23:53:05 -0800 | [diff] [blame] | 54 | }; |
| 55 | |
Nguyễn Thái Ngọc Duy | 95308d6 | 2018-03-06 17:16:14 +0700 | [diff] [blame] | 56 | /* Remember to update object flag allocation in object.h */ |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 57 | #define INCOMPLETE (1u<<10) |
| 58 | #define STUDYING (1u<<11) |
Junio Hamano | 494fbfe | 2009-03-30 21:34:14 -0700 | [diff] [blame] | 59 | #define REACHABLE (1u<<12) |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 60 | |
brian m. carlson | 49a09e7 | 2017-05-06 22:10:16 +0000 | [diff] [blame] | 61 | static int tree_is_complete(const struct object_id *oid) |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 62 | { |
| 63 | struct tree_desc desc; |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 64 | struct name_entry entry; |
| 65 | int complete; |
| 66 | struct tree *tree; |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 67 | |
Stefan Beller | f86bcc7 | 2018-06-28 18:21:56 -0700 | [diff] [blame] | 68 | tree = lookup_tree(the_repository, oid); |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 69 | if (!tree) |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 70 | return 0; |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 71 | if (tree->object.flags & SEEN) |
| 72 | return 1; |
| 73 | if (tree->object.flags & INCOMPLETE) |
| 74 | return 0; |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 75 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 76 | if (!tree->buffer) { |
Nicolas Pitre | 21666f1 | 2007-02-26 14:55:59 -0500 | [diff] [blame] | 77 | enum object_type type; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 78 | unsigned long size; |
brian m. carlson | b4f5aca | 2018-03-12 02:27:53 +0000 | [diff] [blame] | 79 | void *data = read_object_file(oid, &type, &size); |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 80 | if (!data) { |
| 81 | tree->object.flags |= INCOMPLETE; |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 82 | return 0; |
| 83 | } |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 84 | tree->buffer = data; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 85 | tree->size = size; |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 86 | } |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 87 | init_tree_desc(&desc, tree->buffer, tree->size); |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 88 | complete = 1; |
| 89 | while (tree_entry(&desc, &entry)) { |
Junio C Hamano | cba595a | 2019-02-06 22:05:27 -0800 | [diff] [blame] | 90 | if (!has_object_file(&entry.oid) || |
brian m. carlson | ea82b2a | 2019-01-15 00:39:44 +0000 | [diff] [blame] | 91 | (S_ISDIR(entry.mode) && !tree_is_complete(&entry.oid))) { |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 92 | tree->object.flags |= INCOMPLETE; |
| 93 | complete = 0; |
| 94 | } |
| 95 | } |
Jeff King | 6e454b9 | 2013-06-05 18:37:39 -0400 | [diff] [blame] | 96 | free_tree_buffer(tree); |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 97 | |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 98 | if (complete) |
| 99 | tree->object.flags |= SEEN; |
| 100 | return complete; |
| 101 | } |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 102 | |
| 103 | static int commit_is_complete(struct commit *commit) |
| 104 | { |
| 105 | struct object_array study; |
| 106 | struct object_array found; |
| 107 | int is_incomplete = 0; |
| 108 | int i; |
| 109 | |
| 110 | /* early return */ |
| 111 | if (commit->object.flags & SEEN) |
| 112 | return 1; |
| 113 | if (commit->object.flags & INCOMPLETE) |
| 114 | return 0; |
| 115 | /* |
| 116 | * Find all commits that are reachable and are not marked as |
| 117 | * SEEN. Then make sure the trees and blobs contained are |
| 118 | * complete. After that, mark these commits also as SEEN. |
| 119 | * If some of the objects that are needed to complete this |
| 120 | * commit are missing, mark this commit as INCOMPLETE. |
| 121 | */ |
| 122 | memset(&study, 0, sizeof(study)); |
| 123 | memset(&found, 0, sizeof(found)); |
| 124 | add_object_array(&commit->object, NULL, &study); |
| 125 | add_object_array(&commit->object, NULL, &found); |
| 126 | commit->object.flags |= STUDYING; |
| 127 | while (study.nr) { |
| 128 | struct commit *c; |
| 129 | struct commit_list *parent; |
| 130 | |
Martin Ågren | 7199203 | 2017-09-23 01:34:53 +0200 | [diff] [blame] | 131 | c = (struct commit *)object_array_pop(&study); |
Stefan Beller | 109cd76 | 2018-06-28 18:21:51 -0700 | [diff] [blame] | 132 | if (!c->object.parsed && !parse_object(the_repository, &c->object.oid)) |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 133 | c->object.flags |= INCOMPLETE; |
| 134 | |
| 135 | if (c->object.flags & INCOMPLETE) { |
| 136 | is_incomplete = 1; |
| 137 | break; |
| 138 | } |
| 139 | else if (c->object.flags & SEEN) |
| 140 | continue; |
| 141 | for (parent = c->parents; parent; parent = parent->next) { |
| 142 | struct commit *p = parent->item; |
| 143 | if (p->object.flags & STUDYING) |
| 144 | continue; |
| 145 | p->object.flags |= STUDYING; |
| 146 | add_object_array(&p->object, NULL, &study); |
| 147 | add_object_array(&p->object, NULL, &found); |
| 148 | } |
| 149 | } |
| 150 | if (!is_incomplete) { |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 151 | /* |
| 152 | * make sure all commits in "found" array have all the |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 153 | * necessary objects. |
| 154 | */ |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 155 | for (i = 0; i < found.nr; i++) { |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 156 | struct commit *c = |
| 157 | (struct commit *)found.objects[i].item; |
Derrick Stolee | 2e27bd7 | 2018-04-06 19:09:38 +0000 | [diff] [blame] | 158 | if (!tree_is_complete(get_commit_tree_oid(c))) { |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 159 | is_incomplete = 1; |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 160 | c->object.flags |= INCOMPLETE; |
| 161 | } |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 162 | } |
| 163 | if (!is_incomplete) { |
| 164 | /* mark all found commits as complete, iow SEEN */ |
| 165 | for (i = 0; i < found.nr; i++) |
| 166 | found.objects[i].item->flags |= SEEN; |
| 167 | } |
| 168 | } |
| 169 | /* clear flags from the objects we traversed */ |
| 170 | for (i = 0; i < found.nr; i++) |
| 171 | found.objects[i].item->flags &= ~STUDYING; |
| 172 | if (is_incomplete) |
| 173 | commit->object.flags |= INCOMPLETE; |
Junio C Hamano | cd1f9c3 | 2007-01-06 22:32:41 -0800 | [diff] [blame] | 174 | else { |
| 175 | /* |
| 176 | * If we come here, we have (1) traversed the ancestry chain |
| 177 | * from the "commit" until we reach SEEN commits (which are |
| 178 | * known to be complete), and (2) made sure that the commits |
| 179 | * encountered during the above traversal refer to trees that |
| 180 | * are complete. Which means that we know *all* the commits |
| 181 | * we have seen during this process are complete. |
| 182 | */ |
| 183 | for (i = 0; i < found.nr; i++) |
| 184 | found.objects[i].item->flags |= SEEN; |
| 185 | } |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 186 | /* free object arrays */ |
Martin Ågren | dcb572a | 2017-09-23 01:34:52 +0200 | [diff] [blame] | 187 | object_array_clear(&study); |
| 188 | object_array_clear(&found); |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 189 | return !is_incomplete; |
| 190 | } |
| 191 | |
brian m. carlson | 4322478 | 2017-05-06 22:10:00 +0000 | [diff] [blame] | 192 | static int keep_entry(struct commit **it, struct object_id *oid) |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 193 | { |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 194 | struct commit *commit; |
| 195 | |
brian m. carlson | 4322478 | 2017-05-06 22:10:00 +0000 | [diff] [blame] | 196 | if (is_null_oid(oid)) |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 197 | return 1; |
Stefan Beller | 21e1ee8 | 2018-06-28 18:21:57 -0700 | [diff] [blame] | 198 | commit = lookup_commit_reference_gently(the_repository, oid, 1); |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 199 | if (!commit) |
| 200 | return 0; |
| 201 | |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 202 | /* |
| 203 | * Make sure everything in this commit exists. |
| 204 | * |
| 205 | * We have walked all the objects reachable from the refs |
| 206 | * and cache earlier. The commits reachable by this commit |
| 207 | * must meet SEEN commits -- and then we should mark them as |
| 208 | * SEEN as well. |
| 209 | */ |
| 210 | if (!commit_is_complete(commit)) |
Junio C Hamano | 8d8b9f6 | 2006-12-22 00:46:33 -0800 | [diff] [blame] | 211 | return 0; |
| 212 | *it = commit; |
| 213 | return 1; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 214 | } |
| 215 | |
Junio C Hamano | b4ca1db | 2010-04-07 11:09:12 -0700 | [diff] [blame] | 216 | /* |
| 217 | * Starting from commits in the cb->mark_list, mark commits that are |
| 218 | * reachable from them. Stop the traversal at commits older than |
| 219 | * the expire_limit and queue them back, so that the caller can call |
| 220 | * us again to restart the traversal with longer expire_limit. |
| 221 | */ |
Michael Haggerty | ea7b4f6 | 2014-12-12 09:56:52 +0100 | [diff] [blame] | 222 | static void mark_reachable(struct expire_reflog_policy_cb *cb) |
Junio C Hamano | b4ca1db | 2010-04-07 11:09:12 -0700 | [diff] [blame] | 223 | { |
Junio C Hamano | b4ca1db | 2010-04-07 11:09:12 -0700 | [diff] [blame] | 224 | struct commit_list *pending; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 225 | timestamp_t expire_limit = cb->mark_limit; |
Junio C Hamano | b4ca1db | 2010-04-07 11:09:12 -0700 | [diff] [blame] | 226 | struct commit_list *leftover = NULL; |
| 227 | |
| 228 | for (pending = cb->mark_list; pending; pending = pending->next) |
| 229 | pending->item->object.flags &= ~REACHABLE; |
| 230 | |
| 231 | pending = cb->mark_list; |
| 232 | while (pending) { |
Junio C Hamano | b4ca1db | 2010-04-07 11:09:12 -0700 | [diff] [blame] | 233 | struct commit_list *parent; |
René Scharfe | e510ab8 | 2015-10-24 18:21:31 +0200 | [diff] [blame] | 234 | struct commit *commit = pop_commit(&pending); |
Junio C Hamano | b4ca1db | 2010-04-07 11:09:12 -0700 | [diff] [blame] | 235 | if (commit->object.flags & REACHABLE) |
| 236 | continue; |
| 237 | if (parse_commit(commit)) |
| 238 | continue; |
| 239 | commit->object.flags |= REACHABLE; |
| 240 | if (commit->date < expire_limit) { |
| 241 | commit_list_insert(commit, &leftover); |
| 242 | continue; |
| 243 | } |
| 244 | commit->object.flags |= REACHABLE; |
| 245 | parent = commit->parents; |
| 246 | while (parent) { |
| 247 | commit = parent->item; |
| 248 | parent = parent->next; |
| 249 | if (commit->object.flags & REACHABLE) |
| 250 | continue; |
| 251 | commit_list_insert(commit, &pending); |
| 252 | } |
| 253 | } |
| 254 | cb->mark_list = leftover; |
| 255 | } |
| 256 | |
brian m. carlson | 4322478 | 2017-05-06 22:10:00 +0000 | [diff] [blame] | 257 | static int unreachable(struct expire_reflog_policy_cb *cb, struct commit *commit, struct object_id *oid) |
Linus Torvalds | 666e07e | 2009-03-31 09:45:22 -0700 | [diff] [blame] | 258 | { |
| 259 | /* |
| 260 | * We may or may not have the commit yet - if not, look it |
| 261 | * up using the supplied sha1. |
| 262 | */ |
| 263 | if (!commit) { |
brian m. carlson | 4322478 | 2017-05-06 22:10:00 +0000 | [diff] [blame] | 264 | if (is_null_oid(oid)) |
Linus Torvalds | 666e07e | 2009-03-31 09:45:22 -0700 | [diff] [blame] | 265 | return 0; |
| 266 | |
Stefan Beller | 21e1ee8 | 2018-06-28 18:21:57 -0700 | [diff] [blame] | 267 | commit = lookup_commit_reference_gently(the_repository, oid, |
| 268 | 1); |
Linus Torvalds | 666e07e | 2009-03-31 09:45:22 -0700 | [diff] [blame] | 269 | |
| 270 | /* Not a commit -- keep it */ |
| 271 | if (!commit) |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | /* Reachable from the current ref? Don't prune. */ |
Junio Hamano | 494fbfe | 2009-03-30 21:34:14 -0700 | [diff] [blame] | 276 | if (commit->object.flags & REACHABLE) |
| 277 | return 0; |
Linus Torvalds | 666e07e | 2009-03-31 09:45:22 -0700 | [diff] [blame] | 278 | |
Junio C Hamano | b4ca1db | 2010-04-07 11:09:12 -0700 | [diff] [blame] | 279 | if (cb->mark_list && cb->mark_limit) { |
| 280 | cb->mark_limit = 0; /* dig down to the root */ |
| 281 | mark_reachable(cb); |
Junio Hamano | 494fbfe | 2009-03-30 21:34:14 -0700 | [diff] [blame] | 282 | } |
Junio C Hamano | b4ca1db | 2010-04-07 11:09:12 -0700 | [diff] [blame] | 283 | |
| 284 | return !(commit->object.flags & REACHABLE); |
Junio Hamano | 494fbfe | 2009-03-30 21:34:14 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Michael Haggerty | 60cc3c4 | 2014-12-12 09:56:47 +0100 | [diff] [blame] | 287 | /* |
| 288 | * Return true iff the specified reflog entry should be expired. |
| 289 | */ |
brian m. carlson | 4322478 | 2017-05-06 22:10:00 +0000 | [diff] [blame] | 290 | static int should_expire_reflog_ent(struct object_id *ooid, struct object_id *noid, |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 291 | const char *email, timestamp_t timestamp, int tz, |
Michael Haggerty | 60cc3c4 | 2014-12-12 09:56:47 +0100 | [diff] [blame] | 292 | const char *message, void *cb_data) |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 293 | { |
Michael Haggerty | ea7b4f6 | 2014-12-12 09:56:52 +0100 | [diff] [blame] | 294 | struct expire_reflog_policy_cb *cb = cb_data; |
Brandon Williams | dfa5990 | 2018-02-14 10:59:34 -0800 | [diff] [blame] | 295 | struct commit *old_commit, *new_commit; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 296 | |
Michael Haggerty | b729eff | 2014-12-12 09:56:58 +0100 | [diff] [blame] | 297 | if (timestamp < cb->cmd.expire_total) |
Michael Haggerty | 60cc3c4 | 2014-12-12 09:56:47 +0100 | [diff] [blame] | 298 | return 1; |
Brandon Casey | 2b81fab | 2008-02-22 12:56:50 -0600 | [diff] [blame] | 299 | |
Brandon Williams | dfa5990 | 2018-02-14 10:59:34 -0800 | [diff] [blame] | 300 | old_commit = new_commit = NULL; |
Michael Haggerty | b729eff | 2014-12-12 09:56:58 +0100 | [diff] [blame] | 301 | if (cb->cmd.stalefix && |
Brandon Williams | dfa5990 | 2018-02-14 10:59:34 -0800 | [diff] [blame] | 302 | (!keep_entry(&old_commit, ooid) || !keep_entry(&new_commit, noid))) |
Michael Haggerty | 60cc3c4 | 2014-12-12 09:56:47 +0100 | [diff] [blame] | 303 | return 1; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 304 | |
Michael Haggerty | b729eff | 2014-12-12 09:56:58 +0100 | [diff] [blame] | 305 | if (timestamp < cb->cmd.expire_unreachable) { |
Ævar Arnfjörð Bjarmason | 20d6b68 | 2021-12-22 05:06:43 +0100 | [diff] [blame] | 306 | switch (cb->unreachable_expire_kind) { |
| 307 | case UE_ALWAYS: |
Michael Haggerty | 60cc3c4 | 2014-12-12 09:56:47 +0100 | [diff] [blame] | 308 | return 1; |
Ævar Arnfjörð Bjarmason | 20d6b68 | 2021-12-22 05:06:43 +0100 | [diff] [blame] | 309 | case UE_NORMAL: |
| 310 | case UE_HEAD: |
| 311 | if (unreachable(cb, old_commit, ooid) || unreachable(cb, new_commit, noid)) |
| 312 | return 1; |
| 313 | break; |
| 314 | } |
Junio C Hamano | 9bbaa6c | 2007-01-11 19:56:43 -0800 | [diff] [blame] | 315 | } |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 316 | |
Michael Haggerty | b729eff | 2014-12-12 09:56:58 +0100 | [diff] [blame] | 317 | if (cb->cmd.recno && --(cb->cmd.recno) == 0) |
Michael Haggerty | 60cc3c4 | 2014-12-12 09:56:47 +0100 | [diff] [blame] | 318 | return 1; |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 319 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 320 | return 0; |
Michael Haggerty | 60cc3c4 | 2014-12-12 09:56:47 +0100 | [diff] [blame] | 321 | } |
| 322 | |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 323 | static int should_expire_reflog_ent_verbose(struct object_id *ooid, |
| 324 | struct object_id *noid, |
| 325 | const char *email, |
| 326 | timestamp_t timestamp, int tz, |
| 327 | const char *message, void *cb_data) |
| 328 | { |
| 329 | struct expire_reflog_policy_cb *cb = cb_data; |
| 330 | int expire; |
| 331 | |
| 332 | expire = should_expire_reflog_ent(ooid, noid, email, timestamp, tz, |
| 333 | message, cb); |
| 334 | |
| 335 | if (!expire) |
| 336 | printf("keep %s", message); |
| 337 | else if (cb->dry_run) |
| 338 | printf("would prune %s", message); |
| 339 | else |
| 340 | printf("prune %s", message); |
| 341 | |
| 342 | return expire; |
| 343 | } |
| 344 | |
Michael Haggerty | 5bcad1b | 2015-05-25 18:38:40 +0000 | [diff] [blame] | 345 | static int push_tip_to_list(const char *refname, const struct object_id *oid, |
Michael Haggerty | ea7b4f6 | 2014-12-12 09:56:52 +0100 | [diff] [blame] | 346 | int flags, void *cb_data) |
Junio C Hamano | 03cb91b | 2010-04-09 13:20:02 -0700 | [diff] [blame] | 347 | { |
| 348 | struct commit_list **list = cb_data; |
| 349 | struct commit *tip_commit; |
| 350 | if (flags & REF_ISSYMREF) |
| 351 | return 0; |
Stefan Beller | 21e1ee8 | 2018-06-28 18:21:57 -0700 | [diff] [blame] | 352 | tip_commit = lookup_commit_reference_gently(the_repository, oid, 1); |
Junio C Hamano | 03cb91b | 2010-04-09 13:20:02 -0700 | [diff] [blame] | 353 | if (!tip_commit) |
| 354 | return 0; |
| 355 | commit_list_insert(tip_commit, list); |
| 356 | return 0; |
| 357 | } |
| 358 | |
Nguyễn Thái Ngọc Duy | c9ef0d9 | 2018-10-21 10:08:59 +0200 | [diff] [blame] | 359 | static int is_head(const char *refname) |
| 360 | { |
| 361 | switch (ref_type(refname)) { |
| 362 | case REF_TYPE_OTHER_PSEUDOREF: |
| 363 | case REF_TYPE_MAIN_PSEUDOREF: |
| 364 | if (parse_worktree_ref(refname, NULL, NULL, &refname)) |
| 365 | BUG("not a worktree ref: %s", refname); |
| 366 | break; |
| 367 | default: |
| 368 | break; |
| 369 | } |
| 370 | return !strcmp(refname, "HEAD"); |
| 371 | } |
| 372 | |
Michael Haggerty | c48a163 | 2014-12-12 09:56:48 +0100 | [diff] [blame] | 373 | static void reflog_expiry_prepare(const char *refname, |
brian m. carlson | 4322478 | 2017-05-06 22:10:00 +0000 | [diff] [blame] | 374 | const struct object_id *oid, |
Michael Haggerty | b729eff | 2014-12-12 09:56:58 +0100 | [diff] [blame] | 375 | void *cb_data) |
Michael Haggerty | c48a163 | 2014-12-12 09:56:48 +0100 | [diff] [blame] | 376 | { |
Michael Haggerty | b729eff | 2014-12-12 09:56:58 +0100 | [diff] [blame] | 377 | struct expire_reflog_policy_cb *cb = cb_data; |
Ævar Arnfjörð Bjarmason | 20d6b68 | 2021-12-22 05:06:43 +0100 | [diff] [blame] | 378 | struct commit_list *elem; |
Ævar Arnfjörð Bjarmason | 07815e2 | 2021-12-22 05:06:44 +0100 | [diff] [blame] | 379 | struct commit *commit = NULL; |
Michael Haggerty | b729eff | 2014-12-12 09:56:58 +0100 | [diff] [blame] | 380 | |
Nguyễn Thái Ngọc Duy | c9ef0d9 | 2018-10-21 10:08:59 +0200 | [diff] [blame] | 381 | if (!cb->cmd.expire_unreachable || is_head(refname)) { |
Michael Haggerty | c48a163 | 2014-12-12 09:56:48 +0100 | [diff] [blame] | 382 | cb->unreachable_expire_kind = UE_HEAD; |
| 383 | } else { |
Ævar Arnfjörð Bjarmason | daf1d82 | 2021-12-22 05:06:45 +0100 | [diff] [blame] | 384 | commit = lookup_commit(the_repository, oid); |
Ævar Arnfjörð Bjarmason | 07815e2 | 2021-12-22 05:06:44 +0100 | [diff] [blame] | 385 | cb->unreachable_expire_kind = commit ? UE_NORMAL : UE_ALWAYS; |
Michael Haggerty | c48a163 | 2014-12-12 09:56:48 +0100 | [diff] [blame] | 386 | } |
| 387 | |
Michael Haggerty | b729eff | 2014-12-12 09:56:58 +0100 | [diff] [blame] | 388 | if (cb->cmd.expire_unreachable <= cb->cmd.expire_total) |
Michael Haggerty | c48a163 | 2014-12-12 09:56:48 +0100 | [diff] [blame] | 389 | cb->unreachable_expire_kind = UE_ALWAYS; |
| 390 | |
Ævar Arnfjörð Bjarmason | 20d6b68 | 2021-12-22 05:06:43 +0100 | [diff] [blame] | 391 | switch (cb->unreachable_expire_kind) { |
| 392 | case UE_ALWAYS: |
| 393 | return; |
| 394 | case UE_HEAD: |
| 395 | for_each_ref(push_tip_to_list, &cb->tips); |
| 396 | for (elem = cb->tips; elem; elem = elem->next) |
| 397 | commit_list_insert(elem->item, &cb->mark_list); |
| 398 | break; |
| 399 | case UE_NORMAL: |
Ævar Arnfjörð Bjarmason | 07815e2 | 2021-12-22 05:06:44 +0100 | [diff] [blame] | 400 | commit_list_insert(commit, &cb->mark_list); |
| 401 | /* For reflog_expiry_cleanup() below */ |
| 402 | cb->tip_commit = commit; |
Michael Haggerty | c48a163 | 2014-12-12 09:56:48 +0100 | [diff] [blame] | 403 | } |
Ævar Arnfjörð Bjarmason | 20d6b68 | 2021-12-22 05:06:43 +0100 | [diff] [blame] | 404 | cb->mark_limit = cb->cmd.expire_total; |
| 405 | mark_reachable(cb); |
Michael Haggerty | c48a163 | 2014-12-12 09:56:48 +0100 | [diff] [blame] | 406 | } |
| 407 | |
Michael Haggerty | b729eff | 2014-12-12 09:56:58 +0100 | [diff] [blame] | 408 | static void reflog_expiry_cleanup(void *cb_data) |
Michael Haggerty | c48a163 | 2014-12-12 09:56:48 +0100 | [diff] [blame] | 409 | { |
Michael Haggerty | b729eff | 2014-12-12 09:56:58 +0100 | [diff] [blame] | 410 | struct expire_reflog_policy_cb *cb = cb_data; |
Ævar Arnfjörð Bjarmason | 20d6b68 | 2021-12-22 05:06:43 +0100 | [diff] [blame] | 411 | struct commit_list *elem; |
Michael Haggerty | b729eff | 2014-12-12 09:56:58 +0100 | [diff] [blame] | 412 | |
Ævar Arnfjörð Bjarmason | 20d6b68 | 2021-12-22 05:06:43 +0100 | [diff] [blame] | 413 | switch (cb->unreachable_expire_kind) { |
| 414 | case UE_ALWAYS: |
| 415 | return; |
| 416 | case UE_HEAD: |
| 417 | for (elem = cb->tips; elem; elem = elem->next) |
| 418 | clear_commit_marks(elem->item, REACHABLE); |
| 419 | free_commit_list(cb->tips); |
| 420 | break; |
| 421 | case UE_NORMAL: |
| 422 | clear_commit_marks(cb->tip_commit, REACHABLE); |
| 423 | break; |
Michael Haggerty | c48a163 | 2014-12-12 09:56:48 +0100 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
Michael Haggerty | 5bcad1b | 2015-05-25 18:38:40 +0000 | [diff] [blame] | 427 | static int collect_reflog(const char *ref, const struct object_id *oid, int unused, void *cb_data) |
Junio C Hamano | bda3a31 | 2008-01-25 23:53:05 -0800 | [diff] [blame] | 428 | { |
Ævar Arnfjörð Bjarmason | f2919ba | 2021-12-22 05:06:42 +0100 | [diff] [blame] | 429 | struct worktree_reflogs *cb = cb_data; |
| 430 | struct worktree *worktree = cb->worktree; |
Nguyễn Thái Ngọc Duy | c9ef0d9 | 2018-10-21 10:08:59 +0200 | [diff] [blame] | 431 | struct strbuf newref = STRBUF_INIT; |
Junio C Hamano | bda3a31 | 2008-01-25 23:53:05 -0800 | [diff] [blame] | 432 | |
Nguyễn Thái Ngọc Duy | c9ef0d9 | 2018-10-21 10:08:59 +0200 | [diff] [blame] | 433 | /* |
| 434 | * Avoid collecting the same shared ref multiple times because |
| 435 | * they are available via all worktrees. |
| 436 | */ |
Ævar Arnfjörð Bjarmason | f2919ba | 2021-12-22 05:06:42 +0100 | [diff] [blame] | 437 | if (!worktree->is_current && ref_type(ref) == REF_TYPE_NORMAL) |
Nguyễn Thái Ngọc Duy | c9ef0d9 | 2018-10-21 10:08:59 +0200 | [diff] [blame] | 438 | return 0; |
| 439 | |
Ævar Arnfjörð Bjarmason | f2919ba | 2021-12-22 05:06:42 +0100 | [diff] [blame] | 440 | strbuf_worktree_ref(worktree, &newref, ref); |
| 441 | string_list_append_nodup(&cb->reflogs, strbuf_detach(&newref, NULL)); |
Nguyễn Thái Ngọc Duy | c9ef0d9 | 2018-10-21 10:08:59 +0200 | [diff] [blame] | 442 | |
Junio C Hamano | bda3a31 | 2008-01-25 23:53:05 -0800 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 446 | static struct reflog_expire_cfg { |
| 447 | struct reflog_expire_cfg *next; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 448 | timestamp_t expire_total; |
| 449 | timestamp_t expire_unreachable; |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 450 | char pattern[FLEX_ARRAY]; |
| 451 | } *reflog_expire_cfg, **reflog_expire_cfg_tail; |
| 452 | |
| 453 | static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len) |
| 454 | { |
| 455 | struct reflog_expire_cfg *ent; |
| 456 | |
| 457 | if (!reflog_expire_cfg_tail) |
| 458 | reflog_expire_cfg_tail = &reflog_expire_cfg; |
| 459 | |
| 460 | for (ent = reflog_expire_cfg; ent; ent = ent->next) |
Jeff King | c3a700f | 2016-02-19 06:21:08 -0500 | [diff] [blame] | 461 | if (!strncmp(ent->pattern, pattern, len) && |
| 462 | ent->pattern[len] == '\0') |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 463 | return ent; |
| 464 | |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 465 | FLEX_ALLOC_MEM(ent, pattern, pattern, len); |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 466 | *reflog_expire_cfg_tail = ent; |
| 467 | reflog_expire_cfg_tail = &(ent->next); |
| 468 | return ent; |
| 469 | } |
| 470 | |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 471 | /* expiry timer slot */ |
| 472 | #define EXPIRE_TOTAL 01 |
| 473 | #define EXPIRE_UNREACH 02 |
| 474 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 475 | static int reflog_expire_config(const char *var, const char *value, void *cb) |
Junio C Hamano | 4aec56d | 2006-12-27 01:47:57 -0800 | [diff] [blame] | 476 | { |
Jeff King | b3873c3 | 2013-01-23 01:27:37 -0500 | [diff] [blame] | 477 | const char *pattern, *key; |
Jeff King | f5914f4 | 2020-04-10 15:44:28 -0400 | [diff] [blame] | 478 | size_t pattern_len; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 479 | timestamp_t expire; |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 480 | int slot; |
| 481 | struct reflog_expire_cfg *ent; |
| 482 | |
Jeff King | b3873c3 | 2013-01-23 01:27:37 -0500 | [diff] [blame] | 483 | if (parse_config_key(var, "gc", &pattern, &pattern_len, &key) < 0) |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 484 | return git_default_config(var, value, cb); |
| 485 | |
Jeff King | b3873c3 | 2013-01-23 01:27:37 -0500 | [diff] [blame] | 486 | if (!strcmp(key, "reflogexpire")) { |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 487 | slot = EXPIRE_TOTAL; |
Haaris Mehmood | 5f96742 | 2017-11-18 02:27:27 +0000 | [diff] [blame] | 488 | if (git_config_expiry_date(&expire, var, value)) |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 489 | return -1; |
Jeff King | b3873c3 | 2013-01-23 01:27:37 -0500 | [diff] [blame] | 490 | } else if (!strcmp(key, "reflogexpireunreachable")) { |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 491 | slot = EXPIRE_UNREACH; |
Haaris Mehmood | 5f96742 | 2017-11-18 02:27:27 +0000 | [diff] [blame] | 492 | if (git_config_expiry_date(&expire, var, value)) |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 493 | return -1; |
| 494 | } else |
| 495 | return git_default_config(var, value, cb); |
| 496 | |
Jeff King | b3873c3 | 2013-01-23 01:27:37 -0500 | [diff] [blame] | 497 | if (!pattern) { |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 498 | switch (slot) { |
| 499 | case EXPIRE_TOTAL: |
| 500 | default_reflog_expire = expire; |
| 501 | break; |
| 502 | case EXPIRE_UNREACH: |
| 503 | default_reflog_expire_unreachable = expire; |
| 504 | break; |
| 505 | } |
Junio C Hamano | 4f342b9 | 2008-02-11 10:50:06 -0800 | [diff] [blame] | 506 | return 0; |
| 507 | } |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 508 | |
Jeff King | b3873c3 | 2013-01-23 01:27:37 -0500 | [diff] [blame] | 509 | ent = find_cfg_ent(pattern, pattern_len); |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 510 | if (!ent) |
| 511 | return -1; |
| 512 | switch (slot) { |
| 513 | case EXPIRE_TOTAL: |
| 514 | ent->expire_total = expire; |
| 515 | break; |
| 516 | case EXPIRE_UNREACH: |
| 517 | ent->expire_unreachable = expire; |
| 518 | break; |
Junio C Hamano | 4f342b9 | 2008-02-11 10:50:06 -0800 | [diff] [blame] | 519 | } |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static void set_reflog_expiry_param(struct cmd_reflog_expire_cb *cb, int slot, const char *ref) |
| 524 | { |
| 525 | struct reflog_expire_cfg *ent; |
| 526 | |
| 527 | if (slot == (EXPIRE_TOTAL|EXPIRE_UNREACH)) |
| 528 | return; /* both given explicitly -- nothing to tweak */ |
| 529 | |
| 530 | for (ent = reflog_expire_cfg; ent; ent = ent->next) { |
Ævar Arnfjörð Bjarmason | 55d3426 | 2017-06-22 21:38:08 +0000 | [diff] [blame] | 531 | if (!wildmatch(ent->pattern, ref, 0)) { |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 532 | if (!(slot & EXPIRE_TOTAL)) |
| 533 | cb->expire_total = ent->expire_total; |
| 534 | if (!(slot & EXPIRE_UNREACH)) |
| 535 | cb->expire_unreachable = ent->expire_unreachable; |
| 536 | return; |
| 537 | } |
| 538 | } |
| 539 | |
Junio C Hamano | 60bce2b | 2008-06-28 22:24:49 -0700 | [diff] [blame] | 540 | /* |
| 541 | * If unconfigured, make stash never expire |
| 542 | */ |
| 543 | if (!strcmp(ref, "refs/stash")) { |
| 544 | if (!(slot & EXPIRE_TOTAL)) |
| 545 | cb->expire_total = 0; |
| 546 | if (!(slot & EXPIRE_UNREACH)) |
| 547 | cb->expire_unreachable = 0; |
| 548 | return; |
| 549 | } |
| 550 | |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 551 | /* Nothing matched -- use the default value */ |
| 552 | if (!(slot & EXPIRE_TOTAL)) |
| 553 | cb->expire_total = default_reflog_expire; |
| 554 | if (!(slot & EXPIRE_UNREACH)) |
| 555 | cb->expire_unreachable = default_reflog_expire_unreachable; |
Junio C Hamano | 4aec56d | 2006-12-27 01:47:57 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 558 | static int cmd_reflog_expire(int argc, const char **argv, const char *prefix) |
| 559 | { |
Ævar Arnfjörð Bjarmason | 46fbe41 | 2021-12-22 05:06:41 +0100 | [diff] [blame] | 560 | struct cmd_reflog_expire_cb cmd = { 0 }; |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 561 | timestamp_t now = time(NULL); |
Nguyễn Thái Ngọc Duy | c9ef0d9 | 2018-10-21 10:08:59 +0200 | [diff] [blame] | 562 | int i, status, do_all, all_worktrees = 1; |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 563 | int explicit_expiry = 0; |
Michael Haggerty | aba56c8 | 2014-12-12 09:56:49 +0100 | [diff] [blame] | 564 | unsigned int flags = 0; |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 565 | int verbose = 0; |
| 566 | reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 567 | |
Adam Simpkins | 4a9f439 | 2010-02-26 19:50:03 -0800 | [diff] [blame] | 568 | default_reflog_expire_unreachable = now - 30 * 24 * 3600; |
| 569 | default_reflog_expire = now - 90 * 24 * 3600; |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 570 | git_config(reflog_expire_config, NULL); |
Junio C Hamano | 4aec56d | 2006-12-27 01:47:57 -0800 | [diff] [blame] | 571 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 572 | save_commit_buffer = 0; |
| 573 | do_all = status = 0; |
Junio C Hamano | 4aec56d | 2006-12-27 01:47:57 -0800 | [diff] [blame] | 574 | |
Ævar Arnfjörð Bjarmason | 46fbe41 | 2021-12-22 05:06:41 +0100 | [diff] [blame] | 575 | cmd.expire_total = default_reflog_expire; |
| 576 | cmd.expire_unreachable = default_reflog_expire_unreachable; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 577 | |
| 578 | for (i = 1; i < argc; i++) { |
| 579 | const char *arg = argv[i]; |
Junio C Hamano | 145136a | 2020-01-30 11:35:46 -0800 | [diff] [blame] | 580 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 581 | if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n")) |
Michael Haggerty | 98f31d8 | 2014-12-12 09:56:50 +0100 | [diff] [blame] | 582 | flags |= EXPIRE_REFLOGS_DRY_RUN; |
Junio C Hamano | 145136a | 2020-01-30 11:35:46 -0800 | [diff] [blame] | 583 | else if (skip_prefix(arg, "--expire=", &arg)) { |
Ævar Arnfjörð Bjarmason | 46fbe41 | 2021-12-22 05:06:41 +0100 | [diff] [blame] | 584 | if (parse_expiry_date(arg, &cmd.expire_total)) |
Junio C Hamano | 3d27b9b | 2013-04-17 15:38:08 -0700 | [diff] [blame] | 585 | die(_("'%s' is not a valid timestamp"), arg); |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 586 | explicit_expiry |= EXPIRE_TOTAL; |
| 587 | } |
Junio C Hamano | 145136a | 2020-01-30 11:35:46 -0800 | [diff] [blame] | 588 | else if (skip_prefix(arg, "--expire-unreachable=", &arg)) { |
Ævar Arnfjörð Bjarmason | 46fbe41 | 2021-12-22 05:06:41 +0100 | [diff] [blame] | 589 | if (parse_expiry_date(arg, &cmd.expire_unreachable)) |
Junio C Hamano | 3d27b9b | 2013-04-17 15:38:08 -0700 | [diff] [blame] | 590 | die(_("'%s' is not a valid timestamp"), arg); |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 591 | explicit_expiry |= EXPIRE_UNREACH; |
| 592 | } |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 593 | else if (!strcmp(arg, "--stale-fix")) |
Ævar Arnfjörð Bjarmason | 46fbe41 | 2021-12-22 05:06:41 +0100 | [diff] [blame] | 594 | cmd.stalefix = 1; |
Brandon Casey | 2b81fab | 2008-02-22 12:56:50 -0600 | [diff] [blame] | 595 | else if (!strcmp(arg, "--rewrite")) |
Michael Haggerty | 553daf1 | 2014-12-12 09:56:56 +0100 | [diff] [blame] | 596 | flags |= EXPIRE_REFLOGS_REWRITE; |
Brandon Casey | 55f1056 | 2008-02-22 13:04:12 -0600 | [diff] [blame] | 597 | else if (!strcmp(arg, "--updateref")) |
Michael Haggerty | c4c4fbf | 2014-12-12 09:56:51 +0100 | [diff] [blame] | 598 | flags |= EXPIRE_REFLOGS_UPDATE_REF; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 599 | else if (!strcmp(arg, "--all")) |
| 600 | do_all = 1; |
Nguyễn Thái Ngọc Duy | c9ef0d9 | 2018-10-21 10:08:59 +0200 | [diff] [blame] | 601 | else if (!strcmp(arg, "--single-worktree")) |
| 602 | all_worktrees = 0; |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 603 | else if (!strcmp(arg, "--verbose")) |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 604 | verbose = 1; |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 605 | else if (!strcmp(arg, "--")) { |
| 606 | i++; |
| 607 | break; |
| 608 | } |
| 609 | else if (arg[0] == '-') |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 610 | usage(_(reflog_expire_usage)); |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 611 | else |
| 612 | break; |
| 613 | } |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 614 | |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 615 | if (verbose) |
| 616 | should_prune_fn = should_expire_reflog_ent_verbose; |
| 617 | |
Junio C Hamano | 3cb22b8 | 2008-06-15 23:48:46 -0700 | [diff] [blame] | 618 | /* |
| 619 | * We can trust the commits and objects reachable from refs |
| 620 | * even in older repository. We cannot trust what's reachable |
| 621 | * from reflog if the repository was pruned with older git. |
| 622 | */ |
Ævar Arnfjörð Bjarmason | 46fbe41 | 2021-12-22 05:06:41 +0100 | [diff] [blame] | 623 | if (cmd.stalefix) { |
Ævar Arnfjörð Bjarmason | 994b328 | 2021-12-22 05:06:46 +0100 | [diff] [blame] | 624 | struct rev_info revs; |
| 625 | |
| 626 | repo_init_revisions(the_repository, &revs, prefix); |
| 627 | revs.do_not_die_on_missing_tree = 1; |
| 628 | revs.ignore_missing = 1; |
| 629 | revs.ignore_missing_links = 1; |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 630 | if (verbose) |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 631 | printf(_("Marking reachable objects...")); |
Ævar Arnfjörð Bjarmason | 994b328 | 2021-12-22 05:06:46 +0100 | [diff] [blame] | 632 | mark_reachable_objects(&revs, 0, 0, NULL); |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 633 | if (verbose) |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 634 | putchar('\n'); |
| 635 | } |
| 636 | |
Junio C Hamano | bda3a31 | 2008-01-25 23:53:05 -0800 | [diff] [blame] | 637 | if (do_all) { |
Ævar Arnfjörð Bjarmason | f2919ba | 2021-12-22 05:06:42 +0100 | [diff] [blame] | 638 | struct worktree_reflogs collected = { |
| 639 | .reflogs = STRING_LIST_INIT_DUP, |
| 640 | }; |
| 641 | struct string_list_item *item; |
Nguyễn Thái Ngọc Duy | c9ef0d9 | 2018-10-21 10:08:59 +0200 | [diff] [blame] | 642 | struct worktree **worktrees, **p; |
Junio C Hamano | bda3a31 | 2008-01-25 23:53:05 -0800 | [diff] [blame] | 643 | |
Eric Sunshine | 03f2465 | 2020-06-19 19:35:44 -0400 | [diff] [blame] | 644 | worktrees = get_worktrees(); |
Nguyễn Thái Ngọc Duy | c9ef0d9 | 2018-10-21 10:08:59 +0200 | [diff] [blame] | 645 | for (p = worktrees; *p; p++) { |
| 646 | if (!all_worktrees && !(*p)->is_current) |
| 647 | continue; |
Ævar Arnfjörð Bjarmason | f2919ba | 2021-12-22 05:06:42 +0100 | [diff] [blame] | 648 | collected.worktree = *p; |
Nguyễn Thái Ngọc Duy | c9ef0d9 | 2018-10-21 10:08:59 +0200 | [diff] [blame] | 649 | refs_for_each_reflog(get_worktree_ref_store(*p), |
| 650 | collect_reflog, &collected); |
| 651 | } |
| 652 | free_worktrees(worktrees); |
Ævar Arnfjörð Bjarmason | f2919ba | 2021-12-22 05:06:42 +0100 | [diff] [blame] | 653 | |
| 654 | for_each_string_list_item(item, &collected.reflogs) { |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 655 | struct expire_reflog_policy_cb cb = { |
| 656 | .cmd = cmd, |
| 657 | .dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN), |
| 658 | }; |
Ævar Arnfjörð Bjarmason | ae35e16 | 2021-08-23 13:36:10 +0200 | [diff] [blame] | 659 | |
Ævar Arnfjörð Bjarmason | f2919ba | 2021-12-22 05:06:42 +0100 | [diff] [blame] | 660 | set_reflog_expiry_param(&cb.cmd, explicit_expiry, item->string); |
| 661 | status |= reflog_expire(item->string, flags, |
Michael Haggerty | fa5b183 | 2014-12-12 09:56:59 +0100 | [diff] [blame] | 662 | reflog_expiry_prepare, |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 663 | should_prune_fn, |
Michael Haggerty | fa5b183 | 2014-12-12 09:56:59 +0100 | [diff] [blame] | 664 | reflog_expiry_cleanup, |
| 665 | &cb); |
Junio C Hamano | bda3a31 | 2008-01-25 23:53:05 -0800 | [diff] [blame] | 666 | } |
Ævar Arnfjörð Bjarmason | f2919ba | 2021-12-22 05:06:42 +0100 | [diff] [blame] | 667 | string_list_clear(&collected.reflogs, 0); |
Junio C Hamano | bda3a31 | 2008-01-25 23:53:05 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Pieter de Bie | 90fb46e | 2008-08-10 22:22:21 +0200 | [diff] [blame] | 670 | for (; i < argc; i++) { |
| 671 | char *ref; |
Ævar Arnfjörð Bjarmason | 46fbe41 | 2021-12-22 05:06:41 +0100 | [diff] [blame] | 672 | struct expire_reflog_policy_cb cb = { .cmd = cmd }; |
| 673 | |
Ævar Arnfjörð Bjarmason | ae35e16 | 2021-08-23 13:36:10 +0200 | [diff] [blame] | 674 | if (!dwim_log(argv[i], strlen(argv[i]), NULL, &ref)) { |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 675 | status |= error(_("%s points nowhere!"), argv[i]); |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 676 | continue; |
| 677 | } |
Michael Haggerty | b729eff | 2014-12-12 09:56:58 +0100 | [diff] [blame] | 678 | set_reflog_expiry_param(&cb.cmd, explicit_expiry, ref); |
Ævar Arnfjörð Bjarmason | cc40b5c | 2021-08-23 13:36:11 +0200 | [diff] [blame] | 679 | status |= reflog_expire(ref, flags, |
Michael Haggerty | fa5b183 | 2014-12-12 09:56:59 +0100 | [diff] [blame] | 680 | reflog_expiry_prepare, |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 681 | should_prune_fn, |
Michael Haggerty | fa5b183 | 2014-12-12 09:56:59 +0100 | [diff] [blame] | 682 | reflog_expiry_cleanup, |
| 683 | &cb); |
Ævar Arnfjörð Bjarmason | 3c81504 | 2021-10-22 10:55:44 +0200 | [diff] [blame] | 684 | free(ref); |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 685 | } |
| 686 | return status; |
| 687 | } |
| 688 | |
brian m. carlson | 9461d27 | 2017-02-21 23:47:32 +0000 | [diff] [blame] | 689 | static int count_reflog_ent(struct object_id *ooid, struct object_id *noid, |
Johannes Schindelin | dddbad7 | 2017-04-26 21:29:31 +0200 | [diff] [blame] | 690 | const char *email, timestamp_t timestamp, int tz, |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 691 | const char *message, void *cb_data) |
| 692 | { |
Ævar Arnfjörð Bjarmason | 4a0339b | 2021-12-22 05:06:40 +0100 | [diff] [blame] | 693 | struct cmd_reflog_expire_cb *cb = cb_data; |
| 694 | if (!cb->expire_total || timestamp < cb->expire_total) |
| 695 | cb->recno++; |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static int cmd_reflog_delete(int argc, const char **argv, const char *prefix) |
| 700 | { |
Ævar Arnfjörð Bjarmason | 4a0339b | 2021-12-22 05:06:40 +0100 | [diff] [blame] | 701 | struct cmd_reflog_expire_cb cmd = { 0 }; |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 702 | int i, status = 0; |
Michael Haggerty | aba56c8 | 2014-12-12 09:56:49 +0100 | [diff] [blame] | 703 | unsigned int flags = 0; |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 704 | int verbose = 0; |
| 705 | reflog_expiry_should_prune_fn *should_prune_fn = should_expire_reflog_ent; |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 706 | |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 707 | for (i = 1; i < argc; i++) { |
Brandon Casey | 3c386aa | 2008-02-22 15:08:59 -0600 | [diff] [blame] | 708 | const char *arg = argv[i]; |
| 709 | if (!strcmp(arg, "--dry-run") || !strcmp(arg, "-n")) |
Michael Haggerty | 98f31d8 | 2014-12-12 09:56:50 +0100 | [diff] [blame] | 710 | flags |= EXPIRE_REFLOGS_DRY_RUN; |
Brandon Casey | 2b81fab | 2008-02-22 12:56:50 -0600 | [diff] [blame] | 711 | else if (!strcmp(arg, "--rewrite")) |
Michael Haggerty | 553daf1 | 2014-12-12 09:56:56 +0100 | [diff] [blame] | 712 | flags |= EXPIRE_REFLOGS_REWRITE; |
Brandon Casey | 55f1056 | 2008-02-22 13:04:12 -0600 | [diff] [blame] | 713 | else if (!strcmp(arg, "--updateref")) |
Michael Haggerty | c4c4fbf | 2014-12-12 09:56:51 +0100 | [diff] [blame] | 714 | flags |= EXPIRE_REFLOGS_UPDATE_REF; |
Brandon Casey | 3c386aa | 2008-02-22 15:08:59 -0600 | [diff] [blame] | 715 | else if (!strcmp(arg, "--verbose")) |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 716 | verbose = 1; |
Brandon Casey | 3c386aa | 2008-02-22 15:08:59 -0600 | [diff] [blame] | 717 | else if (!strcmp(arg, "--")) { |
| 718 | i++; |
| 719 | break; |
| 720 | } |
| 721 | else if (arg[0] == '-') |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 722 | usage(_(reflog_delete_usage)); |
Brandon Casey | 3c386aa | 2008-02-22 15:08:59 -0600 | [diff] [blame] | 723 | else |
| 724 | break; |
| 725 | } |
| 726 | |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 727 | if (verbose) |
| 728 | should_prune_fn = should_expire_reflog_ent_verbose; |
| 729 | |
Brandon Casey | 3c386aa | 2008-02-22 15:08:59 -0600 | [diff] [blame] | 730 | if (argc - i < 1) |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 731 | return error(_("no reflog specified to delete")); |
Brandon Casey | 3c386aa | 2008-02-22 15:08:59 -0600 | [diff] [blame] | 732 | |
| 733 | for ( ; i < argc; i++) { |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 734 | const char *spec = strstr(argv[i], "@{"); |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 735 | char *ep, *ref; |
| 736 | int recno; |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 737 | struct expire_reflog_policy_cb cb = { |
| 738 | .dry_run = !!(flags & EXPIRE_REFLOGS_DRY_RUN), |
| 739 | }; |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 740 | |
| 741 | if (!spec) { |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 742 | status |= error(_("not a reflog: %s"), argv[i]); |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 743 | continue; |
| 744 | } |
| 745 | |
Ævar Arnfjörð Bjarmason | ae35e16 | 2021-08-23 13:36:10 +0200 | [diff] [blame] | 746 | if (!dwim_log(argv[i], spec - argv[i], NULL, &ref)) { |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 747 | status |= error(_("no reflog for '%s'"), argv[i]); |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 748 | continue; |
| 749 | } |
| 750 | |
| 751 | recno = strtoul(spec + 2, &ep, 10); |
| 752 | if (*ep == '}') { |
Ævar Arnfjörð Bjarmason | 4a0339b | 2021-12-22 05:06:40 +0100 | [diff] [blame] | 753 | cmd.recno = -recno; |
| 754 | for_each_reflog_ent(ref, count_reflog_ent, &cmd); |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 755 | } else { |
Ævar Arnfjörð Bjarmason | 4a0339b | 2021-12-22 05:06:40 +0100 | [diff] [blame] | 756 | cmd.expire_total = approxidate(spec + 2); |
| 757 | for_each_reflog_ent(ref, count_reflog_ent, &cmd); |
| 758 | cmd.expire_total = 0; |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 759 | } |
| 760 | |
Ævar Arnfjörð Bjarmason | 4a0339b | 2021-12-22 05:06:40 +0100 | [diff] [blame] | 761 | cb.cmd = cmd; |
Ævar Arnfjörð Bjarmason | cc40b5c | 2021-08-23 13:36:11 +0200 | [diff] [blame] | 762 | status |= reflog_expire(ref, flags, |
Michael Haggerty | fa5b183 | 2014-12-12 09:56:59 +0100 | [diff] [blame] | 763 | reflog_expiry_prepare, |
Ævar Arnfjörð Bjarmason | fcd2c3d | 2021-12-22 05:06:48 +0100 | [diff] [blame] | 764 | should_prune_fn, |
Michael Haggerty | fa5b183 | 2014-12-12 09:56:59 +0100 | [diff] [blame] | 765 | reflog_expiry_cleanup, |
| 766 | &cb); |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 767 | free(ref); |
| 768 | } |
| 769 | return status; |
| 770 | } |
| 771 | |
David Turner | afcb2e7 | 2015-07-21 17:04:53 -0400 | [diff] [blame] | 772 | static int cmd_reflog_exists(int argc, const char **argv, const char *prefix) |
| 773 | { |
| 774 | int i, start = 0; |
| 775 | |
| 776 | for (i = 1; i < argc; i++) { |
| 777 | const char *arg = argv[i]; |
| 778 | if (!strcmp(arg, "--")) { |
| 779 | i++; |
| 780 | break; |
| 781 | } |
| 782 | else if (arg[0] == '-') |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 783 | usage(_(reflog_exists_usage)); |
David Turner | afcb2e7 | 2015-07-21 17:04:53 -0400 | [diff] [blame] | 784 | else |
| 785 | break; |
| 786 | } |
| 787 | |
| 788 | start = i; |
| 789 | |
| 790 | if (argc - start != 1) |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 791 | usage(_(reflog_exists_usage)); |
David Turner | afcb2e7 | 2015-07-21 17:04:53 -0400 | [diff] [blame] | 792 | |
| 793 | if (check_refname_format(argv[start], REFNAME_ALLOW_ONELEVEL)) |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 794 | die(_("invalid ref format: %s"), argv[start]); |
David Turner | afcb2e7 | 2015-07-21 17:04:53 -0400 | [diff] [blame] | 795 | return !reflog_exists(argv[start]); |
| 796 | } |
| 797 | |
Junio C Hamano | 1389d9d | 2007-01-06 02:16:19 -0800 | [diff] [blame] | 798 | /* |
| 799 | * main "reflog" |
| 800 | */ |
| 801 | |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 802 | static const char reflog_usage[] = |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 803 | N_("git reflog [ show | expire | delete | exists ]"); |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 804 | |
| 805 | int cmd_reflog(int argc, const char **argv, const char *prefix) |
| 806 | { |
Jonathan Nieder | 99caeed | 2009-11-09 09:05:01 -0600 | [diff] [blame] | 807 | if (argc > 1 && !strcmp(argv[1], "-h")) |
Nguyễn Thái Ngọc Duy | dd509db | 2018-11-10 06:16:07 +0100 | [diff] [blame] | 808 | usage(_(reflog_usage)); |
Jonathan Nieder | 99caeed | 2009-11-09 09:05:01 -0600 | [diff] [blame] | 809 | |
Linus Torvalds | cf39f54 | 2007-02-08 09:51:56 -0800 | [diff] [blame] | 810 | /* With no command, we default to showing it. */ |
| 811 | if (argc < 2 || *argv[1] == '-') |
| 812 | return cmd_log_reflog(argc, argv, prefix); |
| 813 | |
| 814 | if (!strcmp(argv[1], "show")) |
| 815 | return cmd_log_reflog(argc - 1, argv + 1, prefix); |
| 816 | |
| 817 | if (!strcmp(argv[1], "expire")) |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 818 | return cmd_reflog_expire(argc - 1, argv + 1, prefix); |
Linus Torvalds | cf39f54 | 2007-02-08 09:51:56 -0800 | [diff] [blame] | 819 | |
Johannes Schindelin | 552cecc | 2007-10-17 02:50:45 +0100 | [diff] [blame] | 820 | if (!strcmp(argv[1], "delete")) |
| 821 | return cmd_reflog_delete(argc - 1, argv + 1, prefix); |
| 822 | |
David Turner | afcb2e7 | 2015-07-21 17:04:53 -0400 | [diff] [blame] | 823 | if (!strcmp(argv[1], "exists")) |
| 824 | return cmd_reflog_exists(argc - 1, argv + 1, prefix); |
| 825 | |
Michael Schubert | bf01d4a | 2011-08-01 13:20:42 +0200 | [diff] [blame] | 826 | return cmd_log_reflog(argc, argv, prefix); |
Junio C Hamano | 4264dc1 | 2006-12-19 00:23:12 -0800 | [diff] [blame] | 827 | } |