Implement blob ID validation in fast-import.
When accepting revision SHA1 IDs from the frontend verify the SHA1 actually refers to a blob and is known to exist. Its an error to use a SHA1 in a tree if the blob doesn't exist as this would cause git-fsck-objects to report a missing blob should the pack get closed without the blob being appended into it or a subsequent pack. So right now we'll just ask that the frontend "pre-declare" any blobs it wants to use in a tree before it can use them. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Shawn O. Pearce committed
Aug 14, 2006 at 02:50 UTC
7111feede9c5905199ba48645fadc369faca5711
1 file changed
+24
-4
fast-import.c
+24
-4
@@ -70,6 +70,7 @@ Format of STDIN stream:
70
struct object_entry
71
{
72
struct object_entry *next;
73
+ enum object_type type;
74
unsigned long offset;
75
unsigned char sha1[20];
76
};
@@ -528,6 +529,7 @@ static int store_object(
529
duplicate_count_by_type[type]++;
530
return 1;
531
}
532
+ e->type = type;
533
e->offset = pack_offset;
534
object_count++;
535
object_count_by_type[type]++;
@@ -713,7 +715,7 @@ static int tree_content_set(
715
}
716
if (!S_ISDIR(e->mode)) {
717
e->tree = new_tree_content(8);
716
- e->mode = 040000;
718
+ e->mode = S_IFDIR;
719
}
720
if (!e->tree)
721
load_tree(e);
@@ -732,7 +734,7 @@ static int tree_content_set(
734
t->entries[t->entry_count++] = e;
735
if (slash1) {
736
e->tree = new_tree_content(8);
735
- e->mode = 040000;
737
+ e->mode = S_IFDIR;
738
tree_content_set(e, slash1 + 1, sha1, mode);
739
} else {
740
e->tree = NULL;
@@ -948,16 +950,28 @@ static void load_branch(struct branch *b)
950
static void file_change_m(struct branch *b)
951
{
952
const char *path = read_path();
953
+ struct object_entry *oe;
954
char hexsha1[41];
955
unsigned char sha1[20];
956
+ char type[20];
957
958
yread(0, hexsha1, 40);
959
hexsha1[40] = 0;
960
961
if (get_sha1_hex(hexsha1, sha1))
962
die("Invalid sha1 %s for %s", hexsha1, path);
963
+ oe = find_object(sha1);
964
+ if (oe) {
965
+ if (oe->type != OBJ_BLOB)
966
+ die("%s is a %s not a blob (for %s)", hexsha1, type_names[oe->type], path);
967
+ } else {
968
+ if (sha1_object_info(sha1, type, NULL))
969
+ die("No blob %s for %s", hexsha1, path);
970
+ if (strcmp(blob_type, type))
971
+ die("%s is a %s not a blob (for %s)", hexsha1, type, path);
972
+ }
973
960
- tree_content_set(&b->branch_tree, path, sha1, 0100644);
974
+ tree_content_set(&b->branch_tree, path, sha1, S_IFREG | 0644);
975
}
976
977
static void file_change_d(struct branch *b)
@@ -986,6 +1000,10 @@ static void cmd_new_commit()
1000
c = body + max_hdr_len;
1001
yread(0, c, acmsglen);
1002
1003
+ /* oddly enough this is all that fsck-objects cares about */
1004
+ if (memcmp(c, "author ", 7))
1005
+ die("Invalid commit format on branch %s", name);
1006
+
1007
/* file_change* */
1008
for (;;) {
1009
unsigned char cmd;
@@ -1104,7 +1122,9 @@ int main(int argc, const char **argv)
1122
fprintf(stderr, " tags : %10lu (%10lu duplicates)\n", object_count_by_type[OBJ_TAG], duplicate_count_by_type[OBJ_TAG]);
1123
fprintf(stderr, "Total branches: %10lu\n", branch_count);
1124
fprintf(stderr, "Total atoms: %10u\n", atom_cnt);
1107
- fprintf(stderr, "Memory pools: %10lu MiB\n", total_allocd/(1024*1024));
1125
+ fprintf(stderr, "Memory total: %10lu KiB\n", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);
1126
+ fprintf(stderr, " pools: %10lu KiB\n", total_allocd/1024);
1127
+ fprintf(stderr, " objects: %10lu KiB\n", (alloc_count*sizeof(struct object_entry))/1024);
1128
fprintf(stderr, "---------------------------------------------------\n");
1129
1130
stat(pack_name, &sb);