@matheusbsbs / matheus-bagliano / commits / 8455e48476
Fork 0

Use .keep files in fast-import during processing.

Because fast-import automatically updates all references (heads and tags) at the end of its run the repository is corrupt unless the objects are available in the .git/objects/pack directory prior to the refs being modified. The easiest way to ensure that is true is to move the packfile and its associated index directly into the .git/objects/pack directory as soon as we have finished output to it. But the only safe way to do this is to create the a temporary .keep file for that pack, so we use the same tricks that index-pack uses when its being invoked by receive-pack. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Jan 16, 2007 at 01:15 UTC 8455e48476634eeff6fd2cd4f245cadfef14bbc8
1 file changed +71 -20
fast-import.c
+71 -20
@@ -241,9 +241,7 @@ static unsigned int atom_cnt;
241 static struct atom_str **atom_table;
242
243 /* The .pack file being generated */
244 -static const char *base_name;
244 static unsigned int pack_id;
246 -static char *idx_name;
245 static struct packed_git *pack_data;
246 static struct packed_git **all_packs;
247 static int pack_fd;
@@ -591,17 +589,17 @@ static void release_tree_entry(struct tree_entry *e)
589
590 static void start_packfile()
591 {
592 + static char tmpfile[PATH_MAX];
593 struct packed_git *p;
594 struct pack_header hdr;
595
597 - idx_name = xmalloc(strlen(base_name) + 11);
598 - p = xcalloc(1, sizeof(*p) + strlen(base_name) + 13);
599 - sprintf(p->pack_name, "%s%5.5i.pack", base_name, pack_id + 1);
600 - sprintf(idx_name, "%s%5.5i.idx", base_name, pack_id + 1);
601 -
602 - pack_fd = open(p->pack_name, O_RDWR|O_CREAT|O_EXCL, 0666);
596 + snprintf(tmpfile, sizeof(tmpfile),
597 + "%s/pack_XXXXXX", get_object_directory());
598 + pack_fd = mkstemp(tmpfile);
599 if (pack_fd < 0)
604 - die("Can't create %s: %s", p->pack_name, strerror(errno));
600 + die("Can't create %s: %s", tmpfile, strerror(errno));
601 + p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
602 + strcpy(p->pack_name, tmpfile);
603 p->pack_fd = pack_fd;
604
605 hdr.hdr_signature = htonl(PACK_SIGNATURE);
@@ -656,13 +654,15 @@ static int oecmp (const void *a_, const void *b_)
654 return hashcmp(a->sha1, b->sha1);
655 }
656
659 -static void write_index(const char *idx_name)
657 +static char* create_index()
658 {
659 + static char tmpfile[PATH_MAX];
660 + SHA_CTX ctx;
661 struct sha1file *f;
662 struct object_entry **idx, **c, **last, *e;
663 struct object_entry_pool *o;
664 unsigned int array[256];
665 - int i;
665 + int i, idx_fd;
666
667 /* Build the sorted table of object IDs. */
668 idx = xmalloc(object_count * sizeof(struct object_entry*));
@@ -689,16 +689,68 @@ static void write_index(const char *idx_name)
689 c = next;
690 }
691
692 - f = sha1create("%s", idx_name);
692 + snprintf(tmpfile, sizeof(tmpfile),
693 + "%s/index_XXXXXX", get_object_directory());
694 + idx_fd = mkstemp(tmpfile);
695 + if (idx_fd < 0)
696 + die("Can't create %s: %s", tmpfile, strerror(errno));
697 + f = sha1fd(idx_fd, tmpfile);
698 sha1write(f, array, 256 * sizeof(int));
699 + SHA1_Init(&ctx);
700 for (c = idx; c != last; c++) {
701 unsigned int offset = htonl((*c)->offset);
702 sha1write(f, &offset, 4);
703 sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
704 + SHA1_Update(&ctx, (*c)->sha1, 20);
705 }
706 sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
700 - sha1close(f, pack_data->sha1, 1);
707 + sha1close(f, NULL, 1);
708 free(idx);
709 + SHA1_Final(pack_data->sha1, &ctx);
710 + return tmpfile;
711 +}
712 +
713 +static char* keep_pack(char *curr_index_name)
714 +{
715 + static char name[PATH_MAX];
716 + static char *keep_msg = "fast-import";
717 + int keep_fd;
718 +
719 + chmod(pack_data->pack_name, 0444);
720 + chmod(curr_index_name, 0444);
721 +
722 + snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
723 + get_object_directory(), sha1_to_hex(pack_data->sha1));
724 + keep_fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
725 + if (keep_fd < 0)
726 + die("cannot create keep file");
727 + write(keep_fd, keep_msg, strlen(keep_msg));
728 + close(keep_fd);
729 +
730 + snprintf(name, sizeof(name), "%s/pack/pack-%s.pack",
731 + get_object_directory(), sha1_to_hex(pack_data->sha1));
732 + if (move_temp_to_file(pack_data->pack_name, name))
733 + die("cannot store pack file");
734 + printf("%s\n", name);
735 +
736 + snprintf(name, sizeof(name), "%s/pack/pack-%s.idx",
737 + get_object_directory(), sha1_to_hex(pack_data->sha1));
738 + if (move_temp_to_file(curr_index_name, name))
739 + die("cannot store index file");
740 + return name;
741 +}
742 +
743 +static void unkeep_all_packs()
744 +{
745 + static char name[PATH_MAX];
746 + int k;
747 +
748 + for (k = 0; k < pack_id; k++) {
749 + struct packed_git *p = all_packs[k];
750 + snprintf(name, sizeof(name), "%s/pack/pack-%s.keep",
751 + get_object_directory(), sha1_to_hex(p->sha1));
752 + unlink(name);
753 + }
754 }
755
756 static void end_packfile()
@@ -706,10 +758,10 @@ static void end_packfile()
758 struct packed_git *old_p = pack_data, *new_p;
759
760 if (object_count) {
761 + char *idx_name;
762 +
763 fixup_header_footer();
710 - write_index(idx_name);
711 - fprintf(stdout, "%s\n", old_p->pack_name);
712 - fflush(stdout);
764 + idx_name = keep_pack(create_index());
765
766 /* Register the packfile with core git's machinary. */
767 new_p = add_packed_git(idx_name, strlen(idx_name), 1);
@@ -725,7 +777,6 @@ static void end_packfile()
777 unlink(old_p->pack_name);
778 }
779 free(old_p);
728 - free(idx_name);
780
781 /* We can't carry a delta across packfiles. */
782 free(last_blob.data);
@@ -1790,7 +1841,7 @@ static void cmd_checkpoint()
1841 }
1842
1843 static const char fast_import_usage[] =
1793 -"git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log] temp.pack";
1844 +"git-fast-import [--objects=n] [--depth=n] [--active-branches=n] [--export-marks=marks.file] [--branch-log=log]";
1845
1846 int main(int argc, const char **argv)
1847 {
@@ -1826,9 +1877,8 @@ int main(int argc, const char **argv)
1877 else
1878 die("unknown option %s", a);
1879 }
1829 - if ((i+1) != argc)
1880 + if (i != argc)
1881 usage(fast_import_usage);
1831 - base_name = argv[i];
1882
1883 alloc_objects(est_obj_cnt);
1884 strbuf_init(&command_buf);
@@ -1860,6 +1910,7 @@ int main(int argc, const char **argv)
1910
1911 dump_branches();
1912 dump_tags();
1913 + unkeep_all_packs();
1914 dump_marks();
1915 if (branch_log)
1916 fclose(branch_log);