Code

builtin-fetch.c (store_updated_refs): Honor update_local_ref() return value
[git.git] / sha1_file.c
index 141f5a713a2e542c0bcccbfa57b0dccc3142f362..adcf37c3f68d16200b01adae441fa3d8f68e3c02 100644 (file)
@@ -176,21 +176,23 @@ char *sha1_file_name(const unsigned char *sha1)
        return base;
 }
 
-char *sha1_pack_name(const unsigned char *sha1)
+static char *sha1_get_pack_name(const unsigned char *sha1,
+                               char **name, char **base, const char *which)
 {
        static const char hex[] = "0123456789abcdef";
-       static char *name, *base, *buf;
+       char *buf;
        int i;
 
-       if (!base) {
+       if (!*base) {
                const char *sha1_file_directory = get_object_directory();
                int len = strlen(sha1_file_directory);
-               base = xmalloc(len + 60);
-               sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.pack", sha1_file_directory);
-               name = base + len + 11;
+               *base = xmalloc(len + 60);
+               sprintf(*base, "%s/pack/pack-1234567890123456789012345678901234567890.%s",
+                       sha1_file_directory, which);
+               *name = *base + len + 11;
        }
 
-       buf = name;
+       buf = *name;
 
        for (i = 0; i < 20; i++) {
                unsigned int val = *sha1++;
@@ -198,32 +200,21 @@ char *sha1_pack_name(const unsigned char *sha1)
                *buf++ = hex[val & 0xf];
        }
 
-       return base;
+       return *base;
 }
 
-char *sha1_pack_index_name(const unsigned char *sha1)
+char *sha1_pack_name(const unsigned char *sha1)
 {
-       static const char hex[] = "0123456789abcdef";
-       static char *name, *base, *buf;
-       int i;
-
-       if (!base) {
-               const char *sha1_file_directory = get_object_directory();
-               int len = strlen(sha1_file_directory);
-               base = xmalloc(len + 60);
-               sprintf(base, "%s/pack/pack-1234567890123456789012345678901234567890.idx", sha1_file_directory);
-               name = base + len + 11;
-       }
+       static char *name, *base;
 
-       buf = name;
+       return sha1_get_pack_name(sha1, &name, &base, "pack");
+}
 
-       for (i = 0; i < 20; i++) {
-               unsigned int val = *sha1++;
-               *buf++ = hex[val >> 4];
-               *buf++ = hex[val & 0xf];
-       }
+char *sha1_pack_index_name(const unsigned char *sha1)
+{
+       static char *name, *base;
 
-       return base;
+       return sha1_get_pack_name(sha1, &name, &base, "idx");
 }
 
 struct alternate_object_database *alt_odb_list;
@@ -380,6 +371,18 @@ static void read_info_alternates(const char * relative_base, int depth)
        munmap(map, mapsz);
 }
 
+void add_to_alternates_file(const char *reference)
+{
+       struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
+       int fd = hold_lock_file_for_append(lock, git_path("objects/info/alternates"), 1);
+       char *alt = mkpath("%s/objects\n", reference);
+       write_or_die(fd, alt, strlen(alt));
+       if (commit_lock_file(lock))
+               die("could not close alternates file");
+       if (alt_odb_tail)
+               link_alt_odb_entries(alt, alt + strlen(alt), '\n', NULL, 0);
+}
+
 void prepare_alt_odb(void)
 {
        const char *alt;