Skip to content

Commit a2fa254

Browse files
committed
Merge branch 'mv/defer-gc'
* mv/defer-gc: contrib/hooks: add an example pre-auto-gc hook Documentation/hooks: add pre-auto-gc hook git-gc --auto: add pre-auto-gc hook
2 parents 3642617 + f949844 commit a2fa254

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

Documentation/hooks.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,10 @@ probably enable this hook.
276276
Both standard output and standard error output are forwarded to
277277
`git-send-pack` on the other end, so you can simply `echo` messages
278278
for the user.
279+
280+
pre-auto-gc
281+
-----------
282+
283+
This hook is invoked by `git-gc --auto`. It takes no parameter, and
284+
exiting with non-zero status from this script causes the `git-gc --auto`
285+
to abort.

builtin-gc.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,34 @@ static int too_many_packs(void)
157157
return gc_auto_pack_limit <= cnt;
158158
}
159159

160+
static int run_hook(void)
161+
{
162+
const char *argv[2];
163+
struct child_process hook;
164+
int ret;
165+
166+
argv[0] = git_path("hooks/pre-auto-gc");
167+
argv[1] = NULL;
168+
169+
if (access(argv[0], X_OK) < 0)
170+
return 0;
171+
172+
memset(&hook, 0, sizeof(hook));
173+
hook.argv = argv;
174+
hook.no_stdin = 1;
175+
hook.stdout_to_stderr = 1;
176+
177+
ret = start_command(&hook);
178+
if (ret) {
179+
warning("Could not spawn %s", argv[0]);
180+
return ret;
181+
}
182+
ret = finish_command(&hook);
183+
if (ret == -ERR_RUN_COMMAND_WAITPID_SIGNAL)
184+
warning("%s exited due to uncaught signal", argv[0]);
185+
return ret;
186+
}
187+
160188
static int need_to_gc(void)
161189
{
162190
/*
@@ -176,6 +204,9 @@ static int need_to_gc(void)
176204
append_option(argv_repack, "-A", MAX_ADD);
177205
else if (!too_many_loose_objects())
178206
return 0;
207+
208+
if (run_hook())
209+
return 0;
179210
return 1;
180211
}
181212

contrib/hooks/pre-auto-gc-battery

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to verify if you are on battery, in case you
4+
# are running Linux. Called by git-gc --auto with no arguments. The hook
5+
# should exit with non-zero status after issuing an appropriate message
6+
# if it wants to stop the auto repacking.
7+
#
8+
# This hook is stored in the contrib/hooks directory. Your distribution
9+
# may have put this somewhere else. If you want to use this hook, you
10+
# should make this script executable then link to it in the repository
11+
# you would like to use it in.
12+
#
13+
# For example, if the hook is stored in
14+
# /usr/share/git-core/contrib/hooks/pre-auto-gc-battery:
15+
#
16+
# chmod a+x pre-auto-gc-battery
17+
# cd /path/to/your/repository.git
18+
# ln -sf /usr/share/git-core/contrib/hooks/pre-auto-gc-battery \
19+
# hooks/pre-auto-gc
20+
21+
if test -x /sbin/on_ac_power && /sbin/on_ac_power
22+
then
23+
exit 0
24+
elif test "$(cat /sys/class/power_supply/AC/online 2>/dev/null)" = 1
25+
then
26+
exit 0
27+
elif grep -q 'on-line' /proc/acpi/ac_adapter/AC/state 2>/dev/null
28+
then
29+
exit 0
30+
elif grep -q '0x01$' /proc/apm 2>/dev/null
31+
then
32+
exit 0
33+
fi
34+
35+
echo "Auto packing deferred; not on AC"
36+
exit 1

0 commit comments

Comments
 (0)