Fix packname hash generation.
This changes the generation of hash packfiles have in their names, from "hash of object names as fed to us" to "hash of object names in the resulting pack, in the order they appear in the index file". The new "git-index-pack" command is taught to output the computed hash value to its standard output. With this, we can store downloaded pack in a temporary file without knowing its final name, run git-index-pack to generate idx for it while finding out its final name, and then rename the pack and idx to their final names. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Oct 12, 2005 at 16:54 UTC
84c8d8aec535e1d92c5acc36e84ac8e00f6e0e7a
2 files changed
+23
-6
index-pack.c
+13
-2
@@ -349,7 +349,7 @@ static int sha1_compare(const void *_a, const void *_b)
349
return memcmp(a->sha1, b->sha1, 20);
350
}
351
352
-static void write_index_file(const char *index_name)
352
+static void write_index_file(const char *index_name, unsigned char *sha1)
353
{
354
struct sha1file *f;
355
struct object_entry **sorted_by_sha =
@@ -358,6 +358,7 @@ static void write_index_file(const char *index_name)
358
struct object_entry **last = sorted_by_sha + nr_objects;
359
unsigned int array[256];
360
int i;
361
+ SHA_CTX ctx;
362
363
for (i = 0; i < nr_objects; ++i)
364
sorted_by_sha[i] = &objects[i];
@@ -385,6 +386,11 @@ static void write_index_file(const char *index_name)
386
}
387
sha1write(f, array, 256 * sizeof(int));
388
389
+ /* recompute the SHA1 hash of sorted object names.
390
+ * currently pack-objects does not do this, but that
391
+ * can be fixed.
392
+ */
393
+ SHA1_Init(&ctx);
394
/*
395
* Write the actual SHA1 entries..
396
*/
@@ -394,10 +400,12 @@ static void write_index_file(const char *index_name)
400
unsigned int offset = htonl(obj->offset);
401
sha1write(f, &offset, 4);
402
sha1write(f, obj->sha1, 20);
403
+ SHA1_Update(&ctx, obj->sha1, 20);
404
}
405
sha1write(f, pack_base + pack_size - 20, 20);
406
sha1close(f, NULL, 1);
407
free(sorted_by_sha);
408
+ SHA1_Final(sha1, &ctx);
409
}
410
411
int main(int argc, char **argv)
@@ -405,6 +413,7 @@ int main(int argc, char **argv)
413
int i;
414
char *index_name = NULL;
415
char *index_name_buf = NULL;
416
+ unsigned char sha1[20];
417
418
for (i = 1; i < argc; i++) {
419
const char *arg = argv[i];
@@ -443,9 +452,11 @@ int main(int argc, char **argv)
452
deltas = xcalloc(nr_objects, sizeof(struct delta_entry));
453
parse_pack_objects();
454
free(deltas);
446
- write_index_file(index_name);
455
+ write_index_file(index_name, sha1);
456
free(objects);
457
free(index_name_buf);
458
459
+ printf("%s\n", sha1_to_hex(sha1));
460
+
461
return 0;
462
}
pack-objects.c
+10
-4
@@ -393,6 +393,7 @@ int main(int argc, char **argv)
393
SHA_CTX ctx;
394
char line[PATH_MAX + 20];
395
int window = 10, depth = 10, pack_to_stdout = 0;
396
+ struct object_entry **list;
397
int i;
398
399
for (i = 1; i < argc; i++) {
@@ -435,7 +436,6 @@ int main(int argc, char **argv)
436
if (pack_to_stdout != !base_name)
437
usage(pack_usage);
438
438
- SHA1_Init(&ctx);
439
while (fgets(line, sizeof(line), stdin) != NULL) {
440
unsigned int hash;
441
char *p;
@@ -451,10 +451,8 @@ int main(int argc, char **argv)
451
continue;
452
hash = hash * 11 + c;
453
}
454
- if (add_object_entry(sha1, hash))
455
- SHA1_Update(&ctx, sha1, 20);
454
+ add_object_entry(sha1, hash);
455
}
457
- SHA1_Final(object_list_sha1, &ctx);
456
if (non_empty && !nr_objects)
457
return 0;
458
get_object_details();
@@ -462,6 +460,14 @@ int main(int argc, char **argv)
460
fprintf(stderr, "Packing %d objects\n", nr_objects);
461
462
sorted_by_sha = create_sorted_list(sha1_sort);
463
+ SHA1_Init(&ctx);
464
+ list = sorted_by_sha;
465
+ for (i = 0; i < nr_objects; i++) {
466
+ struct object_entry *entry = *list++;
467
+ SHA1_Update(&ctx, entry->sha1, 20);
468
+ }
469
+ SHA1_Final(object_list_sha1, &ctx);
470
+
471
sorted_by_type = create_sorted_list(type_size_sort);
472
if (window && depth)
473
find_deltas(sorted_by_type, window+1, depth);