Skip to content

Commit 9e84a6d

Browse files
committed
Merge branch 'jk/submodule-fsck-loose' into maint
* jk/submodule-fsck-loose: fsck: complain when .gitmodules is a symlink index-pack: check .gitmodules files with --strict unpack-objects: call fsck_finish() after fscking objects fsck: call fsck_finish() after fscking objects fsck: check .gitmodules content fsck: handle promisor objects in .gitmodules check fsck: detect gitmodules files fsck: actually fsck blob data fsck: simplify ".git" check index-pack: make fsck error message more specific
2 parents 68f95b2 + b7b1fca commit 9e84a6d

File tree

8 files changed

+271
-30
lines changed

8 files changed

+271
-30
lines changed

builtin/fsck.c

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ static void check_connectivity(void)
337337
}
338338
}
339339

340-
static int fsck_obj(struct object *obj)
340+
static int fsck_obj(struct object *obj, void *buffer, unsigned long size)
341341
{
342342
int err;
343343

@@ -351,7 +351,7 @@ static int fsck_obj(struct object *obj)
351351

352352
if (fsck_walk(obj, NULL, &fsck_obj_options))
353353
objerror(obj, "broken links");
354-
err = fsck_object(obj, NULL, 0, &fsck_obj_options);
354+
err = fsck_object(obj, buffer, size, &fsck_obj_options);
355355
if (err)
356356
goto out;
357357

@@ -396,7 +396,7 @@ static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
396396
}
397397
obj->flags &= ~(REACHABLE | SEEN);
398398
obj->flags |= HAS_OBJ;
399-
return fsck_obj(obj);
399+
return fsck_obj(obj, buffer, size);
400400
}
401401

402402
static int default_refs;
@@ -504,44 +504,42 @@ static void get_default_heads(void)
504504
}
505505
}
506506

507-
static struct object *parse_loose_object(const struct object_id *oid,
508-
const char *path)
507+
static int fsck_loose(const struct object_id *oid, const char *path, void *data)
509508
{
510509
struct object *obj;
511-
void *contents;
512510
enum object_type type;
513511
unsigned long size;
512+
void *contents;
514513
int eaten;
515514

516-
if (read_loose_object(path, oid->hash, &type, &size, &contents) < 0)
517-
return NULL;
515+
if (read_loose_object(path, oid->hash, &type, &size, &contents) < 0) {
516+
errors_found |= ERROR_OBJECT;
517+
error("%s: object corrupt or missing: %s",
518+
oid_to_hex(oid), path);
519+
return 0; /* keep checking other objects */
520+
}
518521

519522
if (!contents && type != OBJ_BLOB)
520-
die("BUG: read_loose_object streamed a non-blob");
523+
BUG("read_loose_object streamed a non-blob");
521524

522525
obj = parse_object_buffer(oid, type, size, contents, &eaten);
523-
524-
if (!eaten)
525-
free(contents);
526-
return obj;
527-
}
528-
529-
static int fsck_loose(const struct object_id *oid, const char *path, void *data)
530-
{
531-
struct object *obj = parse_loose_object(oid, path);
532-
533526
if (!obj) {
534527
errors_found |= ERROR_OBJECT;
535-
error("%s: object corrupt or missing: %s",
528+
error("%s: object could not be parsed: %s",
536529
oid_to_hex(oid), path);
530+
if (!eaten)
531+
free(contents);
537532
return 0; /* keep checking other objects */
538533
}
539534

540535
obj->flags &= ~(REACHABLE | SEEN);
541536
obj->flags |= HAS_OBJ;
542-
if (fsck_obj(obj))
537+
if (fsck_obj(obj, contents, size))
543538
errors_found |= ERROR_OBJECT;
544-
return 0;
539+
540+
if (!eaten)
541+
free(contents);
542+
return 0; /* keep checking other objects, even if we saw an error */
545543
}
546544

547545
static int fsck_cruft(const char *basename, const char *path, void *data)
@@ -750,6 +748,9 @@ int cmd_fsck(int argc, const char **argv, const char *prefix)
750748
}
751749
stop_progress(&progress);
752750
}
751+
752+
if (fsck_finish(&fsck_obj_options))
753+
errors_found |= ERROR_OBJECT;
753754
}
754755

755756
for (i = 0; i < argc; i++) {

builtin/index-pack.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,9 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,
836836
blob->object.flags |= FLAG_CHECKED;
837837
else
838838
die(_("invalid blob object %s"), oid_to_hex(oid));
839+
if (do_fsck_object &&
840+
fsck_object(&blob->object, (void *)data, size, &fsck_options))
841+
die(_("fsck error in packed object"));
839842
} else {
840843
struct object *obj;
841844
int eaten;
@@ -853,7 +856,7 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,
853856
die(_("invalid %s"), type_name(type));
854857
if (do_fsck_object &&
855858
fsck_object(obj, buf, size, &fsck_options))
856-
die(_("Error in object"));
859+
die(_("fsck error in packed object"));
857860
if (strict && fsck_walk(obj, NULL, &fsck_options))
858861
die(_("Not all child objects of %s are reachable"), oid_to_hex(&obj->oid));
859862

@@ -1477,6 +1480,9 @@ static void final(const char *final_pack_name, const char *curr_pack_name,
14771480
} else
14781481
chmod(final_index_name, 0444);
14791482

1483+
if (do_fsck_object)
1484+
add_packed_git(final_index_name, strlen(final_index_name), 0);
1485+
14801486
if (!from_stdin) {
14811487
printf("%s\n", sha1_to_hex(hash));
14821488
} else {
@@ -1818,6 +1824,10 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
18181824
pack_hash);
18191825
else
18201826
close(input_fd);
1827+
1828+
if (do_fsck_object && fsck_finish(&fsck_options))
1829+
die(_("fsck error in pack objects"));
1830+
18211831
free(objects);
18221832
strbuf_release(&index_name_buf);
18231833
if (pack_name == NULL)

builtin/unpack-objects.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static int check_object(struct object *obj, int type, void *data, struct fsck_op
210210
if (!obj_buf)
211211
die("Whoops! Cannot find object '%s'", oid_to_hex(&obj->oid));
212212
if (fsck_object(obj, obj_buf->buffer, obj_buf->size, &fsck_options))
213-
die("Error in object");
213+
die("fsck error in packed object");
214214
fsck_options.walk = check_object;
215215
if (fsck_walk(obj, NULL, &fsck_options))
216216
die("Error on reachable objects of %s", oid_to_hex(&obj->oid));
@@ -572,8 +572,11 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
572572
unpack_all();
573573
the_hash_algo->update_fn(&ctx, buffer, offset);
574574
the_hash_algo->final_fn(oid.hash, &ctx);
575-
if (strict)
575+
if (strict) {
576576
write_rest();
577+
if (fsck_finish(&fsck_options))
578+
die(_("fsck error in pack objects"));
579+
}
577580
if (hashcmp(fill(the_hash_algo->rawsz), oid.hash))
578581
die("final sha1 did not match");
579582
use(the_hash_algo->rawsz);

fsck.c

Lines changed: 134 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
#include "utf8.h"
1111
#include "sha1-array.h"
1212
#include "decorate.h"
13+
#include "oidset.h"
14+
#include "packfile.h"
15+
#include "submodule-config.h"
16+
#include "config.h"
17+
18+
static struct oidset gitmodules_found = OIDSET_INIT;
19+
static struct oidset gitmodules_done = OIDSET_INIT;
1320

1421
#define FSCK_FATAL -1
1522
#define FSCK_INFO -2
@@ -44,13 +51,19 @@
4451
FUNC(MISSING_TAG_ENTRY, ERROR) \
4552
FUNC(MISSING_TAG_OBJECT, ERROR) \
4653
FUNC(MISSING_TREE, ERROR) \
54+
FUNC(MISSING_TREE_OBJECT, ERROR) \
4755
FUNC(MISSING_TYPE, ERROR) \
4856
FUNC(MISSING_TYPE_ENTRY, ERROR) \
4957
FUNC(MULTIPLE_AUTHORS, ERROR) \
5058
FUNC(TAG_OBJECT_NOT_TAG, ERROR) \
5159
FUNC(TREE_NOT_SORTED, ERROR) \
5260
FUNC(UNKNOWN_TYPE, ERROR) \
5361
FUNC(ZERO_PADDED_DATE, ERROR) \
62+
FUNC(GITMODULES_MISSING, ERROR) \
63+
FUNC(GITMODULES_BLOB, ERROR) \
64+
FUNC(GITMODULES_PARSE, ERROR) \
65+
FUNC(GITMODULES_NAME, ERROR) \
66+
FUNC(GITMODULES_SYMLINK, ERROR) \
5467
/* warnings */ \
5568
FUNC(BAD_FILEMODE, WARN) \
5669
FUNC(EMPTY_NAME, WARN) \
@@ -561,10 +574,18 @@ static int fsck_tree(struct tree *item, struct fsck_options *options)
561574
has_empty_name |= !*name;
562575
has_dot |= !strcmp(name, ".");
563576
has_dotdot |= !strcmp(name, "..");
564-
has_dotgit |= (!strcmp(name, ".git") ||
565-
is_hfs_dotgit(name) ||
566-
is_ntfs_dotgit(name));
577+
has_dotgit |= is_hfs_dotgit(name) || is_ntfs_dotgit(name);
567578
has_zero_pad |= *(char *)desc.buffer == '0';
579+
580+
if (is_hfs_dotgitmodules(name) || is_ntfs_dotgitmodules(name)) {
581+
if (!S_ISLNK(mode))
582+
oidset_insert(&gitmodules_found, oid);
583+
else
584+
retval += report(options, &item->object,
585+
FSCK_MSG_GITMODULES_SYMLINK,
586+
".gitmodules is a symbolic link");
587+
}
588+
568589
if (update_tree_entry_gently(&desc)) {
569590
retval += report(options, &item->object, FSCK_MSG_BAD_TREE, "cannot be parsed as a tree");
570591
break;
@@ -901,14 +922,74 @@ static int fsck_tag(struct tag *tag, const char *data,
901922
return fsck_tag_buffer(tag, data, size, options);
902923
}
903924

925+
struct fsck_gitmodules_data {
926+
struct object *obj;
927+
struct fsck_options *options;
928+
int ret;
929+
};
930+
931+
static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
932+
{
933+
struct fsck_gitmodules_data *data = vdata;
934+
const char *subsection, *key;
935+
int subsection_len;
936+
char *name;
937+
938+
if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
939+
!subsection)
940+
return 0;
941+
942+
name = xmemdupz(subsection, subsection_len);
943+
if (check_submodule_name(name) < 0)
944+
data->ret |= report(data->options, data->obj,
945+
FSCK_MSG_GITMODULES_NAME,
946+
"disallowed submodule name: %s",
947+
name);
948+
free(name);
949+
950+
return 0;
951+
}
952+
953+
static int fsck_blob(struct blob *blob, const char *buf,
954+
unsigned long size, struct fsck_options *options)
955+
{
956+
struct fsck_gitmodules_data data;
957+
958+
if (!oidset_contains(&gitmodules_found, &blob->object.oid))
959+
return 0;
960+
oidset_insert(&gitmodules_done, &blob->object.oid);
961+
962+
if (!buf) {
963+
/*
964+
* A missing buffer here is a sign that the caller found the
965+
* blob too gigantic to load into memory. Let's just consider
966+
* that an error.
967+
*/
968+
return report(options, &blob->object,
969+
FSCK_MSG_GITMODULES_PARSE,
970+
".gitmodules too large to parse");
971+
}
972+
973+
data.obj = &blob->object;
974+
data.options = options;
975+
data.ret = 0;
976+
if (git_config_from_mem(fsck_gitmodules_fn, CONFIG_ORIGIN_BLOB,
977+
".gitmodules", buf, size, &data))
978+
data.ret |= report(options, &blob->object,
979+
FSCK_MSG_GITMODULES_PARSE,
980+
"could not parse gitmodules blob");
981+
982+
return data.ret;
983+
}
984+
904985
int fsck_object(struct object *obj, void *data, unsigned long size,
905986
struct fsck_options *options)
906987
{
907988
if (!obj)
908989
return report(options, obj, FSCK_MSG_BAD_OBJECT_SHA1, "no valid object to fsck");
909990

910991
if (obj->type == OBJ_BLOB)
911-
return 0;
992+
return fsck_blob((struct blob *)obj, data, size, options);
912993
if (obj->type == OBJ_TREE)
913994
return fsck_tree((struct tree *) obj, options);
914995
if (obj->type == OBJ_COMMIT)
@@ -932,3 +1013,52 @@ int fsck_error_function(struct fsck_options *o,
9321013
error("object %s: %s", describe_object(o, obj), message);
9331014
return 1;
9341015
}
1016+
1017+
int fsck_finish(struct fsck_options *options)
1018+
{
1019+
int ret = 0;
1020+
struct oidset_iter iter;
1021+
const struct object_id *oid;
1022+
1023+
oidset_iter_init(&gitmodules_found, &iter);
1024+
while ((oid = oidset_iter_next(&iter))) {
1025+
struct blob *blob;
1026+
enum object_type type;
1027+
unsigned long size;
1028+
char *buf;
1029+
1030+
if (oidset_contains(&gitmodules_done, oid))
1031+
continue;
1032+
1033+
blob = lookup_blob(oid);
1034+
if (!blob) {
1035+
ret |= report(options, &blob->object,
1036+
FSCK_MSG_GITMODULES_BLOB,
1037+
"non-blob found at .gitmodules");
1038+
continue;
1039+
}
1040+
1041+
buf = read_sha1_file(oid->hash, &type, &size);
1042+
if (!buf) {
1043+
if (is_promisor_object(&blob->object.oid))
1044+
continue;
1045+
ret |= report(options, &blob->object,
1046+
FSCK_MSG_GITMODULES_MISSING,
1047+
"unable to read .gitmodules blob");
1048+
continue;
1049+
}
1050+
1051+
if (type == OBJ_BLOB)
1052+
ret |= fsck_blob(blob, buf, size, options);
1053+
else
1054+
ret |= report(options, &blob->object,
1055+
FSCK_MSG_GITMODULES_BLOB,
1056+
"non-blob found at .gitmodules");
1057+
free(buf);
1058+
}
1059+
1060+
1061+
oidset_clear(&gitmodules_found);
1062+
oidset_clear(&gitmodules_done);
1063+
return ret;
1064+
}

fsck.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,11 @@ int fsck_walk(struct object *obj, void *data, struct fsck_options *options);
5353
int fsck_object(struct object *obj, void *data, unsigned long size,
5454
struct fsck_options *options);
5555

56+
/*
57+
* Some fsck checks are context-dependent, and may end up queued; run this
58+
* after completing all fsck_object() calls in order to resolve any remaining
59+
* checks.
60+
*/
61+
int fsck_finish(struct fsck_options *options);
62+
5663
#endif

sha1_file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2209,7 +2209,7 @@ int read_loose_object(const char *path,
22092209
goto out;
22102210
}
22112211

2212-
if (*type == OBJ_BLOB) {
2212+
if (*type == OBJ_BLOB && *size > big_file_threshold) {
22132213
if (check_stream_sha1(&stream, hdr, *size, path, expected_sha1) < 0)
22142214
goto out;
22152215
} else {

t/lib-pack.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ pack_obj () {
7979
;;
8080
esac
8181

82+
# If it's not a delta, we can convince pack-objects to generate a pack
83+
# with just our entry, and then strip off the header (12 bytes) and
84+
# trailer (20 bytes).
85+
if test -z "$2"
86+
then
87+
echo "$1" | git pack-objects --stdout >pack_obj.tmp &&
88+
size=$(wc -c <pack_obj.tmp) &&
89+
dd if=pack_obj.tmp bs=1 count=$((size - 20 - 12)) skip=12 &&
90+
rm -f pack_obj.tmp
91+
return
92+
fi
93+
8294
echo >&2 "BUG: don't know how to print $1${2:+ (from $2)}"
8395
return 1
8496
}

0 commit comments

Comments
 (0)