]> asedeno.scripts.mit.edu Git - git.git/blobdiff - sha1_file.c
Add "mkpath()" helper function
[git.git] / sha1_file.c
index 49517a414df74bb3d2a390db9bd9530f2fc518b3..fc4e6bf91f1b402f2e69c1521498551d5e82acb4 100644 (file)
@@ -102,9 +102,27 @@ char *get_index_file(void)
        return git_index_file;
 }
 
+int safe_create_leading_directories(char *path)
+{
+       char *pos = path;
+
+       while (pos) {
+               pos = strchr(pos, '/');
+               if (!pos)
+                       break;
+               *pos = 0;
+               if (mkdir(path, 0777) < 0)
+                       if (errno != EEXIST) {
+                               *pos = '/';
+                               return -1;
+                       }
+               *pos++ = '/';
+       }
+       return 0;
+}
+
 int get_sha1(const char *str, unsigned char *sha1)
 {
-       static char pathname[PATH_MAX];
        static const char *prefix[] = {
                "",
                "refs",
@@ -118,11 +136,8 @@ int get_sha1(const char *str, unsigned char *sha1)
        if (!get_sha1_hex(str, sha1))
                return 0;
 
-       if (!git_dir)
-               setup_git_env();
        for (p = prefix; *p; p++) {
-               snprintf(pathname, sizeof(pathname), "%s/%s/%s",
-                        git_dir, *p, str);
+               char * pathname = git_path("%s/%s", *p, str);
                if (!get_sha1_file(pathname, sha1))
                        return 0;
        }
@@ -294,6 +309,8 @@ static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
                return -1;
 
        index = idx_map;
+       *idx_map_ = idx_map;
+       *idx_size_ = idx_size;
 
        /* check index map */
        if (idx_size < 4*256 + 20 + 20)
@@ -316,8 +333,6 @@ static int check_packed_git_idx(const char *path, unsigned long *idx_size_,
        if (idx_size != 4*256 + nr * 24 + 20 + 20)
                return error("wrong index file size");
 
-       *idx_map_ = idx_map;
-       *idx_size_ = idx_size;
        return 0;
 }
 
@@ -439,6 +454,7 @@ static void prepare_packed_git_one(char *objdir)
                p->next = packed_git;
                packed_git = p;
        }
+       closedir(dir);
 }
 
 void prepare_packed_git(void)
@@ -531,7 +547,7 @@ int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void
        return inflate(stream, 0);
 }
 
-void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
+static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
 {
        int bytes = strlen(buffer) + 1;
        unsigned char *buf = xmalloc(1+size);
@@ -857,7 +873,7 @@ static void *unpack_non_delta_entry(unsigned char *data,
 {
        int st;
        z_stream stream;
-       char *buffer;
+       unsigned char *buffer;
 
        buffer = xmalloc(size + 1);
        buffer[size] = 0;
@@ -949,7 +965,7 @@ int nth_packed_object_sha1(const struct packed_git *p, int n,
 int find_pack_entry_one(const unsigned char *sha1,
                        struct pack_entry *e, struct packed_git *p)
 {
-       int *level1_ofs = p->index_base;
+       unsigned int *level1_ofs = p->index_base;
        int hi = ntohl(level1_ofs[*sha1]);
        int lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
        void *index = p->index_base + 256;
@@ -1267,6 +1283,12 @@ int write_sha1_from_fd(const unsigned char *sha1, int fd)
        return 0;
 }
 
+int has_sha1_pack(const unsigned char *sha1)
+{
+       struct pack_entry e;
+       return find_pack_entry(sha1, &e);
+}
+
 int has_sha1_file(const unsigned char *sha1)
 {
        struct stat st;