Skip to content

Commit 64a99eb

Browse files
pcloudsgitster
authored andcommitted
gc: reject if another gc is running, unless --force is given
This may happen when `git gc --auto` is run automatically, then the user, to avoid wait time, switches to a new terminal, keeps working and `git gc --auto` is started again because the first gc instance has not clean up the repository. This patch tries to avoid multiple gc running, especially in --auto mode. In the worst case, gc may be delayed 12 hours if a daemon reuses the pid stored in gc.pid. kill(pid, 0) support is added to MinGW port so it should work on Windows too. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f59bebb commit 64a99eb

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

Documentation/git-gc.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-gc - Cleanup unnecessary files and optimize the local repository
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune]
12+
'git gc' [--aggressive] [--auto] [--quiet] [--prune=<date> | --no-prune] [--force]
1313

1414
DESCRIPTION
1515
-----------
@@ -71,6 +71,10 @@ automatic consolidation of packs.
7171
--quiet::
7272
Suppress all progress reports.
7373

74+
--force::
75+
Force `git gc` to run even if there may be another `git gc`
76+
instance running on this repository.
77+
7478
Configuration
7579
-------------
7680

builtin/gc.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,69 @@ static int need_to_gc(void)
167167
return 1;
168168
}
169169

170+
/* return NULL on success, else hostname running the gc */
171+
static const char *lock_repo_for_gc(int force, pid_t* ret_pid)
172+
{
173+
static struct lock_file lock;
174+
static char locking_host[128];
175+
char my_host[128];
176+
struct strbuf sb = STRBUF_INIT;
177+
struct stat st;
178+
uintmax_t pid;
179+
FILE *fp;
180+
int fd, should_exit;
181+
182+
if (gethostname(my_host, sizeof(my_host)))
183+
strcpy(my_host, "unknown");
184+
185+
fd = hold_lock_file_for_update(&lock, git_path("gc.pid"),
186+
LOCK_DIE_ON_ERROR);
187+
if (!force) {
188+
fp = fopen(git_path("gc.pid"), "r");
189+
memset(locking_host, 0, sizeof(locking_host));
190+
should_exit =
191+
fp != NULL &&
192+
!fstat(fileno(fp), &st) &&
193+
/*
194+
* 12 hour limit is very generous as gc should
195+
* never take that long. On the other hand we
196+
* don't really need a strict limit here,
197+
* running gc --auto one day late is not a big
198+
* problem. --force can be used in manual gc
199+
* after the user verifies that no gc is
200+
* running.
201+
*/
202+
time(NULL) - st.st_mtime <= 12 * 3600 &&
203+
fscanf(fp, "%"PRIuMAX" %127c", &pid, locking_host) == 2 &&
204+
/* be gentle to concurrent "gc" on remote hosts */
205+
(strcmp(locking_host, my_host) || !kill(pid, 0));
206+
if (fp != NULL)
207+
fclose(fp);
208+
if (should_exit) {
209+
if (fd >= 0)
210+
rollback_lock_file(&lock);
211+
*ret_pid = pid;
212+
return locking_host;
213+
}
214+
}
215+
216+
strbuf_addf(&sb, "%"PRIuMAX" %s",
217+
(uintmax_t) getpid(), my_host);
218+
write_in_full(fd, sb.buf, sb.len);
219+
strbuf_release(&sb);
220+
commit_lock_file(&lock);
221+
222+
return NULL;
223+
}
224+
170225
int cmd_gc(int argc, const char **argv, const char *prefix)
171226
{
172227
int aggressive = 0;
173228
int auto_gc = 0;
174229
int quiet = 0;
230+
int force = 0;
231+
const char *name;
232+
pid_t pid;
175233

176234
struct option builtin_gc_options[] = {
177235
OPT__QUIET(&quiet, N_("suppress progress reporting")),
@@ -180,6 +238,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
180238
PARSE_OPT_OPTARG, NULL, (intptr_t)prune_expire },
181239
OPT_BOOLEAN(0, "aggressive", &aggressive, N_("be more thorough (increased runtime)")),
182240
OPT_BOOLEAN(0, "auto", &auto_gc, N_("enable auto-gc mode")),
241+
OPT_BOOL(0, "force", &force, N_("force running gc even if there may be another gc running")),
183242
OPT_END()
184243
};
185244

@@ -225,6 +284,14 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
225284
} else
226285
add_repack_all_option();
227286

287+
name = lock_repo_for_gc(force, &pid);
288+
if (name) {
289+
if (auto_gc)
290+
return 0; /* be quiet on --auto */
291+
die(_("gc is already running on machine '%s' pid %"PRIuMAX" (use --force if not)"),
292+
name, (uintmax_t)pid);
293+
}
294+
228295
if (pack_refs && run_command_v_opt(pack_refs_cmd.argv, RUN_GIT_CMD))
229296
return error(FAILED_RUN, pack_refs_cmd.argv[0]);
230297

compat/mingw.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,12 @@ int mingw_kill(pid_t pid, int sig)
10861086
errno = err_win_to_posix(GetLastError());
10871087
CloseHandle(h);
10881088
return -1;
1089+
} else if (pid > 0 && sig == 0) {
1090+
HANDLE h = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
1091+
if (h) {
1092+
CloseHandle(h);
1093+
return 0;
1094+
}
10891095
}
10901096

10911097
errno = EINVAL;

0 commit comments

Comments
 (0)