{"thread":{"id":"268","subject":"[resend#2] [PATCH] Add -i option to cat-file to allow operation on displaced git object files","startedAt":"2005-04-23T18:27:01Z","lastAt":"2005-04-23T18:27:01Z","messageCount":1,"participants":["Jon Seymour"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"1395","messageId":"20050423192032.27808.qmail@blackcubes.dyndns.org","threadId":"268","inReplyTo":null,"subject":"[resend#2] [PATCH] Add -i option to cat-file to allow operation on displaced git object files","fromName":"Jon Seymour","fromEmail":"jon.seymour@gmail.com","sentAt":"2005-04-23T18:27:01Z","receivedAt":"2005-04-23T18:27:01Z","isPatch":true,"sender":{"key":"jon.seymour@gmail.com","avatar":"https://avatars.githubusercontent.com/u/207131?v=4"},"body":"[PATCH] Add -i option to cat-file to allow operation on displaced git object files\n\nFor the purposes of merging the contents of remote git object repositories at the filesystem level, it would\nbe helpful to be able to verify the integrity of a remote git object file before it is actually\ncopied into the local repository.\n\nTo enable this, the option syntax for the usage of the cat-file tool is extended with a -i option, per the\nmodified usage string quoted below:\n\n    usage: cat-file [-t | tagname] <sha1> [ -i displaced-git-object-file ]\n\nIf the -i option is specified, cat-file uses the filename specified on the command line rather than the derived\nfilename to locate and process the git object file implied by the sha1 argument.\n\nIn addition, the -i option forces cat-file to check the SHA1 signature of the specified input file against\nthe SHA1 signature specified on the command line and report an error if there is a mismatch.\n\nSigned-off-by: Jon Seymour <jon.seymour@gmail.com>\n\nApologies for the repeated attempts to mail this patch. I'll use qmail in future!\n\n---\nIndex: cache.h\n=================================--- e6d3a81c30ad237102e2ca82f3dfc57d0b9f0ddf/cache.h  (mode:100644 sha1:bf30ac4741d2eeeb483079f566182505898082f3)\n+++ 1b503f0571727a865b413e28641d2be09c8c3304/cache.h  (mode:100644 sha1:56eacfa0ab43c93a838efedca429e865c95f16bc)\n@@ -121,8 +121,9 @@\n extern void * map_sha1_file(const unsigned char *sha1, unsigned long *size);\n extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size);\n extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size);\n+extern void * read_displaced_sha1_file(char *type, unsigned long *size, const char *filename);\n extern int write_sha1_file(char *buf, unsigned len, unsigned char *return_sha1);\n-extern int check_sha1_signature(unsigned char *sha1, void *buf, unsigned long size, const char *type);\n+extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type);\n \n /* Read a tree into the cache */\n extern int read_tree(void *buffer, unsigned long size, int stage);\nIndex: cat-file.c\n=================================--- e6d3a81c30ad237102e2ca82f3dfc57d0b9f0ddf/cat-file.c  (mode:100644 sha1:3c47d79a16305d326a65768fe9f37ee25928510b)\n+++ 1b503f0571727a865b413e28641d2be09c8c3304/cat-file.c  (mode:100644 sha1:c88df7287b6ce7d6d33d016ffb524cc5a793cad2)\n@@ -5,16 +5,32 @@\n  */\n #include \"cache.h\"\n \n+static char _usage[]\"cat-file [-t | tagname] <sha1> [ -i displaced-git-object-file ]\";\n+\n int main(int argc, char **argv)\n {\n \tunsigned char sha1[20];\n \tchar type[20];\n-\tvoid *buf;\n+\tvoid *buf NULL;\n \tunsigned long size;\n \n-\tif (argc ! || get_sha1_hex(argv[2], sha1))\n-\t\tusage(\"cat-file [-t | tagname] <sha1>\");\n-\tbuf read_sha1_file(sha1, type, &size);\n+\tif (argc < 3 || get_sha1_hex(argv[2], sha1))\n+\t\tusage(_usage);\n+\n+        if (argc = 3) {\n+\t    buf read_sha1_file(sha1, type, &size);\n+        } else if (argc = 5 && !strcmp(argv[3], \"-i\")) {\n+            buf read_displaced_sha1_file(type, &size, argv[4]);\n+            if (!buf) \n+                    die(\"bad file: %s\", argv[4]);\n+\t    \n+            if (check_sha1_signature(sha1, buf, size, type) < 0)\n+                    die(\"signature mismatch for %s\", argv[4]);\n+\n+        } else {\n+\t    usage(_usage);\n+        }\n+\n \tif (!buf)\n \t\tdie(\"cat-file %s: bad file\", argv[2]);\n \tif (!strcmp(\"-t\", argv[1])) {\nIndex: sha1_file.c\n=================================--- e6d3a81c30ad237102e2ca82f3dfc57d0b9f0ddf/sha1_file.c  (mode:100644 sha1:6f7228e106d4e24b18f8416cc6adc2a6fd303eb7)\n+++ 1b503f0571727a865b413e28641d2be09c8c3304/sha1_file.c  (mode:100644 sha1:25e2e8b22cf9590a6df05f234ce88db8c2cd1030)\n@@ -80,7 +80,7 @@\n \treturn base;\n }\n \n-int check_sha1_signature(unsigned char *sha1, void *map, unsigned long size, const char *type)\n+int check_sha1_signature(const unsigned char *sha1, void *map, unsigned long size, const char *type)\n {\n \tchar header[100];\n \tunsigned char real_sha1[20];\n@@ -93,13 +93,12 @@\n \treturn memcmp(sha1, real_sha1, 20) ? -1 : 0;\n }\n \n-void *map_sha1_file(const unsigned char *sha1, unsigned long *size)\n+static void *map_file(const char *filename, unsigned long *size)\n {\n-\tchar *filename sha1_file_name(sha1);\n-\tint fd open(filename, O_RDONLY);\n \tstruct stat st;\n \tvoid *map;\n \n+\tint fd open(filename, O_RDONLY);\n \tif (fd < 0) {\n \t\tperror(filename);\n \t\treturn NULL;\n@@ -116,6 +115,12 @@\n \treturn map;\n }\n \n+void *map_sha1_file(const unsigned char *sha1, unsigned long *size)\n+{\n+        char *filename sha1_file_name(sha1);\n+        return map_file(filename, size);\n+}\n+\n void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size)\n {\n \tint ret, bytes;\n@@ -166,6 +171,20 @@\n \treturn NULL;\n }\n \n+void * read_displaced_sha1_file(char *type, unsigned long *size, const char *filename)\n+{\n+\tunsigned long mapsize;\n+\tvoid *map, *buf;\n+\n+\tmap map_file(filename, &mapsize);\n+\tif (map) {\n+\t\tbuf unpack_sha1_file(map, mapsize, type, size);\n+\t\tmunmap(map, mapsize);\n+\t\treturn buf;\n+\t}\n+\treturn NULL;\n+}\n+\n void *read_tree_with_tree_or_commit_sha1(const unsigned char *sha1,\n \t\t\t\t\t unsigned long *size,\n \t\t\t\t\t unsigned char *tree_sha1_return)\n"}]}