mxlib
c++ tools for analyzing astronomical data and other tasks by Jared R. Males. [git repo]
gitRepo.hpp
Go to the documentation of this file.
1 /** \file gitRepo.hpp
2  * \author Jared R. Males
3  * \brief Interrogate the current state of a git repository (declarations)
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 #ifndef gitRepo_hpp
27 #define gitRepo_hpp
28 
29 #include <iostream>
30 #include <set>
31 
32 namespace mx
33 {
34 namespace sys
35 {
36 
37 /// Interrogate the current state of a git repository
38 /** Once the target directory is set, either on construction
39  * or using the dir() member function, the repo name, branch,
40  * commit hash, and modification status are interrogated with
41  * calls to git. This includes a list of uncommitted changes,
42  * including untracked files.
43  *
44  * Examples:
45  * \code
46  mx::sys::gitRepo gr( "/path/to/repo/name");
47  std::cout << gr.modified() << '\n'; //will be 1 if modified, 0 if not modified
48  std::cout << gr.isNotCommitted("filename") << '\n'; //will be 1 if this file is not committed. 0 otherwise.
49  \endcode
50  *
51  */
52 class gitRepo
53 {
54 protected:
55 
56  //Set by user:
57  std::string m_dir; ///< The directory of the git repository
58 
59  //Found using git:
60  std::string m_name; ///< The repo name
61  std::string m_branch; ///< The current branch
62  std::string m_hash; ///< The complete commit hash
63  bool m_modified {false}; ///< The modification status, true or false.
64 
65  std::set<std::string> m_modifiedFiles; ///< Files which git lists as modified
66  std::set<std::string> m_deletedFiles; ///< Files which git lists as deleted
67  std::set<std::string> m_renamedFiles; ///< Files which git lists as renamed-from
68  std::set<std::string> m_renamedFiles2; ///< Files which git lists as renamed-to
69  std::set<std::string> m_untrackedFiles; ///< Files which git lists as untracked
70 
71  /// Get the name of the git repo
72  /** Called whenever m_dir is set.
73  *
74  * \returns 0 on success
75  * \returns -1 on error
76  */
77  int getGitName();
78 
79  /// Get the name of the current commit hash
80  /** Called whenever m_dir is set.
81  *
82  * \returns 0 on success
83  * \returns -1 on error
84  */
85  int getGitHash();
86 
87  /// Get the modification status of the repo
88  /** Called whenever m_dir is set.
89  *
90  * \returns 0 on success
91  * \returns -1 on error
92  */
93  int getGitModified();
94 
95  /// Get the list of modified files, and the branch name.
96  /** Called whenever m_dir is set.
97  *
98  * \returns 0 on success
99  * \returns -1 on error
100  */
101  int getGitFileState();
102 
103 public:
104 
105  /// Default c'tor
106  gitRepo();
107 
108  /// Constructor which sets the directory.
109  /** This results in the git repo status being interrogated.
110  */
111  gitRepo(const std::string & d);
112 
113  /// Set the directory
114  /** This results in the git repo status being interrogated.
115  */
116  void dir(const std::string & d);
117 
118  /// Get the current directory
119  /** \returns the git repo directory
120  */
121  std::string dir();
122 
123  /// Get the current repo's .git directory
124  /** \returns the directory plus "/.git"
125  */
126  std::string gitDir();
127 
128  /// Get the repo's name
129  /** \returns the repo's name
130  */
131  std::string name();
132 
133  /// Get the current branch
134  /** \returns the current branch name
135  */
136  std::string branch();
137 
138  /// Get the current commit hash
139  /** \returns the current value of the hash
140  */
141  std::string hash();
142 
143  /// Get whether the repo is modified
144  /** \returns true is modified
145  * \returns false if not modified
146  */
147  bool modified();
148 
149  /// Check whether a file is listed as not committed
150  /** Not committed means modified, deleted, renamed (from or to), or untracked.
151  *
152  * \returns true if not committed
153  * \returns false otherwise
154  */
155  bool isNotCommitted(const std::string & file);
156 
157 };
158 
159 }
160 }
161 
162 #endif //gitRepo_hpp
Interrogate the current state of a git repository.
Definition: gitRepo.hpp:53
int getGitName()
Get the name of the git repo.
Definition: gitRepo.cpp:69
std::string gitDir()
Get the current repo's .git directory.
Definition: gitRepo.cpp:64
std::set< std::string > m_renamedFiles
Files which git lists as renamed-from.
Definition: gitRepo.hpp:67
std::string m_branch
The current branch.
Definition: gitRepo.hpp:61
std::set< std::string > m_renamedFiles2
Files which git lists as renamed-to.
Definition: gitRepo.hpp:68
bool isNotCommitted(const std::string &file)
Check whether a file is listed as not committed.
Definition: gitRepo.cpp:309
std::string m_dir
The directory of the git repository.
Definition: gitRepo.hpp:57
int getGitHash()
Get the name of the current commit hash.
Definition: gitRepo.cpp:109
std::string branch()
Get the current branch.
Definition: gitRepo.cpp:294
std::string name()
Get the repo's name.
Definition: gitRepo.cpp:289
std::string m_name
The repo name.
Definition: gitRepo.hpp:60
std::set< std::string > m_untrackedFiles
Files which git lists as untracked.
Definition: gitRepo.hpp:69
std::set< std::string > m_deletedFiles
Files which git lists as deleted.
Definition: gitRepo.hpp:66
std::set< std::string > m_modifiedFiles
Files which git lists as modified.
Definition: gitRepo.hpp:65
std::string dir()
Get the current directory.
Definition: gitRepo.cpp:59
std::string m_hash
The complete commit hash.
Definition: gitRepo.hpp:62
bool modified()
Get whether the repo is modified.
Definition: gitRepo.cpp:304
gitRepo()
Default c'tor.
Definition: gitRepo.cpp:39
int getGitModified()
Get the modification status of the repo.
Definition: gitRepo.cpp:149
bool m_modified
The modification status, true or false.
Definition: gitRepo.hpp:63
int getGitFileState()
Get the list of modified files, and the branch name.
Definition: gitRepo.cpp:176
std::string hash()
Get the current commit hash.
Definition: gitRepo.cpp:299
The mxlib c++ namespace.
Definition: mxError.hpp:107