Skip to content

Commit e4d1afb

Browse files
committed
Merge branch 'jc/upload-pack-hook'
* jc/upload-pack-hook: upload-pack: feed "kind [clone|fetch]" to post-upload-pack hook upload-pack: add a trigger for post-upload-pack hook
2 parents 54f0bdc + 11cae06 commit e4d1afb

File tree

4 files changed

+172
-2
lines changed

4 files changed

+172
-2
lines changed

Documentation/git-upload-pack.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ The UI for the protocol is on the 'git-fetch-pack' side, and the
2020
program pair is meant to be used to pull updates from a remote
2121
repository. For push operations, see 'git-send-pack'.
2222

23+
After finishing the operation successfully, `post-upload-pack`
24+
hook is called (see linkgit:githooks[5]).
2325

2426
OPTIONS
2527
-------

Documentation/githooks.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,35 @@ Both standard output and standard error output are forwarded to
307307
'git-send-pack' on the other end, so you can simply `echo` messages
308308
for the user.
309309

310+
post-upload-pack
311+
----------------
312+
313+
After upload-pack successfully finishes its operation, this hook is called
314+
for logging purposes.
315+
316+
The hook is passed various pieces of information, one per line, from its
317+
standard input. Currently the following items can be fed to the hook, but
318+
more types of information may be added in the future:
319+
320+
want SHA-1::
321+
40-byte hexadecimal object name the client asked to include in the
322+
resulting pack. Can occur one or more times in the input.
323+
324+
have SHA-1::
325+
40-byte hexadecimal object name the client asked to exclude from
326+
the resulting pack, claiming to have them already. Can occur zero
327+
or more times in the input.
328+
329+
time float::
330+
Number of seconds spent for creating the packfile.
331+
332+
size decimal::
333+
Size of the resulting packfile in bytes.
334+
335+
kind string:
336+
Either "clone" (when the client did not give us any "have", and asked
337+
for all our refs with "want"), or "fetch" (otherwise).
338+
310339
pre-auto-gc
311340
-----------
312341

t/t5501-post-upload-pack.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/sh
2+
3+
test_description='post upload-hook'
4+
5+
. ./test-lib.sh
6+
7+
LOGFILE=".git/post-upload-pack-log"
8+
9+
test_expect_success setup '
10+
test_commit A &&
11+
test_commit B &&
12+
git reset --hard A &&
13+
test_commit C &&
14+
git branch prev B &&
15+
mkdir -p .git/hooks &&
16+
{
17+
echo "#!$SHELL_PATH" &&
18+
echo "cat >post-upload-pack-log"
19+
} >".git/hooks/post-upload-pack" &&
20+
chmod +x .git/hooks/post-upload-pack
21+
'
22+
23+
test_expect_success initial '
24+
rm -fr sub &&
25+
git init sub &&
26+
(
27+
cd sub &&
28+
git fetch --no-tags .. prev
29+
) &&
30+
want=$(sed -n "s/^want //p" "$LOGFILE") &&
31+
test "$want" = "$(git rev-parse --verify B)" &&
32+
! grep "^have " "$LOGFILE" &&
33+
kind=$(sed -n "s/^kind //p" "$LOGFILE") &&
34+
test "$kind" = fetch
35+
'
36+
37+
test_expect_success second '
38+
rm -fr sub &&
39+
git init sub &&
40+
(
41+
cd sub &&
42+
git fetch --no-tags .. prev:refs/remotes/prev &&
43+
git fetch --no-tags .. master
44+
) &&
45+
want=$(sed -n "s/^want //p" "$LOGFILE") &&
46+
test "$want" = "$(git rev-parse --verify C)" &&
47+
have=$(sed -n "s/^have //p" "$LOGFILE") &&
48+
test "$have" = "$(git rev-parse --verify B)" &&
49+
kind=$(sed -n "s/^kind //p" "$LOGFILE") &&
50+
test "$kind" = fetch
51+
'
52+
53+
test_expect_success all '
54+
rm -fr sub &&
55+
HERE=$(pwd) &&
56+
git init sub &&
57+
(
58+
cd sub &&
59+
git clone "file://$HERE/.git" new
60+
) &&
61+
sed -n "s/^want //p" "$LOGFILE" | sort >actual &&
62+
git rev-parse A B C | sort >expect &&
63+
test_cmp expect actual &&
64+
! grep "^have " "$LOGFILE" &&
65+
kind=$(sed -n "s/^kind //p" "$LOGFILE") &&
66+
test "$kind" = clone
67+
'
68+
69+
test_done

upload-pack.c

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,79 @@ static int do_rev_list(int fd, void *create_full_pack)
146146
return 0;
147147
}
148148

149+
static int feed_msg_to_hook(int fd, const char *fmt, ...)
150+
{
151+
int cnt;
152+
char buf[1024];
153+
va_list params;
154+
155+
va_start(params, fmt);
156+
cnt = vsprintf(buf, fmt, params);
157+
va_end(params);
158+
return write_in_full(fd, buf, cnt) != cnt;
159+
}
160+
161+
static int feed_obj_to_hook(const char *label, struct object_array *oa, int i, int fd)
162+
{
163+
return feed_msg_to_hook(fd, "%s %s\n", label,
164+
sha1_to_hex(oa->objects[i].item->sha1));
165+
}
166+
167+
static int run_post_upload_pack_hook(size_t total, struct timeval *tv)
168+
{
169+
const char *argv[2];
170+
struct child_process proc;
171+
int err, i;
172+
173+
argv[0] = "hooks/post-upload-pack";
174+
argv[1] = NULL;
175+
176+
if (access(argv[0], X_OK) < 0)
177+
return 0;
178+
179+
memset(&proc, 0, sizeof(proc));
180+
proc.argv = argv;
181+
proc.in = -1;
182+
proc.stdout_to_stderr = 1;
183+
err = start_command(&proc);
184+
if (err)
185+
return err;
186+
for (i = 0; !err && i < want_obj.nr; i++)
187+
err |= feed_obj_to_hook("want", &want_obj, i, proc.in);
188+
for (i = 0; !err && i < have_obj.nr; i++)
189+
err |= feed_obj_to_hook("have", &have_obj, i, proc.in);
190+
if (!err)
191+
err |= feed_msg_to_hook(proc.in, "time %ld.%06ld\n",
192+
(long)tv->tv_sec, (long)tv->tv_usec);
193+
if (!err)
194+
err |= feed_msg_to_hook(proc.in, "size %ld\n", (long)total);
195+
if (!err)
196+
err |= feed_msg_to_hook(proc.in, "kind %s\n",
197+
(nr_our_refs == want_obj.nr && !have_obj.nr)
198+
? "clone" : "fetch");
199+
if (close(proc.in))
200+
err = 1;
201+
if (finish_command(&proc))
202+
err = 1;
203+
return err;
204+
}
205+
149206
static void create_pack_file(void)
150207
{
208+
struct timeval start_tv, tv;
151209
struct async rev_list;
152210
struct child_process pack_objects;
153211
int create_full_pack = (nr_our_refs == want_obj.nr && !have_obj.nr);
154212
char data[8193], progress[128];
155213
char abort_msg[] = "aborting due to possible repository "
156214
"corruption on the remote side.";
157215
int buffered = -1;
158-
ssize_t sz;
216+
ssize_t sz, total_sz;
159217
const char *argv[10];
160218
int arg = 0;
161219

220+
gettimeofday(&start_tv, NULL);
221+
total_sz = 0;
162222
if (shallow_nr) {
163223
rev_list.proc = do_rev_list;
164224
rev_list.data = 0;
@@ -267,7 +327,7 @@ static void create_pack_file(void)
267327
sz = xread(pack_objects.out, cp,
268328
sizeof(data) - outsz);
269329
if (0 < sz)
270-
;
330+
total_sz += sz;
271331
else if (sz == 0) {
272332
close(pack_objects.out);
273333
pack_objects.out = -1;
@@ -319,6 +379,16 @@ static void create_pack_file(void)
319379
}
320380
if (use_sideband)
321381
packet_flush(1);
382+
383+
gettimeofday(&tv, NULL);
384+
tv.tv_sec -= start_tv.tv_sec;
385+
if (tv.tv_usec < start_tv.tv_usec) {
386+
tv.tv_sec--;
387+
tv.tv_usec += 1000000;
388+
}
389+
tv.tv_usec -= start_tv.tv_usec;
390+
if (run_post_upload_pack_hook(total_sz, &tv))
391+
warning("post-upload-hook failed");
322392
return;
323393

324394
fail:

0 commit comments

Comments
 (0)