From e90f37966ffda4acbebc5858e7165ad39a9715d3 Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 24 Apr 2026 16:05:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=EF=BC=9A=E5=BD=93=E5=BD=93?= =?UTF-8?q?=E5=A4=A9=E6=8F=90=E4=BA=A4=E6=95=B0=E4=B8=BA0=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E4=BB=A5=E6=9C=80=E5=90=8E=E4=B8=80=E6=AC=A1=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E6=97=A5=E6=9C=9F=E9=87=8D=E6=96=B0=E7=94=9F=E6=88=90?= =?UTF-8?q?mmdd.id=E5=80=BC=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GitVer/GitVer_version.cpp | 116 ++++++++++++++++++++++++++++++++------ GitVer/GitVer_version.h | 2 - 2 files changed, 98 insertions(+), 20 deletions(-) diff --git a/GitVer/GitVer_version.cpp b/GitVer/GitVer_version.cpp index 1351d75..8f6bc09 100644 --- a/GitVer/GitVer_version.cpp +++ b/GitVer/GitVer_version.cpp @@ -122,16 +122,14 @@ BOOL GetLastMajorMinorTag(LPCTSTR lpRepoPath, const CString& strBranch, CString& return !strTag.IsEmpty(); } -void GetTodayFileVersionDate(UINT& nYY, UINT& nMMDD) +void GetFileVersionDateFromSystemTime(const SYSTEMTIME& stLocal, UINT& nYY, UINT& nMMDD) { - SYSTEMTIME stLocal = { 0 }; - ::GetLocalTime(&stLocal); - nYY = stLocal.wYear % 100; + // 为了让版本号对齐4位,月份从1-12映射到11-22; nMMDD = (stLocal.wMonth + 10) * 100 + stLocal.wDay; } -BOOL GetTodayBranchCommitCount(LPCTSTR lpRepoPath, const CString& strBranch, UINT& nId) +BOOL TryGetBranchCommitCountByDate(LPCTSTR lpRepoPath, const CString& strBranch, const SYSTEMTIME& stDate, UINT& nId) { nId = 0; if (strBranch.IsEmpty()) @@ -139,11 +137,8 @@ BOOL GetTodayBranchCommitCount(LPCTSTR lpRepoPath, const CString& strBranch, UIN return FALSE; } - SYSTEMTIME stLocal = { 0 }; - ::GetLocalTime(&stLocal); - CString strDate; - strDate.Format(_T("%04u-%02u-%02u"), stLocal.wYear, stLocal.wMonth, stLocal.wDay); + strDate.Format(_T("%04u-%02u-%02u"), stDate.wYear, stDate.wMonth, stDate.wDay); CString strCmd; strCmd.Format( @@ -172,6 +167,93 @@ BOOL GetTodayBranchCommitCount(LPCTSTR lpRepoPath, const CString& strBranch, UIN return TryParseUInt16(strCount, nId); } +BOOL TryGetLastBranchCommitDate(LPCTSTR lpRepoPath, const CString& strBranch, SYSTEMTIME& stDate) +{ + ::ZeroMemory(&stDate, sizeof(stDate)); + if (strBranch.IsEmpty()) + { + return FALSE; + } + + CString strCmd; + strCmd.Format(_T("cmd /c git log -1 --date=short --format=%%cd \"%s\""), strBranch.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(nYear); + stDate.wMonth = static_cast(nMonth); + stDate.wDay = static_cast(nDay); + return TRUE; +} + +BOOL ResolveFileVersionDateAndCommitCount(LPCTSTR lpRepoPath, const CString& strBranch, UINT& nYY, UINT& nMMDD, UINT& nId) +{ + nYY = 0; + nMMDD = 0; + nId = 0; + + SYSTEMTIME stTargetDate = { 0 }; + ::GetLocalTime(&stTargetDate); + + UINT nTodayCommitCount = 0; + if (!TryGetBranchCommitCountByDate(lpRepoPath, strBranch, stTargetDate, nTodayCommitCount)) + { + _tprintf(_T("错误: 获取当天提交数量失败\n")); + return FALSE; + } + + if (nTodayCommitCount == 0) + { + _tprintf(_T("当天提交数量为0,将获取最后一次提交日期\n")); + + if (!TryGetLastBranchCommitDate(lpRepoPath, strBranch, stTargetDate)) + { + _tprintf(_T("错误: 获取最后一次提交日期失败\n")); + return FALSE; + } + + if (!TryGetBranchCommitCountByDate(lpRepoPath, strBranch, stTargetDate, nTodayCommitCount)) + { + _tprintf(_T("错误: 获取[%s]提交数量失败\n"), strBranch.GetString()); + return FALSE; + } + + _tprintf(_T("获取到最后一次提交日期: %04u-%02u-%02u, 提交数量: %u\n"), + stTargetDate.wYear, stTargetDate.wMonth, stTargetDate.wDay, nTodayCommitCount); + } + else + { + _tprintf(_T("获取到当天提交数量: %u\n"), nTodayCommitCount); + } + + GetFileVersionDateFromSystemTime(stTargetDate, nYY, nMMDD); + nId = nTodayCommitCount; + return TRUE; +} + int BuildVersionsFromRepo( LPCTSTR lpRepoPath, UINT nPid, @@ -200,7 +282,7 @@ int BuildVersionsFromRepo( if (!TryGetBidFromBranch(strBranch, nBid)) { - _tprintf(_T("错误: 从分支获取 bid 失败。\n")); + _tprintf(_T("错误: 获取分支 bid 失败\n")); return errorCodes.nBidErrorCode; } _tprintf(_T("分支 bid: %u\n"), nBid); @@ -212,7 +294,7 @@ int BuildVersionsFromRepo( { if (!bUseDefaultTagWhenMissing) { - _tprintf(_T("错误: 获取标签失败。\n")); + _tprintf(_T("错误: 获取标签失败\n")); return errorCodes.nTagErrorCode; } @@ -222,23 +304,21 @@ int BuildVersionsFromRepo( } else { - _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; - GetTodayFileVersionDate(nYY, nMMDD); - UINT nId = 0; - if (!GetTodayBranchCommitCount(lpRepoPath, strBranch, nId)) + if (!ResolveFileVersionDateAndCommitCount(lpRepoPath, strBranch, nYY, nMMDD, nId)) { - _tprintf(_T("错误: 获取今日分支提交计数失败。\n")); + _tprintf(_T("错误: 获取分支提交信息失败\n")); return errorCodes.nCommitErrorCode; } - _tprintf(_T("今日提交计数: %u\n"), nId); + _tprintf(_T("分支提交信息: yy=%u mmdd=%u id=%u\n"), nYY, nMMDD, nId); strProductVersion.Format(_T("%u.%u.%u.%u"), nPid, nBid, nMajor, nMinor); 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; } \ No newline at end of file diff --git a/GitVer/GitVer_version.h b/GitVer/GitVer_version.h index d6a2ed0..e3f9045 100644 --- a/GitVer/GitVer_version.h +++ b/GitVer/GitVer_version.h @@ -11,8 +11,6 @@ BOOL TryGetCurrentBranch(LPCTSTR lpRepoPath, CString& strBranch); BOOL TryGetBidFromBranch(const CString& strBranch, UINT& nBid); 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); -void GetTodayFileVersionDate(UINT& nYY, UINT& nMMDD); -BOOL GetTodayBranchCommitCount(LPCTSTR lpRepoPath, const CString& strBranch, UINT& nId); int BuildVersionsFromRepo( LPCTSTR lpRepoPath, UINT nPid,