-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommon.cpp
More file actions
144 lines (124 loc) · 3.87 KB
/
common.cpp
File metadata and controls
144 lines (124 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "common.hpp"
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <ranges>
#include <regex>
#include <sstream>
#include <git2.h>
#include <unistd.h>
#include "git_exception.hpp"
libgit2_object::libgit2_object()
{
git_libgit2_init();
}
libgit2_object::~libgit2_object()
{
git_libgit2_shutdown();
}
std::string get_current_git_path()
{
return std::filesystem::current_path(); // TODO: make sure that it goes to the root
}
// // If directory not specified, uses cwd.
// sub->add_option("directory", directory, "info about directory arg")
// ->check(CLI::ExistingDirectory | CLI::NonexistentPath)
// ->default_val(std::filesystem::current_path());
const std::map<git_status_t, status_messages>& get_status_msg_map()
{
static std::map<git_status_t, status_messages> status_msg_map = // TODO : check spaces in short_mod
{
{GIT_STATUS_CURRENT, {"", ""}},
{GIT_STATUS_INDEX_NEW, {"A ", "\tnew file: "}},
{GIT_STATUS_INDEX_MODIFIED, {"M ", "\tmodified: "}},
{GIT_STATUS_INDEX_DELETED, {"D ", "\tdeleted: "}},
{GIT_STATUS_INDEX_RENAMED, {"R ", "\trenamed: "}},
{GIT_STATUS_INDEX_TYPECHANGE, {"T ", "\ttypechange: "}},
{GIT_STATUS_WT_NEW, {"?? ", "\t"}},
{GIT_STATUS_WT_MODIFIED, {" M ", "\tmodified: "}},
{GIT_STATUS_WT_DELETED, {" D ", "\tdeleted: "}},
{GIT_STATUS_WT_TYPECHANGE, {" T ", "\ttypechange: "}},
{GIT_STATUS_WT_RENAMED, {" R ", "\trenamed: "}},
{GIT_STATUS_WT_UNREADABLE, {"", ""}},
{GIT_STATUS_IGNORED, {"!! ", ""}},
{GIT_STATUS_CONFLICTED, {"AA ", "\tboth added: "}},
};
return status_msg_map;
}
status_messages get_status_msg(git_status_t st)
{
return get_status_msg_map().find(st)->second;
}
git_strarray_wrapper::git_strarray_wrapper(std::vector<std::string> patterns)
: m_patterns(std::move(patterns))
{
init_str_array();
}
git_strarray_wrapper::git_strarray_wrapper(git_strarray_wrapper&& rhs)
: m_patterns(std::move(rhs.m_patterns))
{
init_str_array();
rhs.reset_str_array();
}
git_strarray_wrapper& git_strarray_wrapper::operator=(git_strarray_wrapper&& rhs)
{
using std::swap;
swap(m_patterns, rhs.m_patterns);
swap(m_array.strings, rhs.m_array.strings);
swap(m_array.count, rhs.m_array.count);
return *this;
}
git_strarray_wrapper::~git_strarray_wrapper()
{
reset_str_array();
}
git_strarray_wrapper::operator git_strarray*()
{
return &m_array;
}
void git_strarray_wrapper::reset_str_array()
{
delete[] m_array.strings;
m_array = {nullptr, 0};
}
void git_strarray_wrapper::init_str_array()
{
m_array.strings = new char*[m_patterns.size()];
m_array.count = m_patterns.size();
for (size_t i = 0; i < m_patterns.size(); ++i)
{
m_array.strings[i] = const_cast<char*>(m_patterns[i].c_str());
}
}
size_t git_strarray_wrapper::size()
{
return m_patterns.size();
}
std::string read_file(const std::string& path)
{
std::ifstream file(path, std::ios::binary);
if (!file)
{
throw git_exception("error: Could not access " + path, git2cpp_error_code::GENERIC_ERROR);
}
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
std::vector<std::string> split_input_at_newlines(std::string_view str)
{
auto split = str | std::ranges::views::split('\n')
| std::ranges::views::transform(
[](auto&& range)
{
return std::string(range.begin(), range.end());
}
);
return std::vector<std::string>{split.begin(), split.end()};
}
std::string trim(const std::string& str)
{
auto s = std::regex_replace(str, std::regex("^\\s+"), "");
return std::regex_replace(s, std::regex("\\s+$"), "");
}