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
This commit is contained in:
commit
f1ad05f3a5
@ -820,9 +820,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
|
|||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
/* Is it a rev? */
|
/* Is it a rev? */
|
||||||
if (!get_sha1(arg, sha1)) {
|
if (!get_sha1(arg, sha1)) {
|
||||||
struct object *object = parse_object(sha1);
|
struct object *object = parse_object_or_die(sha1, arg);
|
||||||
if (!object)
|
|
||||||
die(_("bad object %s"), arg);
|
|
||||||
add_object_array(object, arg, &list);
|
add_object_array(object, arg, &list);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -149,9 +149,7 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
|
|||||||
const char *name = *argv++;
|
const char *name = *argv++;
|
||||||
|
|
||||||
if (!get_sha1(name, sha1)) {
|
if (!get_sha1(name, sha1)) {
|
||||||
struct object *object = parse_object(sha1);
|
struct object *object = parse_object_or_die(sha1, name);
|
||||||
if (!object)
|
|
||||||
die("bad object: %s", name);
|
|
||||||
add_pending_object(&revs, object, "");
|
add_pending_object(&revs, object, "");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
6
bundle.c
6
bundle.c
@ -279,12 +279,12 @@ int create_bundle(struct bundle_header *header, const char *path,
|
|||||||
if (buf.len > 0 && buf.buf[0] == '-') {
|
if (buf.len > 0 && buf.buf[0] == '-') {
|
||||||
write_or_die(bundle_fd, buf.buf, buf.len);
|
write_or_die(bundle_fd, buf.buf, buf.len);
|
||||||
if (!get_sha1_hex(buf.buf + 1, sha1)) {
|
if (!get_sha1_hex(buf.buf + 1, sha1)) {
|
||||||
struct object *object = parse_object(sha1);
|
struct object *object = parse_object_or_die(sha1, buf.buf);
|
||||||
object->flags |= UNINTERESTING;
|
object->flags |= UNINTERESTING;
|
||||||
add_pending_object(&revs, object, xstrdup(buf.buf));
|
add_pending_object(&revs, object, xstrdup(buf.buf));
|
||||||
}
|
}
|
||||||
} else if (!get_sha1_hex(buf.buf, sha1)) {
|
} else if (!get_sha1_hex(buf.buf, sha1)) {
|
||||||
struct object *object = parse_object(sha1);
|
struct object *object = parse_object_or_die(sha1, buf.buf);
|
||||||
object->flags |= SHOWN;
|
object->flags |= SHOWN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -361,7 +361,7 @@ int create_bundle(struct bundle_header *header, const char *path,
|
|||||||
* end up triggering "empty bundle"
|
* end up triggering "empty bundle"
|
||||||
* error.
|
* error.
|
||||||
*/
|
*/
|
||||||
obj = parse_object(sha1);
|
obj = parse_object_or_die(sha1, e->name);
|
||||||
obj->flags |= SHOWN;
|
obj->flags |= SHOWN;
|
||||||
add_pending_object(&revs, obj, e->name);
|
add_pending_object(&revs, obj, e->name);
|
||||||
}
|
}
|
||||||
|
10
object.c
10
object.c
@ -185,6 +185,16 @@ struct object *parse_object_buffer(const unsigned char *sha1, enum object_type t
|
|||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct object *parse_object_or_die(const unsigned char *sha1,
|
||||||
|
const char *name)
|
||||||
|
{
|
||||||
|
struct object *o = parse_object(sha1);
|
||||||
|
if (o)
|
||||||
|
return o;
|
||||||
|
|
||||||
|
die(_("unable to parse object: %s"), name ? name : sha1_to_hex(sha1));
|
||||||
|
}
|
||||||
|
|
||||||
struct object *parse_object(const unsigned char *sha1)
|
struct object *parse_object(const unsigned char *sha1)
|
||||||
{
|
{
|
||||||
unsigned long size;
|
unsigned long size;
|
||||||
|
13
object.h
13
object.h
@ -54,9 +54,20 @@ struct object *lookup_object(const unsigned char *sha1);
|
|||||||
|
|
||||||
extern void *create_object(const unsigned char *sha1, int type, void *obj);
|
extern void *create_object(const unsigned char *sha1, int type, void *obj);
|
||||||
|
|
||||||
/** Returns the object, having parsed it to find out what it is. **/
|
/*
|
||||||
|
* Returns the object, having parsed it to find out what it is.
|
||||||
|
*
|
||||||
|
* Returns NULL if the object is missing or corrupt.
|
||||||
|
*/
|
||||||
struct object *parse_object(const unsigned char *sha1);
|
struct object *parse_object(const unsigned char *sha1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Like parse_object, but will die() instead of returning NULL. If the
|
||||||
|
* "name" parameter is not NULL, it is included in the error message
|
||||||
|
* (otherwise, the sha1 hex is given).
|
||||||
|
*/
|
||||||
|
struct object *parse_object_or_die(const unsigned char *sha1, const char *name);
|
||||||
|
|
||||||
/* Given the result of read_sha1_file(), returns the object after
|
/* Given the result of read_sha1_file(), returns the object after
|
||||||
* parsing it. eaten_p indicates if the object has a borrowed copy
|
* parsing it. eaten_p indicates if the object has a borrowed copy
|
||||||
* of buffer and the caller should not free() it.
|
* of buffer and the caller should not free() it.
|
||||||
|
@ -27,6 +27,7 @@ static int handle_one_ref(const char *path, const unsigned char *sha1,
|
|||||||
int flags, void *cb_data)
|
int flags, void *cb_data)
|
||||||
{
|
{
|
||||||
struct pack_refs_cb_data *cb = cb_data;
|
struct pack_refs_cb_data *cb = cb_data;
|
||||||
|
struct object *o;
|
||||||
int is_tag_ref;
|
int is_tag_ref;
|
||||||
|
|
||||||
/* Do not pack the symbolic refs */
|
/* Do not pack the symbolic refs */
|
||||||
@ -39,15 +40,14 @@ static int handle_one_ref(const char *path, const unsigned char *sha1,
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
|
fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
|
||||||
if (is_tag_ref) {
|
|
||||||
struct object *o = parse_object(sha1);
|
o = parse_object_or_die(sha1, path);
|
||||||
if (o->type == OBJ_TAG) {
|
if (o->type == OBJ_TAG) {
|
||||||
o = deref_tag(o, path, 0);
|
o = deref_tag(o, path, 0);
|
||||||
if (o)
|
if (o)
|
||||||
fprintf(cb->refs_file, "^%s\n",
|
fprintf(cb->refs_file, "^%s\n",
|
||||||
sha1_to_hex(o->sha1));
|
sha1_to_hex(o->sha1));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ((cb->flags & PACK_REFS_PRUNE) && !do_not_prune(flags)) {
|
if ((cb->flags & PACK_REFS_PRUNE) && !do_not_prune(flags)) {
|
||||||
int namelen = strlen(path) + 1;
|
int namelen = strlen(path) + 1;
|
||||||
@ -128,7 +128,7 @@ int pack_refs(unsigned int flags)
|
|||||||
die_errno("unable to create ref-pack file structure");
|
die_errno("unable to create ref-pack file structure");
|
||||||
|
|
||||||
/* perhaps other traits later as well */
|
/* perhaps other traits later as well */
|
||||||
fprintf(cbdata.refs_file, "# pack-refs with: peeled \n");
|
fprintf(cbdata.refs_file, "# pack-refs with: peeled fully-peeled \n");
|
||||||
|
|
||||||
for_each_ref(handle_one_ref, &cbdata);
|
for_each_ref(handle_one_ref, &cbdata);
|
||||||
if (ferror(cbdata.refs_file))
|
if (ferror(cbdata.refs_file))
|
||||||
|
@ -152,11 +152,9 @@ static int add_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
|
|||||||
|
|
||||||
static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
|
static int add_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
|
||||||
{
|
{
|
||||||
struct object *object = parse_object(sha1);
|
struct object *object = parse_object_or_die(sha1, path);
|
||||||
struct rev_info *revs = (struct rev_info *)cb_data;
|
struct rev_info *revs = (struct rev_info *)cb_data;
|
||||||
|
|
||||||
if (!object)
|
|
||||||
die("bad object ref: %s:%s", path, sha1_to_hex(sha1));
|
|
||||||
add_pending_object(revs, object, "");
|
add_pending_object(revs, object, "");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
49
refs.c
49
refs.c
@ -804,11 +804,38 @@ static const char *parse_ref_line(char *line, unsigned char *sha1)
|
|||||||
return line;
|
return line;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Read f, which is a packed-refs file, into dir.
|
||||||
|
*
|
||||||
|
* A comment line of the form "# pack-refs with: " may contain zero or
|
||||||
|
* more traits. We interpret the traits as follows:
|
||||||
|
*
|
||||||
|
* No traits:
|
||||||
|
*
|
||||||
|
* Probably no references are peeled. But if the file contains a
|
||||||
|
* peeled value for a reference, we will use it.
|
||||||
|
*
|
||||||
|
* peeled:
|
||||||
|
*
|
||||||
|
* References under "refs/tags/", if they *can* be peeled, *are*
|
||||||
|
* peeled in this file. References outside of "refs/tags/" are
|
||||||
|
* probably not peeled even if they could have been, but if we find
|
||||||
|
* a peeled value for such a reference we will use it.
|
||||||
|
*
|
||||||
|
* fully-peeled:
|
||||||
|
*
|
||||||
|
* All references in the file that can be peeled are peeled.
|
||||||
|
* Inversely (and this is more important), any references in the
|
||||||
|
* file for which no peeled value is recorded is not peelable. This
|
||||||
|
* trait should typically be written alongside "peeled" for
|
||||||
|
* compatibility with older clients, but we do not require it
|
||||||
|
* (i.e., "peeled" is a no-op if "fully-peeled" is set).
|
||||||
|
*/
|
||||||
static void read_packed_refs(FILE *f, struct ref_dir *dir)
|
static void read_packed_refs(FILE *f, struct ref_dir *dir)
|
||||||
{
|
{
|
||||||
struct ref_entry *last = NULL;
|
struct ref_entry *last = NULL;
|
||||||
char refline[PATH_MAX];
|
char refline[PATH_MAX];
|
||||||
int flag = REF_ISPACKED;
|
enum { PEELED_NONE, PEELED_TAGS, PEELED_FULLY } peeled = PEELED_NONE;
|
||||||
|
|
||||||
while (fgets(refline, sizeof(refline), f)) {
|
while (fgets(refline, sizeof(refline), f)) {
|
||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
@ -817,15 +844,20 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
|
|||||||
|
|
||||||
if (!strncmp(refline, header, sizeof(header)-1)) {
|
if (!strncmp(refline, header, sizeof(header)-1)) {
|
||||||
const char *traits = refline + sizeof(header) - 1;
|
const char *traits = refline + sizeof(header) - 1;
|
||||||
if (strstr(traits, " peeled "))
|
if (strstr(traits, " fully-peeled "))
|
||||||
flag |= REF_KNOWS_PEELED;
|
peeled = PEELED_FULLY;
|
||||||
|
else if (strstr(traits, " peeled "))
|
||||||
|
peeled = PEELED_TAGS;
|
||||||
/* perhaps other traits later as well */
|
/* perhaps other traits later as well */
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
refname = parse_ref_line(refline, sha1);
|
refname = parse_ref_line(refline, sha1);
|
||||||
if (refname) {
|
if (refname) {
|
||||||
last = create_ref_entry(refname, sha1, flag, 1);
|
last = create_ref_entry(refname, sha1, REF_ISPACKED, 1);
|
||||||
|
if (peeled == PEELED_FULLY ||
|
||||||
|
(peeled == PEELED_TAGS && !prefixcmp(refname, "refs/tags/")))
|
||||||
|
last->flag |= REF_KNOWS_PEELED;
|
||||||
add_ref(dir, last);
|
add_ref(dir, last);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -833,8 +865,15 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
|
|||||||
refline[0] == '^' &&
|
refline[0] == '^' &&
|
||||||
strlen(refline) == 42 &&
|
strlen(refline) == 42 &&
|
||||||
refline[41] == '\n' &&
|
refline[41] == '\n' &&
|
||||||
!get_sha1_hex(refline + 1, sha1))
|
!get_sha1_hex(refline + 1, sha1)) {
|
||||||
hashcpy(last->u.value.peeled, sha1);
|
hashcpy(last->u.value.peeled, sha1);
|
||||||
|
/*
|
||||||
|
* Regardless of what the file header said,
|
||||||
|
* we definitely know the value of *this*
|
||||||
|
* reference:
|
||||||
|
*/
|
||||||
|
last->flag |= REF_KNOWS_PEELED;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
64
t/t3211-peel-ref.sh
Executable file
64
t/t3211-peel-ref.sh
Executable file
@ -0,0 +1,64 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
test_description='tests for the peel_ref optimization of packed-refs'
|
||||||
|
. ./test-lib.sh
|
||||||
|
|
||||||
|
test_expect_success 'create annotated tag in refs/tags' '
|
||||||
|
test_commit base &&
|
||||||
|
git tag -m annotated foo
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'create annotated tag outside of refs/tags' '
|
||||||
|
git update-ref refs/outside/foo refs/tags/foo
|
||||||
|
'
|
||||||
|
|
||||||
|
# This matches show-ref's output
|
||||||
|
print_ref() {
|
||||||
|
echo "$(git rev-parse "$1") $1"
|
||||||
|
}
|
||||||
|
|
||||||
|
test_expect_success 'set up expected show-ref output' '
|
||||||
|
{
|
||||||
|
print_ref "refs/heads/master" &&
|
||||||
|
print_ref "refs/outside/foo" &&
|
||||||
|
print_ref "refs/outside/foo^{}" &&
|
||||||
|
print_ref "refs/tags/base" &&
|
||||||
|
print_ref "refs/tags/foo" &&
|
||||||
|
print_ref "refs/tags/foo^{}"
|
||||||
|
} >expect
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'refs are peeled outside of refs/tags (loose)' '
|
||||||
|
git show-ref -d >actual &&
|
||||||
|
test_cmp expect actual
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'refs are peeled outside of refs/tags (packed)' '
|
||||||
|
git pack-refs --all &&
|
||||||
|
git show-ref -d >actual &&
|
||||||
|
test_cmp expect actual
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'create old-style pack-refs without fully-peeled' '
|
||||||
|
# Git no longer writes without fully-peeled, so we just write our own
|
||||||
|
# from scratch; we could also munge the existing file to remove the
|
||||||
|
# fully-peeled bits, but that seems even more prone to failure,
|
||||||
|
# especially if the format ever changes again. At least this way we
|
||||||
|
# know we are emulating exactly what an older git would have written.
|
||||||
|
{
|
||||||
|
echo "# pack-refs with: peeled " &&
|
||||||
|
print_ref "refs/heads/master" &&
|
||||||
|
print_ref "refs/outside/foo" &&
|
||||||
|
print_ref "refs/tags/base" &&
|
||||||
|
print_ref "refs/tags/foo" &&
|
||||||
|
echo "^$(git rev-parse "refs/tags/foo^{}")"
|
||||||
|
} >tmp &&
|
||||||
|
mv tmp .git/packed-refs
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'refs are peeled outside of refs/tags (old packed)' '
|
||||||
|
git show-ref -d >actual &&
|
||||||
|
test_cmp expect actual
|
||||||
|
'
|
||||||
|
|
||||||
|
test_done
|
Loading…
Reference in New Issue
Block a user