{"thread":{"id":"13026","subject":"Bug: git archive not doing export-subst when using --prefix= option","startedAt":"2008-04-08T21:35:04Z","lastAt":"2008-04-10T10:56:18Z","messageCount":4,"participants":["Ulrik Sverdrup","René Scharfe","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"73881","messageId":"a1b6cb1b0804081435w24074d1aq177e8c1a5e1af0f8@mail.gmail.com","threadId":"13026","inReplyTo":null,"subject":"Bug: git archive not doing export-subst when using --prefix= option","fromName":"Ulrik Sverdrup","fromEmail":"ulrik.sverdrup@gmail.com","sentAt":"2008-04-08T21:35:04Z","receivedAt":"2008-04-08T21:35:04Z","isPatch":false,"sender":{"key":"ulrik.sverdrup@gmail.com","avatar":null},"body":"In one of my small projects, I just tried to add an export-subst format to\nmy version file to have the git version in the released tarball. However it\nseems git-archive --prefix=\"tarballname-version/\" does not work well\ntogether with export-subst.\n\nI have very old git! (v1.5.4.4). So please help me and try to reproduce\nthis with newer versions:\n\nThe repo is publicly available at\n\ngit clone git://repo.or.cz/dragbox.git\n\nThe file in the repo having an export substitution format is\nDragbox/version.py.in\n\nNow try to export the git tree with git-archive, with and without --prefix=\nWe will do some hackery and grep the binary output.\n\n\t$ git archive HEAD | grep --text \"git_version\"\n\tgit_version = \"9c3f6c0af7e67a354cac9a24e24e2057d17778ef\"\n\t$ git archive --prefix=\"dragbox-git/\" HEAD | grep --text \"git_version\"\n\tgit_version = \"$Format:%H$\"\n\n\nI noticed in another even smaller repository that it would not behave the\nsame but equally spurious; it would only perform the substitution for some\nprefixes and not for others (for example with or without ending slash)\n\nRegards\nUlrik Sverdrup\n"},{"id":"73985","messageId":"47FD31C3.8070001@lsrfire.ath.cx","threadId":"13026","inReplyTo":"a1b6cb1b0804081435w24074d1aq177e8c1a5e1af0f8@mail.gmail.com","subject":"[PATCH] git-archive: ignore prefix when checking file attribute","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2008-04-09T21:14:43Z","receivedAt":"2008-04-09T21:14:43Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Ulrik Sverdrup noticed that git-archive doesn't correctly apply the attribute\nexport-subst when the option --prefix is given, too.\n\nWhen it checked if a file has the attribute turned on, git-archive would try\nto look up the full path -- including the prefix -- in .gitattributes.  That's\nwrong, as the prefix doesn't need to have any relation to any existing\ndirectories, tracked or not.\n\nThis patch makes git-archive ignore the prefix when looking up if value of the\nattribute export-subst for a file.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\nUlrik, does this patch fix the problem for you?  It applies to v1.5.4.4 and\nv1.5.5 equally.\n\n archive-tar.c       |    6 ++++--\n archive-zip.c       |    6 ++++--\n t/t5000-tar-tree.sh |   15 ++++++++++++++-\n 3 files changed, 22 insertions(+), 5 deletions(-)\n\ndiff --git a/archive-tar.c b/archive-tar.c\nindex 30aa2e2..4add802 100644\n--- a/archive-tar.c\n+++ b/archive-tar.c\n@@ -17,6 +17,7 @@ static time_t archive_time;\n static int tar_umask = 002;\n static int verbose;\n static const struct commit *commit;\n+static size_t base_len;\n \n /* writes out the whole block, but only if it is full */\n static void write_if_needed(void)\n@@ -251,8 +252,8 @@ static int write_tar_entry(const unsigned char *sha1,\n \t\tbuffer = NULL;\n \t\tsize = 0;\n \t} else {\n-\t\tbuffer = sha1_file_to_archive(path.buf, sha1, mode, &type,\n-\t\t                              &size, commit);\n+\t\tbuffer = sha1_file_to_archive(path.buf + base_len, sha1, mode,\n+\t\t\t\t&type, &size, commit);\n \t\tif (!buffer)\n \t\t\tdie(\"cannot read %s\", sha1_to_hex(sha1));\n \t}\n@@ -272,6 +273,7 @@ int write_tar_archive(struct archiver_args *args)\n \tarchive_time = args->time;\n \tverbose = args->verbose;\n \tcommit = args->commit;\n+\tbase_len = args->base ? strlen(args->base) : 0;\n \n \tif (args->commit_sha1)\n \t\twrite_global_extended_header(args->commit_sha1);\ndiff --git a/archive-zip.c b/archive-zip.c\nindex 74e30f6..18c0f87 100644\n--- a/archive-zip.c\n+++ b/archive-zip.c\n@@ -13,6 +13,7 @@ static int verbose;\n static int zip_date;\n static int zip_time;\n static const struct commit *commit;\n+static size_t base_len;\n \n static unsigned char *zip_dir;\n static unsigned int zip_dir_size;\n@@ -197,8 +198,8 @@ static int write_zip_entry(const unsigned char *sha1,\n \t\tif (S_ISREG(mode) && zlib_compression_level != 0)\n \t\t\tmethod = 8;\n \t\tresult = 0;\n-\t\tbuffer = sha1_file_to_archive(path, sha1, mode, &type, &size,\n-\t\t                              commit);\n+\t\tbuffer = sha1_file_to_archive(path + base_len, sha1, mode,\n+\t\t\t\t&type, &size, commit);\n \t\tif (!buffer)\n \t\t\tdie(\"cannot read %s\", sha1_to_hex(sha1));\n \t\tcrc = crc32(crc, buffer, size);\n@@ -321,6 +322,7 @@ int write_zip_archive(struct archiver_args *args)\n \tzip_dir_size = ZIP_DIRECTORY_MIN_SIZE;\n \tverbose = args->verbose;\n \tcommit = args->commit;\n+\tbase_len = args->base ? strlen(args->base) : 0;\n \n \tif (args->base && plen > 0 && args->base[plen - 1] == '/') {\n \t\tchar *base = xstrdup(args->base);\ndiff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh\nindex dca2067..fa62b6a 100755\n--- a/t/t5000-tar-tree.sh\n+++ b/t/t5000-tar-tree.sh\n@@ -109,9 +109,10 @@ test_expect_success \\\n     'diff -r a c/prefix/a'\n \n test_expect_success \\\n-    'create an archive with a substfiles' \\\n+    'create archives with substfiles' \\\n     'echo \"substfile?\" export-subst >a/.gitattributes &&\n      git archive HEAD >f.tar &&\n+     git archive --prefix=prefix/ HEAD >g.tar &&\n      rm a/.gitattributes'\n \n test_expect_success \\\n@@ -127,6 +128,18 @@ test_expect_success \\\n '\n \n test_expect_success \\\n+    'extract substfiles from archive with prefix' \\\n+    '(mkdir g && cd g && $TAR xf -) <g.tar'\n+\n+test_expect_success \\\n+     'validate substfile contents from archive with prefix' \\\n+     'git log --max-count=1 \"--pretty=format:A${SUBSTFORMAT}O\" HEAD \\\n+      >g/prefix/a/substfile1.expected &&\n+      diff g/prefix/a/substfile1.expected g/prefix/a/substfile1 &&\n+      diff a/substfile2 g/prefix/a/substfile2\n+'\n+\n+test_expect_success \\\n     'git archive --format=zip' \\\n     'git archive --format=zip HEAD >d.zip'\n \n"},{"id":"73994","messageId":"a1b6cb1b0804091558r42b365cbjf216b3a5167588cf@mail.gmail.com","threadId":"13026","inReplyTo":"47FD31C3.8070001@lsrfire.ath.cx","subject":"Re: [PATCH] git-archive: ignore prefix when checking file attribute","fromName":"Ulrik Sverdrup","fromEmail":"ulrik.sverdrup@gmail.com","sentAt":"2008-04-09T22:58:26Z","receivedAt":"2008-04-09T22:58:26Z","isPatch":true,"sender":{"key":"ulrik.sverdrup@gmail.com","avatar":null},"body":"2008/4/9, René Scharfe <rene.scharfe@lsrfire.ath.cx>:\n> Ulrik Sverdrup noticed that git-archive doesn't correctly apply the attribute\n>  export-subst when the option --prefix is given, too.\n>\n>  When it checked if a file has the attribute turned on, git-archive would try\n>  to look up the full path -- including the prefix -- in .gitattributes.  That's\n>  wrong, as the prefix doesn't need to have any relation to any existing\n>  directories, tracked or not.\n>\n>  This patch makes git-archive ignore the prefix when looking up if value of the\n>  attribute export-subst for a file.\n>\n>  Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n>  ---\n>  Ulrik, does this patch fix the problem for you?  It applies to v1.5.4.4 and\n>  v1.5.5 equally.\n\n\nThanks for the patch René!\n\nFirst I reproduced my problem on vanilla git v1.5.5, and it is still\nthere as in v.1.5.4.4.\nI applied this patch and that fixed my issue, testing both tar and zip.\n\nUlrik Sverdrup\n"},{"id":"74052","messageId":"7vskxuvuzx.fsf@gitster.siamese.dyndns.org","threadId":"13026","inReplyTo":"47FD31C3.8070001@lsrfire.ath.cx","subject":"Re: [PATCH] git-archive: ignore prefix when checking file attribute","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-04-10T10:56:18Z","receivedAt":"2008-04-10T10:56:18Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thanks.  Will apply as a fix.\n\nIn the longer term, we probably should teach attributes.c to optionally\nread from a tree (introduce git_attribute_set_tree() call upfront, or\nsomething like that, but I haven't thought through the details yet), so\nthat archive can ignore what happens to be checked out.\n"}]}