#include "git2cpp/diff.h" #include "git2cpp/error.h" #include #ifdef USE_BOOST #include #include #else #include #endif namespace git { namespace diff { namespace stats { namespace format { const type none(GIT_DIFF_STATS_NONE); const type full(GIT_DIFF_STATS_FULL); const type _short(GIT_DIFF_STATS_SHORT); const type number(GIT_DIFF_STATS_NUMBER); const type include_summary(GIT_DIFF_STATS_INCLUDE_SUMMARY); } } #ifdef USE_BOOST template using map_container = boost::container::flat_map; #else struct EnumHash { template std::size_t operator()(T t) const { return static_cast(t); } }; template using map_container = std::unordered_map; #endif git_diff_format_t convert(format f) { static const map_container converter = { {format::patch, GIT_DIFF_FORMAT_PATCH}, {format::patch_header, GIT_DIFF_FORMAT_PATCH_HEADER}, {format::raw, GIT_DIFF_FORMAT_RAW}, {format::name_only, GIT_DIFF_FORMAT_NAME_ONLY}, {format::name_status, GIT_DIFF_FORMAT_NAME_STATUS}}; return converter.at(f); } } namespace { int apply_callback(git_diff_delta const * delta, git_diff_hunk const * hunk, git_diff_line const * line, void * payload) { assert(delta); assert(line); auto cb = reinterpret_cast(payload); (*cb)(*delta, *hunk, *line); return 0; } } void Diff::Destroy::operator() (git_diff* diff) const { git_diff_free(diff); } void Diff::find_similar(git_diff_find_options & findopts) { git_diff_find_similar(diff_.get(), &findopts); } size_t Diff::deltas_num() const { return git_diff_num_deltas(diff_.get()); } void Diff::print(diff::format f, print_callback_t print_callback) const { git_diff_print(diff_.get(), convert(f), &apply_callback, &print_callback); } Diff::Stats Diff::stats() const { git_diff_stats * stats; if (git_diff_get_stats(&stats, diff_.get())) throw error_t("git_diff_get_stats fail"); else return Stats(stats); } void Diff::Stats::Destroy::operator()(git_diff_stats* stats) const { git_diff_stats_free(stats); } Buffer Diff::Stats::to_buf(diff::stats::format::type format, size_t width) const { git_buf buf = GIT_BUF_INIT_CONST(nullptr, 0); if (git_diff_stats_to_buf(&buf, stats_.get(), git_diff_stats_format_t(format.value()), width)) throw error_t("git_diff_stats_to_buf fail"); else return Buffer(buf); } }