From 10749f6ca25179778cf60e411f7de3de1444271d Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Sat, 2 Nov 2013 03:20:05 +0000 Subject: [PATCH 1/3] Checkout: Unifiy const-ness of `opts` parameter Since all 3 checkout APIs perform the same operation with the options, all of them should use the same const-ness. --- include/git2/checkout.h | 4 ++-- src/checkout.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/git2/checkout.h b/include/git2/checkout.h index 3793a4f18..4c6a4ebf7 100644 --- a/include/git2/checkout.h +++ b/include/git2/checkout.h @@ -272,7 +272,7 @@ GIT_EXTERN(int) git_checkout_head( GIT_EXTERN(int) git_checkout_index( git_repository *repo, git_index *index, - git_checkout_opts *opts); + const git_checkout_opts *opts); /** * Updates files in the index and working tree to match the content of the @@ -288,7 +288,7 @@ GIT_EXTERN(int) git_checkout_index( GIT_EXTERN(int) git_checkout_tree( git_repository *repo, const git_object *treeish, - git_checkout_opts *opts); + const git_checkout_opts *opts); /** @} */ GIT_END_DECL diff --git a/src/checkout.c b/src/checkout.c index 76edc6a72..20b964b54 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -2018,7 +2018,7 @@ cleanup: int git_checkout_index( git_repository *repo, git_index *index, - git_checkout_opts *opts) + const git_checkout_opts *opts) { int error; git_iterator *index_i; @@ -2053,7 +2053,7 @@ int git_checkout_index( int git_checkout_tree( git_repository *repo, const git_object *treeish, - git_checkout_opts *opts) + const git_checkout_opts *opts) { int error; git_tree *tree = NULL; From 352214416c0e5fd85ef02077ddf185103f3a92df Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Sat, 2 Nov 2013 03:43:34 +0000 Subject: [PATCH 2/3] Checkout: Don't assert if treeish is NULL In git_checkout_tree, the first check tests if either repo or treeish is NULL and says that eithor of them has to have a valid value. But there is no code to handle the treeish == NULL case. So, do something meaningful in that case: use HEAD instead. --- include/git2/checkout.h | 2 +- src/checkout.c | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/include/git2/checkout.h b/include/git2/checkout.h index 4c6a4ebf7..efafdc3e4 100644 --- a/include/git2/checkout.h +++ b/include/git2/checkout.h @@ -280,7 +280,7 @@ GIT_EXTERN(int) git_checkout_index( * * @param repo repository to check out (must be non-bare) * @param treeish a commit, tag or tree which content will be used to update - * the working directory + * the working directory (or NULL to use HEAD) * @param opts specifies checkout options (may be NULL) * @return 0 on success, GIT_ERROR otherwise (use giterr_last for information * about the error) diff --git a/src/checkout.c b/src/checkout.c index 20b964b54..d818a44bf 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -2073,10 +2073,21 @@ int git_checkout_tree( if (!repo) repo = git_object_owner(treeish); - if (git_object_peel((git_object **)&tree, treeish, GIT_OBJ_TREE) < 0) { - giterr_set( - GITERR_CHECKOUT, "Provided object cannot be peeled to a tree"); - return -1; + if (treeish) { + if (git_object_peel((git_object **)&tree, treeish, GIT_OBJ_TREE) < 0) { + giterr_set( + GITERR_CHECKOUT, "Provided object cannot be peeled to a tree"); + return -1; + } + } + else { + if ((error = checkout_lookup_head_tree(&tree, repo)) < 0) { + if (error != GIT_EUNBORNBRANCH) + giterr_set( + GITERR_CHECKOUT, + "HEAD could not be peeled to a tree and no treeish given"); + return error; + } } if (!(error = git_iterator_for_tree(&tree_i, tree, 0, NULL, NULL))) From 7b3959b22784d63dd6f21bc250602b886d726294 Mon Sep 17 00:00:00 2001 From: Sascha Cunz Date: Sat, 2 Nov 2013 03:45:32 +0000 Subject: [PATCH 3/3] Checkout: git_checkout_head is git_checkout_tree without a treeish The last commit taught git_checkout_tree to actually do something meaningfull, when treeish was NULL. This lets us rewrite git_checkout_head to simply call git_checkout_tree without giving it a treeish. --- src/checkout.c | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/checkout.c b/src/checkout.c index d818a44bf..376d48b86 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -2103,18 +2103,6 @@ int git_checkout_head( git_repository *repo, const git_checkout_opts *opts) { - int error; - git_tree *head = NULL; - git_iterator *head_i = NULL; - assert(repo); - - if (!(error = checkout_lookup_head_tree(&head, repo)) && - !(error = git_iterator_for_tree(&head_i, head, 0, NULL, NULL))) - error = git_checkout_iterator(head_i, opts); - - git_iterator_free(head_i); - git_tree_free(head); - - return error; + return git_checkout_tree(repo, NULL, opts); }