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 /src/diff.c | |
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 'src/diff.c')
-rw-r--r-- | src/diff.c | 36 |
1 files changed, 32 insertions, 4 deletions
diff --git a/src/diff.c b/src/diff.c index eae4543fc..caed8bf40 100644 --- a/src/diff.c +++ b/src/diff.c @@ -442,6 +442,14 @@ static int diff_list_apply_options( diff->new_src = tmp_src; } + /* Unset UPDATE_INDEX unless diffing workdir and index */ + if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) && + (!(diff->old_src == GIT_ITERATOR_TYPE_WORKDIR || + diff->new_src == GIT_ITERATOR_TYPE_WORKDIR) || + !(diff->old_src == GIT_ITERATOR_TYPE_INDEX || + diff->new_src == GIT_ITERATOR_TYPE_INDEX))) + diff->opts.flags &= ~GIT_DIFF_UPDATE_INDEX; + /* if ignore_submodules not explicitly set, check diff config */ if (diff->opts.ignore_submodules <= 0) { const git_config_entry *entry; @@ -523,11 +531,14 @@ int git_diff__oid_for_file( entry.file_size = size; entry.path = (char *)path; - return git_diff__oid_for_entry(out, diff, &entry); + return git_diff__oid_for_entry(out, diff, &entry, NULL); } int git_diff__oid_for_entry( - git_oid *out, git_diff *diff, const git_index_entry *src) + git_oid *out, + git_diff *diff, + const git_index_entry *src, + const git_oid *update_match) { int error = 0; git_buf full_path = GIT_BUF_INIT; @@ -595,7 +606,16 @@ int git_diff__oid_for_entry( git_filter_list_free(fl); } - /* TODO: update index for entry if requested */ + /* update index for entry if requested */ + if (!error && update_match && git_oid_equal(out, update_match)) { + git_index *idx; + + if (!(error = git_repository_index(&idx, diff->repo))) { + memcpy(&entry.id, out, sizeof(entry.id)); + error = git_index_add(idx, &entry); + git_index_free(idx); + } + } git_buf_free(&full_path); return error; @@ -776,7 +796,12 @@ static int maybe_modified( */ if (modified_uncertain && git_oid_iszero(&nitem->id)) { if (git_oid_iszero(&noid)) { - if ((error = git_diff__oid_for_entry(&noid, diff, nitem)) < 0) + const git_oid *update_check = + DIFF_FLAG_IS_SET(diff, GIT_DIFF_UPDATE_INDEX) ? + &oitem->id : NULL; + + if ((error = git_diff__oid_for_entry( + &noid, diff, nitem, update_check)) < 0) return error; } @@ -1208,6 +1233,9 @@ int git_diff_index_to_workdir( &b, repo, GIT_ITERATOR_DONT_AUTOEXPAND, pfx, pfx) ); + if (!error && DIFF_FLAG_IS_SET(*diff, GIT_DIFF_UPDATE_INDEX)) + error = git_index_write(index); + return error; } |