summaryrefslogtreecommitdiff
path: root/builtin/gc.c
diff options
context:
space:
mode:
Diffstat (limited to 'builtin/gc.c')
-rw-r--r--builtin/gc.c234
1 files changed, 156 insertions, 78 deletions
diff --git a/builtin/gc.c b/builtin/gc.c
index 021e9256ae..243ee85d28 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -167,10 +167,9 @@ static void gc_config(void)
struct maintenance_run_opts;
static int maintenance_task_pack_refs(MAYBE_UNUSED struct maintenance_run_opts *opts)
{
- struct strvec pack_refs_cmd = STRVEC_INIT;
- strvec_pushl(&pack_refs_cmd, "pack-refs", "--all", "--prune", NULL);
+ const char *argv[] = { "pack-refs", "--all", "--prune", NULL };
- return run_command_v_opt(pack_refs_cmd.v, RUN_GIT_CMD);
+ return run_command_v_opt(argv, RUN_GIT_CMD);
}
static int too_many_loose_objects(void)
@@ -776,8 +775,9 @@ struct cg_auto_data {
int limit;
};
-static int dfs_on_ref(const char *refname,
- const struct object_id *oid, int flags,
+static int dfs_on_ref(const char *refname UNUSED,
+ const struct object_id *oid,
+ int flags UNUSED,
void *cb_data)
{
struct cg_auto_data *data = (struct cg_auto_data *)cb_data;
@@ -904,12 +904,6 @@ static int fetch_remote(struct remote *remote, void *cbdata)
static int maintenance_task_prefetch(struct maintenance_run_opts *opts)
{
- git_config_set_multivar_gently("log.excludedecoration",
- "refs/prefetch/",
- "refs/prefetch/",
- CONFIG_FLAGS_FIXED_VALUE |
- CONFIG_FLAGS_MULTI_REPLACE);
-
if (for_each_remote(fetch_remote, opts)) {
error(_("failed to prefetch remotes"));
return 1;
@@ -1459,13 +1453,28 @@ static char *get_maintpath(void)
return strbuf_detach(&sb, NULL);
}
-static int maintenance_register(void)
+static char const * const builtin_maintenance_register_usage[] = {
+ "git maintenance register",
+ NULL
+};
+
+static int maintenance_register(int argc, const char **argv, const char *prefix)
{
- int rc;
+ struct option options[] = {
+ OPT_END(),
+ };
+ int found = 0;
+ const char *key = "maintenance.repo";
char *config_value;
- struct child_process config_set = CHILD_PROCESS_INIT;
- struct child_process config_get = CHILD_PROCESS_INIT;
char *maintpath = get_maintpath();
+ struct string_list_item *item;
+ const struct string_list *list;
+
+ argc = parse_options(argc, argv, prefix, options,
+ builtin_maintenance_register_usage, 0);
+ if (argc)
+ usage_with_options(builtin_maintenance_register_usage,
+ options);
/* Disable foreground maintenance */
git_config_set("maintenance.auto", "false");
@@ -1476,46 +1485,95 @@ static int maintenance_register(void)
else
git_config_set("maintenance.strategy", "incremental");
- config_get.git_cmd = 1;
- strvec_pushl(&config_get.args, "config", "--global", "--get",
- "--fixed-value", "maintenance.repo", maintpath, NULL);
- config_get.out = -1;
-
- if (start_command(&config_get)) {
- rc = error(_("failed to run 'git config'"));
- goto done;
+ list = git_config_get_value_multi(key);
+ if (list) {
+ for_each_string_list_item(item, list) {
+ if (!strcmp(maintpath, item->string)) {
+ found = 1;
+ break;
+ }
+ }
}
- /* We already have this value in our config! */
- if (!finish_command(&config_get)) {
- rc = 0;
- goto done;
+ if (!found) {
+ int rc;
+ char *user_config, *xdg_config;
+ git_global_config(&user_config, &xdg_config);
+ if (!user_config)
+ die(_("$HOME not set"));
+ rc = git_config_set_multivar_in_file_gently(
+ user_config, "maintenance.repo", maintpath,
+ CONFIG_REGEX_NONE, 0);
+ free(user_config);
+ free(xdg_config);
+
+ if (rc)
+ die(_("unable to add '%s' value of '%s'"),
+ key, maintpath);
}
- config_set.git_cmd = 1;
- strvec_pushl(&config_set.args, "config", "--add", "--global", "maintenance.repo",
- maintpath, NULL);
-
- rc = run_command(&config_set);
-
-done:
free(maintpath);
- return rc;
+ return 0;
}
-static int maintenance_unregister(void)
+static char const * const builtin_maintenance_unregister_usage[] = {
+ "git maintenance unregister [--force]",
+ NULL
+};
+
+static int maintenance_unregister(int argc, const char **argv, const char *prefix)
{
- int rc;
- struct child_process config_unset = CHILD_PROCESS_INIT;
+ int force = 0;
+ struct option options[] = {
+ OPT__FORCE(&force,
+ N_("return success even if repository was not registered"),
+ PARSE_OPT_NOCOMPLETE),
+ OPT_END(),
+ };
+ const char *key = "maintenance.repo";
char *maintpath = get_maintpath();
+ int found = 0;
+ struct string_list_item *item;
+ const struct string_list *list;
+
+ argc = parse_options(argc, argv, prefix, options,
+ builtin_maintenance_unregister_usage, 0);
+ if (argc)
+ usage_with_options(builtin_maintenance_unregister_usage,
+ options);
+
+ list = git_config_get_value_multi(key);
+ if (list) {
+ for_each_string_list_item(item, list) {
+ if (!strcmp(maintpath, item->string)) {
+ found = 1;
+ break;
+ }
+ }
+ }
- config_unset.git_cmd = 1;
- strvec_pushl(&config_unset.args, "config", "--global", "--unset",
- "--fixed-value", "maintenance.repo", maintpath, NULL);
+ if (found) {
+ int rc;
+ char *user_config, *xdg_config;
+ git_global_config(&user_config, &xdg_config);
+ if (!user_config)
+ die(_("$HOME not set"));
+ rc = git_config_set_multivar_in_file_gently(
+ user_config, key, NULL, maintpath,
+ CONFIG_FLAGS_MULTI_REPLACE | CONFIG_FLAGS_FIXED_VALUE);
+ free(user_config);
+ free(xdg_config);
+
+ if (rc &&
+ (!force || rc == CONFIG_NOTHING_SET))
+ die(_("unable to unset '%s' value of '%s'"),
+ key, maintpath);
+ } else if (!force) {
+ die(_("repository '%s' is not registered"), maintpath);
+ }
- rc = run_command(&config_unset);
free(maintpath);
- return rc;
+ return 0;
}
static const char *get_frequency(enum schedule_priority schedule)
@@ -2059,6 +2117,7 @@ static int crontab_update_schedule(int run_maintenance, int fd)
struct child_process crontab_edit = CHILD_PROCESS_INIT;
FILE *cron_list, *cron_in;
struct strbuf line = STRBUF_INIT;
+ struct tempfile *tmpedit = NULL;
get_schedule_cmd(&cmd, NULL);
strvec_split(&crontab_list.args, cmd);
@@ -2073,6 +2132,17 @@ static int crontab_update_schedule(int run_maintenance, int fd)
/* Ignore exit code, as an empty crontab will return error. */
finish_command(&crontab_list);
+ tmpedit = mks_tempfile_t(".git_cron_edit_tmpXXXXXX");
+ if (!tmpedit) {
+ result = error(_("failed to create crontab temporary file"));
+ goto out;
+ }
+ cron_in = fdopen_tempfile(tmpedit, "w");
+ if (!cron_in) {
+ result = error(_("failed to open temporary file"));
+ goto out;
+ }
+
/*
* Read from the .lock file, filtering out the old
* schedule while appending the new schedule.
@@ -2080,19 +2150,6 @@ static int crontab_update_schedule(int run_maintenance, int fd)
cron_list = fdopen(fd, "r");
rewind(cron_list);
- strvec_split(&crontab_edit.args, cmd);
- crontab_edit.in = -1;
- crontab_edit.git_cmd = 0;
-
- if (start_command(&crontab_edit))
- return error(_("failed to run 'crontab'; your system might not support 'cron'"));
-
- cron_in = fdopen(crontab_edit.in, "w");
- if (!cron_in) {
- result = error(_("failed to open stdin of 'crontab'"));
- goto done_editing;
- }
-
while (!strbuf_getline_lf(&line, cron_list)) {
if (!in_old_region && !strcmp(line.buf, BEGIN_LINE))
in_old_region = 1;
@@ -2126,14 +2183,22 @@ static int crontab_update_schedule(int run_maintenance, int fd)
}
fflush(cron_in);
- fclose(cron_in);
- close(crontab_edit.in);
-done_editing:
+ strvec_split(&crontab_edit.args, cmd);
+ strvec_push(&crontab_edit.args, get_tempfile_path(tmpedit));
+ crontab_edit.git_cmd = 0;
+
+ if (start_command(&crontab_edit)) {
+ result = error(_("failed to run 'crontab'; your system might not support 'cron'"));
+ goto out;
+ }
+
if (finish_command(&crontab_edit))
result = error(_("'crontab' died"));
else
fclose(cron_list);
+out:
+ delete_tempfile(&tmpedit);
return result;
}
@@ -2490,6 +2555,7 @@ static int maintenance_start(int argc, const char **argv, const char *prefix)
PARSE_OPT_NONEG, maintenance_opt_scheduler),
OPT_END()
};
+ const char *register_args[] = { "register", NULL };
argc = parse_options(argc, argv, prefix, options,
builtin_maintenance_start_usage, 0);
@@ -2499,34 +2565,46 @@ static int maintenance_start(int argc, const char **argv, const char *prefix)
opts.scheduler = resolve_scheduler(opts.scheduler);
validate_scheduler(opts.scheduler);
- if (maintenance_register())
+ if (maintenance_register(ARRAY_SIZE(register_args)-1, register_args, NULL))
warning(_("failed to add repo to global config"));
return update_background_schedule(&opts, 1);
}
-static int maintenance_stop(void)
+static const char *const builtin_maintenance_stop_usage[] = {
+ "git maintenance stop",
+ NULL
+};
+
+static int maintenance_stop(int argc, const char **argv, const char *prefix)
{
+ struct option options[] = {
+ OPT_END()
+ };
+ argc = parse_options(argc, argv, prefix, options,
+ builtin_maintenance_stop_usage, 0);
+ if (argc)
+ usage_with_options(builtin_maintenance_stop_usage, options);
return update_background_schedule(NULL, 0);
}
-static const char builtin_maintenance_usage[] = N_("git maintenance <subcommand> [<options>]");
+static const char * const builtin_maintenance_usage[] = {
+ N_("git maintenance <subcommand> [<options>]"),
+ NULL,
+};
int cmd_maintenance(int argc, const char **argv, const char *prefix)
{
- if (argc < 2 ||
- (argc == 2 && !strcmp(argv[1], "-h")))
- usage(builtin_maintenance_usage);
-
- if (!strcmp(argv[1], "run"))
- return maintenance_run(argc - 1, argv + 1, prefix);
- if (!strcmp(argv[1], "start"))
- return maintenance_start(argc - 1, argv + 1, prefix);
- if (!strcmp(argv[1], "stop"))
- return maintenance_stop();
- if (!strcmp(argv[1], "register"))
- return maintenance_register();
- if (!strcmp(argv[1], "unregister"))
- return maintenance_unregister();
-
- die(_("invalid subcommand: %s"), argv[1]);
+ parse_opt_subcommand_fn *fn = NULL;
+ struct option builtin_maintenance_options[] = {
+ OPT_SUBCOMMAND("run", &fn, maintenance_run),
+ OPT_SUBCOMMAND("start", &fn, maintenance_start),
+ OPT_SUBCOMMAND("stop", &fn, maintenance_stop),
+ OPT_SUBCOMMAND("register", &fn, maintenance_register),
+ OPT_SUBCOMMAND("unregister", &fn, maintenance_unregister),
+ OPT_END(),
+ };
+
+ argc = parse_options(argc, argv, prefix, builtin_maintenance_options,
+ builtin_maintenance_usage, 0);
+ return fn(argc, argv, prefix);
}