mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
Loading...
Searching...
No Matches
gitRepo.cpp
Go to the documentation of this file.
1/** \file gitRepo.cpp
2 * \author Jared R. Males
3 * \brief Interrogate the current state of a git repository (definitions)
4 * \ingroup utils_files
5 */
6
7//***********************************************************************//
8// Copyright 2021 Jared R. Males (jaredmales@gmail.com)
9//
10// This file is part of mxlib.
11//
12// mxlib is free software: you can redistribute it and/or modify
13// it under the terms of the GNU General Public License as published by
14// the Free Software Foundation, either version 3 of the License, or
15// (at your option) any later version.
16//
17// mxlib is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU General Public License for more details.
21//
22// You should have received a copy of the GNU General Public License
23// along with mxlib. If not, see <http://www.gnu.org/licenses/>.
24//***********************************************************************//
25
26#include "sys/gitRepo.hpp"
27
30#include "ioutils/fileUtils.hpp"
31
32namespace mx
33{
34namespace sys
35{
36
40
41gitRepo::gitRepo( const std::string &d )
42{
43 dir( d );
44}
45
46void gitRepo::dir( const std::string &d )
47{
48 m_dir = d;
49
50 getGitName();
51 getGitHash();
54}
55
56std::string gitRepo::dir()
57{
58 return m_dir;
59}
60
61std::string gitRepo::gitDir()
62{
63 return m_dir + "/.git";
64}
65
67{
68 int retVal;
69 std::vector<std::string> stdOut, stdErr;
70
71 if( ipc::runCommand( retVal, stdOut, stdErr, { "git", "--git-dir=" + gitDir(), "rev-parse", "--show-toplevel" } ) <
72 0 )
73 {
74 internal::mxlib_error_report(error_t::procerr, "error running git command" );
75 return -1;
76 }
77
78 if( stdErr.size() > 0 )
79 {
80 internal::mxlib_error_report(error_t::procerr, "error returned by git" );
81
82 for( size_t n = 0; n < stdErr.size(); ++n )
83 {
84 std::cerr << "err: " << stdErr[n] << "\n";
85 }
86
87 return -1;
88 }
89
90 if( stdOut.size() < 1 )
91 {
92 internal::mxlib_error_report(error_t::procerr, "nothing returned by git" );
93 return -1;
94 }
95
96 if( stdOut.size() > 1 )
97 {
98 internal::mxlib_error_report(error_t::procerr, "too much returned by git" );
99 return -1;
100 }
101
102 m_name = ioutils::pathFilename( stdOut[0] );
103
104 return 0;
105}
106
108{
109 int retVal;
110 std::vector<std::string> stdOut, stdErr;
111
112 if( ipc::runCommand( retVal,
113 stdOut,
114 stdErr,
115 { "git", "--git-dir=" + gitDir(), "--work-tree=" + dir(), "log", "-1", "--format=%H" } ) < 0 )
116 {
117 internal::mxlib_error_report(error_t::procerr, "error running git command" );
118 return -1;
119 }
120
121 if( stdErr.size() > 0 )
122 {
123 internal::mxlib_error_report(error_t::procerr, "error returned by git" );
124
125 for( size_t n = 0; n < stdErr.size(); ++n )
126 {
127 std::cerr << "err: " << stdErr[n] << "\n";
128 }
129
130 return -1;
131 }
132
133 if( stdOut.size() < 1 )
134 {
135 internal::mxlib_error_report(error_t::procerr, "nothing returned by git" );
136 return -1;
137 }
138
139 if( stdOut.size() > 1 )
140 {
141 internal::mxlib_error_report(error_t::procerr, "too much returned by git" );
142 return -1;
143 }
144
145 m_hash = stdOut[0];
146
147 return 0;
148}
149
151{
152 int retVal;
153 std::vector<std::string> stdOut, stdErr;
154
155 if( ipc::runCommand( retVal,
156 stdOut,
157 stdErr,
158 { "git", "--git-dir=" + gitDir(), "--work-tree=" + dir(), "diff-index", "HEAD", "--" } ) < 0 )
159 {
160 internal::mxlib_error_report(error_t::procerr, "error running git command" );
161 return -1;
162 }
163
164 if( stdErr.size() > 0 )
165 {
166 internal::mxlib_error_report(error_t::procerr, "error returned by git" );
167 for( size_t n = 0; n < stdErr.size(); ++n )
168 {
169 std::cerr << "err: " << stdErr[n] << "\n";
170 }
171
172 return -1;
173 }
174
175 m_modified = ( stdOut.size() > 0 ); // rv;
176
177 return 0;
178}
179
181{
182 int retVal;
183 std::vector<std::string> stdOut, stdErr;
184
185 if( ipc::runCommand(
186 retVal, stdOut, stdErr, { "git", "--git-dir=" + gitDir(), "--work-tree=" + dir(), "status" } ) < 0 )
187 {
188 internal::mxlib_error_report(error_t::procerr, "error running git command" );
189 return -1;
190 }
191
192 if( stdErr.size() > 0 )
193 {
194 internal::mxlib_error_report(error_t::procerr, "error returned by git" );
195
196 for( size_t n = 0; n < stdErr.size(); ++n )
197 {
198 std::cerr << "err: " << stdErr[n] << "\n";
199 }
200
201 return -1;
202 }
203
204 if( stdOut.size() < 1 )
205 {
206 internal::mxlib_error_report(error_t::procerr, "nothing returned by git" );
207 return -1;
208 }
209
210 m_modifiedFiles.clear();
211 m_deletedFiles.clear();
212 m_renamedFiles.clear();
213 m_renamedFiles2.clear();
214 m_untrackedFiles.clear();
215
216 for( size_t n = 0; n < stdOut.size(); ++n )
217 {
218 if( stdOut[n].find( "On b" ) != std::string::npos )
219 {
220 m_branch = stdOut[n].substr( sizeof( "On branch" ) );
221 }
222 else if( stdOut[n].find( "Changes not" ) != std::string::npos )
223 {
224 // Changes not staged for commit
225
226 ++n;
227 while( stdOut[n].size() > 0 )
228 {
229 if( n >= stdOut.size() )
230 break;
231
232 if( stdOut[n].find( "mod" ) != std::string::npos )
233 {
234 m_modifiedFiles.insert( ioutils::removeWhiteSpace( stdOut[n].substr( sizeof( "modified:" ) ) ) );
235 }
236 else if( stdOut[n].find( "del" ) != std::string::npos )
237 {
238 m_deletedFiles.insert( ioutils::removeWhiteSpace( stdOut[n].substr( sizeof( "deleted:" ) ) ) );
239 }
240 else if( stdOut[n].find( "ren" ) != std::string::npos )
241 {
242 size_t a = stdOut[n].find( " -> " );
243
245 stdOut[n].substr( sizeof( "renamed:" ), a - sizeof( "renamed:" ) ) ) );
246 m_renamedFiles2.insert( ioutils::removeWhiteSpace( stdOut[n].substr( a + sizeof( " ->" ) ) ) );
247 }
248 ++n;
249 }
250 }
251 else if( stdOut[n].find( "Changes to" ) != std::string::npos )
252 {
253 // Changes to be committed
254
255 ++n;
256 while( stdOut[n].size() > 0 )
257 {
258 if( n >= stdOut.size() )
259 break;
260
261 if( stdOut[n].find( "mod" ) != std::string::npos )
262 {
263 m_modifiedFiles.insert( ioutils::removeWhiteSpace( stdOut[n].substr( sizeof( "modified:" ) ) ) );
264 }
265 else if( stdOut[n].find( "del" ) != std::string::npos )
266 {
267 m_deletedFiles.insert( ioutils::removeWhiteSpace( stdOut[n].substr( sizeof( "deleted:" ) ) ) );
268 }
269 else if( stdOut[n].find( "ren" ) != std::string::npos )
270 {
271 size_t a = stdOut[n].find( " -> " );
272
274 stdOut[n].substr( sizeof( "renamed: " ), a - sizeof( "renamed:" ) ) ) );
275 m_renamedFiles2.insert( ioutils::removeWhiteSpace( stdOut[n].substr( a + sizeof( " ->" ) ) ) );
276 }
277 ++n;
278 }
279 }
280 else if( stdOut[n].find( "Untracked" ) != std::string::npos )
281 {
282 // Untracked files:
283
284 ++n;
285 ++n;
286 while( stdOut[n].size() > 0 )
287 {
288 if( n >= stdOut.size() )
289 break;
290 m_untrackedFiles.insert( ioutils::removeWhiteSpace( stdOut[n] ) );
291 ++n;
292 }
293 }
294 }
295
296 return 0;
297}
298
299std::string gitRepo::name()
300{
301 return m_name;
302}
303
304std::string gitRepo::branch()
305{
306 return m_branch;
307}
308
309std::string gitRepo::hash()
310{
311 return m_hash;
312}
313
315{
316 return m_modified;
317}
318
319bool gitRepo::isNotCommitted( const std::string &file )
320{
321 return ( m_modifiedFiles.count( file ) + m_deletedFiles.count( file ) + m_renamedFiles.count( file ) +
322 m_renamedFiles2.count( file ) + m_untrackedFiles.count( file ) >
323 0 );
324}
325
326} // namespace sys
327} // namespace mx
int getGitName()
Get the name of the git repo.
Definition gitRepo.cpp:66
std::string gitDir()
Get the current repo's .git directory.
Definition gitRepo.cpp:61
std::set< std::string > m_renamedFiles
Files which git lists as renamed-from.
Definition gitRepo.hpp:66
std::string m_branch
The current branch.
Definition gitRepo.hpp:60
std::set< std::string > m_renamedFiles2
Files which git lists as renamed-to.
Definition gitRepo.hpp:67
bool isNotCommitted(const std::string &file)
Check whether a file is listed as not committed.
Definition gitRepo.cpp:319
std::string m_dir
The directory of the git repository.
Definition gitRepo.hpp:56
int getGitHash()
Get the name of the current commit hash.
Definition gitRepo.cpp:107
std::string branch()
Get the current branch.
Definition gitRepo.cpp:304
std::string name()
Get the repo's name.
Definition gitRepo.cpp:299
std::string m_name
The repo name.
Definition gitRepo.hpp:59
std::set< std::string > m_untrackedFiles
Files which git lists as untracked.
Definition gitRepo.hpp:68
std::set< std::string > m_deletedFiles
Files which git lists as deleted.
Definition gitRepo.hpp:65
std::set< std::string > m_modifiedFiles
Files which git lists as modified.
Definition gitRepo.hpp:64
std::string dir()
Get the current directory.
Definition gitRepo.cpp:56
std::string m_hash
The complete commit hash.
Definition gitRepo.hpp:61
bool modified()
Get whether the repo is modified.
Definition gitRepo.cpp:314
gitRepo()
Default c'tor.
Definition gitRepo.cpp:37
int getGitModified()
Get the modification status of the repo.
Definition gitRepo.cpp:150
bool m_modified
The modification status, true or false.
Definition gitRepo.hpp:62
int getGitFileState()
Get the list of modified files, and the branch name.
Definition gitRepo.cpp:180
std::string hash()
Get the current commit hash.
Definition gitRepo.cpp:309
Declarations of utilities for working with files.
Interrogate the current state of a git repository (declarations)
int runCommand(int &retVal, std::vector< std::string > &commandOutput, std::vector< std::string > &commandStderr, const std::vector< std::string > &commandList)
Runs a command (with parameters) passed in using fork/exec.
@ procerr
An error occurred while starting a process.
error_t mxlib_error_report(const error_t &code, const std::string &expl, const std::source_location &loc=std::source_location::current())
Print a report to stderr given an mxlib error_t code and explanation and return the code.
Definition error.hpp:331
std::string pathFilename(const std::string &fname)
Get the base filename.
Definition fileUtils.cpp:78
void removeWhiteSpace(std::string &outstr, const std::string &instr)
Remove all white space from a string.
The mxlib c++ namespace.
Definition mxlib.hpp:37
Process interface facilities.
Utilities for working with strings.