pack-objects: use full pathname to help hashing with "thin" pack.
This uses the same hashing algorithm to the "preferred base tree" objects and the incoming pathnames, to group the same files from different revs together, while spreading files with the same basename in different directories. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Feb 22, 2006 at 22:10 UTC
1d6b38cc76c348e2477506ca9759fc241e3d0d46
1 file changed
+44
-16
pack-objects.c
+44
-16
@@ -439,9 +439,37 @@ static void rehash_objects(void)
439
}
440
}
441
442
-static int add_object_entry(const unsigned char *sha1, const char *name, int exclude)
442
+struct name_path {
443
+ struct name_path *up;
444
+ const char *elem;
445
+ int len;
446
+};
447
+
448
+static unsigned name_hash(struct name_path *path, const char *name)
449
+{
450
+ struct name_path *p = path;
451
+ const char *n = name + strlen(name);
452
+ unsigned hash = 0;
453
+
454
+ if (n != name && n[-1] == '\n')
455
+ n--;
456
+ while (name <= --n) {
457
+ unsigned char c = *n;
458
+ hash = hash * 11 + c;
459
+ }
460
+ for (p = path; p; p = p->up) {
461
+ hash = hash * 11 + '/';
462
+ n = p->elem + p->len;
463
+ while (p->elem <= --n) {
464
+ unsigned char c = *n;
465
+ hash = hash * 11 + c;
466
+ }
467
+ }
468
+ return hash;
469
+}
470
+
471
+static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude)
472
{
444
- unsigned int hash = 0;
473
unsigned int idx = nr_objects;
474
struct object_entry *entry;
475
struct packed_git *p;
@@ -467,13 +495,6 @@ static int add_object_entry(const unsigned char *sha1, const char *name, int exc
495
if ((entry = locate_object_entry(sha1)) != NULL)
496
goto already_added;
497
470
- while (*name) {
471
- unsigned char c = *name++;
472
- if (isspace(c))
473
- continue;
474
- hash = hash * 11 + c;
475
- }
476
-
498
if (idx >= nr_alloc) {
499
unsigned int needed = (idx + 1024) * 3 / 2;
500
objects = xrealloc(objects, needed * sizeof(*entry));
@@ -507,12 +528,12 @@ static int add_object_entry(const unsigned char *sha1, const char *name, int exc
528
return status;
529
}
530
510
-static void add_pbase_tree(struct tree_desc *tree)
531
+static void add_pbase_tree(struct tree_desc *tree, struct name_path *up)
532
{
533
while (tree->size) {
534
const unsigned char *sha1;
535
const char *name;
515
- unsigned mode;
536
+ unsigned mode, hash;
537
unsigned long size;
538
char type[20];
539
@@ -523,16 +544,22 @@ static void add_pbase_tree(struct tree_desc *tree)
544
if (sha1_object_info(sha1, type, &size))
545
continue;
546
526
- if (!add_object_entry(sha1, name, 1))
547
+ hash = name_hash(up, name);
548
+ if (!add_object_entry(sha1, hash, 1))
549
continue;
550
551
if (!strcmp(type, "tree")) {
552
struct tree_desc sub;
553
void *elem;
554
+ struct name_path me;
555
+
556
elem = read_sha1_file(sha1, type, &sub.size);
557
sub.buf = elem;
558
if (sub.buf) {
535
- add_pbase_tree(&sub);
559
+ me.up = up;
560
+ me.elem = name;
561
+ me.len = strlen(name);
562
+ add_pbase_tree(&sub, &me);
563
free(elem);
564
}
565
}
@@ -543,12 +570,13 @@ static void add_preferred_base(unsigned char *sha1)
570
{
571
struct tree_desc tree;
572
void *elem;
573
+
574
elem = read_object_with_reference(sha1, "tree", &tree.size, NULL);
575
tree.buf = elem;
576
if (!tree.buf)
577
return;
550
- if (add_object_entry(sha1, "", 1))
551
- add_pbase_tree(&tree);
578
+ if (add_object_entry(sha1, name_hash(NULL, ""), 1))
579
+ add_pbase_tree(&tree, NULL);
580
free(elem);
581
}
582
@@ -1031,7 +1059,7 @@ int main(int argc, char **argv)
1059
}
1060
if (get_sha1_hex(line, sha1))
1061
die("expected sha1, got garbage:\n %s", line);
1034
- add_object_entry(sha1, line+40, 0);
1062
+ add_object_entry(sha1, name_hash(NULL, line+41), 0);
1063
}
1064
if (progress)
1065
fprintf(stderr, "Done counting %d objects.\n", nr_objects);