Skip to content

Commit 7a08c91

Browse files
authored
Merge pull request #101 from ChronicallySerious/fix-unstage-when-no-initial-commit
2 parents 97db5d7 + acc0901 commit 7a08c91

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

godot-git-plugin/src/git_api.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,22 @@ void GitAPI::_unstage_file(const String file_path) {
169169
git_strarray array = { paths, 1 };
170170

171171
git_reference_ptr head;
172-
GIT2_CALL(git_repository_head(Capture(head), repo.get()), "Could not get repository HEAD");
172+
GIT2_CALL_IGNORE(git_repository_head(Capture(head), repo.get()), "Could not find repository HEAD", { GIT_ENOTFOUND COMMA GIT_EUNBORNBRANCH });
173173

174-
git_object_ptr head_commit;
175-
GIT2_CALL(git_reference_peel(Capture(head_commit), head.get(), GIT_OBJ_COMMIT), "Could not peel HEAD reference");
176-
GIT2_CALL(git_reset_default(repo.get(), head_commit.get(), &array), "Could not reset " + file_path + " to state at HEAD");
174+
if (head) {
175+
git_object_ptr head_commit;
176+
GIT2_CALL(git_reference_peel(Capture(head_commit), head.get(), GIT_OBJ_COMMIT), "Could not peel HEAD reference");
177+
GIT2_CALL(git_reset_default(repo.get(), head_commit.get(), &array), "Could not reset " + file_path + " to state at HEAD");
178+
} else {
179+
// If there is no HEAD commit, we should just remove the file from the index.
180+
181+
CString c_path(file_path);
182+
183+
git_index_ptr index;
184+
GIT2_CALL(git_repository_index(Capture(index), repo.get()), "Could not get repository index");
185+
GIT2_CALL(git_index_remove_bypath(index.get(), c_path.data), "Could not add " + file_path + " to index");
186+
GIT2_CALL(git_index_write(index.get()), "Could not write changes to disk");
187+
}
177188
}
178189

179190
void GitAPI::create_gitignore_and_gitattributes() {
@@ -623,10 +634,14 @@ Array GitAPI::_get_diff(const String identifier, const int64_t area) {
623634
} break;
624635
case TREE_AREA_STAGED: {
625636
git_object_ptr obj;
626-
GIT2_CALL_R(git_revparse_single(Capture(obj), repo.get(), "HEAD^{tree}"), "Could not get HEAD^{tree} object", diff_contents);
637+
638+
// Ignore the case when HEAD is not found. We need to compare with a null tree in the case where the HEAD reference object is empty.
639+
GIT2_CALL_R_IGNORE(git_revparse_single(Capture(obj), repo.get(), "HEAD^{tree}"), "Could not get HEAD^{tree} object", diff_contents, { GIT_ENOTFOUND });
627640

628641
git_tree_ptr tree;
629-
GIT2_CALL_R(git_tree_lookup(Capture(tree), repo.get(), git_object_id(obj.get())), "Could not lookup HEAD^{tree}", diff_contents);
642+
if (obj) {
643+
GIT2_CALL_R_IGNORE(git_tree_lookup(Capture(tree), repo.get(), git_object_id(obj.get())), "Could not lookup HEAD^{tree}", diff_contents, { GIT_ENOTFOUND });
644+
}
630645

631646
GIT2_CALL_R(git_diff_tree_to_index(Capture(diff), repo.get(), tree.get(), nullptr, &opts), "Could not create diff for tree from index directory", diff_contents);
632647
} break;
@@ -640,6 +655,8 @@ Array GitAPI::_get_diff(const String identifier, const int64_t area) {
640655
GIT2_CALL_R(git_commit_lookup(Capture(commit), repo.get(), git_object_id(obj.get())), "Could not get commit " + identifier, diff_contents);
641656

642657
git_commit_ptr parent;
658+
659+
// We ignore the case when the parent is not found to handle the case when this commit is the root commit. We only need to diff against a null tree in that case.
643660
GIT2_CALL_R_IGNORE(git_commit_parent(Capture(parent), commit.get(), 0), "Could not get parent commit of " + identifier, diff_contents, { GIT_ENOTFOUND });
644661

645662
git_tree_ptr commit_tree;

0 commit comments

Comments
 (0)