Skip to content

Commit 11cae06

Browse files
committed
upload-pack: feed "kind [clone|fetch]" to post-upload-pack hook
A request to clone the repository does not give any "have" but asks for all the refs we offer with "want". When a request does not ask to clone the repository fully, but asks to fetch some refs into an empty repository, it will not give any "have" but its "want" won't ask for all the refs we offer. If we suppose (and I would say this is a rather big if) that it makes sense to distinguish these two cases, a hook cannot reliably do this alone. The hook can detect lack of "have" and bunch of "want", but there is no direct way to tell if the other end asked for all refs we offered, or merely most of them. Between the time we talked with the other end and the time the hook got called, we may have acquired more refs or lost some refs in the repository by concurrent operations. Given that we plan to introduce selective advertisement of refs with a protocol extension, it would become even more difficult for hooks to guess between these two cases. This adds "kind [clone|fetch]" to hook's input, as a stable interface to allow the hooks to tell these cases apart. Signed-off-by: Junio C Hamano <[email protected]>
1 parent a8563ec commit 11cae06

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

Documentation/githooks.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ time float::
332332
size decimal::
333333
Size of the resulting packfile in bytes.
334334

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+
335339
pre-auto-gc
336340
-----------
337341

t/t5501-post-upload-pack.sh

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ test_expect_success initial '
2929
) &&
3030
want=$(sed -n "s/^want //p" "$LOGFILE") &&
3131
test "$want" = "$(git rev-parse --verify B)" &&
32-
! grep "^have " "$LOGFILE"
32+
! grep "^have " "$LOGFILE" &&
33+
kind=$(sed -n "s/^kind //p" "$LOGFILE") &&
34+
test "$kind" = fetch
3335
'
3436

3537
test_expect_success second '
@@ -43,7 +45,25 @@ test_expect_success second '
4345
want=$(sed -n "s/^want //p" "$LOGFILE") &&
4446
test "$want" = "$(git rev-parse --verify C)" &&
4547
have=$(sed -n "s/^have //p" "$LOGFILE") &&
46-
test "$have" = "$(git rev-parse --verify B)"
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
4767
'
4868

4969
test_done

upload-pack.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ static int run_post_upload_pack_hook(size_t total, struct timeval *tv)
187187
(long)tv->tv_sec, (long)tv->tv_usec);
188188
if (!err)
189189
err |= feed_msg_to_hook(proc.in, "size %ld\n", (long)total);
190+
if (!err)
191+
err |= feed_msg_to_hook(proc.in, "kind %s\n",
192+
(nr_our_refs == want_obj.nr && !have_obj.nr)
193+
? "clone" : "fetch");
190194
if (close(proc.in))
191195
err = 1;
192196
if (finish_command(&proc))

0 commit comments

Comments
 (0)