Skip to content

Commit 828adc2

Browse files
committed
improve status output
1 parent 3cd3904 commit 828adc2

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

src/subcommand/status_subcommand.cpp

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,18 @@ status_subcommand::status_subcommand(const libgit2_object&, CLI::App& app)
2626
sub->callback([this]() { this->run(); });
2727
};
2828

29-
const std::string untracked_header = "Untracked files:\n";
30-
// "Untracked files:\n (use \"git add <file>...\" to include in what will be committed)";
29+
const std::string untracked_header = "Untracked files:\n (use \"git add <file>...\" to include in what will be committed)\n";
3130
const std::string tobecommited_header = "Changes to be committed:\n";
32-
// "Changes to be committed:\n (use \"git reset HEAD <file>...\" to unstage)";
33-
const std::string ignored_header = "Ignored files:\n";
34-
// "Ignored files:\n (use \"git add -f <file>...\" to include in what will be committed)"
35-
const std::string notstagged_header = "Changes not staged for commit:\n";
36-
// "Changes not staged for commit:\n (use \"git add%s <file>...\" to update what will be committed)\n (use \"git checkout -- <file>...\" to discard changes in working directory)"
37-
const std::string nothingtocommit_message = "No changes added to commit";
38-
// "No changes added to commit (use \"git add\" and/or \"git commit -a\")"
31+
// (use \"git restore --staged <file>...\" to unstage)\n
32+
// (use \"git reset HEAD <file>...\" to unstage)\n";
33+
const std::string ignored_header = "Ignored files:\n Ignored files:\n (use \"git add -f <file>...\" to include in what will be committed)\n";
34+
const std::string notstagged_header = "Changes not staged for commit:\n (use \"git add <file>...\" to update what will be committed)\n";
35+
// (use \"git restore <file>...\" to discard changes in working directory)\n
36+
// (use \"git checkout -- <file>...\" to discard changes in working directory)\n"
37+
const std::string nothingtocommit_msg = "No changes added to commit (use \"git add\" and/or \"git commit -a\")";
38+
const std::string uptodate_msg = "Nothing to commit, working tree clean.";
39+
const std::string nothingtocommit_untrackedfiles_msg = "Nothing added to commit but untracked files present (use \"git add\" to track)";
40+
// no changes added to commit (use "git add" and/or "git commit -a")
3941

4042
struct status_messages
4143
{
@@ -51,7 +53,7 @@ const std::map<git_status_t, status_messages> status_msg_map = //TODO : check
5153
{ GIT_STATUS_INDEX_DELETED, {"D ", "\tdeleted:"} },
5254
{ GIT_STATUS_INDEX_RENAMED, {"R ", "\trenamed:"} },
5355
{ GIT_STATUS_INDEX_TYPECHANGE, {"T ", "\ttypechange:"} },
54-
{ GIT_STATUS_WT_NEW, {"?? ", ""} },
56+
{ GIT_STATUS_WT_NEW, {"?? ", " "} },
5557
{ GIT_STATUS_WT_MODIFIED, {" M " , "\tmodified:"} },
5658
{ GIT_STATUS_WT_DELETED, {" D ", "\tdeleted:"} },
5759
{ GIT_STATUS_WT_TYPECHANGE, {" T ", "\ttypechange:"} },
@@ -79,7 +81,14 @@ std::string get_print_status(git_status_t status, output_format of)
7981
std::string entry_status;
8082
if ((of == output_format::DEFAULT) || (of == output_format::LONG))
8183
{
82-
entry_status = status_msg_map.at(status).long_mod + " ";
84+
if (status == GIT_STATUS_WT_NEW)
85+
{
86+
entry_status = status_msg_map.at(status).long_mod + "\t";
87+
}
88+
else
89+
{
90+
entry_status = status_msg_map.at(status).long_mod + " ";
91+
}
8392
}
8493
else if (of == output_format::SHORT)
8594
{
@@ -88,14 +97,14 @@ std::string get_print_status(git_status_t status, output_format of)
8897
return entry_status;
8998
}
9099

91-
void update_tracked_dir_set(const char* old_path, const char* new_path, std::set<std::string>* tracked_dir_set = nullptr)
100+
void update_tracked_dir_set(const char* path, std::set<std::string>* tracked_dir_set = nullptr)
92101
{
93102
if (tracked_dir_set)
94103
{
95-
const size_t first_slash_idx = std::string_view(old_path).find('/');
104+
const size_t first_slash_idx = std::string_view(path).find('/');
96105
if (std::string::npos != first_slash_idx)
97106
{
98-
auto directory = std::string_view(old_path).substr(0, first_slash_idx);
107+
auto directory = std::string_view(path).substr(0, first_slash_idx);
99108
tracked_dir_set->insert(std::string(directory));
100109
}
101110
}
@@ -131,7 +140,7 @@ std::vector<print_entry> get_entries_to_print(git_status_t status, status_list_w
131140
const char* old_path = diff_delta->old_file.path;
132141
const char* new_path = diff_delta->new_file.path;
133142

134-
update_tracked_dir_set(old_path, new_path, tracked_dir_set);
143+
update_tracked_dir_set(old_path, tracked_dir_set);
135144

136145
print_entry e = { get_print_status(status, of), get_print_item(old_path, new_path)};
137146

@@ -164,7 +173,8 @@ void print_not_tracked(const std::vector<print_entry>& entries_to_print, const s
164173
const size_t first_slash_idx = e.item.find('/');
165174
if (std::string::npos != first_slash_idx)
166175
{
167-
auto directory = "\t" + e.item.substr(0, first_slash_idx) + "/";
176+
auto directory = e.item.substr(0, first_slash_idx);
177+
auto directory_print = e.item.substr(0, first_slash_idx) + "/";
168178
if (tracked_dir_set.contains(directory))
169179
{
170180
not_tracked_entries_to_print.push_back(e);
@@ -175,7 +185,7 @@ void print_not_tracked(const std::vector<print_entry>& entries_to_print, const s
175185
{}
176186
else
177187
{
178-
not_tracked_entries_to_print.push_back({e.status, directory});
188+
not_tracked_entries_to_print.push_back({e.status, directory_print});
179189
untracked_dir_set.insert(std::string(directory));
180190
}
181191
}
@@ -290,4 +300,21 @@ void status_subcommand::run()
290300
// std::cout << std::endl;
291301
// }
292302
// }
303+
304+
if (!sl.has_tobecommited_header() & (sl.has_notstagged_header() | sl.has_untracked_header()))
305+
{
306+
if (sl.has_untracked_header())
307+
{
308+
std::cout << nothingtocommit_untrackedfiles_msg << std::endl;
309+
}
310+
else
311+
{
312+
std::cout << nothingtocommit_msg << std::endl;
313+
}
314+
}
315+
316+
if (!sl.has_notstagged_header() & !sl.has_untracked_header())
317+
{
318+
std::cout << uptodate_msg << std::endl;
319+
}
293320
}

0 commit comments

Comments
 (0)