41 thread_local bool tl_contextGone =
false;
45 ~CONTEXT() { tl_contextGone =
true; }
51 thread_local CONTEXT tl_context;
53 void setContext(
const wxString& aPath,
bool aIsFile )
58 tl_context.path = aPath;
59 tl_context.isFile = aIsFile;
66 setContext( aPath, !aPath.IsEmpty() && wxFileName( aPath ).FileExists() );
72 if( tl_contextGone || tl_context.path.IsEmpty() )
73 return wxString( wxT(
"." ) );
75 return tl_context.path;
81 return !tl_contextGone && tl_context.isFile;
102 wxString ResolveEffectivePath(
const std::string& aPath )
104 if( aPath.empty() || aPath ==
"." )
107 wxFileName
path( wxString::FromUTF8( aPath ) );
109 if(
path.IsRelative() )
112 context.MakeAbsolute();
116 return path.GetFullPath();
121 using VCS_VALUE = TEXT_EVAL::ENVIRONMENT::VCS_VALUE;
123 VCS_VALUE Capture( VCS_QUERY aQuery,
const std::string& aPath,
const std::string& aArgument,
124 int aOptions,
const std::function<VCS_VALUE()>& aRead )
128 wxFileName
path = wxFileName::DirName( ResolveEffectivePath( aPath ) );
130 const bool contextFile =
GetContextIsFile() && ( aPath.empty() || aPath ==
"." );
131 return environment->VcsValue( { aQuery,
132 path.GetPath( (
int) wxPATH_GET_VOLUME ),
148 wxFileName effective( ResolveEffectivePath( aPath ) );
152 effective.MakeAbsolute();
162 git_repository_free( aRepo );
165 git_oid MakeZeroOid()
168 git_oid_fromstrn( &oid,
"0000000000000000000000000000000000000000", 40 );
172 git_oid GetFileCommit(
git_repository* aRepo,
const std::string& aPath )
175 return MakeZeroOid();
179 if( git_oid_is_zero( &head_oid ) )
180 return MakeZeroOid();
183 if( aPath.empty() || aPath ==
"." )
186 const char* workdir = git_repository_workdir( aRepo );
189 return MakeZeroOid();
191 wxFileName file( ResolveEffectivePath( aPath ) );
194 file.GetPath(), wxString::FromUTF8( workdir ) );
196 if( !file.MakeRelativeTo( base ) )
197 return MakeZeroOid();
199 const std::string treePath = file.GetFullPath( wxPATH_UNIX ).ToStdString( wxConvUTF8 );
201 if( treePath.empty() || treePath ==
"." || treePath ==
".." || treePath.starts_with(
"../" ) )
202 return MakeZeroOid();
205 git_revwalk* walker =
nullptr;
207 if( git_revwalk_new( &walker, aRepo ) != 0 )
208 return MakeZeroOid();
210 git_revwalk_sorting( walker, GIT_SORT_TIME );
212 if( git_revwalk_push( walker, &head_oid ) != 0 )
214 git_revwalk_free( walker );
215 return MakeZeroOid();
219 git_oid
result = MakeZeroOid();
221 git_oid prev_blob_oid = MakeZeroOid();
222 bool first_commit =
true;
224 while( git_revwalk_next( &commit_oid, walker ) == 0 )
226 git_commit* commit =
nullptr;
228 if( git_commit_lookup( &commit, aRepo, &commit_oid ) != 0 )
232 git_tree* tree =
nullptr;
234 if( git_commit_tree( &tree, commit ) == 0 )
237 git_tree_entry* entry =
nullptr;
239 if( git_tree_entry_bypath( &entry, tree, treePath.c_str() ) == 0 )
241 const git_oid* blob_oid = git_tree_entry_id( entry );
246 git_oid_cpy( &prev_blob_oid, blob_oid );
247 git_oid_cpy( &
result, &commit_oid );
248 first_commit =
false;
250 else if( git_oid_cmp( blob_oid, &prev_blob_oid ) != 0 )
253 git_tree_entry_free( entry );
254 git_tree_free( tree );
255 git_commit_free( commit );
261 git_oid_cpy( &
result, &commit_oid );
264 git_tree_entry_free( entry );
266 else if( !first_commit )
270 git_tree_free( tree );
271 git_commit_free( commit );
275 git_tree_free( tree );
278 git_commit_free( commit );
281 git_revwalk_free( walker );
291 DESCRIBE_INFO ReadDescribeInfo(
const std::string& aMatch,
bool aAnyTags )
296 return { std::string(), 0 };
300 if( git_oid_is_zero( &head_oid ) )
303 return { std::string(), 0 };
306 git_strarray tag_names;
308 if( git_tag_list_match( &tag_names, aMatch.empty() ?
"*" : aMatch.c_str(), repo ) != 0 )
311 return { std::string(), 0 };
315 std::map<git_oid, std::string,
decltype(
316 [](
const git_oid& a,
const git_oid& b)
318 return git_oid_cmp(&a, &b) < 0;
321 for(
size_t i = 0; i < tag_names.count; ++i )
323 git_object* tag_obj =
nullptr;
325 if( git_revparse_single( &tag_obj, repo, tag_names.strings[i] ) == 0 )
327 git_object_t type = git_object_type( tag_obj );
329 if( type == GIT_OBJECT_TAG )
331 git_object* target =
nullptr;
333 if( git_tag_peel( &target, (git_tag*) tag_obj ) == 0 )
335 commit_to_tag[*git_object_id( target )] = tag_names.strings[i];
336 git_object_free( target );
339 else if( aAnyTags && type == GIT_OBJECT_COMMIT )
341 commit_to_tag[*git_object_id( tag_obj )] = tag_names.strings[i];
344 git_object_free( tag_obj );
348 git_strarray_dispose( &tag_names );
350 git_revwalk* walker =
nullptr;
352 if( git_revwalk_new( &walker, repo ) != 0 )
355 return { std::string(), 0 };
358 git_revwalk_sorting( walker, GIT_SORT_TOPOLOGICAL | GIT_SORT_TIME );
360 if( git_revwalk_push( walker, &head_oid ) != 0 )
362 git_revwalk_free( walker );
364 return { std::string(), 0 };
367 DESCRIBE_INFO
result{ std::string(), 0 };
371 while( git_revwalk_next( &commit_oid, walker ) == 0 )
373 auto it = commit_to_tag.find( commit_oid );
375 if( it != commit_to_tag.end() )
385 git_revwalk_free( walker );
390 DESCRIBE_INFO GetDescribeInfo(
const std::string& aMatch,
bool aAnyTags )
392 const VCS_VALUE value = Capture( VCS_QUERY::DESCRIPTION,
".", aMatch, aAnyTags,
395 const DESCRIBE_INFO description = ReadDescribeInfo( aMatch, aAnyTags );
396 return { description.tag, description.distance };
398 return { value.text,
static_cast<int>( value.number ) };
401 std::string ReadCommitSignatureField(
const std::string& aPath,
bool aUseCommitter,
bool aGetEmail )
406 return std::string();
408 git_oid oid = GetFileCommit( repo, aPath );
410 if( git_oid_is_zero( &oid ) )
413 return std::string();
416 git_commit* commit =
nullptr;
419 if( git_commit_lookup( &commit, repo, &oid ) == 0 )
421 const git_signature* sig = aUseCommitter ? git_commit_committer( commit ) : git_commit_author( commit );
425 const char* field = aGetEmail ? sig->email : sig->name;
431 git_commit_free( commit );
438 std::string GetCommitSignatureField(
const std::string& aPath,
bool aUseCommitter,
bool aGetEmail )
440 return Capture( VCS_QUERY::SIGNATURE, aPath, aPath, ( aUseCommitter ? 2 : 0 ) | ( aGetEmail ? 1 : 0 ),
443 return { ReadCommitSignatureField( aPath, aUseCommitter, aGetEmail ) };
455 return std::string();
457 git_oid oid = GetFileCommit( repo, aPath );
459 if( git_oid_is_zero( &oid ) )
462 return std::string();
465 char hash[GIT_OID_HEXSZ + 1];
466 git_oid_tostr( hash,
sizeof( hash ), &oid );
475 const VCS_VALUE value = Capture( VCS_QUERY::HASH, aPath, aPath, 0,
480 return value.text.substr( 0, std::clamp( aLength, 4, GIT_OID_HEXSZ ) );
486 return GetDescribeInfo( aMatch, aAnyTags ).tag;
492 return GetDescribeInfo( aMatch, aAnyTags ).distance;
503 git_status_list* status =
nullptr;
504 git_status_options statusOpts;
505 git_status_options_init( &statusOpts, GIT_STATUS_OPTIONS_VERSION );
507 statusOpts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
508 statusOpts.flags = aIncludeUntracked ? GIT_STATUS_OPT_INCLUDE_UNTRACKED : GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
510 bool isDirty =
false;
512 if( git_status_list_new( &status, repo, &statusOpts ) == 0 )
514 isDirty = git_status_list_entrycount( status ) > 0;
515 git_status_list_free( status );
525 return Capture( VCS_QUERY::DIRTY,
".",
"", aIncludeUntracked,
535 return GetCommitSignatureField( aPath,
false,
false );
541 return GetCommitSignatureField( aPath,
false,
true );
547 return GetCommitSignatureField( aPath,
true,
false );
553 return GetCommitSignatureField( aPath,
true,
true );
562 return std::string();
568 return branchName.ToStdString();
574 return Capture( VCS_QUERY::BRANCH,
".",
"", 0,
589 git_oid oid = GetFileCommit( repo, aPath );
591 if( git_oid_is_zero( &oid ) )
597 git_commit* commit =
nullptr;
598 int64_t timestamp = 0;
600 if( git_commit_lookup( &commit, repo, &oid ) == 0 )
602 timestamp =
static_cast<int64_t
>( git_commit_time( commit ) );
603 git_commit_free( commit );
613 return Capture( VCS_QUERY::TIMESTAMP, aPath, aPath, 0,
624 return timestamp > 0 ? std::to_string( timestamp ) : std::string();
633 const auto& [query,
path, argument, options, contextFile] = aKey;
635 if( query == VCS_QUERY::HEAD )
643 if( git_repository_open( &raw,
path.ToUTF8().data() ) != 0 )
649 if( git_reference_name_to_id( &oid, repo.get(),
"HEAD" ) != 0 )
652 char hash[GIT_OID_HEXSZ + 1];
653 git_oid_tostr( hash,
sizeof( hash ), &oid );
657 const bool fileQuery = ( query == VCS_QUERY::HASH
658 || query == VCS_QUERY::SIGNATURE
659 || query == VCS_QUERY::TIMESTAMP )
663 const std::string file = fileQuery ?
path.ToStdString( wxConvUTF8 ) : std::string();
667 case VCS_QUERY::HASH:
670 case VCS_QUERY::DESCRIPTION:
672 const DESCRIBE_INFO description = ReadDescribeInfo( argument, options != 0 );
673 return { description.tag, description.distance };
676 case VCS_QUERY::SIGNATURE:
677 return { ReadCommitSignatureField( file, ( options & 2 ) != 0, ( options & 1 ) != 0 ) };
679 case VCS_QUERY::BRANCH:
682 case VCS_QUERY::DIRTY:
685 case VCS_QUERY::TIMESTAMP:
688 case VCS_QUERY::HEAD:
696 return environment->VcsValue( aKey, read );
std::tuple< VCS_QUERY, wxString, std::string, int, bool > VCS_KEY
Query, absolute path (Git directory for HEAD), selector or pattern, options, and whether the context ...
RAII helper that sets the VCS context path on construction and restores the previous value on destruc...
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.
wxString GetContextPath()
Return the current context path for repo-scoped VCS queries.
bool GetContextIsFile()
File/directory classification captured when the current context was activated.
TEXT_EVAL::ENVIRONMENT::VCS_VALUE ReadSource(const TEXT_EVAL::ENVIRONMENT::VCS_KEY &aKey)
Re-read an owned query descriptor using any active frame memo.
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.
void SetContextPath(const wxString &aPath)
Set the filesystem path used for repository discovery and relative file queries.
std::string GetCommitter(const std::string &aPath)
Get the committer name of the HEAD commit.
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.