2 * git gc builtin command
4 * Cleanup unreachable files and optimize the repository.
6 * Copyright (c) 2007 James Bowes
8 * Based on git-gc.sh, which is
10 * Copyright (c) 2006 Shawn O. Pearce
14 #include "repository.h"
18 #include "parse-options.h"
19 #include "run-command.h"
23 #include "commit-graph.h"
25 #include "object-store.h"
27 #include "pack-objects.h"
30 #include "promisor-remote.h"
33 #define FAILED_RUN "failed to run %s"
35 static const char * const builtin_gc_usage
[] = {
36 N_("git gc [<options>]"),
40 static int pack_refs
= 1;
41 static int prune_reflogs
= 1;
42 static int aggressive_depth
= 50;
43 static int aggressive_window
= 250;
44 static int gc_auto_threshold
= 6700;
45 static int gc_auto_pack_limit
= 50;
46 static int detach_auto
= 1;
47 static timestamp_t gc_log_expire_time
;
48 static const char *gc_log_expire
= "1.day.ago";
49 static const char *prune_expire
= "2.weeks.ago";
50 static const char *prune_worktrees_expire
= "3.months.ago";
51 static unsigned long big_pack_threshold
;
52 static unsigned long max_delta_cache_size
= DEFAULT_DELTA_CACHE_SIZE
;
54 static struct strvec pack_refs_cmd
= STRVEC_INIT
;
55 static struct strvec reflog
= STRVEC_INIT
;
56 static struct strvec repack
= STRVEC_INIT
;
57 static struct strvec prune
= STRVEC_INIT
;
58 static struct strvec prune_worktrees
= STRVEC_INIT
;
59 static struct strvec rerere
= STRVEC_INIT
;
61 static struct tempfile
*pidfile
;
62 static struct lock_file log_lock
;
64 static struct string_list pack_garbage
= STRING_LIST_INIT_DUP
;
66 static void clean_pack_garbage(void)
69 for (i
= 0; i
< pack_garbage
.nr
; i
++)
70 unlink_or_warn(pack_garbage
.items
[i
].string
);
71 string_list_clear(&pack_garbage
, 0);
74 static void report_pack_garbage(unsigned seen_bits
, const char *path
)
76 if (seen_bits
== PACKDIR_FILE_IDX
)
77 string_list_append(&pack_garbage
, path
);
80 static void process_log_file(void)
83 if (fstat(get_lock_file_fd(&log_lock
), &st
)) {
85 * Perhaps there was an i/o error or another
86 * unlikely situation. Try to make a note of
87 * this in gc.log along with any existing
90 int saved_errno
= errno
;
91 fprintf(stderr
, _("Failed to fstat %s: %s"),
92 get_tempfile_path(log_lock
.tempfile
),
93 strerror(saved_errno
));
95 commit_lock_file(&log_lock
);
97 } else if (st
.st_size
) {
98 /* There was some error recorded in the lock file */
99 commit_lock_file(&log_lock
);
101 /* No error, clean up any old gc.log */
102 unlink(git_path("gc.log"));
103 rollback_lock_file(&log_lock
);
107 static void process_log_file_at_exit(void)
113 static void process_log_file_on_signal(int signo
)
120 static int gc_config_is_timestamp_never(const char *var
)
125 if (!git_config_get_value(var
, &value
) && value
) {
126 if (parse_expiry_date(value
, &expire
))
127 die(_("failed to parse '%s' value '%s'"), var
, value
);
133 static void gc_config(void)
137 if (!git_config_get_value("gc.packrefs", &value
)) {
138 if (value
&& !strcmp(value
, "notbare"))
141 pack_refs
= git_config_bool("gc.packrefs", value
);
144 if (gc_config_is_timestamp_never("gc.reflogexpire") &&
145 gc_config_is_timestamp_never("gc.reflogexpireunreachable"))
148 git_config_get_int("gc.aggressivewindow", &aggressive_window
);
149 git_config_get_int("gc.aggressivedepth", &aggressive_depth
);
150 git_config_get_int("gc.auto", &gc_auto_threshold
);
151 git_config_get_int("gc.autopacklimit", &gc_auto_pack_limit
);
152 git_config_get_bool("gc.autodetach", &detach_auto
);
153 git_config_get_expiry("gc.pruneexpire", &prune_expire
);
154 git_config_get_expiry("gc.worktreepruneexpire", &prune_worktrees_expire
);
155 git_config_get_expiry("gc.logexpiry", &gc_log_expire
);
157 git_config_get_ulong("gc.bigpackthreshold", &big_pack_threshold
);
158 git_config_get_ulong("pack.deltacachesize", &max_delta_cache_size
);
160 git_config(git_default_config
, NULL
);
163 static int too_many_loose_objects(void)
166 * Quickly check if a "gc" is needed, by estimating how
167 * many loose objects there are. Because SHA-1 is evenly
168 * distributed, we can check only one and get a reasonable
176 const unsigned hexsz_loose
= the_hash_algo
->hexsz
- 2;
178 dir
= opendir(git_path("objects/17"));
182 auto_threshold
= DIV_ROUND_UP(gc_auto_threshold
, 256);
183 while ((ent
= readdir(dir
)) != NULL
) {
184 if (strspn(ent
->d_name
, "0123456789abcdef") != hexsz_loose
||
185 ent
->d_name
[hexsz_loose
] != '\0')
187 if (++num_loose
> auto_threshold
) {
196 static struct packed_git
*find_base_packs(struct string_list
*packs
,
199 struct packed_git
*p
, *base
= NULL
;
201 for (p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
205 if (p
->pack_size
>= limit
)
206 string_list_append(packs
, p
->pack_name
);
207 } else if (!base
|| base
->pack_size
< p
->pack_size
) {
213 string_list_append(packs
, base
->pack_name
);
218 static int too_many_packs(void)
220 struct packed_git
*p
;
223 if (gc_auto_pack_limit
<= 0)
226 for (cnt
= 0, p
= get_all_packs(the_repository
); p
; p
= p
->next
) {
232 * Perhaps check the size of the pack and count only
233 * very small ones here?
237 return gc_auto_pack_limit
< cnt
;
240 static uint64_t total_ram(void)
242 #if defined(HAVE_SYSINFO)
247 #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
248 int64_t physical_memory
;
253 # if defined(HW_MEMSIZE)
258 length
= sizeof(int64_t);
259 if (!sysctl(mib
, 2, &physical_memory
, &length
, NULL
, 0))
260 return physical_memory
;
261 #elif defined(GIT_WINDOWS_NATIVE)
262 MEMORYSTATUSEX memInfo
;
264 memInfo
.dwLength
= sizeof(MEMORYSTATUSEX
);
265 if (GlobalMemoryStatusEx(&memInfo
))
266 return memInfo
.ullTotalPhys
;
271 static uint64_t estimate_repack_memory(struct packed_git
*pack
)
273 unsigned long nr_objects
= approximate_object_count();
274 size_t os_cache
, heap
;
276 if (!pack
|| !nr_objects
)
280 * First we have to scan through at least one pack.
281 * Assume enough room in OS file cache to keep the entire pack
282 * or we may accidentally evict data of other processes from
285 os_cache
= pack
->pack_size
+ pack
->index_size
;
286 /* then pack-objects needs lots more for book keeping */
287 heap
= sizeof(struct object_entry
) * nr_objects
;
289 * internal rev-list --all --objects takes up some memory too,
290 * let's say half of it is for blobs
292 heap
+= sizeof(struct blob
) * nr_objects
/ 2;
294 * and the other half is for trees (commits and tags are
295 * usually insignificant)
297 heap
+= sizeof(struct tree
) * nr_objects
/ 2;
298 /* and then obj_hash[], underestimated in fact */
299 heap
+= sizeof(struct object
*) * nr_objects
;
300 /* revindex is used also */
301 heap
+= sizeof(struct revindex_entry
) * nr_objects
;
303 * read_sha1_file() (either at delta calculation phase, or
304 * writing phase) also fills up the delta base cache
306 heap
+= delta_base_cache_limit
;
307 /* and of course pack-objects has its own delta cache */
308 heap
+= max_delta_cache_size
;
310 return os_cache
+ heap
;
313 static int keep_one_pack(struct string_list_item
*item
, void *data
)
315 strvec_pushf(&repack
, "--keep-pack=%s", basename(item
->string
));
319 static void add_repack_all_option(struct string_list
*keep_pack
)
321 if (prune_expire
&& !strcmp(prune_expire
, "now"))
322 strvec_push(&repack
, "-a");
324 strvec_push(&repack
, "-A");
326 strvec_pushf(&repack
, "--unpack-unreachable=%s", prune_expire
);
330 for_each_string_list(keep_pack
, keep_one_pack
, NULL
);
333 static void add_repack_incremental_option(void)
335 strvec_push(&repack
, "--no-write-bitmap-index");
338 static int need_to_gc(void)
341 * Setting gc.auto to 0 or negative can disable the
344 if (gc_auto_threshold
<= 0)
348 * If there are too many loose objects, but not too many
349 * packs, we run "repack -d -l". If there are too many packs,
350 * we run "repack -A -d -l". Otherwise we tell the caller
353 if (too_many_packs()) {
354 struct string_list keep_pack
= STRING_LIST_INIT_NODUP
;
356 if (big_pack_threshold
) {
357 find_base_packs(&keep_pack
, big_pack_threshold
);
358 if (keep_pack
.nr
>= gc_auto_pack_limit
) {
359 big_pack_threshold
= 0;
360 string_list_clear(&keep_pack
, 0);
361 find_base_packs(&keep_pack
, 0);
364 struct packed_git
*p
= find_base_packs(&keep_pack
, 0);
365 uint64_t mem_have
, mem_want
;
367 mem_have
= total_ram();
368 mem_want
= estimate_repack_memory(p
);
371 * Only allow 1/2 of memory for pack-objects, leave
372 * the rest for the OS and other processes in the
375 if (!mem_have
|| mem_want
< mem_have
/ 2)
376 string_list_clear(&keep_pack
, 0);
379 add_repack_all_option(&keep_pack
);
380 string_list_clear(&keep_pack
, 0);
381 } else if (too_many_loose_objects())
382 add_repack_incremental_option();
386 if (run_hook_le(NULL
, "pre-auto-gc", NULL
))
391 /* return NULL on success, else hostname running the gc */
392 static const char *lock_repo_for_gc(int force
, pid_t
* ret_pid
)
394 struct lock_file lock
= LOCK_INIT
;
395 char my_host
[HOST_NAME_MAX
+ 1];
396 struct strbuf sb
= STRBUF_INIT
;
403 if (is_tempfile_active(pidfile
))
407 if (xgethostname(my_host
, sizeof(my_host
)))
408 xsnprintf(my_host
, sizeof(my_host
), "unknown");
410 pidfile_path
= git_pathdup("gc.pid");
411 fd
= hold_lock_file_for_update(&lock
, pidfile_path
,
414 static char locking_host
[HOST_NAME_MAX
+ 1];
415 static char *scan_fmt
;
419 scan_fmt
= xstrfmt("%s %%%ds", "%"SCNuMAX
, HOST_NAME_MAX
);
420 fp
= fopen(pidfile_path
, "r");
421 memset(locking_host
, 0, sizeof(locking_host
));
424 !fstat(fileno(fp
), &st
) &&
426 * 12 hour limit is very generous as gc should
427 * never take that long. On the other hand we
428 * don't really need a strict limit here,
429 * running gc --auto one day late is not a big
430 * problem. --force can be used in manual gc
431 * after the user verifies that no gc is
434 time(NULL
) - st
.st_mtime
<= 12 * 3600 &&
435 fscanf(fp
, scan_fmt
, &pid
, locking_host
) == 2 &&
436 /* be gentle to concurrent "gc" on remote hosts */
437 (strcmp(locking_host
, my_host
) || !kill(pid
, 0) || errno
== EPERM
);
442 rollback_lock_file(&lock
);
449 strbuf_addf(&sb
, "%"PRIuMAX
" %s",
450 (uintmax_t) getpid(), my_host
);
451 write_in_full(fd
, sb
.buf
, sb
.len
);
453 commit_lock_file(&lock
);
454 pidfile
= register_tempfile(pidfile_path
);
460 * Returns 0 if there was no previous error and gc can proceed, 1 if
461 * gc should not proceed due to an error in the last run. Prints a
462 * message and returns -1 if an error occurred while reading gc.log
464 static int report_last_gc_error(void)
466 struct strbuf sb
= STRBUF_INIT
;
470 char *gc_log_path
= git_pathdup("gc.log");
472 if (stat(gc_log_path
, &st
)) {
476 ret
= error_errno(_("cannot stat '%s'"), gc_log_path
);
480 if (st
.st_mtime
< gc_log_expire_time
)
483 len
= strbuf_read_file(&sb
, gc_log_path
, 0);
485 ret
= error_errno(_("cannot read '%s'"), gc_log_path
);
488 * A previous gc failed. Report the error, and don't
489 * bother with an automatic gc run since it is likely
490 * to fail in the same way.
492 warning(_("The last gc run reported the following. "
493 "Please correct the root cause\n"
495 "Automatic cleanup will not be performed "
496 "until the file is removed.\n\n"
498 gc_log_path
, sb
.buf
);
507 static void gc_before_repack(void)
510 * We may be called twice, as both the pre- and
511 * post-daemonized phases will call us, but running these
512 * commands more than once is pointless and wasteful.
518 if (pack_refs
&& run_command_v_opt(pack_refs_cmd
.v
, RUN_GIT_CMD
))
519 die(FAILED_RUN
, pack_refs_cmd
.v
[0]);
521 if (prune_reflogs
&& run_command_v_opt(reflog
.v
, RUN_GIT_CMD
))
522 die(FAILED_RUN
, reflog
.v
[0]);
525 int cmd_gc(int argc
, const char **argv
, const char *prefix
)
534 int keep_base_pack
= -1;
537 struct option builtin_gc_options
[] = {
538 OPT__QUIET(&quiet
, N_("suppress progress reporting")),
539 { OPTION_STRING
, 0, "prune", &prune_expire
, N_("date"),
540 N_("prune unreferenced objects"),
541 PARSE_OPT_OPTARG
, NULL
, (intptr_t)prune_expire
},
542 OPT_BOOL(0, "aggressive", &aggressive
, N_("be more thorough (increased runtime)")),
543 OPT_BOOL_F(0, "auto", &auto_gc
, N_("enable auto-gc mode"),
544 PARSE_OPT_NOCOMPLETE
),
545 OPT_BOOL_F(0, "force", &force
,
546 N_("force running gc even if there may be another gc running"),
547 PARSE_OPT_NOCOMPLETE
),
548 OPT_BOOL(0, "keep-largest-pack", &keep_base_pack
,
549 N_("repack all other packs except the largest pack")),
553 if (argc
== 2 && !strcmp(argv
[1], "-h"))
554 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
556 strvec_pushl(&pack_refs_cmd
, "pack-refs", "--all", "--prune", NULL
);
557 strvec_pushl(&reflog
, "reflog", "expire", "--all", NULL
);
558 strvec_pushl(&repack
, "repack", "-d", "-l", NULL
);
559 strvec_pushl(&prune
, "prune", "--expire", NULL
);
560 strvec_pushl(&prune_worktrees
, "worktree", "prune", "--expire", NULL
);
561 strvec_pushl(&rerere
, "rerere", "gc", NULL
);
563 /* default expiry time, overwritten in gc_config */
565 if (parse_expiry_date(gc_log_expire
, &gc_log_expire_time
))
566 die(_("failed to parse gc.logexpiry value %s"), gc_log_expire
);
569 pack_refs
= !is_bare_repository();
571 argc
= parse_options(argc
, argv
, prefix
, builtin_gc_options
,
572 builtin_gc_usage
, 0);
574 usage_with_options(builtin_gc_usage
, builtin_gc_options
);
576 if (prune_expire
&& parse_expiry_date(prune_expire
, &dummy
))
577 die(_("failed to parse prune expiry value %s"), prune_expire
);
580 strvec_push(&repack
, "-f");
581 if (aggressive_depth
> 0)
582 strvec_pushf(&repack
, "--depth=%d", aggressive_depth
);
583 if (aggressive_window
> 0)
584 strvec_pushf(&repack
, "--window=%d", aggressive_window
);
587 strvec_push(&repack
, "-q");
591 * Auto-gc should be least intrusive as possible.
597 fprintf(stderr
, _("Auto packing the repository in background for optimum performance.\n"));
599 fprintf(stderr
, _("Auto packing the repository for optimum performance.\n"));
600 fprintf(stderr
, _("See \"git help gc\" for manual housekeeping.\n"));
603 int ret
= report_last_gc_error();
605 /* an I/O error occurred, already reported */
608 /* Last gc --auto failed. Skip this one. */
611 if (lock_repo_for_gc(force
, &pid
))
613 gc_before_repack(); /* dies on failure */
614 delete_tempfile(&pidfile
);
617 * failure to daemonize is ok, we'll continue
620 daemonized
= !daemonize();
623 struct string_list keep_pack
= STRING_LIST_INIT_NODUP
;
625 if (keep_base_pack
!= -1) {
627 find_base_packs(&keep_pack
, 0);
628 } else if (big_pack_threshold
) {
629 find_base_packs(&keep_pack
, big_pack_threshold
);
632 add_repack_all_option(&keep_pack
);
633 string_list_clear(&keep_pack
, 0);
636 name
= lock_repo_for_gc(force
, &pid
);
639 return 0; /* be quiet on --auto */
640 die(_("gc is already running on machine '%s' pid %"PRIuMAX
" (use --force if not)"),
641 name
, (uintmax_t)pid
);
645 hold_lock_file_for_update(&log_lock
,
648 dup2(get_lock_file_fd(&log_lock
), 2);
649 sigchain_push_common(process_log_file_on_signal
);
650 atexit(process_log_file_at_exit
);
655 if (!repository_format_precious_objects
) {
656 close_object_store(the_repository
->objects
);
657 if (run_command_v_opt(repack
.v
, RUN_GIT_CMD
))
658 die(FAILED_RUN
, repack
.v
[0]);
661 strvec_push(&prune
, prune_expire
);
663 strvec_push(&prune
, "--no-progress");
664 if (has_promisor_remote())
666 "--exclude-promisor-objects");
667 if (run_command_v_opt(prune
.v
, RUN_GIT_CMD
))
668 die(FAILED_RUN
, prune
.v
[0]);
672 if (prune_worktrees_expire
) {
673 strvec_push(&prune_worktrees
, prune_worktrees_expire
);
674 if (run_command_v_opt(prune_worktrees
.v
, RUN_GIT_CMD
))
675 die(FAILED_RUN
, prune_worktrees
.v
[0]);
678 if (run_command_v_opt(rerere
.v
, RUN_GIT_CMD
))
679 die(FAILED_RUN
, rerere
.v
[0]);
681 report_garbage
= report_pack_garbage
;
682 reprepare_packed_git(the_repository
);
683 if (pack_garbage
.nr
> 0) {
684 close_object_store(the_repository
->objects
);
685 clean_pack_garbage();
688 prepare_repo_settings(the_repository
);
689 if (the_repository
->settings
.gc_write_commit_graph
== 1)
690 write_commit_graph_reachable(the_repository
->objects
->odb
,
691 !quiet
&& !daemonized
? COMMIT_GRAPH_WRITE_PROGRESS
: 0,
694 if (auto_gc
&& too_many_loose_objects())
695 warning(_("There are too many unreachable loose objects; "
696 "run 'git prune' to remove them."));
699 unlink(git_path("gc.log"));
704 static const char * const builtin_maintenance_run_usage
[] = {
705 N_("git maintenance run [--auto] [--[no-]quiet] [--task=<task>]"),
709 struct maintenance_run_opts
{
714 /* Remember to update object flag allocation in object.h */
717 struct cg_auto_data
{
718 int num_not_in_graph
;
722 static int dfs_on_ref(const char *refname
,
723 const struct object_id
*oid
, int flags
,
726 struct cg_auto_data
*data
= (struct cg_auto_data
*)cb_data
;
728 struct object_id peeled
;
729 struct commit_list
*stack
= NULL
;
730 struct commit
*commit
;
732 if (!peel_ref(refname
, &peeled
))
734 if (oid_object_info(the_repository
, oid
, NULL
) != OBJ_COMMIT
)
737 commit
= lookup_commit(the_repository
, oid
);
740 if (parse_commit(commit
))
743 commit_list_append(commit
, &stack
);
745 while (!result
&& stack
) {
746 struct commit_list
*parent
;
748 commit
= pop_commit(&stack
);
750 for (parent
= commit
->parents
; parent
; parent
= parent
->next
) {
751 if (parse_commit(parent
->item
) ||
752 commit_graph_position(parent
->item
) != COMMIT_NOT_FROM_GRAPH
||
753 parent
->item
->object
.flags
& SEEN
)
756 parent
->item
->object
.flags
|= SEEN
;
757 data
->num_not_in_graph
++;
759 if (data
->num_not_in_graph
>= data
->limit
) {
764 commit_list_append(parent
->item
, &stack
);
768 free_commit_list(stack
);
772 static int should_write_commit_graph(void)
775 struct cg_auto_data data
;
777 data
.num_not_in_graph
= 0;
779 git_config_get_int("maintenance.commit-graph.auto",
787 result
= for_each_ref(dfs_on_ref
, &data
);
789 clear_commit_marks_all(SEEN
);
794 static int run_write_commit_graph(struct maintenance_run_opts
*opts
)
796 struct child_process child
= CHILD_PROCESS_INIT
;
799 strvec_pushl(&child
.args
, "commit-graph", "write",
800 "--split", "--reachable", NULL
);
803 strvec_push(&child
.args
, "--no-progress");
805 return !!run_command(&child
);
808 static int maintenance_task_commit_graph(struct maintenance_run_opts
*opts
)
810 close_object_store(the_repository
->objects
);
811 if (run_write_commit_graph(opts
)) {
812 error(_("failed to write commit-graph"));
819 static int maintenance_task_gc(struct maintenance_run_opts
*opts
)
821 struct child_process child
= CHILD_PROCESS_INIT
;
824 strvec_push(&child
.args
, "gc");
827 strvec_push(&child
.args
, "--auto");
829 strvec_push(&child
.args
, "--quiet");
831 strvec_push(&child
.args
, "--no-quiet");
833 close_object_store(the_repository
->objects
);
834 return run_command(&child
);
837 typedef int maintenance_task_fn(struct maintenance_run_opts
*opts
);
840 * An auto condition function returns 1 if the task should run
841 * and 0 if the task should NOT run. See needs_to_gc() for an
844 typedef int maintenance_auto_fn(void);
846 struct maintenance_task
{
848 maintenance_task_fn
*fn
;
849 maintenance_auto_fn
*auto_condition
;
852 /* -1 if not selected. */
856 enum maintenance_task_label
{
860 /* Leave as final value */
864 static struct maintenance_task tasks
[] = {
871 [TASK_COMMIT_GRAPH
] = {
873 maintenance_task_commit_graph
,
874 should_write_commit_graph
,
878 static int compare_tasks_by_selection(const void *a_
, const void *b_
)
880 const struct maintenance_task
*a
, *b
;
882 a
= (const struct maintenance_task
*)&a_
;
883 b
= (const struct maintenance_task
*)&b_
;
885 return b
->selected_order
- a
->selected_order
;
888 static int maintenance_run_tasks(struct maintenance_run_opts
*opts
)
890 int i
, found_selected
= 0;
893 struct repository
*r
= the_repository
;
894 char *lock_path
= xstrfmt("%s/maintenance", r
->objects
->odb
->path
);
896 if (hold_lock_file_for_update(&lk
, lock_path
, LOCK_NO_DEREF
) < 0) {
898 * Another maintenance command is running.
900 * If --auto was provided, then it is likely due to a
901 * recursive process stack. Do not report an error in
904 if (!opts
->auto_flag
&& !opts
->quiet
)
905 warning(_("lock file '%s' exists, skipping maintenance"),
912 for (i
= 0; !found_selected
&& i
< TASK__COUNT
; i
++)
913 found_selected
= tasks
[i
].selected_order
>= 0;
916 QSORT(tasks
, TASK__COUNT
, compare_tasks_by_selection
);
918 for (i
= 0; i
< TASK__COUNT
; i
++) {
919 if (found_selected
&& tasks
[i
].selected_order
< 0)
922 if (!found_selected
&& !tasks
[i
].enabled
)
925 if (opts
->auto_flag
&&
926 (!tasks
[i
].auto_condition
||
927 !tasks
[i
].auto_condition()))
930 trace2_region_enter("maintenance", tasks
[i
].name
, r
);
931 if (tasks
[i
].fn(opts
)) {
932 error(_("task '%s' failed"), tasks
[i
].name
);
935 trace2_region_leave("maintenance", tasks
[i
].name
, r
);
938 rollback_lock_file(&lk
);
942 static void initialize_task_config(void)
945 struct strbuf config_name
= STRBUF_INIT
;
948 for (i
= 0; i
< TASK__COUNT
; i
++) {
951 strbuf_setlen(&config_name
, 0);
952 strbuf_addf(&config_name
, "maintenance.%s.enabled",
955 if (!git_config_get_bool(config_name
.buf
, &config_value
))
956 tasks
[i
].enabled
= config_value
;
959 strbuf_release(&config_name
);
962 static int task_option_parse(const struct option
*opt
,
963 const char *arg
, int unset
)
965 int i
, num_selected
= 0;
966 struct maintenance_task
*task
= NULL
;
968 BUG_ON_OPT_NEG(unset
);
970 for (i
= 0; i
< TASK__COUNT
; i
++) {
971 if (tasks
[i
].selected_order
>= 0)
973 if (!strcasecmp(tasks
[i
].name
, arg
)) {
979 error(_("'%s' is not a valid task"), arg
);
983 if (task
->selected_order
>= 0) {
984 error(_("task '%s' cannot be selected multiple times"), arg
);
988 task
->selected_order
= num_selected
+ 1;
993 static int maintenance_run(int argc
, const char **argv
, const char *prefix
)
996 struct maintenance_run_opts opts
;
997 struct option builtin_maintenance_run_options
[] = {
998 OPT_BOOL(0, "auto", &opts
.auto_flag
,
999 N_("run tasks based on the state of the repository")),
1000 OPT_BOOL(0, "quiet", &opts
.quiet
,
1001 N_("do not report progress or other information over stderr")),
1002 OPT_CALLBACK_F(0, "task", NULL
, N_("task"),
1003 N_("run a specific task"),
1004 PARSE_OPT_NONEG
, task_option_parse
),
1007 memset(&opts
, 0, sizeof(opts
));
1009 opts
.quiet
= !isatty(2);
1010 initialize_task_config();
1012 for (i
= 0; i
< TASK__COUNT
; i
++)
1013 tasks
[i
].selected_order
= -1;
1015 argc
= parse_options(argc
, argv
, prefix
,
1016 builtin_maintenance_run_options
,
1017 builtin_maintenance_run_usage
,
1018 PARSE_OPT_STOP_AT_NON_OPTION
);
1021 usage_with_options(builtin_maintenance_run_usage
,
1022 builtin_maintenance_run_options
);
1023 return maintenance_run_tasks(&opts
);
1026 static const char builtin_maintenance_usage
[] = N_("git maintenance run [<options>]");
1028 int cmd_maintenance(int argc
, const char **argv
, const char *prefix
)
1031 (argc
== 2 && !strcmp(argv
[1], "-h")))
1032 usage(builtin_maintenance_usage
);
1034 if (!strcmp(argv
[1], "run"))
1035 return maintenance_run(argc
- 1, argv
+ 1, prefix
);
1037 die(_("invalid subcommand: %s"), argv
[1]);