36 return _(
"<unknown>" ).ToStdString();
39 git_repository* OpenRepo(
const std::string& aPath )
48 void CloseRepo( git_repository* aRepo )
51 git_repository_free( aRepo );
57 git_oid_fromstrn( &oid,
"0000000000000000000000000000000000000000", 40 );
61 git_oid GetFileCommit( git_repository* aRepo,
const std::string& aPath )
69 if( git_reference_name_to_id( &head_oid, aRepo,
"HEAD" ) != 0 )
73 if( aPath.empty() || aPath ==
"." )
77 git_revwalk* walker =
nullptr;
79 if( git_revwalk_new( &walker, aRepo ) != 0 )
82 git_revwalk_sorting( walker, GIT_SORT_TIME );
83 git_revwalk_push( walker, &head_oid );
86 git_oid
result = MakeZeroOid();
88 git_oid prev_blob_oid = MakeZeroOid();
89 bool first_commit =
true;
91 while( git_revwalk_next( &commit_oid, walker ) == 0 )
93 git_commit* commit =
nullptr;
95 if( git_commit_lookup( &commit, aRepo, &commit_oid ) != 0 )
99 git_tree* tree =
nullptr;
101 if( git_commit_tree( &tree, commit ) == 0 )
104 git_tree_entry* entry =
nullptr;
106 if( git_tree_entry_bypath( &entry, tree, aPath.c_str() ) == 0 )
108 const git_oid* blob_oid = git_tree_entry_id( entry );
113 git_oid_cpy( &prev_blob_oid, blob_oid );
114 git_oid_cpy( &
result, &commit_oid );
115 first_commit =
false;
117 else if( git_oid_cmp( blob_oid, &prev_blob_oid ) != 0 )
120 git_tree_entry_free( entry );
121 git_tree_free( tree );
122 git_commit_free( commit );
128 git_oid_cpy( &
result, &commit_oid );
131 git_tree_entry_free( entry );
133 else if( !first_commit )
137 git_tree_free( tree );
138 git_commit_free( commit );
142 git_tree_free( tree );
145 git_commit_free( commit );
148 git_revwalk_free( walker );
158 DescribeInfo GetDescribeInfo(
const std::string& aMatch,
bool aAnyTags )
160 git_repository* repo = OpenRepo(
"." );
163 return { Unknown(), 0 };
167 if( git_reference_name_to_id( &head_oid, repo,
"HEAD" ) != 0 )
170 return { Unknown(), 0 };
173 git_strarray tag_names;
175 if( git_tag_list_match( &tag_names, aMatch.empty() ?
"*" : aMatch.c_str(), repo ) != 0 )
178 return { Unknown(), 0 };
182 std::map<git_oid, std::string,
decltype(
183 [](
const git_oid& a,
const git_oid& b)
185 return git_oid_cmp(&a, &b) < 0;
188 for(
size_t i = 0; i < tag_names.count; ++i )
190 git_object* tag_obj =
nullptr;
192 if( git_revparse_single( &tag_obj, repo, tag_names.strings[i] ) == 0 )
194 git_object_t type = git_object_type( tag_obj );
196 if( type == GIT_OBJECT_TAG )
198 git_object* target =
nullptr;
200 if( git_tag_peel( &target, (git_tag*) tag_obj ) == 0 )
202 commit_to_tag[*git_object_id( target )] = tag_names.strings[i];
203 git_object_free( target );
206 else if( aAnyTags && type == GIT_OBJECT_COMMIT )
208 commit_to_tag[*git_object_id( tag_obj )] = tag_names.strings[i];
211 git_object_free( tag_obj );
215 git_strarray_dispose( &tag_names );
217 git_revwalk* walker =
nullptr;
219 if( git_revwalk_new( &walker, repo ) != 0 )
222 return { Unknown(), 0 };
225 git_revwalk_sorting( walker, GIT_SORT_TOPOLOGICAL | GIT_SORT_TIME );
226 git_revwalk_push( walker, &head_oid );
228 DescribeInfo
result{ Unknown(), 0 };
232 while( git_revwalk_next( &commit_oid, walker ) == 0 )
234 auto it = commit_to_tag.find( commit_oid );
236 if( it != commit_to_tag.end() )
246 git_revwalk_free( walker );
251 std::string GetCommitSignatureField(
const std::string& aPath,
bool aUseCommitter,
bool aGetEmail )
253 git_repository* repo = OpenRepo( aPath );
258 git_oid oid = GetFileCommit( repo, aPath );
260 if( git_oid_is_zero( &oid ) )
266 git_commit* commit =
nullptr;
269 if( git_commit_lookup( &commit, repo, &oid ) == 0 )
271 const git_signature* sig = aUseCommitter ? git_commit_committer( commit ) : git_commit_author( commit );
275 const char* field = aGetEmail ? sig->email : sig->name;
281 git_commit_free( commit );
293 git_repository* repo = OpenRepo( aPath );
298 git_oid oid = GetFileCommit( repo, aPath );
300 if( git_oid_is_zero( &oid ) )
306 int length = std::max( 4, std::min( aLength, GIT_OID_HEXSZ ) );
307 char hash[GIT_OID_HEXSZ + 1];
308 git_oid_tostr( hash, length + 1, &oid );
317 return GetDescribeInfo( aMatch, aAnyTags ).tag;
323 return GetDescribeInfo( aMatch, aAnyTags ).distance;
329 git_repository* repo = OpenRepo(
"." );
334 git_status_list* status =
nullptr;
335 git_status_options statusOpts;
336 git_status_options_init( &statusOpts, GIT_STATUS_OPTIONS_VERSION );
338 statusOpts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
339 statusOpts.flags = aIncludeUntracked ? GIT_STATUS_OPT_INCLUDE_UNTRACKED : GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
341 bool isDirty =
false;
343 if( git_status_list_new( &status, repo, &statusOpts ) == 0 )
345 isDirty = git_status_list_entrycount( status ) > 0;
346 git_status_list_free( status );
356 return GetCommitSignatureField( aPath,
false,
false );
362 return GetCommitSignatureField( aPath,
false,
true );
368 return GetCommitSignatureField( aPath,
true,
false );
374 return GetCommitSignatureField( aPath,
true,
true );
380 git_repository* repo = OpenRepo(
"." );
389 return branchName.ToStdString();
395 git_repository* repo = OpenRepo( aPath );
400 git_oid oid = GetFileCommit( repo, aPath );
402 if( git_oid_is_zero( &oid ) )
408 git_commit* commit =
nullptr;
409 int64_t timestamp = 0;
411 if( git_commit_lookup( &commit, repo, &oid ) == 0 )
413 timestamp =
static_cast<int64_t
>( git_commit_time( commit ) );
414 git_commit_free( commit );
425 return timestamp > 0 ? std::to_string( timestamp ) : Unknown();
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.