Skip to content

Commit de3e512

Browse files
committed
address review comments
1 parent 2d73235 commit de3e512

File tree

3 files changed

+24
-24
lines changed

3 files changed

+24
-24
lines changed

src/subcommand/checkout_subcommand.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ checkout_subcommand::checkout_subcommand(const libgit2_object&, CLI::App& app)
2020
sub->callback([this]() { this->run(); });
2121
}
2222

23+
void print_no_switch(status_list_wrapper& sl)
24+
{
25+
std::cout << "Your local changes to the following files would be overwritten by checkout:" << std::endl;
26+
27+
for (const auto* entry : sl.get_entry_list(GIT_STATUS_WT_MODIFIED))
28+
{
29+
std::cout << "\t" << entry->index_to_workdir->new_file.path << std::endl;
30+
}
31+
for (const auto* entry : sl.get_entry_list(GIT_STATUS_WT_DELETED))
32+
{
33+
std::cout << "\t" << entry->index_to_workdir->old_file.path << std::endl;
34+
}
35+
36+
std::cout << "Please commit your changes or stash them before you switch branches.\nAborting" << std::endl;
37+
return;
38+
}
39+
2340
void checkout_subcommand::run()
2441
{
2542
auto directory = get_current_git_path();
@@ -37,10 +54,10 @@ void checkout_subcommand::run()
3754
{
3855
options.checkout_strategy = GIT_CHECKOUT_FORCE;
3956
}
40-
// else
41-
// {
42-
// options.checkout_strategy = GIT_CHECKOUT_SAFE;
43-
// }
57+
else
58+
{
59+
options.checkout_strategy = GIT_CHECKOUT_SAFE;
60+
}
4461

4562
if (m_create_flag || m_force_create_flag)
4663
{
@@ -71,25 +88,9 @@ void checkout_subcommand::run()
7188
{
7289
if (sl.has_notstagged_header())
7390
{
74-
std::cout << "Your local changes to the following files would be overwritten by checkout:" << std::endl;
75-
76-
for (const auto* entry : sl.get_entry_list(GIT_STATUS_WT_MODIFIED))
77-
{
78-
std::cout << "\t" << entry->index_to_workdir->new_file.path << std::endl;
79-
}
80-
for (const auto* entry : sl.get_entry_list(GIT_STATUS_WT_DELETED))
81-
{
82-
std::cout << "\t" << entry->index_to_workdir->old_file.path << std::endl;
83-
}
84-
85-
std::cout << "Please commit your changes or stash them before you switch branches.\nAborting" << std::endl;
86-
return;
87-
}
88-
else
89-
{
90-
throw e;
91+
print_no_switch(sl);
9192
}
92-
return;
93+
throw e;
9394
}
9495

9596
if (sl.has_notstagged_header())

src/subcommand/checkout_subcommand.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ class checkout_subcommand
1212
{
1313
public:
1414

15-
void print_message(repository_wrapper& repo);
1615
explicit checkout_subcommand(const libgit2_object&, CLI::App& app);
1716
void run();
1817

test/test_checkout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def test_checkout_refuses_overwrite(
167167
)
168168

169169
if force_flag == "":
170-
assert p_checkout.returncode == 0
170+
assert p_checkout.returncode != 0
171171
assert (
172172
"Your local changes to the following files would be overwritten by checkout:"
173173
in p_checkout.stdout

0 commit comments

Comments
 (0)