Skip to content
  • Junio C Hamano's avatar
    ls-files -k: a directory only can be killed if the index has a non-directory · 2eac2a4c
    Junio C Hamano authored
    
    
    "ls-files -o" and "ls-files -k" both traverse the working tree down
    to find either all untracked paths or those that will be "killed"
    (removed from the working tree to make room) when the paths recorded
    in the index are checked out.  It is necessary to traverse the
    working tree fully when enumerating all the "other" paths, but when
    we are only interested in "killed" paths, we can take advantage of
    the fact that paths that do not overlap with entries in the index
    can never be killed.
    
    The treat_one_path() helper function, which is called during the
    recursive traversal, is the ideal place to implement an
    optimization.
    
    When we are looking at a directory P in the working tree, there are
    three cases:
    
     (1) P exists in the index.  Everything inside the directory P in
         the working tree needs to go when P is checked out from the
         index.
    
     (2) P does not exist in the index, but there is P/Q in the index.
         We know P will stay a directory when we check out the contents
         of the index, but we do not know yet if there is a directory
         P/Q in the working tree to be killed, so we need to recurse.
    
     (3) P does not exist in the index, and there is no P/Q in the index
         to require P to be a directory, either.  Only in this case, we
         know that everything inside P will not be killed without
         recursing.
    
    Note that this helper is called by treat_leading_path() that decides
    if we need to traverse only subdirectories of a single common
    leading directory, which is essential for this optimization to be
    correct.  This caller checks each level of the leading path
    component from shallower directory to deeper ones, and that is what
    allows us to only check if the path appears in the index.  If the
    call to treat_one_path() weren't there, given a path P/Q/R, the real
    traversal may start from directory P/Q/R, even when the index
    records P as a regular file, and we would end up having to check if
    any leading subpath in P/Q/R, e.g. P, appears in the index.
    
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    2eac2a4c