{"thread":{"id":"3052","subject":"[PATCH] Require packfiles to follow the naming convention (preparation).","startedAt":"2006-01-12T00:05:19Z","lastAt":"2006-01-12T00:05:19Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"14519","messageId":"7virsqjoow.fsf@assigned-by-dhcp.cox.net","threadId":"3052","inReplyTo":null,"subject":"[PATCH] Require packfiles to follow the naming convention (preparation).","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-01-12T00:05:19Z","receivedAt":"2006-01-12T00:05:19Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\n\nNames of the packfiles will be required to be of the form\n\"pack-[0-9a-f]{40}.pack\".  This commit prepares a switch that I\ncan toggle to enforce the naming convention, but the switch is\ncurrently toggled off.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n\n---\n * Currently in \"pu\".\n\n Documentation/git-convert-packs.txt |    2 -\n Documentation/git.txt               |    3 +\n sha1_file.c                         |   51 ++++++++++++++++++++++\n t/t5300-pack-object.sh              |   81 +++++++++++++++++++----------------\n 4 files changed, 97 insertions(+), 40 deletions(-)\n\na8fe7257b8427d31cfcca0aa336335bb43689fc9\ndiff --git a/Documentation/git-convert-packs.txt b/Documentation/git-convert-packs.txt\nindex ba02641..ac82033 100644\n--- a/Documentation/git-convert-packs.txt\n+++ b/Documentation/git-convert-packs.txt\n@@ -3,7 +3,7 @@ git-convert-packs(1)\n \n NAME\n ----\n-git-convert-packs - Renames packfiles created with pre 0.99.9 GIT\n+git-convert-packs - Renames packfiles created with pre 0.99.9 GIT.\n \n \n SYNOPSIS\ndiff --git a/Documentation/git.txt b/Documentation/git.txt\nindex 92cfe0e..4ab07a4 100644\n--- a/Documentation/git.txt\n+++ b/Documentation/git.txt\n@@ -350,6 +350,9 @@ gitlink:git-archimport[1]::\n gitlink:git-convert-objects[1]::\n \tConverts old-style git repository.\n \n+gitlink:git-convert-packs[1]::\n+\tRenames packfiles created with pre 0.99.9 GIT.\n+\n gitlink:git-cvsimport[1]::\n \tSalvage your data out of another SCM people love to hate.\n \ndiff --git a/sha1_file.c b/sha1_file.c\nindex 8bebbb2..22d56fc 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -346,6 +346,15 @@ static int pack_used_ctr;\n static unsigned long pack_mapped;\n struct packed_git *packed_git;\n \n+/*\n+ * 0: do not check, do not do anything.\n+ * 1: check and warn, but do not reject.\n+ * 2: check and reject if pack name is obsolete.\n+ */\n+#ifndef PACK_NAME_ENFORCEMENT_PHASE\n+#define PACK_NAME_ENFORCEMENT_PHASE 0\n+#endif\n+\n static int check_packed_git_idx(const char *path, unsigned long *idx_size_,\n \t\t\t\tvoid **idx_map_)\n {\n@@ -357,6 +366,9 @@ static int check_packed_git_idx(const ch\n \tint nr, i;\n \tint fd;\n \tstruct stat st;\n+#if PACK_NAME_ENFORCEMENT_PHASE\n+\tunsigned char packname[20];\n+#endif\n \n \tfd = open(path, O_RDONLY);\n \tif (fd < 0)\n@@ -365,6 +377,22 @@ static int check_packed_git_idx(const ch\n \t\tclose(fd);\n \t\treturn -1;\n \t}\n+\n+#if PACK_NAME_ENFORCEMENT_PHASE\n+\t/* We require \"...../pack-XXXX{40}XXXX.idx\" now.\n+\t * 5-byte \"pack-\", 40-byte hex and 4-byte .idx.\n+\t */\n+\ti = strlen(path);\n+\tif (i < 49 || strcmp(path + i - 4, \".idx\") ||\n+\t    strncmp(path + i - 49, \"pack-\", 5) ||\n+\t    get_sha1_hex(path + i - 44, packname))\n+#if PACK_NAME_ENFORCEMENT_PHASE == 1\n+\t\tfprintf(stderr, \"warning: index file name '%s' does not \"\n+\t\t\t\"match pack-X{40}.idx convention.\\n\", path);\n+#else\n+\t\treturn error(\"non index file '%s' ignored\", path);\n+#endif\n+#endif\n \tidx_size = st.st_size;\n \tidx_map = mmap(NULL, idx_size, PROT_READ, MAP_PRIVATE, fd, 0);\n \tclose(fd);\n@@ -397,7 +425,7 @@ static int check_packed_git_idx(const ch\n \t\treturn error(\"wrong index file size\");\n \n \t/*\n-\t * File checksum.\n+\t * File checksum and index name.\n \t */\n \tSHA1_Init(&ctx);\n \tSHA1_Update(&ctx, idx_map, idx_size-20);\n@@ -406,6 +434,27 @@ static int check_packed_git_idx(const ch\n \tif (memcmp(sha1, idx_map + idx_size - 20, 20))\n \t\treturn error(\"index checksum mismatch\");\n \n+#if PACK_NAME_ENFORCEMENT_PHASE\n+\tSHA1_Init(&ctx);\n+\tfor (i = 0; i < nr; i++) {\n+\t\tunsigned char *ent = (idx_map + 4*256) + i * 24 + 4;\n+\t\tSHA1_Update(&ctx, ent, 20);\n+\t}\n+\tSHA1_Final(sha1, &ctx);\n+\t\n+\tif (memcmp(sha1, packname, 20)) {\n+#if PACK_NAME_ENFORCEMENT_PHASE == 1\n+\t\tfprintf(stderr, \"warning: index name '%s' does not match \"\n+\t\t\t\"contents of the pack '%s'\\n\",\n+\t\t\tpath, sha1_to_hex(sha1));\n+#else\n+\t\treturn error(\"index name '%s' does not match \"\n+\t\t\t     \"contents of the pack '%s'\\n\",\n+\t\t\t     path, sha1_to_hex(sha1));\n+#endif\n+\t}\n+#endif\n+\n \treturn 0;\n }\n \ndiff --git a/t/t5300-pack-object.sh b/t/t5300-pack-object.sh\nindex de45ac4..77fd8d7 100755\n--- a/t/t5300-pack-object.sh\n+++ b/t/t5300-pack-object.sh\n@@ -10,6 +10,8 @@ test_description='git-pack-object\n \n TRASH=`pwd`\n \n+mkdir test-1 test-2\n+\n test_expect_success \\\n     'setup' \\\n     'rm -f .git/index*\n@@ -35,7 +37,7 @@ test_expect_success \\\n \n test_expect_success \\\n     'pack without delta' \\\n-    'packname_1=$(git-pack-objects --window=0 test-1 <obj-list)'\n+    'packname_1=$(git-pack-objects --window=0 test-1/pack <obj-list)'\n \n rm -fr .git2\n mkdir .git2\n@@ -45,8 +47,8 @@ test_expect_success \\\n     \"GIT_OBJECT_DIRECTORY=.git2/objects &&\n      export GIT_OBJECT_DIRECTORY &&\n      git-init-db &&\n-     git-unpack-objects -n <test-1-${packname_1}.pack &&\n-     git-unpack-objects <test-1-${packname_1}.pack\"\n+     git-unpack-objects -n <test-1/pack-${packname_1}.pack &&\n+     git-unpack-objects <test-1/pack-${packname_1}.pack\"\n \n unset GIT_OBJECT_DIRECTORY\n cd \"$TRASH/.git2\"\n@@ -66,7 +68,8 @@ cd \"$TRASH\"\n test_expect_success \\\n     'pack with delta' \\\n     'pwd &&\n-     packname_2=$(git-pack-objects test-2 <obj-list)'\n+     packname_2=$(git-pack-objects test-2/pack <obj-list) &&\n+     test \"$packname_2\" = \"$packname_1\"'\n \n rm -fr .git2\n mkdir .git2\n@@ -76,8 +79,8 @@ test_expect_success \\\n     'GIT_OBJECT_DIRECTORY=.git2/objects &&\n      export GIT_OBJECT_DIRECTORY &&\n      git-init-db &&\n-     git-unpack-objects -n <test-2-${packname_2}.pack &&\n-     git-unpack-objects <test-2-${packname_2}.pack'\n+     git-unpack-objects -n <test-2/pack-${packname_2}.pack &&\n+     git-unpack-objects <test-2/pack-${packname_2}.pack'\n \n unset GIT_OBJECT_DIRECTORY\n cd \"$TRASH/.git2\"\n@@ -101,7 +104,8 @@ test_expect_success \\\n     'GIT_OBJECT_DIRECTORY=.git2/objects &&\n      export GIT_OBJECT_DIRECTORY &&\n      git-init-db &&\n-     cp test-1-${packname_1}.pack test-1-${packname_1}.idx .git2/objects/pack && {\n+     cp test-1/pack-${packname_1}.pack test-1/pack-${packname_1}.idx \\\n+     \t.git2/objects/pack && {\n \t git-diff-tree --root -p $commit &&\n \t while read object\n \t do\n@@ -116,8 +120,9 @@ test_expect_success \\\n     'use packed deltified objects' \\\n     'GIT_OBJECT_DIRECTORY=.git2/objects &&\n      export GIT_OBJECT_DIRECTORY &&\n-     rm -f .git2/objects/pack/test-?.idx &&\n-     cp test-2-${packname_2}.pack test-2-${packname_2}.idx .git2/objects/pack && {\n+     rm -f .git2/objects/pack/pack-* &&\n+     cp test-2/pack-${packname_2}.pack test-2/pack-${packname_2}.idx \\\n+     \t.git2/objects/pack && {\n \t git-diff-tree --root -p $commit &&\n \t while read object\n \t do\n@@ -131,47 +136,47 @@ unset GIT_OBJECT_DIRECTORY\n \n test_expect_success \\\n     'verify pack' \\\n-    'git-verify-pack test-1-${packname_1}.idx test-2-${packname_2}.idx'\n+    'git-verify-pack test-?/pack-${packname_1}.idx'\n \n test_expect_success \\\n     'corrupt a pack and see if verify catches' \\\n-    'cp test-1-${packname_1}.idx test-3.idx &&\n-     cp test-2-${packname_2}.pack test-3.pack &&\n-     if git-verify-pack test-3.idx\n+    'cp test-1/pack-${packname_1}.idx . &&\n+     cp test-2/pack-${packname_2}.pack . &&\n+     if git-verify-pack pack-${packname_1}.idx\n      then false\n      else :;\n      fi &&\n \n      : PACK_SIGNATURE &&\n-     cp test-1-${packname_1}.pack test-3.pack &&\n-     dd if=/dev/zero of=test-3.pack count=1 bs=1 conv=notrunc seek=2 &&\n-     if git-verify-pack test-3.idx\n+     cp test-1/pack-${packname_1}.pack . &&\n+     dd if=/dev/zero of=pack-${packname_1}.pack count=1 bs=1 conv=notrunc seek=2 &&\n+     if git-verify-pack pack-${packname_1}.idx\n      then false\n      else :;\n      fi &&\n \n      : PACK_VERSION &&\n-     cp test-1-${packname_1}.pack test-3.pack &&\n-     dd if=/dev/zero of=test-3.pack count=1 bs=1 conv=notrunc seek=7 &&\n-     if git-verify-pack test-3.idx\n+     cp test-1/pack-${packname_1}.pack . &&\n+     dd if=/dev/zero of=pack-${packname_1}.pack count=1 bs=1 conv=notrunc seek=7 &&\n+     if git-verify-pack pack-${packname_1}.idx\n      then false\n      else :;\n      fi &&\n \n      : TYPE/SIZE byte of the first packed object data &&\n-     cp test-1-${packname_1}.pack test-3.pack &&\n-     dd if=/dev/zero of=test-3.pack count=1 bs=1 conv=notrunc seek=12 &&\n-     if git-verify-pack test-3.idx\n+     cp test-1/pack-${packname_1}.pack . &&\n+     dd if=/dev/zero of=pack-${packname_1}.pack count=1 bs=1 conv=notrunc seek=12 &&\n+     if git-verify-pack pack-${packname_1}.idx\n      then false\n      else :;\n      fi &&\n \n      : sum of the index file itself &&\n-     l=`wc -c <test-3.idx` &&\n+     l=`wc -c <pack-${packname_1}.idx` &&\n      l=`expr $l - 20` &&\n-     cp test-1-${packname_1}.pack test-3.pack &&\n-     dd if=/dev/zero of=test-3.idx count=20 bs=1 conv=notrunc seek=$l &&\n-     if git-verify-pack test-3.pack\n+     cp test-1/pack-${packname_1}.pack . &&\n+     dd if=/dev/zero of=pack-${packname_1}.idx count=20 bs=1 conv=notrunc seek=$l &&\n+     if git-verify-pack pack-${packname_1}.idx\n      then false\n      else :;\n      fi &&\n@@ -180,19 +185,19 @@ test_expect_success \\\n \n test_expect_success \\\n     'build pack index for an existing pack' \\\n-    'cp test-1-${packname_1}.pack test-3.pack &&\n-     git-index-pack -o tmp.idx test-3.pack &&\n-     cmp tmp.idx test-1-${packname_1}.idx &&\n-\n-     git-index-pack test-3.pack &&\n-     cmp test-3.idx test-1-${packname_1}.idx &&\n-\n-     cp test-2-${packname_2}.pack test-3.pack &&\n-     git-index-pack -o tmp.idx test-2-${packname_2}.pack &&\n-     cmp tmp.idx test-2-${packname_2}.idx &&\n+    'cp test-1/pack-${packname_1}.pack unknown.pack &&\n+     git-index-pack -o tmp.idx unknown.pack &&\n+     cmp tmp.idx test-1/pack-${packname_1}.idx &&\n+\n+     git-index-pack unknown.pack &&\n+     cmp unknown.idx test-1/pack-${packname_1}.idx &&\n+\n+     cp test-2/pack-${packname_2}.pack unknown.pack &&\n+     git-index-pack -o tmp.idx unknown.pack &&\n+     cmp tmp.idx test-2/pack-${packname_2}.idx &&\n \n-     git-index-pack test-3.pack &&\n-     cmp test-3.idx test-2-${packname_2}.idx &&\n+     git-index-pack unknown.pack &&\n+     cmp unknown.idx test-2/pack-${packname_2}.idx &&\n \n      :'\n \n-- \n1.1.1-g8ecb\n"}]}