Merge pull request #1238 from nulltoken/fix/checkout-index checkout: Teach checkout to cope with orphaned Head
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
diff --git a/src/checkout.c b/src/checkout.c
index b58ef9f..d5a471d 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -1173,7 +1173,14 @@ static int checkout_data_init(
if (!data->opts.baseline) {
data->opts_free_baseline = true;
- if ((error = checkout_lookup_head_tree(&data->opts.baseline, repo)) < 0)
+ error = checkout_lookup_head_tree(&data->opts.baseline, repo);
+
+ if (error == GIT_EORPHANEDHEAD) {
+ error = 0;
+ giterr_clear();
+ }
+
+ if (error < 0)
goto cleanup;
}
diff --git a/src/diff_output.c b/src/diff_output.c
index 933d44e..e79bf30 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -842,7 +842,7 @@ static int diff_patch_line_cb(
{
git_diff_patch *patch = payload;
diff_patch_hunk *hunk;
- diff_patch_line *last, *line;
+ diff_patch_line *line;
GIT_UNUSED(delta);
GIT_UNUSED(range);
@@ -872,8 +872,6 @@ static int diff_patch_line_cb(
patch->lines_asize = new_size;
}
- last = (patch->lines_size > 0) ?
- &patch->lines[patch->lines_size - 1] : NULL;
line = &patch->lines[patch->lines_size++];
line->ptr = content;
diff --git a/tests-clar/checkout/index.c b/tests-clar/checkout/index.c
index 2dc0871..22c6217 100644
--- a/tests-clar/checkout/index.c
+++ b/tests-clar/checkout/index.c
@@ -498,3 +498,13 @@ void test_checkout_index__can_update_prefixed_files(void)
cl_assert(!git_path_exists("testrepo/branch_file"));
cl_assert(!git_path_exists("testrepo/branch_file.txt.after"));
}
+
+void test_checkout_index__can_checkout_a_newly_initialized_repository(void)
+{
+ test_checkout_index__cleanup();
+
+ g_repo = cl_git_sandbox_init("empty_standard_repo");
+ cl_git_remove_placeholders(git_repository_path(g_repo), "dummy-marker.txt");
+
+ cl_git_pass(git_checkout_index(g_repo, NULL, NULL));
+}
diff --git a/tests-clar/checkout/tree.c b/tests-clar/checkout/tree.c
index 34ac1ed..b877b2e 100644
--- a/tests-clar/checkout/tree.c
+++ b/tests-clar/checkout/tree.c
@@ -367,7 +367,6 @@ void assert_conflict(
git_object *hack_tree;
git_reference *branch, *head;
git_buf file_path = GIT_BUF_INIT;
- git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
cl_git_pass(git_repository_index(&index, g_repo));
@@ -383,7 +382,7 @@ void assert_conflict(
git_reference_free(branch);
/* Checkout the parent */
- opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+ g_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_tree(g_repo, g_object, &g_opts));
/* Hack-ishy workaound to ensure *all* the index entries
@@ -403,7 +402,7 @@ void assert_conflict(
/* Trying to checkout the original commit */
cl_git_pass(git_revparse_single(&g_object, g_repo, commit_sha));
- opts.checkout_strategy = GIT_CHECKOUT_SAFE;
+ g_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
cl_assert_equal_i(
GIT_EMERGECONFLICT, git_checkout_tree(g_repo, g_object, &g_opts));