From: Junio C Hamano Date: Wed, 23 Oct 2013 20:33:08 +0000 (-0700) Subject: Merge branch 'jc/ls-files-killed-optim' into maint X-Git-Tag: v1.8.4.2~8 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/e03a5010b3fc17a1d559ae0fdbcdad63af8e30c9?hp=-c Merge branch 'jc/ls-files-killed-optim' into maint "git ls-files -k" needs to crawl only the part of the working tree that may overlap the paths in the index to find killed files, but shared code with the logic to find all the untracked files, which made it unnecessarily inefficient. * jc/ls-files-killed-optim: dir.c::test_one_path(): work around directory_exists_in_index_icase() breakage t3010: update to demonstrate "ls-files -k" optimization pitfalls ls-files -k: a directory only can be killed if the index has a non-directory dir.c: use the cache_* macro to access the current index --- e03a5010b3fc17a1d559ae0fdbcdad63af8e30c9 diff --combined builtin/ls-files.c index 5cf3e31370,c7eb6f4873..85004460bd --- a/builtin/ls-files.c +++ b/builtin/ls-files.c @@@ -46,14 -46,10 +46,14 @@@ static const char *tag_modified = "" static const char *tag_skip_worktree = ""; static const char *tag_resolve_undo = ""; -static void write_name(const char* name, size_t len) +static void write_name(const char *name) { - write_name_quoted_relative(name, len, prefix, prefix_len, stdout, - line_terminator); + /* + * With "--full-name", prefix_len=0; this caller needs to pass + * an empty string in that case (a NULL is good for ""). + */ + write_name_quoted_relative(name, prefix_len ? prefix : NULL, + stdout, line_terminator); } static void show_dir_entry(const char *tag, struct dir_entry *ent) @@@ -67,7 -63,7 +67,7 @@@ return; fputs(tag, stdout); - write_name(ent->name, ent->len); + write_name(ent->name); } static void show_other_files(struct dir_struct *dir) @@@ -131,7 -127,7 +131,7 @@@ static void show_killed_files(struct di } } -static void show_ce_entry(const char *tag, struct cache_entry *ce) +static void show_ce_entry(const char *tag, const struct cache_entry *ce) { int len = max_prefix_len; @@@ -167,15 -163,13 +167,15 @@@ find_unique_abbrev(ce->sha1,abbrev), ce_stage(ce)); } - write_name(ce->name, ce_namelen(ce)); + write_name(ce->name); if (debug_mode) { - printf(" ctime: %d:%d\n", ce->ce_ctime.sec, ce->ce_ctime.nsec); - printf(" mtime: %d:%d\n", ce->ce_mtime.sec, ce->ce_mtime.nsec); - printf(" dev: %d\tino: %d\n", ce->ce_dev, ce->ce_ino); - printf(" uid: %d\tgid: %d\n", ce->ce_uid, ce->ce_gid); - printf(" size: %d\tflags: %x\n", ce->ce_size, ce->ce_flags); + const struct stat_data *sd = &ce->ce_stat_data; + + printf(" ctime: %d:%d\n", sd->sd_ctime.sec, sd->sd_ctime.nsec); + printf(" mtime: %d:%d\n", sd->sd_mtime.sec, sd->sd_mtime.nsec); + printf(" dev: %d\tino: %d\n", sd->sd_dev, sd->sd_ino); + printf(" uid: %d\tgid: %d\n", sd->sd_uid, sd->sd_gid); + printf(" size: %d\tflags: %x\n", sd->sd_size, ce->ce_flags); } } @@@ -202,12 -196,12 +202,12 @@@ static void show_ru_info(void printf("%s%06o %s %d\t", tag_resolve_undo, ui->mode[i], find_unique_abbrev(ui->sha1[i], abbrev), i + 1); - write_name(path, len); + write_name(path); } } } -static int ce_excluded(struct dir_struct *dir, struct cache_entry *ce) +static int ce_excluded(struct dir_struct *dir, const struct cache_entry *ce) { int dtype = ce_to_dtype(ce); return is_excluded(dir, ce->name, &dtype); @@@ -219,15 -213,17 +219,17 @@@ static void show_files(struct dir_struc /* For cached/deleted files we don't need to even do the readdir */ if (show_others || show_killed) { + if (!show_others) + dir->flags |= DIR_COLLECT_KILLED_ONLY; fill_directory(dir, pathspec); if (show_others) show_other_files(dir); if (show_killed) show_killed_files(dir); } - if (show_cached | show_stage) { + if (show_cached || show_stage) { for (i = 0; i < active_nr; i++) { - struct cache_entry *ce = active_cache[i]; + const struct cache_entry *ce = active_cache[i]; if ((dir->flags & DIR_SHOW_IGNORED) && !ce_excluded(dir, ce)) continue; @@@ -239,9 -235,9 +241,9 @@@ (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce); } } - if (show_deleted | show_modified) { + if (show_deleted || show_modified) { for (i = 0; i < active_nr; i++) { - struct cache_entry *ce = active_cache[i]; + const struct cache_entry *ce = active_cache[i]; struct stat st; int err; if ((dir->flags & DIR_SHOW_IGNORED) && @@@ -277,7 -273,7 +279,7 @@@ static void prune_cache(const char *pre last = active_nr; while (last > first) { int next = (last + first) >> 1; - struct cache_entry *ce = active_cache[next]; + const struct cache_entry *ce = active_cache[next]; if (!strncmp(ce->name, prefix, max_prefix_len)) { first = next+1; continue; @@@ -395,7 -391,7 +397,7 @@@ int report_path_error(const char *ps_ma if (found_dup) continue; - name = quote_path_relative(pathspec[num], -1, &sb, prefix); + name = quote_path_relative(pathspec[num], prefix, &sb); error("pathspec '%s' did not match any file(s) known to git.", name); errors++; @@@ -577,8 -573,8 +579,8 @@@ int cmd_ls_files(int argc, const char * die("ls-files --ignored needs some exclude pattern"); /* With no flags, we default to showing the cached files */ - if (!(show_stage | show_deleted | show_others | show_unmerged | - show_killed | show_modified | show_resolve_undo)) + if (!(show_stage || show_deleted || show_others || show_unmerged || + show_killed || show_modified || show_resolve_undo)) show_cached = 1; if (max_prefix) diff --combined dir.c index 910bfcde4e,1000dc2368..1d63e98987 --- a/dir.c +++ b/dir.c @@@ -472,15 -472,14 +472,14 @@@ static void *read_skip_worktree_file_fr unsigned long sz; enum object_type type; void *data; - struct index_state *istate = &the_index; len = strlen(path); - pos = index_name_pos(istate, path, len); + pos = cache_name_pos(path, len); if (pos < 0) return NULL; - if (!ce_skip_worktree(istate->cache[pos])) + if (!ce_skip_worktree(active_cache[pos])) return NULL; - data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz); + data = read_sha1_file(active_cache[pos]->sha1, &type, &sz); if (!data || type != OBJ_BLOB) { free(data); return NULL; @@@ -821,9 -820,6 +820,9 @@@ static void prep_exclude(struct dir_str dir->basebuf, stk->baselen - 1, dir->basebuf + current, &dt); dir->basebuf[stk->baselen - 1] = '/'; + if (dir->exclude && + dir->exclude->flags & EXC_FLAG_NEGATIVE) + dir->exclude = NULL; if (dir->exclude) { dir->basebuf[stk->baselen] = 0; dir->exclude_stack = stk; @@@ -927,13 -923,13 +926,13 @@@ enum exist_status }; /* - * Do not use the alphabetically stored index to look up + * Do not use the alphabetically sorted index to look up * the directory name; instead, use the case insensitive * name hash. */ static enum exist_status directory_exists_in_index_icase(const char *dirname, int len) { - const struct cache_entry *ce = index_name_exists(&the_index, dirname, len + 1, ignore_case); - struct cache_entry *ce = cache_name_exists(dirname, len + 1, ignore_case); ++ const struct cache_entry *ce = cache_name_exists(dirname, len + 1, ignore_case); unsigned char endchar; if (!ce) @@@ -977,7 -973,7 +976,7 @@@ static enum exist_status directory_exis if (pos < 0) pos = -pos-1; while (pos < active_nr) { - struct cache_entry *ce = active_cache[pos++]; + const struct cache_entry *ce = active_cache[pos++]; unsigned char endchar; if (strncmp(ce->name, dirname, len)) @@@ -1036,7 -1032,9 +1035,7 @@@ static enum path_treatment treat_direct return path_recurse; case index_gitdir: - if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES) - return path_none; - return path_untracked; + return path_none; case index_nonexistent: if (dir->flags & DIR_SHOW_OTHER_DIRECTORIES) @@@ -1113,7 -1111,7 +1112,7 @@@ static int exclude_matches_pathspec(con static int get_index_dtype(const char *path, int len) { int pos; - struct cache_entry *ce; + const struct cache_entry *ce; ce = cache_name_exists(path, len, 0); if (ce) { @@@ -1175,14 -1173,51 +1174,51 @@@ static enum path_treatment treat_one_pa int dtype, struct dirent *de) { int exclude; + int has_path_in_index = !!cache_name_exists(path->buf, path->len, ignore_case); + if (dtype == DT_UNKNOWN) dtype = get_dtype(de, path->buf, path->len); /* Always exclude indexed files */ - if (dtype != DT_DIR && - cache_name_exists(path->buf, path->len, ignore_case)) + if (dtype != DT_DIR && has_path_in_index) return path_none; + /* + * 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. + */ + if ((dir->flags & DIR_COLLECT_KILLED_ONLY) && + (dtype == DT_DIR) && + !has_path_in_index) { + /* + * NEEDSWORK: directory_exists_in_index_icase() + * assumes that one byte past the given path is + * readable and has '/', which needs to be fixed, but + * until then, work it around in the caller. + */ + strbuf_addch(path, '/'); + if (directory_exists_in_index(path->buf, path->len - 1) == + index_nonexistent) { + strbuf_setlen(path, path->len - 1); + return path_none; + } + strbuf_setlen(path, path->len - 1); + } + exclude = is_excluded(dir, path->buf, &dtype); /* @@@ -1543,9 -1578,9 +1579,9 @@@ void setup_standard_excludes(struct dir home_config_paths(NULL, &xdg_path, "ignore"); excludes_file = xdg_path; } - if (!access_or_warn(path, R_OK)) + if (!access_or_warn(path, R_OK, 0)) add_excludes_from_file(dir, path); - if (excludes_file && !access_or_warn(excludes_file, R_OK)) + if (excludes_file && !access_or_warn(excludes_file, R_OK, 0)) add_excludes_from_file(dir, excludes_file); } diff --combined t/t3010-ls-files-killed-modified.sh index f611d799b6,ab1deae3b2..6d3b828a95 --- a/t/t3010-ls-files-killed-modified.sh +++ b/t/t3010-ls-files-killed-modified.sh @@@ -11,8 -11,7 +11,9 @@@ This test prepares the following in th path1 - a symlink path2/file2 - a file in a directory path3/file3 - a file in a directory + pathx/ju - a file in a directory + submod1/ - a submodule + submod2/ - another submodule and the following on the filesystem: @@@ -23,11 -22,10 +24,12 @@@ path4 - a file path5 - a symlink path6/file6 - a file in a directory + pathx/ju/nk - a file in a directory to be killed + submod1/ - a submodule (modified from the cache) + submod2/ - a submodule (matches the cache) -git ls-files -k should report that existing filesystem -objects except path4, path5 and path6/file6 to be killed. +git ls-files -k should report that existing filesystem objects +path0/*, path1/*, path2 and path3 to be killed. Also for modification test, the cache and working tree have: @@@ -37,82 -35,82 +39,91 @@@ path10 - a non-empty file, cache dirtied. We should report path0, path1, path2/file2, path3/file3, path7 and path8 -modified without reporting path9 and path10. +modified without reporting path9 and path10. submod1 is also modified. ' . ./test-lib.sh -date >path0 -if test_have_prereq SYMLINKS -then - ln -s xyzzy path1 -else - date > path1 -fi -mkdir path2 path3 pathx -date >path2/file2 -date >path3/file3 ->pathx/ju -: >path7 -date >path8 -: >path9 -date >path10 -test_expect_success \ - 'git update-index --add to add various paths.' \ - "git update-index --add -- path0 path1 path?/file? pathx/ju path7 path8 path9 path10" - -rm -fr path? ;# leave path10 alone -date >path2 -if test_have_prereq SYMLINKS -then - ln -s frotz path3 - ln -s nitfol path5 -else - date > path3 - date > path5 -fi -mkdir -p path0 path1 path6 pathx/ju -date >path0/file0 -date >path1/file1 -date >path6/file6 -date >path7 -: >path8 -: >path9 -touch path10 ->pathx/ju/nk - -cat >.expected <.output && - test_cmp .expected .output +test_expect_success 'git update-index --add to add various paths.' ' + date >path0 && + test_ln_s_add xyzzy path1 && - mkdir path2 path3 && ++ mkdir path2 path3 pathx && + date >path2/file2 && + date >path3/file3 && ++ >pathx/ju && + : >path7 && + date >path8 && + : >path9 && + date >path10 && - git update-index --add -- path0 path?/file? path7 path8 path9 path10 && ++ git update-index --add -- path0 path?/file? pathx/ju path7 path8 path9 path10 && + for i in 1 2 + do + git init submod$i && + ( + cd submod$i && git commit --allow-empty -m "empty $i" + ) || break + done && + git update-index --add submod[12] + ( + cd submod1 && + git commit --allow-empty -m "empty 1 (updated)" + ) && + rm -fr path? # leave path10 alone ' -test_expect_success 'git ls-files -k to show killed files (w/ icase)' ' - git -c core.ignorecase=true ls-files -k >.output && - test_cmp .expected .output +test_expect_success 'git ls-files -k to show killed files.' ' + date >path2 && + if test_have_prereq SYMLINKS + then + ln -s frotz path3 && + ln -s nitfol path5 + else + date >path3 && + date >path5 + fi && - mkdir path0 path1 path6 && ++ mkdir -p path0 path1 path6 pathx/ju && + date >path0/file0 && + date >path1/file1 && + date >path6/file6 && + date >path7 && + : >path8 && + : >path9 && + touch path10 && - git ls-files -k >.output - ' - - test_expect_success 'validate git ls-files -k output.' ' - cat >.expected <<-\EOF && ++ >pathx/ju/nk && ++ cat >.expected <<-\EOF + path0/file0 + path1/file1 + path2 + path3 ++ pathx/ju/nk + EOF + ' + -test_expect_success \ - 'git ls-files -m to show modified files.' \ - 'git ls-files -m >.output' -cat >.expected <.output ++ test_cmp .expected .output ++' ++ ++test_expect_success 'git ls-files -k output (w/ icase)' ' ++ git -c core.ignorecase=true ls-files -k >.output + test_cmp .expected .output +' + +test_expect_success 'git ls-files -m to show modified files.' ' + git ls-files -m >.output +' + +test_expect_success 'validate git ls-files -m output.' ' + cat >.expected <<-\EOF && + path0 + path1 + path2/file2 + path3/file3 + path7 + path8 ++ pathx/ju + submod1 + EOF + test_cmp .expected .output +' test_done