]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/maintenance: introduce "reflog-expire" task
authorPatrick Steinhardt <ps@pks.im>
Tue, 8 Apr 2025 06:22:17 +0000 (08:22 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 8 Apr 2025 14:53:27 +0000 (07:53 -0700)
By default, git-maintenance(1) uses the "gc" task to ensure that the
repository is well-maintained. This can be changed, for example by
either explicitly configuring which tasks should be enabled or by using
the "incremental" maintenance strategy. If so, git-maintenance(1) does
not know to expire reflog entries, which is a subtask that git-gc(1)
knows to perform for the user. Consequently, the reflog will grow
indefinitely unless the user manually trims it.

Introduce a new "reflog-expire" task that plugs this gap:

  - When running the task directly, then we simply execute `git reflog
    expire --all`, which is the same as git-gc(1).

  - When running git-maintenance(1) with the `--auto` flag, then we only
    run the task in case the "HEAD" reflog has at least N reflog entries
    that would be discarded. By default, N is set to 100, but this can
    be configured via "maintenance.reflog-expire.auto". When a negative
    integer has been provided we always expire entries, zero causes us
    to never expire entries, and a positive value specifies how many
    entries need to exist before we consider pruning the entries.

Note that the condition for the `--auto` flags is merely a heuristic and
optimized for being fast. This is because `git maintenance run --auto`
will be executed quite regularly, so scanning through all reflogs would
likely be too expensive in many repositories.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/config/maintenance.adoc
Documentation/git-maintenance.adoc
builtin/gc.c
t/t7900-maintenance.sh

index 72a9d6cf816928d9ca285c649d2438e300ba7a99..e57f346a067268e9cd0ef955f2a45cf0afb0acbf 100644 (file)
@@ -69,3 +69,12 @@ maintenance.incremental-repack.auto::
        Otherwise, a positive value implies the command should run when the
        number of pack-files not in the multi-pack-index is at least the value
        of `maintenance.incremental-repack.auto`. The default value is 10.
+
+maintenance.reflog-expire.auto::
+       This integer config option controls how often the `reflog-expire` task
+       should be run as part of `git maintenance run --auto`. If zero, then
+       the `reflog-expire` task will not run with the `--auto` option. A
+       negative value will force the task to run every time. Otherwise, a
+       positive value implies the command should run when the number of
+       expired reflog entries in the "HEAD" reflog is at least the value of
+       `maintenance.loose-objects.auto`. The default value is 100.
index 0450d74aff1ea225a526bc29b60c941f37fe07d7..8bc94a6d4ffa97c7043e2faaf56dd14e06df2c1a 100644 (file)
@@ -158,6 +158,10 @@ pack-refs::
        need to iterate across many references. See linkgit:git-pack-refs[1]
        for more information.
 
+reflog-expire::
+       The `reflog-expire` task deletes any entries in the reflog older than the
+       expiry threshold. See linkgit:git-reflog[1] for more information.
+
 OPTIONS
 -------
 --auto::
index e8f5705dc59f52ca8f08ad433853a7335b972ccd..ce5bb2630f8c01fdbfe06e14ba5dcbe5b1643de3 100644 (file)
@@ -33,6 +33,7 @@
 #include "pack.h"
 #include "pack-objects.h"
 #include "path.h"
+#include "reflog.h"
 #include "blob.h"
 #include "tree.h"
 #include "promisor-remote.h"
@@ -285,6 +286,49 @@ static int maintenance_task_pack_refs(struct maintenance_run_opts *opts,
        return run_command(&cmd);
 }
 
+struct count_reflog_entries_data {
+       struct expire_reflog_policy_cb policy;
+       size_t count;
+       size_t limit;
+};
+
+static int count_reflog_entries(struct object_id *old_oid, struct object_id *new_oid,
+                               const char *committer, timestamp_t timestamp,
+                               int tz, const char *msg, void *cb_data)
+{
+       struct count_reflog_entries_data *data = cb_data;
+       if (should_expire_reflog_ent(old_oid, new_oid, committer, timestamp, tz, msg, &data->policy))
+               data->count++;
+       return data->count >= data->limit;
+}
+
+static int reflog_expire_condition(struct gc_config *cfg UNUSED)
+{
+       timestamp_t now = time(NULL);
+       struct count_reflog_entries_data data = {
+               .policy = {
+                       .opts = REFLOG_EXPIRE_OPTIONS_INIT(now),
+               },
+       };
+       int limit = 100;
+
+       git_config_get_int("maintenance.reflog-expire.auto", &limit);
+       if (!limit)
+               return 0;
+       if (limit < 0)
+               return 1;
+       data.limit = limit;
+
+       repo_config(the_repository, reflog_expire_config, &data.policy.opts);
+
+       reflog_expire_options_set_refname(&data.policy.opts, "HEAD");
+       refs_for_each_reflog_ent(get_main_ref_store(the_repository), "HEAD",
+                                count_reflog_entries, &data);
+
+       reflog_expiry_cleanup(&data.policy);
+       return data.count >= data.limit;
+}
+
 static int maintenance_task_reflog_expire(struct maintenance_run_opts *opts UNUSED,
                                          struct gc_config *cfg UNUSED)
 {
@@ -1383,6 +1427,7 @@ enum maintenance_task_label {
        TASK_GC,
        TASK_COMMIT_GRAPH,
        TASK_PACK_REFS,
+       TASK_REFLOG_EXPIRE,
 
        /* Leave as final value */
        TASK__COUNT
@@ -1419,6 +1464,11 @@ static struct maintenance_task tasks[] = {
                maintenance_task_pack_refs,
                pack_refs_condition,
        },
+       [TASK_REFLOG_EXPIRE] = {
+               "reflog-expire",
+               maintenance_task_reflog_expire,
+               reflog_expire_condition,
+       },
 };
 
 static int compare_tasks_by_selection(const void *a_, const void *b_)
index 1909aed95e08ad4167fcd34d066e561be0329441..ff98cde92c01e2a7d044a45108fd286b27fa7107 100755 (executable)
@@ -447,6 +447,24 @@ test_expect_success 'pack-refs task' '
        test_subcommand git pack-refs --all --prune <pack-refs.txt
 '
 
+test_expect_success 'reflog-expire task' '
+       GIT_TRACE2_EVENT="$(pwd)/reflog-expire.txt" \
+               git maintenance run --task=reflog-expire &&
+       test_subcommand git reflog expire --all <reflog-expire.txt
+'
+
+test_expect_success 'reflog-expire task --auto only packs when exceeding limits' '
+       git reflog expire --all --expire=now &&
+       test_commit reflog-one &&
+       test_commit reflog-two &&
+       GIT_TRACE2_EVENT="$(pwd)/reflog-expire-auto.txt" \
+               git -c maintenance.reflog-expire.auto=3 maintenance run --auto --task=reflog-expire &&
+       test_subcommand ! git reflog expire --all <reflog-expire-auto.txt &&
+       GIT_TRACE2_EVENT="$(pwd)/reflog-expire-auto.txt" \
+               git -c maintenance.reflog-expire.auto=2 maintenance run --auto --task=reflog-expire &&
+       test_subcommand git reflog expire --all <reflog-expire-auto.txt
+'
+
 test_expect_success '--auto and --schedule incompatible' '
        test_must_fail git maintenance run --auto --schedule=daily 2>err &&
        test_grep "at most one" err