diff options
| author | Ben Straub <bstraub@github.com> | 2012-07-27 20:29:06 -0700 |
|---|---|---|
| committer | Ben Straub <bstraub@github.com> | 2012-07-27 20:31:05 -0700 |
| commit | b31667fb695dab0510cc5fc259e0569ff2a2ef41 (patch) | |
| tree | 859bd97b8f4ec70a9605385fadea1da108556541 /src | |
| parent | 4d83399d35f0d3d489c50f2358bd5481a90ddce5 (diff) | |
| download | libgit2-b31667fb695dab0510cc5fc259e0569ff2a2ef41.tar.gz | |
Checkout: add head- and ref-centric checkouts.
Renamed git_checkout_index to what it really was,
and removed duplicate code from clone.c. Added
git_checkout_ref, which updates HEAD and hands off
to git_checkout_head.
Added tests for the options the caller can pass to
git_checkout_*.
Diffstat (limited to 'src')
| -rw-r--r-- | src/checkout.c | 34 | ||||
| -rw-r--r-- | src/clone.c | 21 |
2 files changed, 27 insertions, 28 deletions
diff --git a/src/checkout.c b/src/checkout.c index 24d2149c8..81389a77a 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -145,7 +145,7 @@ static int checkout_walker(const char *path, const git_tree_entry *entry, void * } -int git_checkout_index(git_repository *repo, git_checkout_opts *opts, git_indexer_stats *stats) +int git_checkout_head(git_repository *repo, git_checkout_opts *opts, git_indexer_stats *stats) { int retcode = GIT_ERROR; git_indexer_stats dummy_stats; @@ -188,12 +188,14 @@ int git_checkout_index(git_repository *repo, git_checkout_opts *opts, git_indexe payload.repo = repo; if (git_repository_odb(&payload.odb, repo) < 0) return GIT_ERROR; - /* TODO: stats->total is never calculated. */ - if (!git_repository_head_tree(&tree, repo)) { - /* Checkout the files */ - if (!git_tree_walk(tree, checkout_walker, GIT_TREEWALK_POST, &payload)) { - retcode = 0; + git_index *idx; + if (!(retcode = git_repository_index(&idx, repo))) { + /* TODO: Make git_index_read_tree fill in stats->total */ + if (!(retcode = git_index_read_tree(idx, tree))) { + retcode = git_tree_walk(tree, checkout_walker, GIT_TREEWALK_POST, &payload); + } + git_index_free(idx); } git_tree_free(tree); } @@ -203,11 +205,25 @@ int git_checkout_index(git_repository *repo, git_checkout_opts *opts, git_indexe } -int git_checkout_head(git_repository *repo, git_checkout_opts *opts, git_indexer_stats *stats) +int git_checkout_reference(git_reference *ref, + git_checkout_opts *opts, + git_indexer_stats *stats) { - /* TODO: read HEAD into index */ + git_repository *repo= git_reference_owner(ref); + git_reference *head = NULL; + int retcode = GIT_ERROR; - return git_checkout_index(repo, opts, stats); + if ((retcode = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0) + return retcode; + + if ((retcode = git_reference_set_target(head, git_reference_name(ref))) < 0) + goto gcr_cleanup; + + retcode = git_checkout_head(git_reference_owner(ref), opts, stats); + +gcr_cleanup: + git_reference_free(head); + return retcode; } diff --git a/src/clone.c b/src/clone.c index 7ae32a067..9b7ab8945 100644 --- a/src/clone.c +++ b/src/clone.c @@ -96,25 +96,8 @@ static int update_head_to_new_branch(git_repository *repo, const git_oid *target git_reference *head; if (!git_reference_lookup(&head, repo, GIT_HEAD_FILE)) { git_buf targetbuf = GIT_BUF_INIT; - if (!git_buf_printf(&targetbuf, "refs/heads/%s", name) && /* TODO: "refs/heads" constant? */ - !git_reference_set_target(head, git_buf_cstr(&targetbuf))) { - /* Read the tree into the index */ - git_commit *commit; - if (!git_commit_lookup(&commit, repo, target)) { - git_tree *tree; - if (!git_commit_tree(&tree, commit)) { - git_index *index; - if (!git_repository_index(&index, repo)) { - if (!git_index_read_tree(index, tree)) { - git_index_write(index); - retcode = 0; - } - git_index_free(index); - } - git_tree_free(tree); - } - git_commit_free(commit); - } + if (!git_buf_printf(&targetbuf, "refs/heads/%s", name)) { + retcode = git_reference_set_target(head, git_buf_cstr(&targetbuf)); } git_buf_free(&targetbuf); git_reference_free(head); |
