diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2015-06-19 21:11:12 +0300 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2015-06-26 09:00:20 +0300 |
commit | 619423f238b2c782173d1cf83864cbd1219ab6a7 (patch) | |
tree | 55901a936065e543a019292cd8ca8fa2891dd677 /tests/diff/workdir.c | |
parent | c0280bdd1542640a83d732d7c50c30dddf4dc8df (diff) |
diff: test we don't update index unnecessarily
Test that workdir diffs, when presented with UPDATE_INDEX, only
write the index when they actually make a change.
Diffstat (limited to 'tests/diff/workdir.c')
-rw-r--r-- | tests/diff/workdir.c | 76 |
1 files changed, 69 insertions, 7 deletions
diff --git a/tests/diff/workdir.c b/tests/diff/workdir.c index 13de6a98b..34bad48b3 100644 --- a/tests/diff/workdir.c +++ b/tests/diff/workdir.c @@ -1544,19 +1544,21 @@ void test_diff_workdir__with_stale_index(void) static int touch_file(void *payload, git_buf *path) { - int fd; - char b; + struct stat st; + struct timeval times[2]; 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)); + cl_must_pass(p_stat(path->ptr, &st)); + + times[0].tv_sec = st.st_mtime + 3; + times[0].tv_usec = 0; + times[1].tv_sec = st.st_mtime + 3; + times[1].tv_usec = 0; + cl_must_pass(p_utimes(path->ptr, times)); return 0; } @@ -1783,3 +1785,63 @@ void test_diff_workdir__to_index_conflicted(void) { git_index_free(index); git_tree_free(a); } + +void test_diff_workdir__only_writes_index_when_necessary(void) +{ + git_index *index; + git_diff_options opts = GIT_DIFF_OPTIONS_INIT; + git_diff *diff = NULL; + git_diff_perfdata perf = GIT_DIFF_PERFDATA_INIT; + git_reference *head; + git_object *head_object; + git_oid initial, first, second; + git_buf path = GIT_BUF_INIT; + struct stat st; + struct timeval times[2]; + + opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED | GIT_DIFF_UPDATE_INDEX; + + g_repo = cl_git_sandbox_init("status"); + + cl_git_pass(git_repository_index(&index, g_repo)); + cl_git_pass(git_repository_head(&head, g_repo)); + cl_git_pass(git_reference_peel(&head_object, head, GIT_OBJ_COMMIT)); + + cl_git_pass(git_reset(g_repo, head_object, GIT_RESET_HARD, NULL)); + + git_oid_cpy(&initial, git_index_checksum(index)); + + /* update the index timestamp to avoid raciness */ + cl_must_pass(p_stat("status/.git/index", &st)); + + times[0].tv_sec = st.st_mtime + 5; + times[0].tv_usec = 0; + times[1].tv_sec = st.st_mtime + 5; + times[1].tv_usec = 0; + + cl_must_pass(p_utimes("status/.git/index", times)); + + /* ensure diff doesn't touch the index */ + cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); + git_diff_free(diff); + + git_oid_cpy(&first, git_index_checksum(index)); + cl_assert(!git_oid_equal(&initial, &first)); + + /* touch all the files so stat times are different */ + cl_git_pass(git_buf_sets(&path, "status")); + cl_git_pass(git_path_direach(&path, 0, touch_file, NULL)); + + cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts)); + git_diff_free(diff); + + /* ensure the second diff did update the index */ + git_oid_cpy(&second, git_index_checksum(index)); + cl_assert(!git_oid_equal(&first, &second)); + + git_buf_free(&path); + git_object_free(head_object); + git_reference_free(head); + git_index_free(index); +} + |