]> asedeno.scripts.mit.edu Git - git.git/blobdiff - sha1_file.c
graph API: don't print branch lines for uninteresting merge parents
[git.git] / sha1_file.c
index c2ab7ea11d7a838c54e5cc8f91ae06a3ee0a2e67..141f5a713a2e542c0bcccbfa57b0dccc3142f362 100644 (file)
@@ -2102,26 +2102,16 @@ int hash_sha1_file(const void *buf, unsigned long len, const char *type,
        return 0;
 }
 
-int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
+static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
+                             void *buf, unsigned long len, time_t mtime)
 {
-       int size, ret;
+       int fd, size, ret;
        unsigned char *compressed;
        z_stream stream;
-       unsigned char sha1[20];
        char *filename;
        static char tmpfile[PATH_MAX];
-       char hdr[32];
-       int fd, hdrlen;
 
-       /* Normally if we have it in the pack then we do not bother writing
-        * it out into .git/objects/??/?{38} file.
-        */
-       write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
        filename = sha1_file_name(sha1);
-       if (returnsha1)
-               hashcpy(returnsha1, sha1);
-       if (has_sha1_file(sha1))
-               return 0;
        fd = open(filename, O_RDONLY);
        if (fd >= 0) {
                /*
@@ -2182,9 +2172,53 @@ int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned cha
                die("unable to write sha1 file");
        free(compressed);
 
+       if (mtime) {
+               struct utimbuf utb;
+               utb.actime = mtime;
+               utb.modtime = mtime;
+               if (utime(tmpfile, &utb) < 0)
+                       warning("failed utime() on %s: %s",
+                               tmpfile, strerror(errno));
+       }
+
        return move_temp_to_file(tmpfile, filename);
 }
 
+int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
+{
+       unsigned char sha1[20];
+       char hdr[32];
+       int hdrlen;
+
+       /* Normally if we have it in the pack then we do not bother writing
+        * it out into .git/objects/??/?{38} file.
+        */
+       write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
+       if (returnsha1)
+               hashcpy(returnsha1, sha1);
+       if (has_sha1_file(sha1))
+               return 0;
+       return write_loose_object(sha1, hdr, hdrlen, buf, len, 0);
+}
+
+int force_object_loose(const unsigned char *sha1, time_t mtime)
+{
+       struct stat st;
+       void *buf;
+       unsigned long len;
+       enum object_type type;
+       char hdr[32];
+       int hdrlen;
+
+       if (find_sha1_file(sha1, &st))
+               return 0;
+       buf = read_packed_sha1(sha1, &type, &len);
+       if (!buf)
+               return error("cannot read sha1_file for %s", sha1_to_hex(sha1));
+       hdrlen = sprintf(hdr, "%s %lu", typename(type), len) + 1;
+       return write_loose_object(sha1, hdr, hdrlen, buf, len, mtime);
+}
+
 /*
  * We need to unpack and recompress the object for writing
  * it out to a different file.
@@ -2466,16 +2500,10 @@ int index_path(unsigned char *sha1, const char *path, struct stat *st, int write
 
 int read_pack_header(int fd, struct pack_header *header)
 {
-       char *c = (char*)header;
-       ssize_t remaining = sizeof(struct pack_header);
-       do {
-               ssize_t r = xread(fd, c, remaining);
-               if (r <= 0)
-                       /* "eof before pack header was fully read" */
-                       return PH_ERROR_EOF;
-               remaining -= r;
-               c += r;
-       } while (remaining > 0);
+       if (read_in_full(fd, header, sizeof(*header)) < sizeof(*header))
+               /* "eof before pack header was fully read" */
+               return PH_ERROR_EOF;
+
        if (header->hdr_signature != htonl(PACK_SIGNATURE))
                /* "protocol error (pack signature mismatch detected)" */
                return PH_ERROR_PACK_SIGNATURE;