34 auto OpenRepo(
const std::string& aPath ) -> git_repository*
43 auto CloseRepo( git_repository* aRepo ) ->
void
46 git_repository_free( aRepo );
49 auto MakeZeroOid() -> git_oid
52 git_oid_fromstrn( &oid,
"0000000000000000000000000000000000000000", 40 );
56 auto GetFileCommit( git_repository* aRepo,
const std::string& aPath ) -> git_oid
63 if( git_reference_name_to_id( &head_oid, aRepo,
"HEAD" ) != 0 )
67 if( aPath.empty() || aPath ==
"." )
71 git_revwalk* walker =
nullptr;
72 if( git_revwalk_new( &walker, aRepo ) != 0 )
75 git_revwalk_sorting( walker, GIT_SORT_TIME );
76 git_revwalk_push( walker, &head_oid );
79 git_oid
result = MakeZeroOid();
81 git_oid prev_blob_oid = MakeZeroOid();
82 bool first_commit =
true;
84 while( git_revwalk_next( &commit_oid, walker ) == 0 )
86 git_commit* commit =
nullptr;
87 if( git_commit_lookup( &commit, aRepo, &commit_oid ) != 0 )
91 git_tree* tree =
nullptr;
92 if( git_commit_tree( &tree, commit ) == 0 )
95 git_tree_entry* entry =
nullptr;
96 if( git_tree_entry_bypath( &entry, tree, aPath.c_str() ) == 0 )
98 const git_oid* blob_oid = git_tree_entry_id( entry );
103 git_oid_cpy( &prev_blob_oid, blob_oid );
104 git_oid_cpy( &
result, &commit_oid );
105 first_commit =
false;
107 else if( git_oid_cmp( blob_oid, &prev_blob_oid ) != 0 )
110 git_tree_entry_free( entry );
111 git_tree_free( tree );
112 git_commit_free( commit );
118 git_oid_cpy( &
result, &commit_oid );
121 git_tree_entry_free( entry );
123 else if( !first_commit )
127 git_tree_free( tree );
128 git_commit_free( commit );
132 git_tree_free( tree );
134 git_commit_free( commit );
137 git_revwalk_free( walker );
147 auto GetDescribeInfo(
const std::string& aMatch,
bool aAnyTags ) -> DescribeInfo
149 auto repo = OpenRepo(
"." );
154 if( git_reference_name_to_id( &head_oid, repo,
"HEAD" ) != 0 )
160 git_strarray tag_names;
161 if( git_tag_list_match( &tag_names, aMatch.empty() ?
"*" : aMatch.c_str(), repo ) != 0 )
168 std::map<git_oid, std::string,
decltype([](
const git_oid& a,
const git_oid& b) {
169 return git_oid_cmp(&a, &b) < 0;
172 for(
size_t i = 0; i < tag_names.count; ++i )
174 git_object* tag_obj =
nullptr;
175 if( git_revparse_single( &tag_obj, repo, tag_names.strings[i] ) == 0 )
177 git_object_t type = git_object_type( tag_obj );
179 if( type == GIT_OBJECT_TAG )
181 git_object* target =
nullptr;
182 if( git_tag_peel( &target, (git_tag*) tag_obj ) == 0 )
184 commit_to_tag[*git_object_id( target )] = tag_names.strings[i];
185 git_object_free( target );
188 else if( aAnyTags && type == GIT_OBJECT_COMMIT )
190 commit_to_tag[*git_object_id( tag_obj )] = tag_names.strings[i];
192 git_object_free( tag_obj );
196 git_strarray_dispose( &tag_names );
198 git_revwalk* walker =
nullptr;
199 if( git_revwalk_new( &walker, repo ) != 0 )
205 git_revwalk_sorting( walker, GIT_SORT_TOPOLOGICAL | GIT_SORT_TIME );
206 git_revwalk_push( walker, &head_oid );
208 DescribeInfo
result{
"", 0 };
212 while( git_revwalk_next( &commit_oid, walker ) == 0 )
214 auto it = commit_to_tag.find( commit_oid );
215 if( it != commit_to_tag.end() )
224 git_revwalk_free( walker );
229 auto GetCommitSignatureField(
const std::string& aPath,
bool aUseCommitter,
bool aGetEmail ) -> std::string
231 auto repo = OpenRepo( aPath );
235 git_oid oid = GetFileCommit( repo, aPath );
237 if( git_oid_is_zero( &oid ) )
243 git_commit* commit =
nullptr;
246 if( git_commit_lookup( &commit, repo, &oid ) == 0 )
248 const git_signature* sig = aUseCommitter ? git_commit_committer( commit ) : git_commit_author( commit );
252 const char* field = aGetEmail ? sig->email : sig->name;
257 git_commit_free( commit );
268 auto repo = OpenRepo( aPath );
272 git_oid oid = GetFileCommit( repo, aPath );
274 if( git_oid_is_zero( &oid ) )
280 int length = std::max( 4, std::min( aLength, GIT_OID_HEXSZ ) );
281 char hash[GIT_OID_HEXSZ + 1];
282 git_oid_tostr( hash, length + 1, &oid );
290 return GetDescribeInfo( aMatch, aAnyTags ).tag;
295 return GetDescribeInfo( aMatch, aAnyTags ).distance;
300 auto repo = OpenRepo(
"." );
304 git_status_list* status =
nullptr;
305 git_status_options statusOpts;
306 git_status_options_init( &statusOpts, GIT_STATUS_OPTIONS_VERSION );
308 statusOpts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
309 statusOpts.flags = aIncludeUntracked ? GIT_STATUS_OPT_INCLUDE_UNTRACKED : GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
311 bool isDirty =
false;
313 if( git_status_list_new( &status, repo, &statusOpts ) == 0 )
315 isDirty = git_status_list_entrycount( status ) > 0;
316 git_status_list_free( status );
325 return GetCommitSignatureField( aPath,
false,
false );
330 return GetCommitSignatureField( aPath,
false,
true );
335 return GetCommitSignatureField( aPath,
true,
false );
340 return GetCommitSignatureField( aPath,
true,
true );
345 auto repo = OpenRepo(
"." );
353 return branchName.ToStdString();
358 auto repo = OpenRepo( aPath );
362 git_oid oid = GetFileCommit( repo, aPath );
364 if( git_oid_is_zero( &oid ) )
370 git_commit* commit =
nullptr;
371 int64_t timestamp = 0;
373 if( git_commit_lookup( &commit, repo, &oid ) == 0 )
375 timestamp =
static_cast<int64_t
>( git_commit_time( commit ) );
376 git_commit_free( commit );
386 return timestamp > 0 ? std::to_string( timestamp ) :
"";
VCS (Version Control System) utility functions for text evaluation.
std::string GetAuthor(const std::string &aPath)
Get the author name of the HEAD commit.
bool IsDirty(bool aIncludeUntracked)
Check if the repository has uncommitted changes.
std::string GetCommitterEmail(const std::string &aPath)
Get the committer email of the HEAD commit.
std::string GetCommitDate(const std::string &aPath)
Get the commit date of the HEAD commit as a timestamp string.
std::string GetAuthorEmail(const std::string &aPath)
Get the author email of the HEAD commit.
std::string GetCommitter(const std::string &aPath)
Get the committer name of the HEAD commit.
std::string GetBranch()
Get the current branch name.
std::string GetNearestTag(const std::string &aMatch, bool aAnyTags)
Get the nearest tag/label from HEAD.
int64_t GetCommitTimestamp(const std::string &aPath)
Get the commit timestamp (Unix time) of the HEAD commit.
int GetDistanceFromTag(const std::string &aMatch, bool aAnyTags)
Get the number of commits since the nearest matching tag.
wxString result
Test unit parsing edge cases and error handling.