@@ -1338,21 +1338,37 @@ static int write_loose_object_to_stdin(const struct object_id *oid,
13381338 return ++ (d -> count ) > d -> batch_size ;
13391339}
13401340
1341+ static const char * shared_object_dir = NULL ;
1342+
13411343static int pack_loose (struct maintenance_run_opts * opts )
13421344{
13431345 struct repository * r = the_repository ;
13441346 int result = 0 ;
13451347 struct write_loose_object_data data ;
13461348 struct child_process pack_proc = CHILD_PROCESS_INIT ;
1349+ struct odb_source * prev_source = NULL ;
1350+ const char * object_dir = r -> objects -> sources -> path ;
1351+
1352+ /* If set, use the shared object directory. */
1353+ if (shared_object_dir ) {
1354+ prev_source =
1355+ odb_set_temporary_primary_source (r -> objects ,
1356+ shared_object_dir , 0 );
1357+ object_dir = shared_object_dir ;
1358+ }
13471359
13481360 /*
13491361 * Do not start pack-objects process
13501362 * if there are no loose objects.
13511363 */
13521364 if (!for_each_loose_file_in_source (r -> objects -> sources ,
13531365 bail_on_loose ,
1354- NULL , NULL , NULL ))
1366+ NULL , NULL , NULL )) {
1367+ if (shared_object_dir )
1368+ odb_restore_primary_source (r -> objects , prev_source ,
1369+ shared_object_dir );
13551370 return 0 ;
1371+ }
13561372
13571373 pack_proc .git_cmd = 1 ;
13581374
@@ -1361,7 +1377,7 @@ static int pack_loose(struct maintenance_run_opts *opts)
13611377 strvec_push (& pack_proc .args , "--quiet" );
13621378 else
13631379 strvec_push (& pack_proc .args , "--no-quiet" );
1364- strvec_pushf (& pack_proc .args , "%s/pack/loose" , r -> objects -> sources -> path );
1380+ strvec_pushf (& pack_proc .args , "%s/pack/loose" , object_dir );
13651381
13661382 pack_proc .in = -1 ;
13671383
@@ -1373,6 +1389,9 @@ static int pack_loose(struct maintenance_run_opts *opts)
13731389
13741390 if (start_command (& pack_proc )) {
13751391 error (_ ("failed to start 'git pack-objects' process" ));
1392+ if (shared_object_dir )
1393+ odb_restore_primary_source (r -> objects , prev_source ,
1394+ shared_object_dir );
13761395 return 1 ;
13771396 }
13781397
@@ -1400,6 +1419,10 @@ static int pack_loose(struct maintenance_run_opts *opts)
14001419 result = 1 ;
14011420 }
14021421
1422+ if (shared_object_dir )
1423+ odb_restore_primary_source (r -> objects , prev_source ,
1424+ shared_object_dir );
1425+
14031426 return result ;
14041427}
14051428
@@ -1832,11 +1855,12 @@ static int task_option_parse(const struct option *opt,
18321855}
18331856
18341857static int maintenance_run (int argc , const char * * argv , const char * prefix ,
1835- struct repository * repo UNUSED )
1858+ struct repository * repo )
18361859{
18371860 struct maintenance_run_opts opts = MAINTENANCE_RUN_OPTS_INIT ;
18381861 struct string_list selected_tasks = STRING_LIST_INIT_DUP ;
18391862 struct gc_config cfg = GC_CONFIG_INIT ;
1863+ const char * tmp_obj_dir = NULL ;
18401864 struct option builtin_maintenance_run_options [] = {
18411865 OPT_BOOL (0 , "auto" , & opts .auto_flag ,
18421866 N_ ("run tasks based on the state of the repository" )),
@@ -1873,6 +1897,17 @@ static int maintenance_run(int argc, const char **argv, const char *prefix,
18731897 usage_with_options (builtin_maintenance_run_usage ,
18741898 builtin_maintenance_run_options );
18751899
1900+ /*
1901+ * To enable the VFS for Git/Scalar shared object cache, use
1902+ * the gvfs.sharedcache config option to redirect the
1903+ * maintenance to that location.
1904+ */
1905+ if (!repo_config_get_value (repo , "gvfs.sharedcache" , & tmp_obj_dir ) &&
1906+ tmp_obj_dir ) {
1907+ shared_object_dir = xstrdup (tmp_obj_dir );
1908+ setenv (DB_ENVIRONMENT , shared_object_dir , 1 );
1909+ }
1910+
18761911 ret = maintenance_run_tasks (& opts , & cfg );
18771912
18781913 string_list_clear (& selected_tasks , 0 );
0 commit comments