diff options
author | Russell Belfer <rb@github.com> | 2014-04-28 14:48:41 -0700 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2014-05-02 09:21:33 -0700 |
commit | 94fb4aadc80c927a59696dc01db03f3a0629dae7 (patch) | |
tree | c01ad832b1656dcaf9d7e246ae9c3a58586fd838 /tests | |
parent | 0fc8e1f6bd9a5148d3a262142e9a70126f5c3a42 (diff) | |
download | libgit2-94fb4aadc80c927a59696dc01db03f3a0629dae7.tar.gz |
Add diff option to update index stat cache
When diff is scanning the working directory, if it finds a file
where it is not sure if the index entry matches the working dir,
it will recalculate the OID (which is pretty expensive). This
adds a new flag to diff so that if the OID calculation finds that
the file actually has not changed (i.e. just the modified time was
altered or such), then it will refresh the stat cache in the index
so that future calls to diff will not have to check the oid again.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/diff/workdir.c | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/tests/diff/workdir.c b/tests/diff/workdir.c index 84c8866e0..9e4608e9d 100644 --- a/tests/diff/workdir.c +++ b/tests/diff/workdir.c @@ -1502,3 +1502,90 @@ void test_diff_workdir__with_stale_index(void) git_index_free(idx); } + +static int touch_file(void *payload, git_buf *path) +{ + int fd; + char b; + + GIT_UNUSED(payload); + if (git_path_isdir(path->ptr)) + return 0; + + cl_assert((fd = p_open(path->ptr, O_RDWR)) >= 0); + cl_assert_equal_i(1, p_read(fd, &b, 1)); + cl_must_pass(p_lseek(fd, 0, SEEK_SET)); + cl_must_pass(p_write(fd, &b, 1)); + cl_must_pass(p_close(fd)); + + return 0; +} + +static void basic_diff_status(git_diff **out, const git_diff_options *opts) +{ + diff_expects exp; + + cl_git_pass(git_diff_index_to_workdir(out, g_repo, NULL, opts)); + + memset(&exp, 0, sizeof(exp)); + + cl_git_pass(git_diff_foreach( + *out, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp)); + + cl_assert_equal_i(13, exp.files); + cl_assert_equal_i(0, exp.file_status[GIT_DELTA_ADDED]); + cl_assert_equal_i(4, exp.file_status[GIT_DELTA_DELETED]); + cl_assert_equal_i(4, exp.file_status[GIT_DELTA_MODIFIED]); + cl_assert_equal_i(1, exp.file_status[GIT_DELTA_IGNORED]); + cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNTRACKED]); +} + +void test_diff_workdir__can_update_index(void) +{ + git_diff_options opts = GIT_DIFF_OPTIONS_INIT; + git_diff *diff = NULL; + + g_repo = cl_git_sandbox_init("status"); + + /* touch all the files so stat times are different */ + { + git_buf path = GIT_BUF_INIT; + cl_git_pass(git_buf_sets(&path, "status")); + cl_git_pass(git_path_direach(&path, 0, touch_file, NULL)); + git_buf_free(&path); + } + + opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; + + basic_diff_status(&diff, &opts); +#ifdef GIT_PERF + cl_assert_equal_sz(diff->stat_calls, 13 + 3); + cl_assert_equal_sz(diff->oid_calculations, 5); + cl_assert_equal_sz(diff->submodule_lookups, 1); +#endif + + git_diff_free(diff); + + /* now allow diff to update stat cache */ + opts.flags |= GIT_DIFF_UPDATE_INDEX; + + basic_diff_status(&diff, &opts); +#ifdef GIT_PERF + cl_assert_equal_sz(diff->stat_calls, 13 + 3); + cl_assert_equal_sz(diff->oid_calculations, 5); + cl_assert_equal_sz(diff->submodule_lookups, 1); +#endif + + git_diff_free(diff); + + /* now if we do it again, we should see fewer OID calculations */ + + basic_diff_status(&diff, &opts); +#ifdef GIT_PERF + cl_assert_equal_sz(diff->stat_calls, 13 + 3); + cl_assert_equal_sz(diff->oid_calculations, 0); /* Yay */ + cl_assert_equal_sz(diff->submodule_lookups, 1); +#endif + + git_diff_free(diff); +} |