{"thread":{"id":"2035","subject":"[PATCH] Fix wrong filename listing bug in git-ls-tree.","startedAt":"2005-10-06T21:52:13Z","lastAt":"2005-10-06T21:52:13Z","messageCount":1,"participants":["robfitz@273k.net"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"9778","messageId":"112863553379-git-send-email-robfitz@273k.net","threadId":"2035","inReplyTo":null,"subject":"[PATCH] Fix wrong filename listing bug in git-ls-tree.","fromName":"","fromEmail":"robfitz@273k.net","sentAt":"2005-10-06T21:52:13Z","receivedAt":"2005-10-06T21:52:13Z","isPatch":true,"sender":{"key":"robfitz@273k.net","avatar":null},"body":"This patch fixes a bug in git-ls-tee in which the wrong filenames are\nlisted if the exact same file and directory contents are present in\nanother location in the tree.\n\nAdded a new series of test cases for directory and filename handling.\n\nSigned-off-by: Robert Fitzsimons <robfitz@273k.net>\n\n---\n\n ls-tree.c                  |   55 ++++++++-------\n t/t3101-ls-tree-dirname.sh |  160 ++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 189 insertions(+), 26 deletions(-)\n create mode 100644 t/t3101-ls-tree-dirname.sh\n\napplies-to: 9eec9ff9326032bca405a12a265918725edf4ac7\n223dbed3ac931657f88466fec22e30e10197403e\ndiff --git a/ls-tree.c b/ls-tree.c\n--- a/ls-tree.c\n+++ b/ls-tree.c\n@@ -54,11 +54,13 @@ static int prepare_children(struct tree_\n \treturn 0;\n }\n \n-static struct tree_entry_list *find_entry(const char *path)\n+static struct tree_entry_list *find_entry(const char *path, char *pathbuf)\n {\n \tconst char *next, *slash;\n \tint len;\n-\tstruct tree_entry_list *elem = &root_entry;\n+\tstruct tree_entry_list *elem = &root_entry, *oldelem = NULL;\n+\n+\t*(pathbuf) = '\\0';\n \n \t/* Find tree element, descending from root, that\n \t * corresponds to the named path, lazily expanding\n@@ -86,6 +88,10 @@ static struct tree_entry_list *find_entr\n \t\t\tlen = slash - path;\n \t\t}\n \t\tif (len) {\n+\t\t\tif (oldelem) {\n+\t\t\t\tpathbuf += sprintf(pathbuf, \"%s/\", oldelem->name);\n+\t\t\t}\n+\n \t\t\t/* (len == 0) if the original path was \"drivers/char/\"\n \t\t\t * and we have run already two rounds, having elem\n \t\t\t * pointing at the drivers/char directory.\n@@ -101,6 +107,8 @@ static struct tree_entry_list *find_entr\n \t\t\t}\n \t\t\tif (!elem)\n \t\t\t\treturn NULL;\n+\n+\t\t\toldelem = elem;\n \t\t}\n \t\tpath = next;\n \t}\n@@ -108,19 +116,6 @@ static struct tree_entry_list *find_entr\n \treturn elem;\n }\n \n-static void show_entry_name(struct tree_entry_list *e)\n-{\n-\t/* This is yucky.  The root level is there for\n-\t * our convenience but we really want to do a\n-\t * forest.\n-\t */\n-\tif (e->parent && e->parent != &root_entry) {\n-\t\tshow_entry_name(e->parent);\n-\t\tputchar('/');\n-\t}\n-\tprintf(\"%s\", e->name);\n-}\n-\n static const char *entry_type(struct tree_entry_list *e)\n {\n \treturn (e->directory ? \"tree\" : \"blob\");\n@@ -134,28 +129,35 @@ static const char *entry_hex(struct tree\n }\n \n /* forward declaration for mutually recursive routines */\n-static int show_entry(struct tree_entry_list *, int);\n+static int show_entry(struct tree_entry_list *, int, char *pathbuf);\n \n-static int show_children(struct tree_entry_list *e, int level)\n+static int show_children(struct tree_entry_list *e, int level, char *pathbuf)\n {\n+\tint oldlen = strlen(pathbuf);\n+\n+\tif (e != &root_entry)\n+\t\tsprintf(pathbuf + oldlen, \"%s/\", e->name);\n+\n \tif (prepare_children(e))\n \t\tdie(\"internal error: ls-tree show_children called with non tree\");\n \te = e->item.tree->entries;\n \twhile (e) {\n-\t\tshow_entry(e, level);\n+\t\tshow_entry(e, level, pathbuf);\n \t\te = e->next;\n \t}\n+\n+\tpathbuf[oldlen] = '\\0';\n+\n \treturn 0;\n }\n \n-static int show_entry(struct tree_entry_list *e, int level)\n+static int show_entry(struct tree_entry_list *e, int level, char *pathbuf)\n {\n \tint err = 0; \n \n \tif (e != &root_entry) {\n-\t\tprintf(\"%06o %s %s\t\", e->mode, entry_type(e),\n-\t\t       entry_hex(e));\n-\t\tshow_entry_name(e);\n+\t\tprintf(\"%06o %s %s\t%s%s\", e->mode, entry_type(e),\n+\t\t       entry_hex(e), pathbuf, e->name);\n \t\tputchar(line_termination);\n \t}\n \n@@ -176,10 +178,10 @@ static int show_entry(struct tree_entry_\n \t\t */\n \t\tif (level == 0 && !(ls_options & LS_TREE_ONLY))\n \t\t\t/* case (1)-a and (1)-b */\n-\t\t\terr = err | show_children(e, level+1);\n+\t\t\terr = err | show_children(e, level+1, pathbuf);\n \t\telse if (level && ls_options & LS_RECURSIVE)\n \t\t\t/* case (2)-b */\n-\t\t\terr = err | show_children(e, level+1);\n+\t\t\terr = err | show_children(e, level+1, pathbuf);\n \t}\n \treturn err;\n }\n@@ -187,7 +189,8 @@ static int show_entry(struct tree_entry_\n static int list_one(const char *path)\n {\n \tint err = 0;\n-\tstruct tree_entry_list *e = find_entry(path);\n+\tchar pathbuf[MAXPATHLEN + 1];\n+\tstruct tree_entry_list *e = find_entry(path, pathbuf);\n \tif (!e) {\n \t\t/* traditionally ls-tree does not complain about\n \t\t * missing path.  We may change this later to match\n@@ -195,7 +198,7 @@ static int list_one(const char *path)\n \t\t */\n \t\treturn err;\n \t}\n-\terr = err | show_entry(e, 0);\n+\terr = err | show_entry(e, 0, pathbuf);\n \treturn err;\n }\n \ndiff --git a/t/t3101-ls-tree-dirname.sh b/t/t3101-ls-tree-dirname.sh\nnew file mode 100644\n--- /dev/null\n+++ b/t/t3101-ls-tree-dirname.sh\n@@ -0,0 +1,160 @@\n+#!/bin/sh\n+#\n+# Copyright (c) 2005 Junio C Hamano\n+# Copyright (c) 2005 Robert Fitzsimons\n+#\n+\n+test_description='git-ls-tree directory and filenames handling.\n+\n+This test runs git-ls-tree with the following in a tree.\n+\n+    1.txt              - a file\n+    2.txt              - a file\n+    path0/a/b/c/1.txt  - a file in a directory\n+    path1/b/c/1.txt    - a file in a directory\n+    path2/1.txt        - a file in a directory\n+    path3/1.txt        - a file in a directory\n+    path3/2.txt        - a file in a directory\n+\n+Test the handling of mulitple directories which have matching file\n+entries.  Also test odd filename and missing entries handling.\n+'\n+. ./test-lib.sh\n+\n+test_expect_success \\\n+    'setup' \\\n+    'echo 111 >1.txt &&\n+     echo 222 >2.txt &&\n+     mkdir path0 path0/a path0/a/b path0/a/b/c &&\n+     echo 111 >path0/a/b/c/1.txt &&\n+     mkdir path1 path1/b path1/b/c &&\n+     echo 111 >path1/b/c/1.txt &&\n+     mkdir path2 &&\n+     echo 111 >path2/1.txt &&\n+     mkdir path3 &&\n+     echo 111 >path3/1.txt &&\n+     echo 222 >path3/2.txt &&\n+     find *.txt path* \\( -type f -o -type l \\) -print |\n+     xargs git-update-index --add &&\n+     tree=`git-write-tree` &&\n+     echo $tree'\n+\n+_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'\n+_x40=\"$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40\"\n+test_output () {\n+    sed -e \"s/ $_x40\t/ X\t/\" <current >check\n+    diff -u expected check\n+}\n+\n+test_expect_success \\\n+    'ls-tree plain' \\\n+    'git-ls-tree $tree >current &&\n+     cat >expected <<\\EOF &&\n+100644 blob X\t1.txt\n+100644 blob X\t2.txt\n+040000 tree X\tpath0\n+040000 tree X\tpath1\n+040000 tree X\tpath2\n+040000 tree X\tpath3\n+EOF\n+     test_output'\n+\n+test_expect_success \\\n+    'ls-tree recursive' \\\n+    'git-ls-tree -r $tree >current &&\n+     cat >expected <<\\EOF &&\n+100644 blob X\t1.txt\n+100644 blob X\t2.txt\n+040000 tree X\tpath0\n+040000 tree X\tpath0/a\n+040000 tree X\tpath0/a/b\n+040000 tree X\tpath0/a/b/c\n+100644 blob X\tpath0/a/b/c/1.txt\n+040000 tree X\tpath1\n+040000 tree X\tpath1/b\n+040000 tree X\tpath1/b/c\n+100644 blob X\tpath1/b/c/1.txt\n+040000 tree X\tpath2\n+100644 blob X\tpath2/1.txt\n+040000 tree X\tpath3\n+100644 blob X\tpath3/1.txt\n+100644 blob X\tpath3/2.txt\n+EOF\n+     test_output'\n+\n+test_expect_success \\\n+    'ls-tree filter 1.txt' \\\n+    'git-ls-tree $tree 1.txt >current &&\n+     cat >expected <<\\EOF &&\n+100644 blob X\t1.txt\n+EOF\n+     test_output'\n+\n+test_expect_success \\\n+    'ls-tree filter path1/b/c/1.txt' \\\n+    'git-ls-tree $tree path1/b/c/1.txt >current &&\n+     cat >expected <<\\EOF &&\n+100644 blob X\tpath1/b/c/1.txt\n+EOF\n+     test_output'\n+\n+test_expect_success \\\n+    'ls-tree filter all 1.txt files' \\\n+    'git-ls-tree $tree 1.txt path0/a/b/c/1.txt path1/b/c/1.txt path2/1.txt path3/1.txt >current &&\n+     cat >expected <<\\EOF &&\n+100644 blob X\t1.txt\n+100644 blob X\tpath0/a/b/c/1.txt\n+100644 blob X\tpath1/b/c/1.txt\n+100644 blob X\tpath2/1.txt\n+100644 blob X\tpath3/1.txt\n+EOF\n+     test_output'\n+\n+test_expect_success \\\n+    'ls-tree filter directories' \\\n+    'git-ls-tree $tree path3 path2 path0/a/b/c path1/b/c path0/a >current &&\n+     cat >expected <<\\EOF &&\n+040000 tree X\tpath3\n+100644 blob X\tpath3/1.txt\n+100644 blob X\tpath3/2.txt\n+040000 tree X\tpath2\n+100644 blob X\tpath2/1.txt\n+040000 tree X\tpath0/a/b/c\n+100644 blob X\tpath0/a/b/c/1.txt\n+040000 tree X\tpath1/b/c\n+100644 blob X\tpath1/b/c/1.txt\n+040000 tree X\tpath0/a\n+040000 tree X\tpath0/a/b\n+EOF\n+     test_output'\n+\n+test_expect_success \\\n+    'ls-tree filter odd names' \\\n+    'git-ls-tree $tree 1.txt /1.txt //1.txt path3/1.txt /path3/1.txt //path3//1.txt path3 /path3/ path3// >current &&\n+     cat >expected <<\\EOF &&\n+100644 blob X\t1.txt\n+100644 blob X\t1.txt\n+100644 blob X\t1.txt\n+100644 blob X\tpath3/1.txt\n+100644 blob X\tpath3/1.txt\n+100644 blob X\tpath3/1.txt\n+040000 tree X\tpath3\n+100644 blob X\tpath3/1.txt\n+100644 blob X\tpath3/2.txt\n+040000 tree X\tpath3\n+100644 blob X\tpath3/1.txt\n+100644 blob X\tpath3/2.txt\n+040000 tree X\tpath3\n+100644 blob X\tpath3/1.txt\n+100644 blob X\tpath3/2.txt\n+EOF\n+     test_output'\n+\n+test_expect_success \\\n+    'ls-tree filter missing files and extra slashes' \\\n+    'git-ls-tree $tree 1.txt/ abc.txt path3//23.txt path3/2.txt/// >current &&\n+     cat >expected <<\\EOF &&\n+EOF\n+     test_output'\n+\n+test_done\n---\n0.99.8.GIT\n"}]}