Skip to content
  • Patrick Steinhardt's avatar
    dir-iterator: support iteration in sorted order · de34f265
    Patrick Steinhardt authored and Junio C Hamano's avatar Junio C Hamano committed
    
    
    The `struct dir_iterator` is a helper that allows us to iterate through
    directory entries. This iterator returns entries in the exact same order
    as readdir(3P) does -- or in other words, it guarantees no specific
    order at all.
    
    This is about to become problematic as we are introducing a new reflog
    subcommand to list reflogs. As the "files" backend uses the directory
    iterator to enumerate reflogs, returning reflog names and exposing them
    to the user would inherit the indeterministic ordering. Naturally, it
    would make for a terrible user interface to show a list with no
    discernible order.
    
    While this could be handled at a higher level by the new subcommand
    itself by collecting and ordering the reflogs, this would be inefficient
    because we would first have to collect all reflogs before we can sort
    them, which would introduce additional latency when there are many
    reflogs.
    
    Instead, introduce a new option into the directory iterator that asks
    for its entries to be yielded in lexicographical order. If set, the
    iterator will read all directory entries greedily and sort them before
    we start to iterate over them.
    
    While this will of course also incur overhead as we cannot yield the
    directory entries immediately, it should at least be more efficient than
    having to sort the complete list of reflogs as we only need to sort one
    directory at a time.
    
    This functionality will be used in a follow-up commit.
    
    Signed-off-by: default avatarPatrick Steinhardt <ps@pks.im>
    Signed-off-by: default avatarJunio C Hamano <gitster@pobox.com>
    de34f265