Skip to content

Commit 87546ec

Browse files
committed
third try
1 parent a2148dd commit 87546ec

File tree

1 file changed

+108
-136
lines changed

1 file changed

+108
-136
lines changed

src/subcommand/status_subcommand.cpp

Lines changed: 108 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -72,133 +72,120 @@ enum class output_format
7272
SHORT = 2
7373
};
7474

75-
// void print_entries(git_status_t status, status_list_wrapper& sl, bool head_selector, output_format of,
76-
// std::set<std::string>* tracked_dir_set = nullptr)
77-
// {
78-
// const auto& entry_list = sl.get_entry_list(status);
79-
// if (!entry_list.empty())
80-
// {
81-
// for (auto* entry : entry_list)
82-
// {
83-
// if ((of == output_format::DEFAULT) || (of == output_format::LONG))
84-
// {
85-
// std::cout << status_msg_map.at(status).long_mod << "\t";
86-
// }
87-
// else if (of == output_format::SHORT)
88-
// {
89-
// std::cout << status_msg_map.at(status).short_mod;
90-
// }
75+
struct print_entry
76+
{
77+
std::string status;
78+
std::string item;
79+
};
80+
81+
std::string get_print_status(git_status_t status, output_format of)
82+
{
83+
std::string entry_status;
84+
if ((of == output_format::DEFAULT) || (of == output_format::LONG))
85+
{
86+
entry_status = status_msg_map.at(status).long_mod + "\t";
87+
}
88+
else if (of == output_format::SHORT)
89+
{
90+
entry_status = status_msg_map.at(status).short_mod;
91+
}
92+
return entry_status;
93+
}
94+
95+
void update_tracked_dir_set(const char* old_path, const char* new_path, std::set<std::string>* tracked_dir_set = nullptr)
96+
{
97+
if (tracked_dir_set)
98+
{
99+
const size_t first_slash_idx = std::string_view(old_path).find('/');
100+
if (std::string::npos != first_slash_idx)
101+
{
102+
auto directory = std::string_view(old_path).substr(0, first_slash_idx);
103+
tracked_dir_set->insert(std::string(directory));
104+
}
105+
}
106+
}
107+
108+
std::string get_print_item(const char* old_path, const char* new_path)
109+
{
110+
std::string entry_item;
111+
if (old_path && new_path && std::strcmp(old_path, new_path))
112+
{
113+
entry_item = std::string(old_path) + " -> " + std::string(new_path);
114+
}
115+
else
116+
{
117+
if (old_path)
118+
{
119+
entry_item = old_path;
120+
}
121+
else
122+
{
123+
entry_item = new_path;
124+
}
125+
}
126+
return entry_item;
127+
}
91128

92-
// git_diff_delta* diff_delta;
93-
// if (head_selector)
94-
// {
95-
// diff_delta = entry->head_to_index;
96-
// }
97-
// else
98-
// {
99-
// diff_delta = entry->index_to_workdir;
100-
// }
101-
// const char* old_path = diff_delta->old_file.path;
102-
// const char* new_path = diff_delta->new_file.path;
103-
// if (tracked_dir_set)
104-
// {
105-
// const size_t first_slash_idx = std::string_view(old_path).find('/');
106-
// if (std::string::npos != first_slash_idx)
107-
// {
108-
// auto directory = std::string_view(old_path).substr(0, first_slash_idx);
109-
// tracked_dir_set->insert(std::string(directory));
110-
// }
111-
// }
112-
// if (old_path && new_path && std::strcmp(old_path, new_path))
113-
// {
114-
// std::cout << old_path << " -> " << new_path << std::endl;
115-
// }
116-
// else
117-
// {
118-
// if (old_path)
119-
// {
120-
// std::cout << old_path << std::endl;
121-
// }
122-
// else
123-
// {
124-
// std::cout << new_path << std::endl;
125-
// }
126-
// }
127-
// }
128-
// }
129-
// else
130-
// {}
131-
// }
132-
//
133-
std::vector<std::pair<std::string, std::string>> get_entries_to_print(git_status_t status, status_list_wrapper& sl,
129+
std::vector<print_entry> get_entries_to_print(git_status_t status, status_list_wrapper& sl,
134130
bool head_selector, output_format of, std::set<std::string>* tracked_dir_set = nullptr)
135131
{
136-
std::vector<std::pair<std::string, std::string>> entries_to_print{};
132+
std::vector<print_entry> entries_to_print{};
137133
const auto& entry_list = sl.get_entry_list(status);
138-
if (!entry_list.empty())
134+
if (entry_list.empty())
139135
{
140-
for (auto* entry : entry_list)
141-
{
142-
std::string bla;
143-
if ((of == output_format::DEFAULT) || (of == output_format::LONG))
144-
{
145-
bla = status_msg_map.at(status).long_mod + "\t";
146-
}
147-
else if (of == output_format::SHORT)
148-
{
149-
bla = status_msg_map.at(status).short_mod;
150-
}
136+
return entries_to_print;
137+
}
151138

152-
git_diff_delta* diff_delta;
153-
if (head_selector)
154-
{
155-
diff_delta = entry->head_to_index;
156-
}
157-
else
158-
{
159-
diff_delta = entry->index_to_workdir;
160-
}
161-
const char* old_path = diff_delta->old_file.path;
162-
const char* new_path = diff_delta->new_file.path;
163-
std::string blou;
164-
if (tracked_dir_set)
165-
{
166-
const size_t first_slash_idx = std::string_view(old_path).find('/');
167-
if (std::string::npos != first_slash_idx)
168-
{
169-
auto directory = std::string_view(old_path).substr(0, first_slash_idx);
170-
tracked_dir_set->insert(std::string(directory));
171-
}
172-
}
173-
if (old_path && new_path && std::strcmp(old_path, new_path))
139+
for (auto* entry : entry_list)
140+
{
141+
git_diff_delta* diff_delta = head_selector ? entry->head_to_index : entry->index_to_workdir;
142+
const char* old_path = diff_delta->old_file.path;
143+
const char* new_path = diff_delta->new_file.path;
144+
145+
update_tracked_dir_set(old_path, new_path, tracked_dir_set);
146+
147+
print_entry e = { get_print_status(status, of), get_print_item(old_path, new_path)};
148+
149+
entries_to_print.push_back(std::move(e));
150+
}
151+
return entries_to_print;
152+
}
153+
154+
void print_entries(std::vector<print_entry> entries_to_print)
155+
{
156+
for (auto e: entries_to_print)
157+
{
158+
std::cout << e.status << e.item << std::endl;
159+
}
160+
}
161+
162+
void print_not_tracked(std::vector<print_entry> entries_to_print, std::set<std::string>* tracked_dir_set,
163+
std::set<std::string>* untracked_dir_set)
164+
{
165+
std::vector<print_entry> not_tracked_entries_to_print{};
166+
for (auto e: entries_to_print)
167+
{
168+
const size_t first_slash_idx = std::string_view(e.item).find('/');
169+
if (std::string::npos != first_slash_idx)
170+
{
171+
auto directory = e.item.substr(0, first_slash_idx);
172+
if (tracked_dir_set->contains(directory))
174173
{
175-
blou = std::string(old_path) + " -> " + std::string(new_path);
174+
not_tracked_entries_to_print.push_back(e);
176175
}
177176
else
178177
{
179-
if (old_path)
180-
{
181-
blou = old_path;
182-
}
178+
if (untracked_dir_set->contains(directory))
179+
{}
183180
else
184181
{
185-
blou = new_path;
182+
not_tracked_entries_to_print.push_back({e.status, directory});
183+
untracked_dir_set->insert(std::string(directory));
186184
}
187185
}
188-
entries_to_print.push_back({bla, blou});
189186
}
190187
}
191-
else
192-
{}
193-
return entries_to_print;
194-
}
195-
196-
void print_entries(std::vector<std::pair<std::string, std::string>> entries_to_print)
197-
{
198-
for (auto e: entries_to_print)
199-
{
200-
std::cout << e.first << e.second << std::endl;
201-
}
188+
print_entries(not_tracked_entries_to_print);
202189
}
203190

204191
void status_subcommand::run()
@@ -257,6 +244,11 @@ void status_subcommand::run()
257244
std::cout << std::endl;
258245
}
259246
}
247+
// std::cout << "to be commited: " << tracked_dir_set->size() << std::endl;
248+
// for (auto t : tracked_dir_set*)
249+
// {
250+
// std::cout << t << std::endl;
251+
// }
260252

261253
if (sl.has_notstagged_header())
262254
{
@@ -273,37 +265,16 @@ void status_subcommand::run()
273265
std::cout << std::endl;
274266
}
275267
}
268+
// std::cout << "not stagged: " << tracked_dir_set->size() << std::endl;
276269

277270
if (sl.has_untracked_header())
278271
{
279272
if (is_long)
280273
{
281274
std::cout << untracked_header << std::endl;
282275
}
283-
std::vector<std::pair<std::string, std::string>> untracked_untries_to_print{};
284-
for (auto e: get_entries_to_print(GIT_STATUS_WT_NEW, sl, false, of))
285-
{
286-
const size_t first_slash_idx = std::string_view(e.second).find('/');
287-
if (std::string::npos != first_slash_idx)
288-
{
289-
auto directory = std::string_view(e.second).substr(0, first_slash_idx);
290-
if (auto directory in tracked_dir_set)
291-
{
292-
293-
}
294-
else
295-
{
296-
if (auto directory in untracked_dir_set)
297-
{}
298-
else
299-
{
300-
untracked_untries_to_print.push_back({e.first, directory});
301-
untracked_dir_set->insert(std::string(directory));
302-
}
303-
}
304-
}
305-
}
306-
print_entries(untracked_untries_to_print);
276+
// print_entries(get_entries_to_print(GIT_STATUS_WT_NEW, sl, false, of));
277+
print_not_tracked(get_entries_to_print(GIT_STATUS_WT_NEW, sl, false, of), tracked_dir_set, untracked_dir_set);
307278
if (is_long)
308279
{
309280
std::cout << std::endl;
@@ -316,7 +287,8 @@ void status_subcommand::run()
316287
{
317288
std::cout << ignored_header << std::endl;
318289
}
319-
print_entries(get_entries_to_print(GIT_STATUS_IGNORED, sl, false, of)); // TODO: same as untracked
290+
// print_entries(get_entries_to_print(GIT_STATUS_IGNORED, sl, false, of));
291+
print_not_tracked(get_entries_to_print(GIT_STATUS_IGNORED, sl, false, of), tracked_dir_set, untracked_dir_set);
320292
if (is_long)
321293
{
322294
std::cout << std::endl;

0 commit comments

Comments
 (0)