34 git_repository* OpenRepo(
const std::string& aPath )
42 void CloseRepo( git_repository* aRepo )
45 git_repository_free( aRepo );
51 git_oid_fromstrn( &oid,
"0000000000000000000000000000000000000000", 40 );
55 git_oid GetFileCommit( git_repository* aRepo,
const std::string& aPath )
63 if( git_reference_name_to_id( &head_oid, aRepo,
"HEAD" ) != 0 )
67 if( aPath.empty() || aPath ==
"." )
71 git_revwalk* walker =
nullptr;
73 if( git_revwalk_new( &walker, aRepo ) != 0 )
76 git_revwalk_sorting( walker, GIT_SORT_TIME );
77 git_revwalk_push( walker, &head_oid );
80 git_oid
result = MakeZeroOid();
82 git_oid prev_blob_oid = MakeZeroOid();
83 bool first_commit =
true;
85 while( git_revwalk_next( &commit_oid, walker ) == 0 )
87 git_commit* commit =
nullptr;
89 if( git_commit_lookup( &commit, aRepo, &commit_oid ) != 0 )
93 git_tree* tree =
nullptr;
95 if( git_commit_tree( &tree, commit ) == 0 )
98 git_tree_entry* entry =
nullptr;
100 if( git_tree_entry_bypath( &entry, tree, aPath.c_str() ) == 0 )
102 const git_oid* blob_oid = git_tree_entry_id( entry );
107 git_oid_cpy( &prev_blob_oid, blob_oid );
108 git_oid_cpy( &
result, &commit_oid );
109 first_commit =
false;
111 else if( git_oid_cmp( blob_oid, &prev_blob_oid ) != 0 )
114 git_tree_entry_free( entry );
115 git_tree_free( tree );
116 git_commit_free( commit );
122 git_oid_cpy( &
result, &commit_oid );
125 git_tree_entry_free( entry );
127 else if( !first_commit )
131 git_tree_free( tree );
132 git_commit_free( commit );
136 git_tree_free( tree );
139 git_commit_free( commit );
142 git_revwalk_free( walker );
152 DescribeInfo GetDescribeInfo(
const std::string& aMatch,
bool aAnyTags )
154 git_repository* repo = OpenRepo(
"." );
157 return { std::string(), 0 };
161 if( git_reference_name_to_id( &head_oid, repo,
"HEAD" ) != 0 )
164 return { std::string(), 0 };
167 git_strarray tag_names;
169 if( git_tag_list_match( &tag_names, aMatch.empty() ?
"*" : aMatch.c_str(), repo ) != 0 )
172 return { std::string(), 0 };
176 std::map<git_oid, std::string,
decltype(
177 [](
const git_oid& a,
const git_oid& b)
179 return git_oid_cmp(&a, &b) < 0;
182 for(
size_t i = 0; i < tag_names.count; ++i )
184 git_object* tag_obj =
nullptr;
186 if( git_revparse_single( &tag_obj, repo, tag_names.strings[i] ) == 0 )
188 git_object_t type = git_object_type( tag_obj );
190 if( type == GIT_OBJECT_TAG )
192 git_object* target =
nullptr;
194 if( git_tag_peel( &target, (git_tag*) tag_obj ) == 0 )
196 commit_to_tag[*git_object_id( target )] = tag_names.strings[i];
197 git_object_free( target );
200 else if( aAnyTags && type == GIT_OBJECT_COMMIT )
202 commit_to_tag[*git_object_id( tag_obj )] = tag_names.strings[i];
205 git_object_free( tag_obj );
209 git_strarray_dispose( &tag_names );
211 git_revwalk* walker =
nullptr;
213 if( git_revwalk_new( &walker, repo ) != 0 )
216 return { std::string(), 0 };
219 git_revwalk_sorting( walker, GIT_SORT_TOPOLOGICAL | GIT_SORT_TIME );
220 git_revwalk_push( walker, &head_oid );
222 DescribeInfo
result{ std::string(), 0 };
226 while( git_revwalk_next( &commit_oid, walker ) == 0 )
228 auto it = commit_to_tag.find( commit_oid );
230 if( it != commit_to_tag.end() )
240 git_revwalk_free( walker );
245 std::string GetCommitSignatureField(
const std::string& aPath,
bool aUseCommitter,
bool aGetEmail )
247 git_repository* repo = OpenRepo( aPath );
250 return std::string();
252 git_oid oid = GetFileCommit( repo, aPath );
254 if( git_oid_is_zero( &oid ) )
257 return std::string();
260 git_commit* commit =
nullptr;
263 if( git_commit_lookup( &commit, repo, &oid ) == 0 )
265 const git_signature* sig = aUseCommitter ? git_commit_committer( commit ) : git_commit_author( commit );
269 const char* field = aGetEmail ? sig->email : sig->name;
275 git_commit_free( commit );
287 git_repository* repo = OpenRepo( aPath );
290 return std::string();
292 git_oid oid = GetFileCommit( repo, aPath );
294 if( git_oid_is_zero( &oid ) )
297 return std::string();
300 int length = std::max( 4, std::min( aLength, GIT_OID_HEXSZ ) );
301 char hash[GIT_OID_HEXSZ + 1];
302 git_oid_tostr( hash, length + 1, &oid );
311 return GetDescribeInfo( aMatch, aAnyTags ).tag;
317 return GetDescribeInfo( aMatch, aAnyTags ).distance;
323 git_repository* repo = OpenRepo(
"." );
328 git_status_list* status =
nullptr;
329 git_status_options statusOpts;
330 git_status_options_init( &statusOpts, GIT_STATUS_OPTIONS_VERSION );
332 statusOpts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
333 statusOpts.flags = aIncludeUntracked ? GIT_STATUS_OPT_INCLUDE_UNTRACKED : GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
335 bool isDirty =
false;
337 if( git_status_list_new( &status, repo, &statusOpts ) == 0 )
339 isDirty = git_status_list_entrycount( status ) > 0;
340 git_status_list_free( status );
350 return GetCommitSignatureField( aPath,
false,
false );
356 return GetCommitSignatureField( aPath,
false,
true );
362 return GetCommitSignatureField( aPath,
true,
false );
368 return GetCommitSignatureField( aPath,
true,
true );
374 git_repository* repo = OpenRepo(
"." );
377 return std::string();
383 return branchName.ToStdString();
389 git_repository* repo = OpenRepo( aPath );
394 git_oid oid = GetFileCommit( repo, aPath );
396 if( git_oid_is_zero( &oid ) )
402 git_commit* commit =
nullptr;
403 int64_t timestamp = 0;
405 if( git_commit_lookup( &commit, repo, &oid ) == 0 )
407 timestamp =
static_cast<int64_t
>( git_commit_time( commit ) );
408 git_commit_free( commit );
419 return timestamp > 0 ? std::to_string( timestamp ) : std::string();
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.