Skip to content

Commit 4c4ac4d

Browse files
committed
Merge branch 'nd/daemonize-gc'
Allow running "gc --auto" in the background. * nd/daemonize-gc: gc: config option for running --auto in background daemon: move daemonize() to libgit.a
2 parents 6376463 + 9f673f9 commit 4c4ac4d

File tree

6 files changed

+52
-31
lines changed

6 files changed

+52
-31
lines changed

Documentation/config.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,10 @@ gc.autopacklimit::
11751175
--auto` consolidates them into one larger pack. The
11761176
default value is 50. Setting this to 0 disables it.
11771177

1178+
gc.autodetach::
1179+
Make `git gc --auto` return immediately andrun in background
1180+
if the system supports it. Default is true.
1181+
11781182
gc.packrefs::
11791183
Running `git pack-refs` in a repository renders it
11801184
unclonable by Git versions prior to 1.5.1.2 over dumb

builtin/gc.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static int pack_refs = 1;
2929
static int aggressive_window = 250;
3030
static int gc_auto_threshold = 6700;
3131
static int gc_auto_pack_limit = 50;
32+
static int detach_auto = 1;
3233
static const char *prune_expire = "2.weeks.ago";
3334

3435
static struct argv_array pack_refs_cmd = ARGV_ARRAY_INIT;
@@ -73,6 +74,10 @@ static int gc_config(const char *var, const char *value, void *cb)
7374
gc_auto_pack_limit = git_config_int(var, value);
7475
return 0;
7576
}
77+
if (!strcmp(var, "gc.autodetach")) {
78+
detach_auto = git_config_bool(var, value);
79+
return 0;
80+
}
7681
if (!strcmp(var, "gc.pruneexpire")) {
7782
if (value && strcmp(value, "now")) {
7883
unsigned long now = approxidate("now");
@@ -302,11 +307,19 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
302307
*/
303308
if (!need_to_gc())
304309
return 0;
305-
if (!quiet)
306-
fprintf(stderr,
307-
_("Auto packing the repository for optimum performance. You may also\n"
308-
"run \"git gc\" manually. See "
309-
"\"git help gc\" for more information.\n"));
310+
if (!quiet) {
311+
if (detach_auto)
312+
fprintf(stderr, _("Auto packing the repository in background for optimum performance.\n"));
313+
else
314+
fprintf(stderr, _("Auto packing the repository for optimum performance.\n"));
315+
fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
316+
}
317+
if (detach_auto)
318+
/*
319+
* failure to daemonize is ok, we'll continue
320+
* in foreground
321+
*/
322+
daemonize();
310323
} else
311324
add_repack_all_option();
312325

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ extern int set_git_dir_init(const char *git_dir, const char *real_git_dir, int);
433433
extern int init_db(const char *template_dir, unsigned int flags);
434434

435435
extern void sanitize_stdfds(void);
436+
extern int daemonize(void);
436437

437438
#define alloc_nr(x) (((x)+16)*3/2)
438439

daemon.c

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,11 +1056,6 @@ static void drop_privileges(struct credentials *cred)
10561056
/* nothing */
10571057
}
10581058

1059-
static void daemonize(void)
1060-
{
1061-
die("--detach not supported on this platform");
1062-
}
1063-
10641059
static struct credentials *prepare_credentials(const char *user_name,
10651060
const char *group_name)
10661061
{
@@ -1102,24 +1097,6 @@ static struct credentials *prepare_credentials(const char *user_name,
11021097

11031098
return &c;
11041099
}
1105-
1106-
static void daemonize(void)
1107-
{
1108-
switch (fork()) {
1109-
case 0:
1110-
break;
1111-
case -1:
1112-
die_errno("fork failed");
1113-
default:
1114-
exit(0);
1115-
}
1116-
if (setsid() == -1)
1117-
die_errno("setsid failed");
1118-
close(0);
1119-
close(1);
1120-
close(2);
1121-
sanitize_stdfds();
1122-
}
11231100
#endif
11241101

11251102
static void store_pid(const char *path)
@@ -1333,9 +1310,10 @@ int main(int argc, char **argv)
13331310
if (inetd_mode || serve_mode)
13341311
return execute();
13351312

1336-
if (detach)
1337-
daemonize();
1338-
else
1313+
if (detach) {
1314+
if (daemonize())
1315+
die("--detach not supported on this platform");
1316+
} else
13391317
sanitize_stdfds();
13401318

13411319
if (pid_file)

setup.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,3 +841,27 @@ void sanitize_stdfds(void)
841841
if (fd > 2)
842842
close(fd);
843843
}
844+
845+
int daemonize(void)
846+
{
847+
#ifdef NO_POSIX_GOODIES
848+
errno = ENOSYS;
849+
return -1;
850+
#else
851+
switch (fork()) {
852+
case 0:
853+
break;
854+
case -1:
855+
die_errno("fork failed");
856+
default:
857+
exit(0);
858+
}
859+
if (setsid() == -1)
860+
die_errno("setsid failed");
861+
close(0);
862+
close(1);
863+
close(2);
864+
sanitize_stdfds();
865+
return 0;
866+
#endif
867+
}

t/t5400-send-pack.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ test_expect_success 'receive-pack runs auto-gc in remote repo' '
164164
# Set the child to auto-pack if more than one pack exists
165165
cd child &&
166166
git config gc.autopacklimit 1 &&
167+
git config gc.autodetach false &&
167168
git branch test_auto_gc &&
168169
# And create a file that follows the temporary object naming
169170
# convention for the auto-gc to remove

0 commit comments

Comments
 (0)