Merge pull request #1004 from nulltoken/error/GIT_EORPHANEDHEAD More orphaned head love
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
diff --git a/src/reset.c b/src/reset.c
index dfa095b..560ae17 100644
--- a/src/reset.c
+++ b/src/reset.c
@@ -19,6 +19,45 @@ static int reset_error_invalid(const char *msg)
return -1;
}
+static int update_head(git_repository *repo, git_object *commit)
+{
+ int error;
+ git_reference *head = NULL, *target = NULL;
+
+ error = git_repository_head(&head, repo);
+
+ if (error < 0 && error != GIT_EORPHANEDHEAD)
+ return error;
+
+ if (error == GIT_EORPHANEDHEAD) {
+ giterr_clear();
+
+ /*
+ * TODO: This is a bit weak as this doesn't support chained
+ * symbolic references. yet.
+ */
+ if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
+ goto cleanup;
+
+ if ((error = git_reference_create_oid(
+ &target,
+ repo,
+ git_reference_target(head),
+ git_object_id(commit), 0)) < 0)
+ goto cleanup;
+ } else {
+ if ((error = git_reference_set_oid(head, git_object_id(commit))) < 0)
+ goto cleanup;
+ }
+
+ error = 0;
+
+cleanup:
+ git_reference_free(head);
+ git_reference_free(target);
+ return error;
+}
+
int git_reset(
git_repository *repo,
git_object *target,
@@ -29,7 +68,6 @@ int git_reset(
git_tree *tree = NULL;
int error = -1;
git_checkout_opts opts;
- git_reference *head = NULL;
assert(repo && target);
assert(reset_type == GIT_RESET_SOFT
@@ -52,10 +90,7 @@ int git_reset(
//TODO: Check for unmerged entries
- if (git_repository_head(&head, repo) < 0)
- goto cleanup;
-
- if (git_reference_set_oid(head, git_object_id(commit)) < 0)
+ if (update_head(repo, commit) < 0)
goto cleanup;
if (reset_type == GIT_RESET_SOFT) {
@@ -102,7 +137,6 @@ int git_reset(
error = 0;
cleanup:
- git_reference_free(head);
git_object_free(commit);
git_index_free(index);
git_tree_free(tree);
diff --git a/tests-clar/checkout/head.c b/tests-clar/checkout/head.c
index f2f81e5..d36034c 100644
--- a/tests-clar/checkout/head.c
+++ b/tests-clar/checkout/head.c
@@ -1,5 +1,6 @@
#include "clar_libgit2.h"
#include "refs.h"
+#include "repo/repo_helpers.h"
static git_repository *g_repo;
@@ -15,10 +16,7 @@ void test_checkout_head__cleanup(void)
void test_checkout_head__checking_out_an_orphaned_head_returns_GIT_EORPHANEDHEAD(void)
{
- git_reference *head;
-
- cl_git_pass(git_reference_create_symbolic(&head, g_repo, GIT_HEAD_FILE, "refs/heads/hide/and/seek", 1));
- git_reference_free(head);
+ make_head_orphaned(g_repo, NON_EXISTING_HEAD);
cl_assert_equal_i(GIT_EORPHANEDHEAD, git_checkout_head(g_repo, NULL, NULL));
}
diff --git a/tests-clar/refs/branches/delete.c b/tests-clar/refs/branches/delete.c
index 99af44e..4e9c709 100644
--- a/tests-clar/refs/branches/delete.c
+++ b/tests-clar/refs/branches/delete.c
@@ -1,5 +1,6 @@
#include "clar_libgit2.h"
#include "refs.h"
+#include "repo/repo_helpers.h"
static git_repository *repo;
static git_reference *fake_remote;
@@ -52,11 +53,9 @@ void test_refs_branches_delete__can_delete_a_branch_even_if_HEAD_is_missing(void
void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_orphaned(void)
{
- git_reference *head;
git_reference *branch;
- cl_git_pass(git_reference_create_symbolic(&head, repo, GIT_HEAD_FILE, "refs/heads/hide/and/seek", 1));
- git_reference_free(head);
+ make_head_orphaned(repo, NON_EXISTING_HEAD);
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
@@ -64,13 +63,15 @@ void test_refs_branches_delete__can_delete_a_branch_when_HEAD_is_orphaned(void)
void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)
{
- git_reference *master, *head, *branch;
+ git_reference *head, *branch;
- /* Detach HEAD and make it target the commit that "master" points to */
- cl_git_pass(git_reference_lookup(&master, repo, "refs/heads/master"));
- cl_git_pass(git_reference_create_oid(&head, repo, "HEAD", git_reference_oid(master), 1));
+ cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
+ cl_assert_equal_i(GIT_REF_SYMBOLIC, git_reference_type(head));
+ cl_assert_equal_s("refs/heads/master", git_reference_target(head));
git_reference_free(head);
- git_reference_free(master);
+
+ /* Detach HEAD and make it target the commit that "master" points to */
+ git_repository_detach_head(repo);
cl_git_pass(git_branch_lookup(&branch, repo, "master", GIT_BRANCH_LOCAL));
cl_git_pass(git_branch_delete(branch));
@@ -89,4 +90,3 @@ void test_refs_branches_delete__can_delete_a_remote_branch(void)
cl_git_pass(git_branch_lookup(&branch, repo, "nulltoken/master", GIT_BRANCH_REMOTE));
cl_git_pass(git_branch_delete(branch));
}
-
diff --git a/tests-clar/refs/branches/ishead.c b/tests-clar/refs/branches/ishead.c
index 0d57f00..ab17482 100644
--- a/tests-clar/refs/branches/ishead.c
+++ b/tests-clar/refs/branches/ishead.c
@@ -1,5 +1,6 @@
#include "clar_libgit2.h"
#include "refs.h"
+#include "repo/repo_helpers.h"
static git_repository *repo;
static git_reference *branch;
@@ -22,21 +23,13 @@ void test_refs_branches_ishead__can_tell_if_a_branch_is_pointed_at_by_HEAD(void)
cl_assert_equal_i(true, git_branch_is_head(branch));
}
-static void make_head_orphaned(void)
-{
- git_reference *head;
-
- cl_git_pass(git_reference_create_symbolic(&head, repo, GIT_HEAD_FILE, "refs/heads/hide/and/seek", 1));
- git_reference_free(head);
-}
-
void test_refs_branches_ishead__can_properly_handle_orphaned_HEAD(void)
{
git_repository_free(repo);
repo = cl_git_sandbox_init("testrepo.git");
- make_head_orphaned();
+ make_head_orphaned(repo, NON_EXISTING_HEAD);
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
diff --git a/tests-clar/repo/head.c b/tests-clar/repo/head.c
index 4113289..2294716 100644
--- a/tests-clar/repo/head.c
+++ b/tests-clar/repo/head.c
@@ -1,5 +1,6 @@
#include "clar_libgit2.h"
#include "refs.h"
+#include "repo_helpers.h"
git_repository *repo;
@@ -16,29 +17,18 @@ void test_repo_head__cleanup(void)
void test_repo_head__head_detached(void)
{
git_reference *ref;
- git_oid oid;
cl_assert(git_repository_head_detached(repo) == 0);
- /* detach the HEAD */
- git_oid_fromstr(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd");
- cl_git_pass(git_reference_create_oid(&ref, repo, "HEAD", &oid, 1));
- cl_assert(git_repository_head_detached(repo) == 1);
- git_reference_free(ref);
+ git_repository_detach_head(repo);
+
+ cl_assert_equal_i(true, git_repository_head_detached(repo));
/* take the reop back to it's original state */
cl_git_pass(git_reference_create_symbolic(&ref, repo, "HEAD", "refs/heads/master", 1));
- cl_assert(git_repository_head_detached(repo) == 0);
-
git_reference_free(ref);
-}
-
-static void make_head_orphaned(void)
-{
- git_reference *head;
- cl_git_pass(git_reference_create_symbolic(&head, repo, GIT_HEAD_FILE, "refs/heads/hide/and/seek", 1));
- git_reference_free(head);
+ cl_assert_equal_i(false, git_repository_head_detached(repo));
}
void test_repo_head__head_orphan(void)
@@ -47,7 +37,7 @@ void test_repo_head__head_orphan(void)
cl_assert(git_repository_head_orphan(repo) == 0);
- make_head_orphaned();
+ make_head_orphaned(repo, NON_EXISTING_HEAD);
cl_assert(git_repository_head_orphan(repo) == 1);
@@ -174,7 +164,7 @@ void test_repo_head__detach_head_Fails_if_HEAD_and_point_to_a_non_commitish(void
void test_repo_head__detaching_an_orphaned_head_returns_GIT_EORPHANEDHEAD(void)
{
- make_head_orphaned();
+ make_head_orphaned(repo, NON_EXISTING_HEAD);
cl_assert_equal_i(GIT_EORPHANEDHEAD, git_repository_detach_head(repo));
}
@@ -183,7 +173,16 @@ void test_repo_head__retrieving_an_orphaned_head_returns_GIT_EORPHANEDHEAD(void)
{
git_reference *head;
- make_head_orphaned();
+ make_head_orphaned(repo, NON_EXISTING_HEAD);
cl_assert_equal_i(GIT_EORPHANEDHEAD, git_repository_head(&head, repo));
}
+
+void test_repo_head__can_tell_if_an_orphaned_head_is_detached(void)
+{
+ git_reference *head;
+
+ make_head_orphaned(repo, NON_EXISTING_HEAD);
+
+ cl_assert_equal_i(false, git_repository_head_detached(repo));
+}
diff --git a/tests-clar/repo/repo_helpers.c b/tests-clar/repo/repo_helpers.c
new file mode 100644
index 0000000..35271fe
--- /dev/null
+++ b/tests-clar/repo/repo_helpers.c
@@ -0,0 +1,11 @@
+#include "clar_libgit2.h"
+#include "refs.h"
+#include "repo_helpers.h"
+
+void make_head_orphaned(git_repository* repo, const char *target)
+{
+ git_reference *head;
+
+ cl_git_pass(git_reference_create_symbolic(&head, repo, GIT_HEAD_FILE, target, 1));
+ git_reference_free(head);
+}
diff --git a/tests-clar/repo/repo_helpers.h b/tests-clar/repo/repo_helpers.h
new file mode 100644
index 0000000..e6aeb48
--- /dev/null
+++ b/tests-clar/repo/repo_helpers.h
@@ -0,0 +1,5 @@
+#include "common.h"
+
+#define NON_EXISTING_HEAD "refs/heads/hide/and/seek"
+
+extern void make_head_orphaned(git_repository* repo, const char *target);
diff --git a/tests-clar/reset/soft.c b/tests-clar/reset/soft.c
index 3200c15..1872baf 100644
--- a/tests-clar/reset/soft.c
+++ b/tests-clar/reset/soft.c
@@ -1,5 +1,6 @@
#include "clar_libgit2.h"
#include "reset_helpers.h"
+#include "repo/repo_helpers.h"
static git_repository *repo;
static git_object *target;
@@ -39,20 +40,9 @@ void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(vo
assert_reset_soft(false);
}
-static void detach_head(void)
-{
- git_reference *head;
- git_oid oid;
-
- cl_git_pass(git_reference_name_to_oid(&oid, repo, "HEAD"));
-
- cl_git_pass(git_reference_create_oid(&head, repo, "HEAD", &oid, true));
- git_reference_free(head);
-}
-
void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void)
{
- detach_head();
+ git_repository_detach_head(repo);
assert_reset_soft(true);
}
@@ -100,3 +90,23 @@ void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void)
retrieve_target_from_oid(&target, repo, "521d87c1ec3aef9824daf6d96cc0ae3710766d91");
cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT));
}
+
+void test_reset_soft__resetting_against_an_orphaned_head_repo_makes_the_head_no_longer_orphaned(void)
+{
+ git_reference *head;
+
+ retrieve_target_from_oid(&target, repo, KNOWN_COMMIT_IN_BARE_REPO);
+
+ make_head_orphaned(repo, NON_EXISTING_HEAD);
+
+ cl_assert_equal_i(true, git_repository_head_orphan(repo));
+
+ cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT));
+
+ cl_assert_equal_i(false, git_repository_head_orphan(repo));
+
+ cl_git_pass(git_reference_lookup(&head, repo, NON_EXISTING_HEAD));
+ cl_assert_equal_i(0, git_oid_streq(git_reference_oid(head), KNOWN_COMMIT_IN_BARE_REPO));
+
+ git_reference_free(head);
+}