diff options
author | Russell Belfer <rb@github.com> | 2012-11-14 23:29:48 -0800 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2012-11-14 23:29:48 -0800 |
commit | bbe6dbec81d2050fb52b600bc27e2dacdc780e77 (patch) | |
tree | 61f442146ed5b98352016f4ecec3ac4736e10c8c /src | |
parent | bad68c0a998116685ad75cab84210004dd2c5be1 (diff) | |
download | libgit2-bbe6dbec81d2050fb52b600bc27e2dacdc780e77.tar.gz |
Add explicit git_index ptr to diff and checkout
A number of diff APIs and the `git_checkout_index` API take a
`git_repository` object an operate on the index. This updates
them to take a `git_index` pointer explicitly and only fall back
on the `git_repository` index if the index input is NULL. This
makes it easier to operate on a temporary index.
Diffstat (limited to 'src')
-rw-r--r-- | src/checkout.c | 8 | ||||
-rw-r--r-- | src/diff.c | 57 | ||||
-rw-r--r-- | src/diff.h | 6 | ||||
-rw-r--r-- | src/reset.c | 2 | ||||
-rw-r--r-- | src/stash.c | 4 | ||||
-rw-r--r-- | src/status.c | 4 | ||||
-rw-r--r-- | src/submodule.c | 4 |
7 files changed, 47 insertions, 38 deletions
diff --git a/src/checkout.c b/src/checkout.c index bf1ff4f12..03536d446 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -601,12 +601,12 @@ static int checkout_create_submodules( int git_checkout_index( git_repository *repo, + git_index *index, git_checkout_opts *opts) { git_diff_list *diff = NULL; git_diff_options diff_opts = {0}; git_checkout_opts checkout_opts; - checkout_diff_data data; git_buf workdir = GIT_BUF_INIT; uint32_t *actions = NULL; @@ -625,7 +625,7 @@ int git_checkout_index( if (opts && opts->paths.count > 0) diff_opts.pathspec = opts->paths; - if ((error = git_diff_workdir_to_index(&diff, repo, &diff_opts)) < 0) + if ((error = git_diff_workdir_to_index(&diff, repo, index, &diff_opts)) < 0) goto cleanup; if ((error = git_buf_puts(&workdir, git_repository_workdir(repo))) < 0) @@ -706,12 +706,14 @@ int git_checkout_tree( return -1; } + /* TODO: create a temp index, load tree there and check it out */ + /* load paths in tree that match pathspec into index */ if (!(error = git_repository_index(&index, repo)) && !(error = git_index_read_tree_match( index, tree, opts ? &opts->paths : NULL)) && !(error = git_index_write(index))) - error = git_checkout_index(repo, opts); + error = git_checkout_index(repo, NULL, opts); git_index_free(index); git_tree_free(tree); diff --git a/src/diff.c b/src/diff.c index e6634e6d2..728e23712 100644 --- a/src/diff.c +++ b/src/diff.c @@ -753,15 +753,13 @@ fail: } -#define DIFF_FROM_ITERATORS(SETUP, MAKE_FIRST, MAKE_SECOND) \ - int error; \ +#define DIFF_FROM_ITERATORS(MAKE_FIRST, MAKE_SECOND) do { \ git_iterator *a = NULL, *b = NULL; \ char *pfx = opts ? git_pathspec_prefix(&opts->pathspec) : NULL; \ - SETUP; \ if (!(error = MAKE_FIRST) && !(error = MAKE_SECOND)) \ error = diff_from_iterators(diff, repo, a, b, opts); \ git__free(pfx); git_iterator_free(a); git_iterator_free(b); \ - return error + } while (0) int git_diff_tree_to_tree( git_diff_list **diff, @@ -770,49 +768,59 @@ int git_diff_tree_to_tree( git_tree *new_tree, const git_diff_options *opts) { + int error = 0; + + assert(diff && repo); + DIFF_FROM_ITERATORS( - assert(repo && old_tree && new_tree && diff), git_iterator_for_tree_range(&a, repo, old_tree, pfx, pfx), git_iterator_for_tree_range(&b, repo, new_tree, pfx, pfx) ); -} -int git_diff__tree_to_index( - git_diff_list **diff, - git_tree *tree, - git_index *index, - const git_diff_options *opts) -{ - DIFF_FROM_ITERATORS( - git_repository *repo = git_index_owner(index), - git_iterator_for_index_range(&a, index, pfx, pfx), - git_iterator_for_tree_range(&b, repo, tree, pfx, pfx) - ); + return error; } int git_diff_index_to_tree( git_diff_list **diff, git_repository *repo, git_tree *old_tree, + git_index *index, const git_diff_options *opts) { + int error = 0; + + assert(diff && repo); + + if (!index && (error = git_repository_index__weakptr(&index, repo)) < 0) + return error; + DIFF_FROM_ITERATORS( - assert(repo && diff), git_iterator_for_tree_range(&a, repo, old_tree, pfx, pfx), - git_iterator_for_repo_index_range(&b, repo, pfx, pfx) + git_iterator_for_index_range(&b, index, pfx, pfx) ); + + return error; } int git_diff_workdir_to_index( git_diff_list **diff, git_repository *repo, + git_index *index, const git_diff_options *opts) { + int error = 0; + + assert(diff && repo); + + if (!index && (error = git_repository_index__weakptr(&index, repo)) < 0) + return error; + DIFF_FROM_ITERATORS( - assert(repo && diff), - git_iterator_for_repo_index_range(&a, repo, pfx, pfx), + git_iterator_for_index_range(&a, index, pfx, pfx), git_iterator_for_workdir_range(&b, repo, pfx, pfx) ); + + return error; } @@ -822,9 +830,14 @@ int git_diff_workdir_to_tree( git_tree *old_tree, const git_diff_options *opts) { + int error = 0; + + assert(diff && repo); + DIFF_FROM_ITERATORS( - assert(repo && diff), git_iterator_for_tree_range(&a, repo, old_tree, pfx, pfx), git_iterator_for_workdir_range(&b, repo, pfx, pfx) ); + + return error; } diff --git a/src/diff.h b/src/diff.h index 3df7ce6d6..1e3be7593 100644 --- a/src/diff.h +++ b/src/diff.h @@ -61,11 +61,5 @@ extern bool git_diff_delta__should_skip( extern int git_diff__oid_for_file( git_repository *, const char *, uint16_t, git_off_t, git_oid *); -extern int git_diff__tree_to_index( - git_diff_list **diff, - git_tree *tree, - git_index *index, - const git_diff_options *opts); - #endif diff --git a/src/reset.c b/src/reset.c index 69a9c4f04..8f470b26a 100644 --- a/src/reset.c +++ b/src/reset.c @@ -139,7 +139,7 @@ int git_reset( memset(&opts, 0, sizeof(opts)); opts.checkout_strategy = GIT_CHECKOUT_FORCE; - if (git_checkout_index(repo, &opts) < 0) { + if (git_checkout_index(repo, NULL, &opts) < 0) { giterr_set(GITERR_INDEX, "%s - Failed to checkout the index.", ERROR_MSG); goto cleanup; } diff --git a/src/stash.c b/src/stash.c index 2af3ca9fa..627d271f4 100644 --- a/src/stash.c +++ b/src/stash.c @@ -322,10 +322,10 @@ static int build_workdir_tree( if (git_commit_tree(&b_tree, b_commit) < 0) goto cleanup; - if (git_diff_index_to_tree(&diff, repo, b_tree, &opts) < 0) + if (git_diff_index_to_tree(&diff, repo, b_tree, NULL, &opts) < 0) goto cleanup; - if (git_diff_workdir_to_index(&diff2, repo, &opts) < 0) + if (git_diff_workdir_to_index(&diff2, repo, NULL, &opts) < 0) goto cleanup; if (git_diff_merge(diff, diff2) < 0) diff --git a/src/status.c b/src/status.c index 9140ac2a8..b8c15ef92 100644 --- a/src/status.c +++ b/src/status.c @@ -142,11 +142,11 @@ int git_status_foreach_ext( /* TODO: support EXCLUDE_SUBMODULES flag */ if (show != GIT_STATUS_SHOW_WORKDIR_ONLY && - (err = git_diff_index_to_tree(&idx2head, repo, head, &diffopt)) < 0) + (err = git_diff_index_to_tree(&idx2head, repo, head, NULL, &diffopt)) < 0) goto cleanup; if (show != GIT_STATUS_SHOW_INDEX_ONLY && - (err = git_diff_workdir_to_index(&wd2idx, repo, &diffopt)) < 0) + (err = git_diff_workdir_to_index(&wd2idx, repo, NULL, &diffopt)) < 0) goto cleanup; usercb.cb = cb; diff --git a/src/submodule.c b/src/submodule.c index 527d9e453..6eb1c52f7 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -1455,7 +1455,7 @@ static int submodule_wd_status(unsigned int *status, git_submodule *sm) if (sm->ignore == GIT_SUBMODULE_IGNORE_NONE) opt.flags |= GIT_DIFF_INCLUDE_UNTRACKED; - error = git_diff_index_to_tree(&diff, sm_repo, sm_head, &opt); + error = git_diff_index_to_tree(&diff, sm_repo, sm_head, NULL, &opt); if (!error) { if (git_diff_num_deltas(diff) > 0) @@ -1472,7 +1472,7 @@ static int submodule_wd_status(unsigned int *status, git_submodule *sm) /* perform index-to-workdir diff on submodule */ - error = git_diff_workdir_to_index(&diff, sm_repo, &opt); + error = git_diff_workdir_to_index(&diff, sm_repo, NULL, &opt); if (!error) { size_t untracked = |