Teach git-fetch to grab a tag at the same time as a commit
authorShawn O. Pearce <spearce@spearce.org>
Mon, 3 Mar 2008 02:35:33 +0000 (21:35 -0500)
committerJunio C Hamano <gitster@pobox.com>
Mon, 3 Mar 2008 08:05:45 +0000 (00:05 -0800)
If the situation is the following on the remote and L is the common
base between both sides:

T - tag1 S - tag2
/ /
L - A - O - O - B
\ \
origin/master master

and we have decided to fetch "master" to acquire the range L..B we
can also nab tag S at the same time during the first connection,
as we can clearly see from the refs advertised by upload-pack that
S^{} = B and master = B.

Unfortunately we still cannot nab T at the same time as we are not
able to see that T^{} will also be in the range implied by L..B.
Such computations must be performed on the remote side (not yet
supported) or on the client side as post-processing (the current
behavior).

This optimization is an extension of the previous one in that it
helps on projects which tend to publish both a new commit and a
new tag, then lay idle for a while before publishing anything else.
Most followers are able to download both the new commit and the new
tag in one connection, rather than two. git.git tends to follow
such patterns with its roughly once-daily updates from Junio.

A protocol extension and additional server side logic would be
necessary to also ensure T is grabbed on the first connection.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin-fetch.c
t/t5503-tagfollow.sh [new file with mode: 0755]
index a58efa6ece78a66cf807a80b96198d08e2e2ea0b..26c3d74b76de701a8e1a5e0c7031751599c1487f 100644 (file)
@@ -459,6 +459,17 @@ static int add_existing(const char *refname, const unsigned char *sha1,
        return 0;
 }
 
+static int will_fetch(struct ref **head, const unsigned char *sha1)
+{
+       struct ref *rm = *head;
+       while (rm) {
+               if (!hashcmp(rm->old_sha1, sha1))
+                       return 1;
+               rm = rm->next;
+       }
+       return 0;
+}
+
 static void find_non_local_tags(struct transport *transport,
                        struct ref **head,
                        struct ref ***tail)
@@ -495,7 +506,8 @@ static void find_non_local_tags(struct transport *transport,
 
                if (!path_list_has_path(&existing_refs, ref_name) &&
                    !path_list_has_path(&new_refs, ref_name) &&
-                   has_sha1_file(ref->old_sha1)) {
+                   (has_sha1_file(ref->old_sha1) ||
+                    will_fetch(head, ref->old_sha1))) {
                        path_list_insert(ref_name, &new_refs);
 
                        rm = alloc_ref(strlen(ref_name) + 1);
diff --git a/t/t5503-tagfollow.sh b/t/t5503-tagfollow.sh
new file mode 100755 (executable)
index 0000000..45ff982
--- /dev/null
@@ -0,0 +1,124 @@
+#!/bin/sh
+
+test_description='test automatic tag following'
+
+. ./test-lib.sh
+
+# End state of the repository:
+#
+#         T - tag1          S - tag2
+#        /                 /
+#   L - A ------ O ------ B
+#    \   \                 \
+#     \   C - origin/cat    \
+#      origin/master         master
+
+test_expect_success setup '
+       test_tick &&
+       echo ichi >file &&
+       git add file &&
+       git commit -m L &&
+       L=$(git rev-parse --verify HEAD) &&
+
+       (
+               mkdir cloned &&
+               cd cloned &&
+               git init-db &&
+               git remote add -f origin ..
+       ) &&
+
+       test_tick &&
+       echo A >file &&
+       git add file &&
+       git commit -m A &&
+       A=$(git rev-parse --verify HEAD)
+'
+
+U=UPLOAD_LOG
+
+cat - <<EOF >expect
+#S
+want $A
+#E
+EOF
+test_expect_success 'fetch A (new commit : 1 connection)' '
+       rm -f $U
+       (
+               cd cloned &&
+               GIT_DEBUG_SEND_PACK=3 git fetch 3>../$U &&
+               test $A = $(git rev-parse --verify origin/master)
+       ) &&
+       test -s $U &&
+       cut -d" " -f1,2 $U >actual &&
+       git diff expect actual
+'
+
+test_expect_success "create tag T on A, create C on branch cat" '
+       git tag -a -m tag1 tag1 $A &&
+       T=$(git rev-parse --verify tag1) &&
+
+       git checkout -b cat &&
+       echo C >file &&
+       git add file &&
+       git commit -m C &&
+       C=$(git rev-parse --verify HEAD) &&
+       git checkout master
+'
+
+cat - <<EOF >expect
+#S
+want $C
+want $T
+#E
+EOF
+test_expect_success 'fetch C, T (new branch, tag : 1 connection)' '
+       rm -f $U
+       (
+               cd cloned &&
+               GIT_DEBUG_SEND_PACK=3 git fetch 3>../$U &&
+               test $C = $(git rev-parse --verify origin/cat) &&
+               test $T = $(git rev-parse --verify tag1) &&
+               test $A = $(git rev-parse --verify tag1^0)
+       ) &&
+       test -s $U &&
+       cut -d" " -f1,2 $U >actual &&
+       git diff expect actual
+'
+
+test_expect_success "create commits O, B, tag S on B" '
+       test_tick &&
+       echo O >file &&
+       git add file &&
+       git commit -m O &&
+
+       test_tick &&
+       echo B >file &&
+       git add file &&
+       git commit -m B &&
+       B=$(git rev-parse --verify HEAD) &&
+
+       git tag -a -m tag2 tag2 $B &&
+       S=$(git rev-parse --verify tag2)
+'
+
+cat - <<EOF >expect
+#S
+want $B
+want $S
+#E
+EOF
+test_expect_success 'fetch B, S (commit and tag : 1 connection)' '
+       rm -f $U
+       (
+               cd cloned &&
+               GIT_DEBUG_SEND_PACK=3 git fetch 3>../$U &&
+               test $B = $(git rev-parse --verify origin/master) &&
+               test $B = $(git rev-parse --verify tag2^0) &&
+               test $S = $(git rev-parse --verify tag2)
+       ) &&
+       test -s $U &&
+       cut -d" " -f1,2 $U >actual &&
+       git diff expect actual
+'
+
+test_done