|
| 1 | +#include <iostream> |
| 2 | +#include <ostream> |
| 3 | +#include <string> |
| 4 | + |
| 5 | +#include <git2.h> |
| 6 | + |
| 7 | +#include "status_subcommand.hpp" |
| 8 | +#include "../wrapper/status_wrapper.hpp" |
| 9 | + |
| 10 | +status_subcommand::status_subcommand(const libgit2_object&, CLI::App& app) |
| 11 | +{ |
| 12 | + auto *sub = app.add_subcommand("status", "Show modified files in working directory, staged for your next commit"); |
| 13 | + // Displays paths that have differences between the index file and the current HEAD commit, |
| 14 | + // paths that have differences between the working tree and the index file, and paths in the |
| 15 | + // working tree that are not tracked by Git (and are not ignored by gitignore[5]). |
| 16 | + // The first are what you would commit by running git commit; |
| 17 | + // the second and third are what you could commit by running git add before running git commit. |
| 18 | + |
| 19 | + sub->add_flag("-s,--short", short_flag, "Give the output in the short-format."); |
| 20 | + sub->add_flag("--long", long_flag, "Give the output in the long-format. This is the default."); |
| 21 | + // sub->add_flag("--porcelain[=<version>]", porcelain, "Give the output in an easy-to-parse format for scripts. |
| 22 | + // This is similar to the short output, but will remain stable across Git versions and regardless of user configuration. |
| 23 | + // See below for details. The version parameter is used to specify the format version. This is optional and defaults |
| 24 | + // to the original version v1 format."); |
| 25 | + |
| 26 | + sub->callback([this]() { this->run(); }); |
| 27 | +}; |
| 28 | + |
| 29 | +const std::string untracked_header = "Untracked files:\n"; |
| 30 | +// "Untracked files:\n (use \"git add <file>...\" to include in what will be committed)"; |
| 31 | +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\")" |
| 39 | + |
| 40 | +struct status_messages |
| 41 | +{ |
| 42 | + std::string short_mod; |
| 43 | + std::string long_mod; |
| 44 | +}; |
| 45 | + |
| 46 | +const std::map<git_status_t, status_messages> status_msg_map = //TODO : check spaces in short_mod |
| 47 | +{ |
| 48 | + { GIT_STATUS_CURRENT, {"", ""} }, |
| 49 | + { GIT_STATUS_INDEX_NEW, {"A ", "\t new file:"} }, |
| 50 | + { GIT_STATUS_INDEX_MODIFIED, {"M ", "\t modified:"} }, |
| 51 | + { GIT_STATUS_INDEX_DELETED, {"D ", "\t deleted:"} }, |
| 52 | + { GIT_STATUS_INDEX_RENAMED, {"R ", "\t renamed:"} }, |
| 53 | + { GIT_STATUS_INDEX_TYPECHANGE, {"T ", "\t typechange:"} }, |
| 54 | + { GIT_STATUS_WT_NEW, {"?? ", ""} }, |
| 55 | + { GIT_STATUS_WT_MODIFIED, {" M " , "\t modified:"} }, |
| 56 | + { GIT_STATUS_WT_DELETED, {" D ", "\t deleted:"} }, |
| 57 | + { GIT_STATUS_WT_TYPECHANGE, {" T ", "\t typechange:"} }, |
| 58 | + { GIT_STATUS_WT_RENAMED, {" R ", "\t renamed:"} }, |
| 59 | + { GIT_STATUS_WT_UNREADABLE, {"", ""} }, |
| 60 | + { GIT_STATUS_IGNORED, {"!! ", ""} }, |
| 61 | + { GIT_STATUS_CONFLICTED, {"", ""} }, |
| 62 | +}; |
| 63 | + |
| 64 | +enum class output_format |
| 65 | +{ |
| 66 | + DEFAULT = 0, |
| 67 | + LONG = 1, |
| 68 | + SHORT = 2 |
| 69 | +}; |
| 70 | + |
| 71 | +void print_entries(git_status_t status, status_list_wrapper& sl, bool head_selector, output_format of) // TODO: add different mods |
| 72 | +{ |
| 73 | + const auto& entry_list = sl.get_entry_list(status); |
| 74 | + if (!entry_list.empty()) |
| 75 | + { |
| 76 | + for (auto* entry : entry_list) |
| 77 | + { |
| 78 | + if ((of == output_format::DEFAULT) || (of == output_format::LONG)) |
| 79 | + { |
| 80 | + std::cout << status_msg_map.at(status).long_mod << "\t"; |
| 81 | + } |
| 82 | + else if (of == output_format::SHORT) |
| 83 | + { |
| 84 | + std::cout << status_msg_map.at(status).short_mod; |
| 85 | + } |
| 86 | + |
| 87 | + git_diff_delta* diff_delta; |
| 88 | + if (head_selector) |
| 89 | + { |
| 90 | + diff_delta = entry->head_to_index; |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + diff_delta = entry->index_to_workdir; |
| 95 | + } |
| 96 | + const char* old_path = diff_delta->old_file.path; |
| 97 | + const char* new_path = diff_delta->new_file.path; |
| 98 | + if (old_path && new_path && std::strcmp(old_path, new_path)) |
| 99 | + { |
| 100 | + std::cout << old_path << " -> " << new_path << std::endl; |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + if (old_path) |
| 105 | + { |
| 106 | + std::cout << old_path << std::endl; |
| 107 | + } |
| 108 | + else |
| 109 | + { |
| 110 | + std::cout << new_path << std::endl; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + else |
| 116 | + {} |
| 117 | +} |
| 118 | + |
| 119 | +void status_subcommand::run() |
| 120 | +{ |
| 121 | + auto directory = get_current_git_path(); |
| 122 | + auto bare = false; |
| 123 | + auto repo = repository_wrapper::init(directory, bare); |
| 124 | + auto sl = status_list_wrapper::status_list(repo); |
| 125 | + |
| 126 | + // TODO: add branch info |
| 127 | + |
| 128 | + output_format of = output_format::DEFAULT; |
| 129 | + if (short_flag) |
| 130 | + { |
| 131 | + of = output_format::SHORT; |
| 132 | + } |
| 133 | + if (long_flag) |
| 134 | + { |
| 135 | + of = output_format::LONG; |
| 136 | + } |
| 137 | + // else if (porcelain_format) |
| 138 | + // { |
| 139 | + // output_format = 3; |
| 140 | + // } |
| 141 | + |
| 142 | + bool is_long; |
| 143 | + is_long = ((of == output_format::DEFAULT) || (of == output_format::LONG)); |
| 144 | + if (sl.has_tobecommited_header()) |
| 145 | + { |
| 146 | + if (is_long) |
| 147 | + { |
| 148 | + std::cout << tobecommited_header << std::endl; |
| 149 | + } |
| 150 | + print_entries(GIT_STATUS_INDEX_NEW, sl, true, of); |
| 151 | + print_entries(GIT_STATUS_INDEX_MODIFIED, sl, true, of); |
| 152 | + print_entries(GIT_STATUS_INDEX_DELETED, sl, true, of); |
| 153 | + print_entries(GIT_STATUS_INDEX_RENAMED, sl, true, of); |
| 154 | + print_entries(GIT_STATUS_INDEX_TYPECHANGE, sl, true, of); |
| 155 | + if (is_long) |
| 156 | + { |
| 157 | + std::cout << std::endl; |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + if (sl.has_notstagged_header()) |
| 162 | + { |
| 163 | + if (is_long) |
| 164 | + { |
| 165 | + std::cout << notstagged_header << std::endl; |
| 166 | + } |
| 167 | + print_entries(GIT_STATUS_WT_MODIFIED, sl, false, of); |
| 168 | + print_entries(GIT_STATUS_WT_DELETED, sl, false, of); |
| 169 | + print_entries(GIT_STATUS_WT_TYPECHANGE, sl, false, of); |
| 170 | + print_entries(GIT_STATUS_WT_RENAMED, sl, false, of); |
| 171 | + if (is_long) |
| 172 | + { |
| 173 | + std::cout << std::endl; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + if (sl.has_untracked_header()) |
| 178 | + { |
| 179 | + if (is_long) |
| 180 | + { |
| 181 | + std::cout << untracked_header << std::endl; |
| 182 | + } |
| 183 | + print_entries(GIT_STATUS_WT_NEW, sl, false, of); |
| 184 | + if (is_long) |
| 185 | + { |
| 186 | + std::cout << std::endl; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if (sl.has_ignored_header()) |
| 191 | + { |
| 192 | + if (is_long) |
| 193 | + { |
| 194 | + std::cout << ignored_header << std::endl; |
| 195 | + } |
| 196 | + print_entries(GIT_STATUS_IGNORED, sl, false, of); |
| 197 | + if (is_long) |
| 198 | + { |
| 199 | + std::cout << std::endl; |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments