@@ -210,6 +210,12 @@ static int set_recommended_config(int reconfigure)
210210 return 0 ;
211211}
212212
213+ /**
214+ * Enable or disable the maintenance mode for the current repository:
215+ *
216+ * * If 'enable' is nonzero, run 'git maintenance start'.
217+ * * If 'enable' is zero, run 'git maintenance unregister --force'.
218+ */
213219static int toggle_maintenance (int enable )
214220{
215221 return run_git ("maintenance" ,
@@ -260,16 +266,25 @@ static int stop_fsmonitor_daemon(void)
260266 return 0 ;
261267}
262268
263- static int register_dir (void )
269+ /**
270+ * Register the current directory as a Scalar enlistment, and set the
271+ * recommended configuration.
272+ *
273+ * * If 'maintenance' is non-zero, then enable background maintenance.
274+ * * If 'maintenance' is zero, then leave background maintenance as it is
275+ * currently configured.
276+ */
277+ static int register_dir (int maintenance )
264278{
265279 if (add_or_remove_enlistment (1 ))
266280 return error (_ ("could not add enlistment" ));
267281
268282 if (set_recommended_config (0 ))
269283 return error (_ ("could not set recommended config" ));
270284
271- if (toggle_maintenance (1 ))
272- warning (_ ("could not turn on maintenance" ));
285+ if (maintenance &&
286+ toggle_maintenance (maintenance ))
287+ warning (_ ("could not toggle maintenance" ));
273288
274289 if (have_fsmonitor_support () && start_fsmonitor_daemon ()) {
275290 return error (_ ("could not start the FSMonitor daemon" ));
@@ -412,7 +427,7 @@ static int cmd_clone(int argc, const char **argv)
412427 const char * branch = NULL ;
413428 char * branch_to_free = NULL ;
414429 int full_clone = 0 , single_branch = 0 , show_progress = isatty (2 );
415- int src = 1 , tags = 1 ;
430+ int src = 1 , tags = 1 , maintenance = 1 ;
416431 struct option clone_options [] = {
417432 OPT_STRING ('b' , "branch" , & branch , N_ ("<branch>" ),
418433 N_ ("branch to checkout after clone" )),
@@ -425,11 +440,13 @@ static int cmd_clone(int argc, const char **argv)
425440 N_ ("create repository within 'src' directory" )),
426441 OPT_BOOL (0 , "tags" , & tags ,
427442 N_ ("specify if tags should be fetched during clone" )),
443+ OPT_BOOL (0 , "maintenance" , & maintenance ,
444+ N_ ("specify if background maintenance should be enabled" )),
428445 OPT_END (),
429446 };
430447 const char * const clone_usage [] = {
431448 N_ ("scalar clone [--single-branch] [--branch <main-branch>] [--full-clone]\n"
432- "\t[--[no-]src] [--[no-]tags] <url> [<enlistment>]" ),
449+ "\t[--[no-]src] [--[no-]tags] [--[no-]maintenance] <url> [<enlistment>]" ),
433450 NULL
434451 };
435452 const char * url ;
@@ -551,7 +568,8 @@ static int cmd_clone(int argc, const char **argv)
551568 if (res )
552569 goto cleanup ;
553570
554- res = register_dir ();
571+ /* If --no-maintenance, then skip maintenance command entirely. */
572+ res = register_dir (maintenance );
555573
556574cleanup :
557575 free (branch_to_free );
@@ -598,11 +616,14 @@ static int cmd_list(int argc, const char **argv UNUSED)
598616
599617static int cmd_register (int argc , const char * * argv )
600618{
619+ int maintenance = 1 ;
601620 struct option options [] = {
621+ OPT_BOOL (0 , "maintenance" , & maintenance ,
622+ N_ ("specify if background maintenance should be enabled" )),
602623 OPT_END (),
603624 };
604625 const char * const usage [] = {
605- N_ ("scalar register [<enlistment>]" ),
626+ N_ ("scalar register [--[no-]maintenance] [ <enlistment>]" ),
606627 NULL
607628 };
608629
@@ -611,7 +632,8 @@ static int cmd_register(int argc, const char **argv)
611632
612633 setup_enlistment_directory (argc , argv , usage , options , NULL );
613634
614- return register_dir ();
635+ /* If --no-maintenance, then leave maintenance as-is. */
636+ return register_dir (maintenance );
615637}
616638
617639static int get_scalar_repos (const char * key , const char * value ,
@@ -647,13 +669,19 @@ static int remove_deleted_enlistment(struct strbuf *path)
647669static int cmd_reconfigure (int argc , const char * * argv )
648670{
649671 int all = 0 ;
672+ const char * maintenance_str = NULL ;
673+ int maintenance = 1 ; /* Enable maintenance by default. */
674+
650675 struct option options [] = {
651676 OPT_BOOL ('a' , "all" , & all ,
652677 N_ ("reconfigure all registered enlistments" )),
678+ OPT_STRING (0 , "maintenance" , & maintenance_str ,
679+ N_ ("<mode>" ),
680+ N_ ("signal how to adjust background maintenance" )),
653681 OPT_END (),
654682 };
655683 const char * const usage [] = {
656- N_ ("scalar reconfigure [--all | <enlistment>]" ),
684+ N_ ("scalar reconfigure [--maintenance=<mode>] [-- all | <enlistment>]" ),
657685 NULL
658686 };
659687 struct string_list scalar_repos = STRING_LIST_INIT_DUP ;
@@ -673,6 +701,18 @@ static int cmd_reconfigure(int argc, const char **argv)
673701 usage_msg_opt (_ ("--all or <enlistment>, but not both" ),
674702 usage , options );
675703
704+ if (maintenance_str ) {
705+ if (!strcmp (maintenance_str , "enable" ))
706+ maintenance = 1 ;
707+ else if (!strcmp (maintenance_str , "disable" ))
708+ maintenance = 0 ;
709+ else if (!strcmp (maintenance_str , "keep" ))
710+ maintenance = -1 ;
711+ else
712+ die (_ ("unknown mode for --maintenance option: %s" ),
713+ maintenance_str );
714+ }
715+
676716 git_config (get_scalar_repos , & scalar_repos );
677717
678718 for (size_t i = 0 ; i < scalar_repos .nr ; i ++ ) {
@@ -737,7 +777,8 @@ static int cmd_reconfigure(int argc, const char **argv)
737777 the_repository = old_repo ;
738778 repo_clear (& r );
739779
740- if (toggle_maintenance (1 ) >= 0 )
780+ if (maintenance >= 0 &&
781+ toggle_maintenance (maintenance ) >= 0 )
741782 succeeded = 1 ;
742783
743784loop_end :
@@ -804,13 +845,13 @@ static int cmd_run(int argc, const char **argv)
804845 strbuf_release (& buf );
805846
806847 if (i == 0 )
807- return register_dir ();
848+ return register_dir (1 );
808849
809850 if (i > 0 )
810851 return run_git ("maintenance" , "run" ,
811852 "--task" , tasks [i ].task , NULL );
812853
813- if (register_dir ())
854+ if (register_dir (1 ))
814855 return -1 ;
815856 for (i = 1 ; tasks [i ].arg ; i ++ )
816857 if (run_git ("maintenance" , "run" ,
0 commit comments