From c64f99c57a093e76d5fc22c0096caac8a3a8df80 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Fri, 12 Sep 2025 06:00:27 -0700 Subject: [PATCH] ADDED: VCSHASH and VCSSHORTHASH Right now, git hashes only resolved by the variables ${VCSHASH} or ${VCSSHORTHASH} --- common/git/project_git_utils.cpp | 33 ++++++++++++++++++++++++++++++++ common/git/project_git_utils.h | 10 ++++++++++ common/project.cpp | 13 +++++++++++++ 3 files changed, 56 insertions(+) diff --git a/common/git/project_git_utils.cpp b/common/git/project_git_utils.cpp index c3e1c2cbc7..4ed459cbde 100644 --- a/common/git/project_git_utils.cpp +++ b/common/git/project_git_utils.cpp @@ -24,6 +24,9 @@ #include "project_git_utils.h" #include "git_backend.h" +#include +#include + namespace KIGIT { @@ -43,4 +46,34 @@ bool PROJECT_GIT_UTILS::RemoveVCS( git_repository*& aRepo, const wxString& aProj return GetGitBackend()->RemoveVCS( aRepo, aProjectPath, aRemoveGitDir, aErrors ); } +wxString PROJECT_GIT_UTILS::GetCurrentHash( const wxString& aProjectFile, bool aShort ) +{ + wxString result = wxT( "no hash" ); + git_repository* repo = PROJECT_GIT_UTILS::GetRepositoryForFile( TO_UTF8( aProjectFile ) ); + + if( repo ) + { + git_reference* head = nullptr; + + if( git_repository_head( &head, repo ) == 0 ) + { + const git_oid* oid = git_reference_target( head ); + + if( oid ) + { + char buf[41]; + size_t len = aShort ? 8 : 41; + git_oid_tostr( buf, len, oid ); + result = wxString::FromUTF8( buf ); + } + + git_reference_free( head ); + } + + git_repository_free( repo ); + } + + return result; +} + } // namespace KIGIT diff --git a/common/git/project_git_utils.h b/common/git/project_git_utils.h index 82d523043c..2905477d75 100644 --- a/common/git/project_git_utils.h +++ b/common/git/project_git_utils.h @@ -51,6 +51,16 @@ public: */ static int CreateBranch( git_repository* aRepo, const wxString& aBranchName ); + /** + * Return the current HEAD commit hash for the repository containing aProjectFile. + * + * @param aProjectFile Absolute path to any file within the repository (typically the + * project file path). + * @param aShort If true, return the short (8 char) hash, otherwise full hash. + * @return wxString containing the hash or "no hash" if unavailable. + */ + static wxString GetCurrentHash( const wxString& aProjectFile, bool aShort ); + /** * Remove version control from a directory by freeing the repository and * optionally removing the .git directory. diff --git a/common/project.cpp b/common/project.cpp index 99436ed0b0..972196abb5 100644 --- a/common/project.cpp +++ b/common/project.cpp @@ -36,6 +36,8 @@ #include #include #include +#include +#include #include #include #include @@ -45,6 +47,7 @@ #include + PROJECT::PROJECT() : m_readOnly( false ), m_textVarsTicker( 0 ), @@ -85,6 +88,16 @@ bool PROJECT::TextVarResolver( wxString* aToken ) const *aToken = TITLE_BLOCK::GetCurrentDate(); return true; } + else if( aToken->IsSameAs( wxT( "VCSHASH" ) ) ) + { + *aToken = KIGIT::PROJECT_GIT_UTILS::GetCurrentHash( GetProjectFullName(), false ); + return true; + } + else if( aToken->IsSameAs( wxT( "VCSSHORTHASH" ) ) ) + { + *aToken = KIGIT::PROJECT_GIT_UTILS::GetCurrentHash( GetProjectFullName(), true ); + return true; + } else if( GetTextVars().count( *aToken ) > 0 ) { *aToken = GetTextVars().at( *aToken );