@matheusbsbs / matheus-bagliano / commits / 2057d75038
Fork 0

maintenance: create basic maintenance runner

The 'gc' builtin is our current entrypoint for automatically maintaining a repository. This one tool does many operations, such as repacking the repository, packing refs, and rewriting the commit-graph file. The name implies it performs "garbage collection" which means several different things, and some users may not want to use this operation that rewrites the entire object database. Create a new 'maintenance' builtin that will become a more general- purpose command. To start, it will only support the 'run' subcommand, but will later expand to add subcommands for scheduling maintenance in the background. For now, the 'maintenance' builtin is a thin shim over the 'gc' builtin. In fact, the only option is the '--auto' toggle, which is handed directly to the 'gc' builtin. The current change is isolated to this simple operation to prevent more interesting logic from being lost in all of the boilerplate of adding a new builtin. Use existing builtin/gc.c file because we want to share code between the two builtins. It is possible that we will have 'maintenance' replace the 'gc' builtin entirely at some point, leaving 'git gc' as an alias for some specific arguments to 'git maintenance run'. Create a new test_subcommand helper that allows us to test if a certain subcommand was run. It requires storing the GIT_TRACE2_EVENT logs in a file. A negation mode is available that will be used in later tests. Helped-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Sep 17, 2020 at 18:11 UTC 2057d75038541cd16debb1c55f3f897fd244965c
8 files changed +175
.gitignore
+1
@@ -90,6 +90,7 @@
90 /git-ls-tree
91 /git-mailinfo
92 /git-mailsplit
93 +/git-maintenance
94 /git-merge
95 /git-merge-base
96 /git-merge-index
Documentation/git-maintenance.txt new
+57
@@ -0,0 +1,57 @@
1 +git-maintenance(1)
2 +==================
3 +
4 +NAME
5 +----
6 +git-maintenance - Run tasks to optimize Git repository data
7 +
8 +
9 +SYNOPSIS
10 +--------
11 +[verse]
12 +'git maintenance' run [<options>]
13 +
14 +
15 +DESCRIPTION
16 +-----------
17 +Run tasks to optimize Git repository data, speeding up other Git commands
18 +and reducing storage requirements for the repository.
19 +
20 +Git commands that add repository data, such as `git add` or `git fetch`,
21 +are optimized for a responsive user experience. These commands do not take
22 +time to optimize the Git data, since such optimizations scale with the full
23 +size of the repository while these user commands each perform a relatively
24 +small action.
25 +
26 +The `git maintenance` command provides flexibility for how to optimize the
27 +Git repository.
28 +
29 +SUBCOMMANDS
30 +-----------
31 +
32 +run::
33 + Run one or more maintenance tasks.
34 +
35 +TASKS
36 +-----
37 +
38 +gc::
39 + Clean up unnecessary files and optimize the local repository. "GC"
40 + stands for "garbage collection," but this task performs many
41 + smaller tasks. This task can be expensive for large repositories,
42 + as it repacks all Git objects into a single pack-file. It can also
43 + be disruptive in some situations, as it deletes stale data. See
44 + linkgit:git-gc[1] for more details on garbage collection in Git.
45 +
46 +OPTIONS
47 +-------
48 +--auto::
49 + When combined with the `run` subcommand, run maintenance tasks
50 + only if certain thresholds are met. For example, the `gc` task
51 + runs when the number of loose objects exceeds the number stored
52 + in the `gc.auto` config setting, or when the number of pack-files
53 + exceeds the `gc.autoPackLimit` config setting.
54 +
55 +GIT
56 +---
57 +Part of the linkgit:git[1] suite
builtin.h
+1
@@ -167,6 +167,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix);
167 int cmd_ls_remote(int argc, const char **argv, const char *prefix);
168 int cmd_mailinfo(int argc, const char **argv, const char *prefix);
169 int cmd_mailsplit(int argc, const char **argv, const char *prefix);
170 +int cmd_maintenance(int argc, const char **argv, const char *prefix);
171 int cmd_merge(int argc, const char **argv, const char *prefix);
172 int cmd_merge_base(int argc, const char **argv, const char *prefix);
173 int cmd_merge_index(int argc, const char **argv, const char *prefix);
builtin/gc.c
+58
@@ -699,3 +699,61 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
699
700 return 0;
701 }
702 +
703 +static const char * const builtin_maintenance_run_usage[] = {
704 + N_("git maintenance run [--auto]"),
705 + NULL
706 +};
707 +
708 +struct maintenance_run_opts {
709 + int auto_flag;
710 +};
711 +
712 +static int maintenance_task_gc(struct maintenance_run_opts *opts)
713 +{
714 + struct child_process child = CHILD_PROCESS_INIT;
715 +
716 + child.git_cmd = 1;
717 + strvec_push(&child.args, "gc");
718 +
719 + if (opts->auto_flag)
720 + strvec_push(&child.args, "--auto");
721 +
722 + close_object_store(the_repository->objects);
723 + return run_command(&child);
724 +}
725 +
726 +static int maintenance_run(int argc, const char **argv, const char *prefix)
727 +{
728 + struct maintenance_run_opts opts;
729 + struct option builtin_maintenance_run_options[] = {
730 + OPT_BOOL(0, "auto", &opts.auto_flag,
731 + N_("run tasks based on the state of the repository")),
732 + OPT_END()
733 + };
734 + memset(&opts, 0, sizeof(opts));
735 +
736 + argc = parse_options(argc, argv, prefix,
737 + builtin_maintenance_run_options,
738 + builtin_maintenance_run_usage,
739 + PARSE_OPT_STOP_AT_NON_OPTION);
740 +
741 + if (argc != 0)
742 + usage_with_options(builtin_maintenance_run_usage,
743 + builtin_maintenance_run_options);
744 + return maintenance_task_gc(&opts);
745 +}
746 +
747 +static const char builtin_maintenance_usage[] = N_("git maintenance run [<options>]");
748 +
749 +int cmd_maintenance(int argc, const char **argv, const char *prefix)
750 +{
751 + if (argc < 2 ||
752 + (argc == 2 && !strcmp(argv[1], "-h")))
753 + usage(builtin_maintenance_usage);
754 +
755 + if (!strcmp(argv[1], "run"))
756 + return maintenance_run(argc - 1, argv + 1, prefix);
757 +
758 + die(_("invalid subcommand: %s"), argv[1]);
759 +}
command-list.txt
+1
@@ -117,6 +117,7 @@ git-ls-remote plumbinginterrogators
117 git-ls-tree plumbinginterrogators
118 git-mailinfo purehelpers
119 git-mailsplit purehelpers
120 +git-maintenance mainporcelain
121 git-merge mainporcelain history
122 git-merge-base plumbinginterrogators
123 git-merge-file plumbingmanipulators
git.c
+1
@@ -529,6 +529,7 @@ static struct cmd_struct commands[] = {
529 { "ls-tree", cmd_ls_tree, RUN_SETUP },
530 { "mailinfo", cmd_mailinfo, RUN_SETUP_GENTLY | NO_PARSEOPT },
531 { "mailsplit", cmd_mailsplit, NO_PARSEOPT },
532 + { "maintenance", cmd_maintenance, RUN_SETUP_GENTLY | NO_PARSEOPT },
533 { "merge", cmd_merge, RUN_SETUP | NEED_WORK_TREE },
534 { "merge-base", cmd_merge_base, RUN_SETUP },
535 { "merge-file", cmd_merge_file, RUN_SETUP_GENTLY },
t/t7900-maintenance.sh new
+23
@@ -0,0 +1,23 @@
1 +#!/bin/sh
2 +
3 +test_description='git maintenance builtin'
4 +
5 +. ./test-lib.sh
6 +
7 +test_expect_success 'help text' '
8 + test_expect_code 129 git maintenance -h 2>err &&
9 + test_i18ngrep "usage: git maintenance run" err &&
10 + test_expect_code 128 git maintenance barf 2>err &&
11 + test_i18ngrep "invalid subcommand: barf" err &&
12 + test_expect_code 129 git maintenance 2>err &&
13 + test_i18ngrep "usage: git maintenance" err
14 +'
15 +
16 +test_expect_success 'run [--auto]' '
17 + GIT_TRACE2_EVENT="$(pwd)/run-no-auto.txt" git maintenance run &&
18 + GIT_TRACE2_EVENT="$(pwd)/run-auto.txt" git maintenance run --auto &&
19 + test_subcommand git gc <run-no-auto.txt &&
20 + test_subcommand git gc --auto <run-auto.txt
21 +'
22 +
23 +test_done
t/test-lib-functions.sh
+33
@@ -1628,3 +1628,36 @@ test_path_is_hidden () {
1628 case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
1629 return 1
1630 }
1631 +
1632 +# Check that the given command was invoked as part of the
1633 +# trace2-format trace on stdin.
1634 +#
1635 +# test_subcommand [!] <command> <args>... < <trace>
1636 +#
1637 +# For example, to look for an invocation of "git upload-pack
1638 +# /path/to/repo"
1639 +#
1640 +# GIT_TRACE2_EVENT=event.log git fetch ... &&
1641 +# test_subcommand git upload-pack "$PATH" <event.log
1642 +#
1643 +# If the first parameter passed is !, this instead checks that
1644 +# the given command was not called.
1645 +#
1646 +test_subcommand () {
1647 + local negate=
1648 + if test "$1" = "!"
1649 + then
1650 + negate=t
1651 + shift
1652 + fi
1653 +
1654 + local expr=$(printf '"%s",' "$@")
1655 + expr="${expr%,}"
1656 +
1657 + if test -n "$negate"
1658 + then
1659 + ! grep "\[$expr\]"
1660 + else
1661 + grep "\[$expr\]"
1662 + fi
1663 +}