@@ -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.\n Aborting" << std::endl;
37+ return ;
38+ }
39+
2340void 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.\n Aborting" << 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 ())
0 commit comments