添加对gitlab runner执行ci时游离分支的支持

This commit is contained in:
2026-06-23 21:12:56 +08:00
parent a1f4ebd45a
commit 71f99a55b4
4 changed files with 214 additions and 30 deletions

View File

@@ -827,6 +827,10 @@ int main(int argc, TCHAR* argv[], TCHAR* envp[])
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL, MAKEINTRESOURCE(IDI_ICON_APP))); SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(NULL, MAKEINTRESOURCE(IDI_ICON_APP)));
HMODULE hModule = ::GetModuleHandle(NULL); HMODULE hModule = ::GetModuleHandle(NULL);
#ifdef _DEBUG
Sleep(15000);
#endif
GetDirInfo(); GetDirInfo();
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{ {

View File

@@ -57,6 +57,9 @@
<ClInclude Include="GitVer_version.h"> <ClInclude Include="GitVer_version.h">
<Filter>头文件</Filter> <Filter>头文件</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="GitVer_setup.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="GitVer.cpp"> <ClCompile Include="GitVer.cpp">
@@ -89,6 +92,9 @@
<ClCompile Include="GitVer_version.cpp"> <ClCompile Include="GitVer_version.cpp">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="GitVer_setup.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ResourceCompile Include="GitVer.rc"> <ResourceCompile Include="GitVer.rc">

View File

@@ -4,6 +4,21 @@
#include "gitver_process.h" #include "gitver_process.h"
#include "gitver_version.h" #include "gitver_version.h"
BOOL TryGetGitStatus(LPCTSTR lpRepoPath, CString& strStatus)
{
strStatus = StartProcess(NULL, _T("cmd /c git symbolic-ref --quiet HEAD"), lpRepoPath);
strStatus.Replace(_T("\r"), _T(""));
strStatus.Trim();
if (strStatus.IsEmpty())
{// 游离状态
return FALSE;
}
// 正常状态;
return TRUE;
}
BOOL TryGetCurrentBranch(LPCTSTR lpRepoPath, CString& strBranch) BOOL TryGetCurrentBranch(LPCTSTR lpRepoPath, CString& strBranch)
{ {
_tprintf(_T("获取当前分支名称: \n")); _tprintf(_T("获取当前分支名称: \n"));
@@ -20,6 +35,39 @@ BOOL TryGetCurrentBranch(LPCTSTR lpRepoPath, CString& strBranch)
return TRUE; return TRUE;
} }
BOOL TryGetDetectedBranch(LPCTSTR lpRepoPath, CString& strBranch)
{
// 远程仓库名必须以 origin 开头;
strBranch = StartProcess(NULL, _T("cmd /c git branch -r --contains HEAD | head -n1 | sed 's/.*origin\\///'"), lpRepoPath);
strBranch.Replace(_T("\r"), _T(""));
strBranch.Replace(_T("\n"), _T(""));
strBranch.Trim();
if (strBranch.IsEmpty())
{
// 没有找到远程分支;
return FALSE;
}
return TRUE;
}
BOOL TryGetDetectedTag(LPCTSTR lpRepoPath, CString& strTag)
{
strTag = StartProcess(NULL, _T("cmd /c git tag --points-at HEAD"), lpRepoPath);
strTag.Replace(_T("\r"), _T(""));
strTag.Replace(_T("\n"), _T(""));
strTag.Trim();
if (strTag.IsEmpty())
{
// 没有找到标签;
return FALSE;
}
return TRUE;
}
BOOL TryGetBidFromBranch(const CString& strBranch, UINT& nBid) BOOL TryGetBidFromBranch(const CString& strBranch, UINT& nBid)
{ {
nBid = 0; nBid = 0;
@@ -34,7 +82,7 @@ BOOL TryGetBidFromBranch(const CString& strBranch, UINT& nBid)
return TRUE; return TRUE;
} }
if (strBranch.CompareNoCase(_T("release")) == 0 ) if (strBranch.CompareNoCase(_T("release")) == 0)
{ {
nBid = 1; nBid = 1;
return TRUE; return TRUE;
@@ -221,6 +269,89 @@ BOOL TryGetLastBranchCommitDate(LPCTSTR lpRepoPath, const CString& strBranch, SY
return TRUE; return TRUE;
} }
BOOL TryGetTagCommitDate(LPCTSTR lpRepoPath, const CString& strTag, SYSTEMTIME& stDate)
{
::ZeroMemory(&stDate, sizeof(stDate));
if (strTag.IsEmpty())
{
return FALSE;
}
CString strCmd;
strCmd.Format(_T("cmd /c git log -1 --date=short --format=%%cd \"%s\""), strTag.GetString());
CString strDate = StartProcess(NULL, strCmd.GetBuffer(), lpRepoPath);
strCmd.ReleaseBuffer();
if (strDate.IsEmpty())
{
return FALSE;
}
strDate.Replace(_T("\r"), _T(""));
strDate.Replace(_T("\n"), _T(""));
strDate.Trim();
if (strDate.IsEmpty() || strDate.Left(6).CompareNoCase(_T("fatal:")) == 0)
{
return FALSE;
}
unsigned int nYear = 0;
unsigned int nMonth = 0;
unsigned int nDay = 0;
if (_stscanf_s(strDate.GetString(), _T("%u-%u-%u"), &nYear, &nMonth, &nDay) != 3)
{
return FALSE;
}
stDate.wYear = static_cast<WORD>(nYear);
stDate.wMonth = static_cast<WORD>(nMonth);
stDate.wDay = static_cast<WORD>(nDay);
return TRUE;
}
BOOL ResolveTagFileVersionDateAndCommitCount(LPCTSTR lpRepoPath, const CString& strBranch, const CString& strTag, UINT& nYY, UINT& nMMDD, UINT& nId)
{
nYY = 0;
nMMDD = 0;
nId = 0;
if (strTag.IsEmpty())
{
_tprintf(_T("错误: 标签名称为空\n"));
return FALSE;
}
SYSTEMTIME stTargetDate = { 0 };
if (!TryGetTagCommitDate(lpRepoPath, strTag, stTargetDate))
{
_tprintf(_T("错误: 获取标签 [%s] 对应提交日期失败\n"), strTag.GetString());
return FALSE;
}
UINT nCommitCount = 0;
if (!TryGetBranchCommitCountByDate(lpRepoPath, strBranch, stTargetDate, nCommitCount))
{
_tprintf(_T("错误: 获取 [%s] 在 [%04u-%02u-%02u] 的提交数量失败\n"),
strBranch.GetString(),
stTargetDate.wYear,
stTargetDate.wMonth,
stTargetDate.wDay);
return FALSE;
}
_tprintf(_T("获取标签提交信息成功: %04u-%02u-%02u, 提交数量: %u\n"),
stTargetDate.wYear,
stTargetDate.wMonth,
stTargetDate.wDay,
nCommitCount);
GetFileVersionDateFromSystemTime(stTargetDate, nYY, nMMDD);
nId = nCommitCount;
return TRUE;
}
BOOL ResolveFileVersionDateAndCommitCount(LPCTSTR lpRepoPath, const CString& strBranch, UINT& nYY, UINT& nMMDD, UINT& nId) BOOL ResolveFileVersionDateAndCommitCount(LPCTSTR lpRepoPath, const CString& strBranch, UINT& nYY, UINT& nMMDD, UINT& nId)
{ {
nYY = 0; nYY = 0;
@@ -282,7 +413,42 @@ int BuildVersionsFromRepo(
nDefaultMinor); nDefaultMinor);
UINT nBid = 0; UINT nBid = 0;
CString strStatus;
CString strBranch; CString strBranch;
UINT nMajor = 0, nMinor = 0;
UINT nYY = 0, nMMDD = 0, nId = 0;
if (!TryGetGitStatus(lpRepoPath, strStatus))
{
_tprintf(_T("警告: 当前处于 HEAD 游离状态\n"));
if (!TryGetDetectedBranch(lpRepoPath, strBranch))
{
_tprintf(_T("错误: 获取远程分支失败\n"));
return errorCodes.nStatusErrorCode;
}
CString strTag;
if (!TryGetDetectedTag(lpRepoPath, strTag))
{
_tprintf(_T("错误: 获取远程分支标签失败\n"));
return errorCodes.nStatusErrorCode;
}
// 处于游离状态,使用远程分支和标签来解析版本号;
if (TryParseTagMajorMinor(strBranch, strTag, nMajor, nMinor))
{
_tprintf(_T("游离标签版本: %s (major=%u minor=%u)\n"), strTag.GetString(), nMajor, nMinor);
}
if (!ResolveTagFileVersionDateAndCommitCount(lpRepoPath, strBranch, strTag, nYY, nMMDD, nId))
{
_tprintf(_T("错误: 获取游离标签提交信息失败\n"));
return errorCodes.nCommitErrorCode;
}
_tprintf(_T("游离标签提交信息: yy=%u mmdd=%u id=%u\n"), nYY, nMMDD, nId);
}
else
{
if (!TryGetCurrentBranch(lpRepoPath, strBranch)) if (!TryGetCurrentBranch(lpRepoPath, strBranch))
{ {
_tprintf(_T("错误: 获取当前分支失败\n")); _tprintf(_T("错误: 获取当前分支失败\n"));
@@ -298,8 +464,6 @@ int BuildVersionsFromRepo(
_tprintf(_T("分支 bid: %u\n"), nBid); _tprintf(_T("分支 bid: %u\n"), nBid);
CString strLastTag; CString strLastTag;
UINT nMajor = 0;
UINT nMinor = 0;
if (!GetLastMajorMinorTag(lpRepoPath, strBranch, strLastTag, nMajor, nMinor)) if (!GetLastMajorMinorTag(lpRepoPath, strBranch, strLastTag, nMajor, nMinor))
{ {
nMajor = nDefaultMajor; nMajor = nDefaultMajor;
@@ -311,18 +475,17 @@ int BuildVersionsFromRepo(
_tprintf(_T("最近标签: %s (major=%u minor=%u)\n"), strLastTag.GetString(), nMajor, nMinor); _tprintf(_T("最近标签: %s (major=%u minor=%u)\n"), strLastTag.GetString(), nMajor, nMinor);
} }
UINT nYY = 0;
UINT nMMDD = 0;
UINT nId = 0;
if (!ResolveFileVersionDateAndCommitCount(lpRepoPath, strBranch, nYY, nMMDD, nId)) if (!ResolveFileVersionDateAndCommitCount(lpRepoPath, strBranch, nYY, nMMDD, nId))
{ {
_tprintf(_T("错误: 获取分支提交信息失败\n")); _tprintf(_T("错误: 获取分支提交信息失败\n"));
return errorCodes.nCommitErrorCode; return errorCodes.nCommitErrorCode;
} }
_tprintf(_T("分支提交信息: yy=%u mmdd=%u id=%u\n"), nYY, nMMDD, nId); _tprintf(_T("分支提交信息: yy=%u mmdd=%u id=%u\n"), nYY, nMMDD, nId);
}
strProductVersion.Format(_T("%u.%u.%u.%u"), nPid, nBid, nMajor, nMinor); strProductVersion.Format(_T("%u.%u.%u.%u"), nPid, nBid, nMajor, nMinor);
strFileVersion.Format(_T("%u.%u.%u.%u"), nPid, nYY, nMMDD, nId); strFileVersion.Format(_T("%u.%u.%u.%u"), nPid, nYY, nMMDD, nId);
_tprintf(_T("文件版本信息: ProductVersion=%s FileVersion=%s\n"), strProductVersion.GetString(), strFileVersion.GetString()); _tprintf(_T("文件版本信息: ProductVersion=%s FileVersion=%s\n"), strProductVersion.GetString(), strFileVersion.GetString());
return 0; return 0;
} }

View File

@@ -5,12 +5,23 @@ struct VersionBuildErrorCodes
int nBidErrorCode; int nBidErrorCode;
int nTagErrorCode; int nTagErrorCode;
int nCommitErrorCode; int nCommitErrorCode;
int nStatusErrorCode;
}; };
BOOL TryGetGitStatus(LPCTSTR lpRepoPath, CString& strStatus);
BOOL TryGetCurrentBranch(LPCTSTR lpRepoPath, CString& strBranch); BOOL TryGetCurrentBranch(LPCTSTR lpRepoPath, CString& strBranch);
BOOL TryGetDetectedBranch(LPCTSTR lpRepoPath, CString& strBranch);
BOOL TryGetDetectedTag(LPCTSTR lpRepoPath, CString& strTag);
BOOL TryGetBidFromBranch(const CString& strBranch, UINT& nBid); BOOL TryGetBidFromBranch(const CString& strBranch, UINT& nBid);
BOOL TryParseTagMajorMinor(const CString& strBranch, const CString& strTag, UINT& nMajor, UINT& nMinor); BOOL TryParseTagMajorMinor(const CString& strBranch, const CString& strTag, UINT& nMajor, UINT& nMinor);
BOOL GetLastMajorMinorTag(LPCTSTR lpRepoPath, const CString& strBranch, CString& strTag, UINT& nMajor, UINT& nMinor); BOOL GetLastMajorMinorTag(LPCTSTR lpRepoPath, const CString& strBranch, CString& strTag, UINT& nMajor, UINT& nMinor);
BOOL ResolveTagFileVersionDateAndCommitCount(
LPCTSTR lpRepoPath,
const CString& strBranch,
const CString& strTag,
UINT& nYY,
UINT& nMMDD,
UINT& nId);
int BuildVersionsFromRepo( int BuildVersionsFromRepo(
LPCTSTR lpRepoPath, LPCTSTR lpRepoPath,
UINT nPid, UINT nPid,