From bd4ca902b5c8b95106e53fa31f95ab8992cf1b65 Mon Sep 17 00:00:00 2001 From: Russell Belfer Date: Wed, 16 May 2012 17:02:06 -0700 Subject: [PATCH] Fix status for files under ignored dirs There was a bug where tracked files inside directories that were inside ignored directories where not being found by status. To make that a little clearer, if you have a .gitignore with: ignore/ And then have the following files: ignore/dir/tracked <-- actually a tracked file ignore/dir/untracked <-- should be ignored Then we would show the tracked file as being removed (because when we got the to contained item "dir/" inside the ignored directory, we decided it was safe to skip -- bzzt, wrong!). This update is much more careful about checking that we are not skipping over any prefix of a tracked item, regardless of whether it is ignored or not. As documented in diff.c, this commit does create behavior that still differs from core git with regards to the handling of untracked files contained inside ignored directories. With libgit2, those files will just not show up in status or diff. With core git, those files don't show up in status or diff either *unless* they are explicitly ignored by a .gitignore pattern in which case they show up as ignored files. Needless to say, this is a local behavior difference only, so it should not be important and (to me) the libgit2 behavior seems more consistent. --- src/diff.c | 60 ++++++++---- tests-clar/resources/issue_592b/.gitted/HEAD | 1 + .../resources/issue_592b/.gitted/config | 6 ++ .../resources/issue_592b/.gitted/description | 1 + .../.gitted/hooks/post-update.sample | 8 ++ tests-clar/resources/issue_592b/.gitted/index | Bin 0 -> 376 bytes .../resources/issue_592b/.gitted/info/exclude | 6 ++ .../resources/issue_592b/.gitted/logs/HEAD | 1 + .../issue_592b/.gitted/logs/refs/heads/master | 1 + .../3f/bf1852f72fd268e36457b13a18cdd9a4c9ea35 | 2 + .../6f/a891d3e578c83e1c03bdb9e0fdd8e6e934157f | Bin 0 -> 28 bytes .../80/07d41d5794e6ce4d4d2c97e370d5a9aa6d5213 | Bin 0 -> 24 bytes .../a6/5fb6583a7c425284142f285bc359a2d6565513 | Bin 0 -> 93 bytes .../ae/be7a55922c7097ef91ca3a7bc327a901d87c2c | Bin 0 -> 122 bytes .../b3/44b055867fcdc1f01eaa75056a43e868eb4fbc | Bin 0 -> 36 bytes .../f7/d75fbfad8b1d2e307ced287ea78aad403cdce3 | Bin 0 -> 57 bytes .../issue_592b/.gitted/refs/heads/master | 1 + tests-clar/resources/issue_592b/gitignore | 1 + .../issue_592b/ignored/contained/ignored3.txt | 1 + .../issue_592b/ignored/contained/tracked3.txt | 1 + .../resources/issue_592b/ignored/ignored2.txt | 1 + .../resources/issue_592b/ignored/tracked2.txt | 1 + tests-clar/resources/issue_592b/ignored1.txt | 1 + tests-clar/resources/issue_592b/tracked1.txt | 1 + tests-clar/status/worktree.c | 90 ++++++++++++++---- 25 files changed, 148 insertions(+), 36 deletions(-) create mode 100644 tests-clar/resources/issue_592b/.gitted/HEAD create mode 100644 tests-clar/resources/issue_592b/.gitted/config create mode 100644 tests-clar/resources/issue_592b/.gitted/description create mode 100755 tests-clar/resources/issue_592b/.gitted/hooks/post-update.sample create mode 100644 tests-clar/resources/issue_592b/.gitted/index create mode 100644 tests-clar/resources/issue_592b/.gitted/info/exclude create mode 100644 tests-clar/resources/issue_592b/.gitted/logs/HEAD create mode 100644 tests-clar/resources/issue_592b/.gitted/logs/refs/heads/master create mode 100644 tests-clar/resources/issue_592b/.gitted/objects/3f/bf1852f72fd268e36457b13a18cdd9a4c9ea35 create mode 100644 tests-clar/resources/issue_592b/.gitted/objects/6f/a891d3e578c83e1c03bdb9e0fdd8e6e934157f create mode 100644 tests-clar/resources/issue_592b/.gitted/objects/80/07d41d5794e6ce4d4d2c97e370d5a9aa6d5213 create mode 100644 tests-clar/resources/issue_592b/.gitted/objects/a6/5fb6583a7c425284142f285bc359a2d6565513 create mode 100644 tests-clar/resources/issue_592b/.gitted/objects/ae/be7a55922c7097ef91ca3a7bc327a901d87c2c create mode 100644 tests-clar/resources/issue_592b/.gitted/objects/b3/44b055867fcdc1f01eaa75056a43e868eb4fbc create mode 100644 tests-clar/resources/issue_592b/.gitted/objects/f7/d75fbfad8b1d2e307ced287ea78aad403cdce3 create mode 100644 tests-clar/resources/issue_592b/.gitted/refs/heads/master create mode 100644 tests-clar/resources/issue_592b/gitignore create mode 100644 tests-clar/resources/issue_592b/ignored/contained/ignored3.txt create mode 100644 tests-clar/resources/issue_592b/ignored/contained/tracked3.txt create mode 100644 tests-clar/resources/issue_592b/ignored/ignored2.txt create mode 100644 tests-clar/resources/issue_592b/ignored/tracked2.txt create mode 100644 tests-clar/resources/issue_592b/ignored1.txt create mode 100644 tests-clar/resources/issue_592b/tracked1.txt diff --git a/src/diff.c b/src/diff.c index c8670b53e..d5c0c8ba5 100644 --- a/src/diff.c +++ b/src/diff.c @@ -551,29 +551,27 @@ static int diff_from_iterators( * matched in old (and/or descend into directories as needed) */ else if (nitem && (!oitem || strcmp(oitem->path, nitem->path) > 0)) { - int is_ignored; - git_delta_t delta_type = GIT_DELTA_ADDED; + git_delta_t delta_type = GIT_DELTA_UNTRACKED; - /* contained in ignored parent directory, so this can be skipped. */ + /* check if contained in ignored parent directory */ if (git_buf_len(&ignore_prefix) && git__prefixcmp(nitem->path, git_buf_cstr(&ignore_prefix)) == 0) - { - if (git_iterator_advance(new_iter, &nitem) < 0) - goto fail; - - continue; - } - - is_ignored = git_iterator_current_is_ignored(new_iter); + delta_type = GIT_DELTA_IGNORED; if (S_ISDIR(nitem->mode)) { - /* recurse into directory if explicitly requested or - * if there are tracked items inside the directory + /* recurse into directory only if there are tracked items in + * it or if the user requested the contents of untracked + * directories and it is not under an ignored directory. */ - if ((diff->opts.flags & GIT_DIFF_RECURSE_UNTRACKED_DIRS) || - (oitem && git__prefixcmp(oitem->path, nitem->path) == 0)) + if ((oitem && git__prefixcmp(oitem->path, nitem->path) == 0) || + (delta_type == GIT_DELTA_UNTRACKED && + (diff->opts.flags & GIT_DIFF_RECURSE_UNTRACKED_DIRS) != 0)) { - if (is_ignored) + /* if this directory is ignored, remember it as the + * "ignore_prefix" for processing contained items + */ + if (delta_type == GIT_DELTA_UNTRACKED && + git_iterator_current_is_ignored(new_iter)) git_buf_sets(&ignore_prefix, nitem->path); if (git_iterator_advance_into_directory(new_iter, &nitem) < 0) @@ -581,12 +579,34 @@ static int diff_from_iterators( continue; } - delta_type = GIT_DELTA_UNTRACKED; } - else if (is_ignored) + + /* In core git, the next two "else if" clauses are effectively + * reversed -- i.e. when an untracked file contained in an + * ignored directory is individually ignored, it shows up as an + * ignored file in the diff list, even though other untracked + * files in the same directory are skipped completely. + * + * To me, this is odd. If the directory is ignored and the file + * is untracked, we should skip it consistently, regardless of + * whether it happens to match a pattern in the ignore file. + * + * To match the core git behavior, just reverse the following + * two "else if" cases so that individual file ignores are + * checked before container directory exclusions are used to + * skip the file. + */ + else if (delta_type == GIT_DELTA_IGNORED) { + if (git_iterator_advance(new_iter, &nitem) < 0) + goto fail; + continue; /* ignored parent directory, so skip completely */ + } + + else if (git_iterator_current_is_ignored(new_iter)) delta_type = GIT_DELTA_IGNORED; - else if (new_iter->type == GIT_ITERATOR_WORKDIR) - delta_type = GIT_DELTA_UNTRACKED; + + else if (new_iter->type != GIT_ITERATOR_WORKDIR) + delta_type = GIT_DELTA_ADDED; if (diff_delta__from_one(diff, delta_type, nitem) < 0 || git_iterator_advance(new_iter, &nitem) < 0) diff --git a/tests-clar/resources/issue_592b/.gitted/HEAD b/tests-clar/resources/issue_592b/.gitted/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/tests-clar/resources/issue_592b/.gitted/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/tests-clar/resources/issue_592b/.gitted/config b/tests-clar/resources/issue_592b/.gitted/config new file mode 100644 index 000000000..af107929f --- /dev/null +++ b/tests-clar/resources/issue_592b/.gitted/config @@ -0,0 +1,6 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true + ignorecase = true diff --git a/tests-clar/resources/issue_592b/.gitted/description b/tests-clar/resources/issue_592b/.gitted/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/tests-clar/resources/issue_592b/.gitted/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/tests-clar/resources/issue_592b/.gitted/hooks/post-update.sample b/tests-clar/resources/issue_592b/.gitted/hooks/post-update.sample new file mode 100755 index 000000000..ec17ec193 --- /dev/null +++ b/tests-clar/resources/issue_592b/.gitted/hooks/post-update.sample @@ -0,0 +1,8 @@ +#!/bin/sh +# +# An example hook script to prepare a packed repository for use over +# dumb transports. +# +# To enable this hook, rename this file to "post-update". + +exec git update-server-info diff --git a/tests-clar/resources/issue_592b/.gitted/index b/tests-clar/resources/issue_592b/.gitted/index new file mode 100644 index 0000000000000000000000000000000000000000..5964382162a9cc3d173c54eeee943be80085da9b GIT binary patch literal 376 zcmZ?q402{*U|<4b7XK|Oen6T5M)Lv17*>|7XJBYt!oa}z6(}VF#GDQ6S7gJdJUi#> zt26y^!PS+ka)X2!xb)I9OES~*@{3YIW`KbI7UdJjG}OE-H1k9@yKD$;t3P}2gWRf8 z)-2~28L$2KFvvm7O3_cw&nrpH%u7wtFDXh)&Q47+)+?zfftafTau3M8U>a(EADa0* z`70(~ep+$DPKJ5!&If;QJbP&(TF)Q?GaqWI5zJJud0bF)!8Fv|PNK}^ftqKCFz?bE X?hg~%?(*1Bp literal 0 HcmV?d00001 diff --git a/tests-clar/resources/issue_592b/.gitted/info/exclude b/tests-clar/resources/issue_592b/.gitted/info/exclude new file mode 100644 index 000000000..a5196d1be --- /dev/null +++ b/tests-clar/resources/issue_592b/.gitted/info/exclude @@ -0,0 +1,6 @@ +# git ls-files --others --exclude-from=.git/info/exclude +# Lines that start with '#' are comments. +# For a project mostly in C, the following would be a good set of +# exclude patterns (uncomment them if you want to use them): +# *.[oa] +# *~ diff --git a/tests-clar/resources/issue_592b/.gitted/logs/HEAD b/tests-clar/resources/issue_592b/.gitted/logs/HEAD new file mode 100644 index 000000000..6f3ba90cc --- /dev/null +++ b/tests-clar/resources/issue_592b/.gitted/logs/HEAD @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 3fbf1852f72fd268e36457b13a18cdd9a4c9ea35 Russell Belfer 1337205933 -0700 commit (initial): Initial commit diff --git a/tests-clar/resources/issue_592b/.gitted/logs/refs/heads/master b/tests-clar/resources/issue_592b/.gitted/logs/refs/heads/master new file mode 100644 index 000000000..6f3ba90cc --- /dev/null +++ b/tests-clar/resources/issue_592b/.gitted/logs/refs/heads/master @@ -0,0 +1 @@ +0000000000000000000000000000000000000000 3fbf1852f72fd268e36457b13a18cdd9a4c9ea35 Russell Belfer 1337205933 -0700 commit (initial): Initial commit diff --git a/tests-clar/resources/issue_592b/.gitted/objects/3f/bf1852f72fd268e36457b13a18cdd9a4c9ea35 b/tests-clar/resources/issue_592b/.gitted/objects/3f/bf1852f72fd268e36457b13a18cdd9a4c9ea35 new file mode 100644 index 000000000..6eaf64b46 --- /dev/null +++ b/tests-clar/resources/issue_592b/.gitted/objects/3f/bf1852f72fd268e36457b13a18cdd9a4c9ea35 @@ -0,0 +1,2 @@ +xK +1]}%BwnAq xzVƃv ɂc&%9@9xdu.]".=EבO+ۘBEk\N_<>E U%9 \ No newline at end of file diff --git a/tests-clar/resources/issue_592b/.gitted/objects/6f/a891d3e578c83e1c03bdb9e0fdd8e6e934157f b/tests-clar/resources/issue_592b/.gitted/objects/6f/a891d3e578c83e1c03bdb9e0fdd8e6e934157f new file mode 100644 index 0000000000000000000000000000000000000000..c4becfe2f1e3346ab4868611c31637ca67503936 GIT binary patch literal 28 kcmb`T0E##X@c;k- literal 0 HcmV?d00001 diff --git a/tests-clar/resources/issue_592b/.gitted/objects/a6/5fb6583a7c425284142f285bc359a2d6565513 b/tests-clar/resources/issue_592b/.gitted/objects/a6/5fb6583a7c425284142f285bc359a2d6565513 new file mode 100644 index 0000000000000000000000000000000000000000..9b74072213b43924e48b2b521487d31512f371d6 GIT binary patch literal 93 zcmV-j0HXhR0V^p=O;s>AV=yrQ0)^!KypqJsywnti@7Lq^ukDuAGpKp1QMbHnt%J>- z$A$(5W+o;IB}IwJ*{LZ;dL-Z@Z literal 0 HcmV?d00001 diff --git a/tests-clar/resources/issue_592b/.gitted/objects/ae/be7a55922c7097ef91ca3a7bc327a901d87c2c b/tests-clar/resources/issue_592b/.gitted/objects/ae/be7a55922c7097ef91ca3a7bc327a901d87c2c new file mode 100644 index 0000000000000000000000000000000000000000..1494ed8229d77d650cdf2aef711ce298a15c7a0f GIT binary patch literal 122 zcmV-=0EPc}0V^p=O;s>7G-5C`FfcPQQP4}zEXhpI%P&f0Xkfo08$RXPIbUC$>5mJp zu3VKHBy3^;1PTxZDGbZvw?$ahI0dzc=xanDj$CvtEL0e3PDxQ>a&~Hpp4U@$Z=Ff%bxC@D%z&Q47+)+?zfVc6`lA+)Xj?7