{"thread":{"id":"13505","subject":"[PATCH 1/2] add a force_object_loose() function","startedAt":"2008-05-14T05:32:48Z","lastAt":"2008-05-14T06:02:21Z","messageCount":2,"participants":["Nicolas Pitre","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"76914","messageId":"alpine.LFD.1.10.0805140130090.23581@xanadu.home","threadId":"13505","inReplyTo":null,"subject":"[PATCH 1/2] add a force_object_loose() function","fromName":"Nicolas Pitre","fromEmail":"nico@cam.org","sentAt":"2008-05-14T05:32:48Z","receivedAt":"2008-05-14T05:32:48Z","isPatch":true,"sender":{"key":"nico@fluxnic.net","avatar":"https://avatars.githubusercontent.com/u/702790?v=4"},"body":"\nThis is meant to force the creation of a loose object even if it\nalready exists packed.  Needed for the next commit.\n\nSigned-off-by: Nicolas Pitre <nico@cam.org>\n\ndiff --git a/cache.h b/cache.h\nindex a23d1ac..c761915 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -527,6 +527,7 @@ extern void * read_sha1_file(const unsigned char *sha1, enum object_type *type,\n extern int hash_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1);\n extern int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *return_sha1);\n extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);\n+extern int force_object_loose(const unsigned char *sha1, time_t mtime);\n \n extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type);\n \ndiff --git a/sha1_file.c b/sha1_file.c\nindex d21e23b..206ea2d 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -2114,26 +2114,16 @@ int hash_sha1_file(const void *buf, unsigned long len, const char *type,\n \treturn 0;\n }\n \n-int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)\n+static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,\n+\t\t\t      void *buf, unsigned long len, time_t mtime)\n {\n-\tint size, ret;\n+\tint fd, size, ret;\n \tunsigned char *compressed;\n \tz_stream stream;\n-\tunsigned char sha1[20];\n \tchar *filename;\n \tstatic char tmpfile[PATH_MAX];\n-\tchar hdr[32];\n-\tint fd, hdrlen;\n \n-\t/* Normally if we have it in the pack then we do not bother writing\n-\t * it out into .git/objects/??/?{38} file.\n-\t */\n-\twrite_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);\n \tfilename = sha1_file_name(sha1);\n-\tif (returnsha1)\n-\t\thashcpy(returnsha1, sha1);\n-\tif (has_sha1_file(sha1))\n-\t\treturn 0;\n \tfd = open(filename, O_RDONLY);\n \tif (fd >= 0) {\n \t\t/*\n@@ -2194,9 +2184,53 @@ int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned cha\n \t\tdie(\"unable to write sha1 file\");\n \tfree(compressed);\n \n+\tif (mtime) {\n+\t\tstruct utimbuf utb;\n+\t\tutb.actime = mtime;\n+\t\tutb.modtime = mtime;\n+\t\tif (utime(tmpfile, &utb) < 0)\n+\t\t\twarning(\"failed utime() on %s: %s\",\n+\t\t\t\ttmpfile, strerror(errno));\n+\t}\n+\n \treturn move_temp_to_file(tmpfile, filename);\n }\n \n+int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)\n+{\n+\tunsigned char sha1[20];\n+\tchar hdr[32];\n+\tint hdrlen;\n+\n+\t/* Normally if we have it in the pack then we do not bother writing\n+\t * it out into .git/objects/??/?{38} file.\n+\t */\n+\twrite_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);\n+\tif (returnsha1)\n+\t\thashcpy(returnsha1, sha1);\n+\tif (has_sha1_file(sha1))\n+\t\treturn 0;\n+\treturn write_loose_object(sha1, hdr, hdrlen, buf, len, 0);\n+}\n+\n+int force_object_loose(const unsigned char *sha1, time_t mtime)\n+{\n+\tstruct stat st;\n+\tvoid *buf;\n+\tunsigned long len;\n+\tenum object_type type;\n+\tchar hdr[32];\n+\tint hdrlen;\n+\n+\tif (find_sha1_file(sha1, &st))\n+\t\treturn 0;\n+\tbuf = read_packed_sha1(sha1, &type, &len);\n+\tif (!buf)\n+\t\treturn error(\"cannot read sha1_file for %s\", sha1_to_hex(sha1));\n+\thdrlen = sprintf(hdr, \"%s %lu\", typename(type), len) + 1;\n+\treturn write_loose_object(sha1, hdr, hdrlen, buf, len, mtime);\n+}\n+\n /*\n  * We need to unpack and recompress the object for writing\n  * it out to a different file.\n"},{"id":"76919","messageId":"7v3aolqv82.fsf@gitster.siamese.dyndns.org","threadId":"13505","inReplyTo":"alpine.LFD.1.10.0805140130090.23581@xanadu.home","subject":"Re: [PATCH 1/2] add a force_object_loose() function","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-05-14T06:02:21Z","receivedAt":"2008-05-14T06:02:21Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Nicolas Pitre <nico@cam.org> writes:\n\n> This is meant to force the creation of a loose object even if it\n> already exists packed.\n\nThanks, will queue.\n\nThis looks familiar, but much more thorough than what I suggested\nearlier.\n"}]}