Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0a29421

Browse files
committed
Merge pull request libgit2#3542 from libgit2/cmn/reset-dir-file
reset: perform the checkout before moving HEAD or the index
2 parents 8288525 + 465c3b3 commit 0a29421

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

src/reset.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,19 @@ static int reset(
145145
if ((error = git_buf_printf(&log_message, "reset: moving to %s", to)) < 0)
146146
return error;
147147

148-
/* move HEAD to the new target */
149-
if ((error = git_reference__update_terminal(repo, GIT_HEAD_FILE,
150-
git_object_id(commit), NULL, git_buf_cstr(&log_message))) < 0)
151-
goto cleanup;
152-
153148
if (reset_type == GIT_RESET_HARD) {
154-
/* overwrite working directory with HEAD */
149+
/* overwrite working directory with the new tree */
155150
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
156151

157152
if ((error = git_checkout_tree(repo, (git_object *)tree, &opts)) < 0)
158153
goto cleanup;
159154
}
160155

156+
/* move HEAD to the new target */
157+
if ((error = git_reference__update_terminal(repo, GIT_HEAD_FILE,
158+
git_object_id(commit), NULL, git_buf_cstr(&log_message))) < 0)
159+
goto cleanup;
160+
161161
if (reset_type > GIT_RESET_SOFT) {
162162
/* reset index to the target content */
163163

tests/reset/hard.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,55 @@ void test_reset_hard__reflog_is_correct(void)
235235
git_annotated_commit_free(annotated);
236236

237237
}
238+
239+
void test_reset_hard__switch_file_to_dir(void)
240+
{
241+
git_index_entry entry = { 0 };
242+
git_index *idx;
243+
git_object *commit;
244+
git_tree *tree;
245+
git_signature *sig;
246+
git_oid src_tree_id, tgt_tree_id;
247+
git_oid src_id, tgt_id;
248+
249+
entry.mode = GIT_FILEMODE_BLOB;
250+
cl_git_pass(git_oid_fromstr(&entry.id, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"));
251+
cl_git_pass(git_index_new(&idx));
252+
cl_git_pass(git_signature_now(&sig, "foo", "bar"));
253+
254+
/* Create the old tree */
255+
entry.path = "README";
256+
cl_git_pass(git_index_add(idx, &entry));
257+
entry.path = "dir";
258+
cl_git_pass(git_index_add(idx, &entry));
259+
260+
cl_git_pass(git_index_write_tree_to(&src_tree_id, idx, repo));
261+
cl_git_pass(git_index_clear(idx));
262+
263+
cl_git_pass(git_tree_lookup(&tree, repo, &src_tree_id));
264+
cl_git_pass(git_commit_create(&src_id, repo, NULL, sig, sig, NULL, "foo", tree, 0, NULL));
265+
git_tree_free(tree);
266+
267+
/* Create the new tree */
268+
entry.path = "README";
269+
cl_git_pass(git_index_add(idx, &entry));
270+
entry.path = "dir/FILE";
271+
cl_git_pass(git_index_add(idx, &entry));
272+
273+
cl_git_pass(git_index_write_tree_to(&tgt_tree_id, idx, repo));
274+
cl_git_pass(git_tree_lookup(&tree, repo, &tgt_tree_id));
275+
cl_git_pass(git_commit_create(&tgt_id, repo, NULL, sig, sig, NULL, "foo", tree, 0, NULL));
276+
git_tree_free(tree);
277+
git_index_free(idx);
278+
git_signature_free(sig);
279+
280+
/* Let's go to a known state of the src commit with the file named 'dir' */
281+
cl_git_pass(git_object_lookup(&commit, repo, &src_id, GIT_OBJ_COMMIT));
282+
cl_git_pass(git_reset(repo, commit, GIT_RESET_HARD, NULL));
283+
git_object_free(commit);
284+
285+
/* And now we move over to the commit with the directory named 'dir' */
286+
cl_git_pass(git_object_lookup(&commit, repo, &tgt_id, GIT_OBJ_COMMIT));
287+
cl_git_pass(git_reset(repo, commit, GIT_RESET_HARD, NULL));
288+
git_object_free(commit);
289+
}

0 commit comments

Comments
 (0)