mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
mx::sys::gitRepo Class Reference

Interrogate the current state of a git repository.

Once the target directory is set, either on construction or using the dir() member function, the repo name, branch, commit hash, and modification status are interrogated with calls to git. This includes a list of uncommitted changes, including untracked files.

Examples:

mx::sys::gitRepo gr( "/path/to/repo/name");
std::cout << gr.modified() << '\n'; //will be 1 if modified, 0 if not modified
std::cout << gr.isNotCommitted("filename") << '\n'; //will be 1 if this file is not committed. 0 otherwise.
Interrogate the current state of a git repository.
Definition: gitRepo.hpp:53

Definition at line 52 of file gitRepo.hpp.

#include <sys/gitRepo.hpp>

Public Member Functions

 gitRepo ()
 Default c'tor. More...
 
 gitRepo (const std::string &d)
 Constructor which sets the directory. More...
 
void dir (const std::string &d)
 Set the directory. More...
 
std::string dir ()
 Get the current directory. More...
 
std::string gitDir ()
 Get the current repo's .git directory. More...
 
std::string name ()
 Get the repo's name. More...
 
std::string branch ()
 Get the current branch. More...
 
std::string hash ()
 Get the current commit hash. More...
 
bool modified ()
 Get whether the repo is modified. More...
 
bool isNotCommitted (const std::string &file)
 Check whether a file is listed as not committed. More...
 

Protected Member Functions

int getGitName ()
 Get the name of the git repo. More...
 
int getGitHash ()
 Get the name of the current commit hash. More...
 
int getGitModified ()
 Get the modification status of the repo. More...
 
int getGitFileState ()
 Get the list of modified files, and the branch name. More...
 

Protected Attributes

std::string m_dir
 The directory of the git repository. More...
 
std::string m_name
 The repo name. More...
 
std::string m_branch
 The current branch. More...
 
std::string m_hash
 The complete commit hash. More...
 
bool m_modified {false}
 The modification status, true or false. More...
 
std::set< std::string > m_modifiedFiles
 Files which git lists as modified. More...
 
std::set< std::string > m_deletedFiles
 Files which git lists as deleted. More...
 
std::set< std::string > m_renamedFiles
 Files which git lists as renamed-from. More...
 
std::set< std::string > m_renamedFiles2
 Files which git lists as renamed-to. More...
 
std::set< std::string > m_untrackedFiles
 Files which git lists as untracked. More...
 

Constructor & Destructor Documentation

◆ gitRepo() [1/2]

mx::sys::gitRepo::gitRepo ( )

Default c'tor.

Definition at line 39 of file gitRepo.cpp.

◆ gitRepo() [2/2]

mx::sys::gitRepo::gitRepo ( const std::string &  d)

Constructor which sets the directory.

This results in the git repo status being interrogated.

Definition at line 43 of file gitRepo.cpp.

References dir().

Member Function Documentation

◆ branch()

std::string mx::sys::gitRepo::branch ( )

Get the current branch.

Returns
the current branch name

Definition at line 294 of file gitRepo.cpp.

References m_branch.

◆ dir() [1/2]

std::string mx::sys::gitRepo::dir ( )

Get the current directory.

Returns
the git repo directory

Definition at line 59 of file gitRepo.cpp.

References m_dir.

Referenced by gitRepo(), getGitFileState(), getGitHash(), and getGitModified().

◆ dir() [2/2]

void mx::sys::gitRepo::dir ( const std::string &  d)

Set the directory.

This results in the git repo status being interrogated.

Definition at line 48 of file gitRepo.cpp.

References getGitFileState(), getGitHash(), getGitModified(), getGitName(), and m_dir.

◆ getGitFileState()

int mx::sys::gitRepo::getGitFileState ( )
protected

Get the list of modified files, and the branch name.

Called whenever m_dir is set.

Returns
0 on success
-1 on error

Definition at line 176 of file gitRepo.cpp.

References dir(), gitDir(), m_branch, m_deletedFiles, m_modifiedFiles, m_renamedFiles, m_renamedFiles2, m_untrackedFiles, mx::ioutils::removeWhiteSpace(), and mx::ipc::runCommand().

Referenced by dir().

◆ getGitHash()

int mx::sys::gitRepo::getGitHash ( )
protected

Get the name of the current commit hash.

Called whenever m_dir is set.

Returns
0 on success
-1 on error

Definition at line 109 of file gitRepo.cpp.

References dir(), gitDir(), m_hash, and mx::ipc::runCommand().

Referenced by dir().

◆ getGitModified()

int mx::sys::gitRepo::getGitModified ( )
protected

Get the modification status of the repo.

Called whenever m_dir is set.

Returns
0 on success
-1 on error

Definition at line 149 of file gitRepo.cpp.

References dir(), gitDir(), m_modified, and mx::ipc::runCommand().

Referenced by dir().

◆ getGitName()

int mx::sys::gitRepo::getGitName ( )
protected

Get the name of the git repo.

Called whenever m_dir is set.

Returns
0 on success
-1 on error

Definition at line 69 of file gitRepo.cpp.

References gitDir(), m_name, mx::ioutils::pathFilename(), and mx::ipc::runCommand().

Referenced by dir().

◆ gitDir()

std::string mx::sys::gitRepo::gitDir ( )

Get the current repo's .git directory.

Returns
the directory plus "/.git"

Definition at line 64 of file gitRepo.cpp.

References m_dir.

Referenced by getGitFileState(), getGitHash(), getGitModified(), and getGitName().

◆ hash()

std::string mx::sys::gitRepo::hash ( )

Get the current commit hash.

Returns
the current value of the hash

Definition at line 299 of file gitRepo.cpp.

References m_hash.

◆ isNotCommitted()

bool mx::sys::gitRepo::isNotCommitted ( const std::string &  file)

Check whether a file is listed as not committed.

Not committed means modified, deleted, renamed (from or to), or untracked.

Returns
true if not committed
false otherwise

Definition at line 309 of file gitRepo.cpp.

References m_deletedFiles, m_modifiedFiles, m_renamedFiles, m_renamedFiles2, and m_untrackedFiles.

◆ modified()

bool mx::sys::gitRepo::modified ( )

Get whether the repo is modified.

Returns
true is modified
false if not modified

Definition at line 304 of file gitRepo.cpp.

References m_modified.

◆ name()

std::string mx::sys::gitRepo::name ( )

Get the repo's name.

Returns
the repo's name

Definition at line 289 of file gitRepo.cpp.

References m_name.

Member Data Documentation

◆ m_branch

std::string mx::sys::gitRepo::m_branch
protected

The current branch.

Definition at line 61 of file gitRepo.hpp.

Referenced by branch(), and getGitFileState().

◆ m_deletedFiles

std::set<std::string> mx::sys::gitRepo::m_deletedFiles
protected

Files which git lists as deleted.

Definition at line 66 of file gitRepo.hpp.

Referenced by getGitFileState(), and isNotCommitted().

◆ m_dir

std::string mx::sys::gitRepo::m_dir
protected

The directory of the git repository.

Definition at line 57 of file gitRepo.hpp.

Referenced by dir(), and gitDir().

◆ m_hash

std::string mx::sys::gitRepo::m_hash
protected

The complete commit hash.

Definition at line 62 of file gitRepo.hpp.

Referenced by getGitHash(), and hash().

◆ m_modified

bool mx::sys::gitRepo::m_modified {false}
protected

The modification status, true or false.

Definition at line 63 of file gitRepo.hpp.

Referenced by getGitModified(), and modified().

◆ m_modifiedFiles

std::set<std::string> mx::sys::gitRepo::m_modifiedFiles
protected

Files which git lists as modified.

Definition at line 65 of file gitRepo.hpp.

Referenced by getGitFileState(), and isNotCommitted().

◆ m_name

std::string mx::sys::gitRepo::m_name
protected

The repo name.

Definition at line 60 of file gitRepo.hpp.

Referenced by getGitName(), and name().

◆ m_renamedFiles

std::set<std::string> mx::sys::gitRepo::m_renamedFiles
protected

Files which git lists as renamed-from.

Definition at line 67 of file gitRepo.hpp.

Referenced by getGitFileState(), and isNotCommitted().

◆ m_renamedFiles2

std::set<std::string> mx::sys::gitRepo::m_renamedFiles2
protected

Files which git lists as renamed-to.

Definition at line 68 of file gitRepo.hpp.

Referenced by getGitFileState(), and isNotCommitted().

◆ m_untrackedFiles

std::set<std::string> mx::sys::gitRepo::m_untrackedFiles
protected

Files which git lists as untracked.

Definition at line 69 of file gitRepo.hpp.

Referenced by getGitFileState(), and isNotCommitted().


The documentation for this class was generated from the following files: