pack-refs: add fully-peeled trait

Older versions of pack-refs did not write peel lines for refs outside of refs/tags. This meant that on reading the pack-refs file, we might set the REF_KNOWS_PEELED flag for such a ref, even though we do not know anything about its peeled value. The previous commit updated the writer to always peel, no matter what the ref is. That means that packed-refs files written by newer versions of git are fine to be read by both old and new versions of git. However, we still have the problem of reading packed-refs files written by older versions of git, or by other implementations which have not yet learned the same trick. The simplest fix would be to always unset the REF_KNOWS_PEELED flag for refs outside of refs/tags that do not have a peel line (if it has a peel line, we know it is valid, but we cannot assume a missing peel line means anything). But that loses an important optimization, as upload-pack should not need to load the object pointed to by refs/heads/foo to determine that it is not a tag. Instead, we add a "fully-peeled" trait to the packed-refs file. If it is set, we know that we can trust a missing peel line to mean that a ref cannot be peeled. Otherwise, we fall back to assuming nothing. [commit message and tests by Jeff King <peff@peff.net>] Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Mar 18, 2013 at 07:37 UTC c29c46fa2e21e608ce2e603649af5bf38e7969c2
3 files changed +67 -6
pack-refs.c
+1 -1
@@ -128,7 +128,7 @@ int pack_refs(unsigned int flags)
128 die_errno("unable to create ref-pack file structure");
129
130 /* perhaps other traits later as well */
131 - fprintf(cbdata.refs_file, "# pack-refs with: peeled \n");
131 + fprintf(cbdata.refs_file, "# pack-refs with: peeled fully-peeled \n");
132
133 for_each_ref(handle_one_ref, &cbdata);
134 if (ferror(cbdata.refs_file))
refs.c
+44 -5
@@ -804,11 +804,38 @@ static const char *parse_ref_line(char *line, unsigned char *sha1)
804 return line;
805 }
806
807 +/*
808 + * Read f, which is a packed-refs file, into dir.
809 + *
810 + * A comment line of the form "# pack-refs with: " may contain zero or
811 + * more traits. We interpret the traits as follows:
812 + *
813 + * No traits:
814 + *
815 + * Probably no references are peeled. But if the file contains a
816 + * peeled value for a reference, we will use it.
817 + *
818 + * peeled:
819 + *
820 + * References under "refs/tags/", if they *can* be peeled, *are*
821 + * peeled in this file. References outside of "refs/tags/" are
822 + * probably not peeled even if they could have been, but if we find
823 + * a peeled value for such a reference we will use it.
824 + *
825 + * fully-peeled:
826 + *
827 + * All references in the file that can be peeled are peeled.
828 + * Inversely (and this is more important), any references in the
829 + * file for which no peeled value is recorded is not peelable. This
830 + * trait should typically be written alongside "peeled" for
831 + * compatibility with older clients, but we do not require it
832 + * (i.e., "peeled" is a no-op if "fully-peeled" is set).
833 + */
834 static void read_packed_refs(FILE *f, struct ref_dir *dir)
835 {
836 struct ref_entry *last = NULL;
837 char refline[PATH_MAX];
811 - int flag = REF_ISPACKED;
838 + enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
839
840 while (fgets(refline, sizeof(refline), f)) {
841 unsigned char sha1[20];
@@ -817,15 +844,20 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
844
845 if (!strncmp(refline, header, sizeof(header)-1)) {
846 const char *traits = refline + sizeof(header) - 1;
820 - if (strstr(traits, " peeled "))
821 - flag |= REF_KNOWS_PEELED;
847 + if (strstr(traits, " fully-peeled "))
848 + peeled = PEELED_FULLY;
849 + else if (strstr(traits, " peeled "))
850 + peeled = PEELED_TAGS;
851 /* perhaps other traits later as well */
852 continue;
853 }
854
855 refname = parse_ref_line(refline, sha1);
856 if (refname) {
828 - last = create_ref_entry(refname, sha1, flag, 1);
857 + last = create_ref_entry(refname, sha1, REF_ISPACKED, 1);
858 + if (peeled == PEELED_FULLY ||
859 + (peeled == PEELED_TAGS && !prefixcmp(refname, "refs/tags/")))
860 + last->flag |= REF_KNOWS_PEELED;
861 add_ref(dir, last);
862 continue;
863 }
@@ -833,8 +865,15 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
865 refline[0] == '^' &&
866 strlen(refline) == 42 &&
867 refline[41] == '\n' &&
836 - !get_sha1_hex(refline + 1, sha1))
868 + !get_sha1_hex(refline + 1, sha1)) {
869 hashcpy(last->u.value.peeled, sha1);
870 + /*
871 + * Regardless of what the file header said,
872 + * we definitely know the value of *this*
873 + * reference:
874 + */
875 + last->flag |= REF_KNOWS_PEELED;
876 + }
877 }
878 }
879
t/t3211-peel-ref.sh
+22
@@ -39,4 +39,26 @@ test_expect_success 'refs are peeled outside of refs/tags (packed)' '
39 test_cmp expect actual
40 '
41
42 +test_expect_success 'create old-style pack-refs without fully-peeled' '
43 + # Git no longer writes without fully-peeled, so we just write our own
44 + # from scratch; we could also munge the existing file to remove the
45 + # fully-peeled bits, but that seems even more prone to failure,
46 + # especially if the format ever changes again. At least this way we
47 + # know we are emulating exactly what an older git would have written.
48 + {
49 + echo "# pack-refs with: peeled " &&
50 + print_ref "refs/heads/master" &&
51 + print_ref "refs/outside/foo" &&
52 + print_ref "refs/tags/base" &&
53 + print_ref "refs/tags/foo" &&
54 + echo "^$(git rev-parse "refs/tags/foo^{}")"
55 + } >tmp &&
56 + mv tmp .git/packed-refs
57 +'
58 +
59 +test_expect_success 'refs are peeled outside of refs/tags (old packed)' '
60 + git show-ref -d >actual &&
61 + test_cmp expect actual
62 +'
63 +
64 test_done