Skip to content

Commit f1ad05f

Browse files
committed
Merge branch 'jk/fully-peeled-packed-ref' into maint-1.8.1
* jk/fully-peeled-packed-ref: pack-refs: add fully-peeled trait pack-refs: write peeled entry for non-tags use parse_object_or_die instead of die("bad object") avoid segfaults on parse_object failure
2 parents 8f780ca + c29c46f commit f1ad05f

File tree

9 files changed

+145
-27
lines changed

9 files changed

+145
-27
lines changed

builtin/grep.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -820,9 +820,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
820820
unsigned char sha1[20];
821821
/* Is it a rev? */
822822
if (!get_sha1(arg, sha1)) {
823-
struct object *object = parse_object(sha1);
824-
if (!object)
825-
die(_("bad object %s"), arg);
823+
struct object *object = parse_object_or_die(sha1, arg);
826824
add_object_array(object, arg, &list);
827825
continue;
828826
}

builtin/prune.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
149149
const char *name = *argv++;
150150

151151
if (!get_sha1(name, sha1)) {
152-
struct object *object = parse_object(sha1);
153-
if (!object)
154-
die("bad object: %s", name);
152+
struct object *object = parse_object_or_die(sha1, name);
155153
add_pending_object(&revs, object, "");
156154
}
157155
else

bundle.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,12 @@ int create_bundle(struct bundle_header *header, const char *path,
279279
if (buf.len > 0 && buf.buf[0] == '-') {
280280
write_or_die(bundle_fd, buf.buf, buf.len);
281281
if (!get_sha1_hex(buf.buf + 1, sha1)) {
282-
struct object *object = parse_object(sha1);
282+
struct object *object = parse_object_or_die(sha1, buf.buf);
283283
object->flags |= UNINTERESTING;
284284
add_pending_object(&revs, object, xstrdup(buf.buf));
285285
}
286286
} else if (!get_sha1_hex(buf.buf, sha1)) {
287-
struct object *object = parse_object(sha1);
287+
struct object *object = parse_object_or_die(sha1, buf.buf);
288288
object->flags |= SHOWN;
289289
}
290290
}
@@ -361,7 +361,7 @@ int create_bundle(struct bundle_header *header, const char *path,
361361
* end up triggering "empty bundle"
362362
* error.
363363
*/
364-
obj = parse_object(sha1);
364+
obj = parse_object_or_die(sha1, e->name);
365365
obj->flags |= SHOWN;
366366
add_pending_object(&revs, obj, e->name);
367367
}

object.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,16 @@ struct object *parse_object_buffer(const unsigned char *sha1, enum object_type t
185185
return obj;
186186
}
187187

188+
struct object *parse_object_or_die(const unsigned char *sha1,
189+
const char *name)
190+
{
191+
struct object *o = parse_object(sha1);
192+
if (o)
193+
return o;
194+
195+
die(_("unable to parse object: %s"), name ? name : sha1_to_hex(sha1));
196+
}
197+
188198
struct object *parse_object(const unsigned char *sha1)
189199
{
190200
unsigned long size;

object.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,20 @@ struct object *lookup_object(const unsigned char *sha1);
5454

5555
extern void *create_object(const unsigned char *sha1, int type, void *obj);
5656

57-
/** Returns the object, having parsed it to find out what it is. **/
57+
/*
58+
* Returns the object, having parsed it to find out what it is.
59+
*
60+
* Returns NULL if the object is missing or corrupt.
61+
*/
5862
struct object *parse_object(const unsigned char *sha1);
5963

64+
/*
65+
* Like parse_object, but will die() instead of returning NULL. If the
66+
* "name" parameter is not NULL, it is included in the error message
67+
* (otherwise, the sha1 hex is given).
68+
*/
69+
struct object *parse_object_or_die(const unsigned char *sha1, const char *name);
70+
6071
/* Given the result of read_sha1_file(), returns the object after
6172
* parsing it. eaten_p indicates if the object has a borrowed copy
6273
* of buffer and the caller should not free() it.

pack-refs.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ static int handle_one_ref(const char *path, const unsigned char *sha1,
2727
int flags, void *cb_data)
2828
{
2929
struct pack_refs_cb_data *cb = cb_data;
30+
struct object *o;
3031
int is_tag_ref;
3132

3233
/* Do not pack the symbolic refs */
@@ -39,14 +40,13 @@ static int handle_one_ref(const char *path, const unsigned char *sha1,
3940
return 0;
4041

4142
fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
42-
if (is_tag_ref) {
43-
struct object *o = parse_object(sha1);
44-
if (o->type == OBJ_TAG) {
45-
o = deref_tag(o, path, 0);
46-
if (o)
47-
fprintf(cb->refs_file, "^%s\n",
48-
sha1_to_hex(o->sha1));
49-
}
43+
44+
o = parse_object_or_die(sha1, path);
45+
if (o->type == OBJ_TAG) {
46+
o = deref_tag(o, path, 0);
47+
if (o)
48+
fprintf(cb->refs_file, "^%s\n",
49+
sha1_to_hex(o->sha1));
5050
}
5151

5252
if ((cb->flags & PACK_REFS_PRUNE) && !do_not_prune(flags)) {
@@ -128,7 +128,7 @@ int pack_refs(unsigned int flags)
128128
die_errno("unable to create ref-pack file structure");
129129

130130
/* 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");
132132

133133
for_each_ref(handle_one_ref, &cbdata);
134134
if (ferror(cbdata.refs_file))

reachable.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,9 @@ static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
152152

153153
static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
154154
{
155-
struct object *object = parse_object(sha1);
155+
struct object *object = parse_object_or_die(sha1, path);
156156
struct rev_info *revs = (struct rev_info *)cb_data;
157157

158-
if (!object)
159-
die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
160158
add_pending_object(revs, object, "");
161159

162160
return 0;

refs.c

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -804,11 +804,38 @@ static const char *parse_ref_line(char *line, unsigned char *sha1)
804804
return line;
805805
}
806806

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+
*/
807834
static void read_packed_refs(FILE *f, struct ref_dir *dir)
808835
{
809836
struct ref_entry *last = NULL;
810837
char refline[PATH_MAX];
811-
int flag = REF_ISPACKED;
838+
enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
812839

813840
while (fgets(refline, sizeof(refline), f)) {
814841
unsigned char sha1[20];
@@ -817,24 +844,36 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
817844

818845
if (!strncmp(refline, header, sizeof(header)-1)) {
819846
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;
822851
/* perhaps other traits later as well */
823852
continue;
824853
}
825854

826855
refname = parse_ref_line(refline, sha1);
827856
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;
829861
add_ref(dir, last);
830862
continue;
831863
}
832864
if (last &&
833865
refline[0] == '^' &&
834866
strlen(refline) == 42 &&
835867
refline[41] == '\n' &&
836-
!get_sha1_hex(refline + 1, sha1))
868+
!get_sha1_hex(refline + 1, sha1)) {
837869
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+
}
838877
}
839878
}
840879

t/t3211-peel-ref.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/sh
2+
3+
test_description='tests for the peel_ref optimization of packed-refs'
4+
. ./test-lib.sh
5+
6+
test_expect_success 'create annotated tag in refs/tags' '
7+
test_commit base &&
8+
git tag -m annotated foo
9+
'
10+
11+
test_expect_success 'create annotated tag outside of refs/tags' '
12+
git update-ref refs/outside/foo refs/tags/foo
13+
'
14+
15+
# This matches show-ref's output
16+
print_ref() {
17+
echo "$(git rev-parse "$1") $1"
18+
}
19+
20+
test_expect_success 'set up expected show-ref output' '
21+
{
22+
print_ref "refs/heads/master" &&
23+
print_ref "refs/outside/foo" &&
24+
print_ref "refs/outside/foo^{}" &&
25+
print_ref "refs/tags/base" &&
26+
print_ref "refs/tags/foo" &&
27+
print_ref "refs/tags/foo^{}"
28+
} >expect
29+
'
30+
31+
test_expect_success 'refs are peeled outside of refs/tags (loose)' '
32+
git show-ref -d >actual &&
33+
test_cmp expect actual
34+
'
35+
36+
test_expect_success 'refs are peeled outside of refs/tags (packed)' '
37+
git pack-refs --all &&
38+
git show-ref -d >actual &&
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

0 commit comments

Comments
 (0)