From 71f99a55b45ae9aa9debba3f81facd575cacfea7 Mon Sep 17 00:00:00 2001
From: Jeff <632006142@qq.com>
Date: Tue, 23 Jun 2026 21:12:56 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9gitlab=20runner?=
=?UTF-8?q?=E6=89=A7=E8=A1=8Cci=E6=97=B6=E6=B8=B8=E7=A6=BB=E5=88=86?=
=?UTF-8?q?=E6=94=AF=E7=9A=84=E6=94=AF=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
GitVer/GitVer.cpp | 4 +
GitVer/GitVer.vcxproj.filters | 6 +
GitVer/GitVer_version.cpp | 223 +++++++++++++++++++++++++++++-----
GitVer/GitVer_version.h | 11 ++
4 files changed, 214 insertions(+), 30 deletions(-)
diff --git a/GitVer/GitVer.cpp b/GitVer/GitVer.cpp
index 2c55ae1..f76b470 100644
--- a/GitVer/GitVer.cpp
+++ b/GitVer/GitVer.cpp
@@ -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)));
HMODULE hModule = ::GetModuleHandle(NULL);
+#ifdef _DEBUG
+ Sleep(15000);
+#endif
+
GetDirInfo();
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
diff --git a/GitVer/GitVer.vcxproj.filters b/GitVer/GitVer.vcxproj.filters
index ca3691f..01f373b 100644
--- a/GitVer/GitVer.vcxproj.filters
+++ b/GitVer/GitVer.vcxproj.filters
@@ -57,6 +57,9 @@
头文件
+
+ 头文件
+
@@ -89,6 +92,9 @@
源文件
+
+ 源文件
+
diff --git a/GitVer/GitVer_version.cpp b/GitVer/GitVer_version.cpp
index 136f6eb..111906e 100644
--- a/GitVer/GitVer_version.cpp
+++ b/GitVer/GitVer_version.cpp
@@ -4,6 +4,21 @@
#include "gitver_process.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)
{
_tprintf(_T("ȡǰ֧: \n"));
@@ -20,6 +35,39 @@ BOOL TryGetCurrentBranch(LPCTSTR lpRepoPath, CString& strBranch)
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)
{
nBid = 0;
@@ -34,7 +82,7 @@ BOOL TryGetBidFromBranch(const CString& strBranch, UINT& nBid)
return TRUE;
}
- if (strBranch.CompareNoCase(_T("release")) == 0 )
+ if (strBranch.CompareNoCase(_T("release")) == 0)
{
nBid = 1;
return TRUE;
@@ -221,6 +269,89 @@ BOOL TryGetLastBranchCommitDate(LPCTSTR lpRepoPath, const CString& strBranch, SY
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(nYear);
+ stDate.wMonth = static_cast(nMonth);
+ stDate.wDay = static_cast(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)
{
nYY = 0;
@@ -282,47 +413,79 @@ int BuildVersionsFromRepo(
nDefaultMinor);
UINT nBid = 0;
+ CString strStatus;
CString strBranch;
- if (!TryGetCurrentBranch(lpRepoPath, strBranch))
+ UINT nMajor = 0, nMinor = 0;
+ UINT nYY = 0, nMMDD = 0, nId = 0;
+ if (!TryGetGitStatus(lpRepoPath, strStatus))
{
- _tprintf(_T(": ȡǰ֧ʧ\n"));
- return errorCodes.nBidErrorCode;
- }
- _tprintf(_T("ǰ֧: %s\n"), strBranch.GetString());
+ _tprintf(_T(": ǰ HEAD ״̬\n"));
+ if (!TryGetDetectedBranch(lpRepoPath, strBranch))
+ {
+ _tprintf(_T(": ȡԶ̷֧ʧ\n"));
+ return errorCodes.nStatusErrorCode;
+ }
- if (!TryGetBidFromBranch(strBranch, nBid))
- {
- _tprintf(_T(": ȡ֧ bid ʧ\n"));
- return errorCodes.nBidErrorCode;
- }
- _tprintf(_T("֧ bid: %u\n"), nBid);
+ CString strTag;
+ if (!TryGetDetectedTag(lpRepoPath, strTag))
+ {
+ _tprintf(_T(": ȡԶ̷֧ǩʧ\n"));
+ return errorCodes.nStatusErrorCode;
+ }
- CString strLastTag;
- UINT nMajor = 0;
- UINT nMinor = 0;
- if (!GetLastMajorMinorTag(lpRepoPath, strBranch, strLastTag, nMajor, nMinor))
- {
- nMajor = nDefaultMajor;
- nMinor = nDefaultMinor;
- _tprintf(_T("ʹĬϱǩ %u %u\n"), nMajor, nMinor);
+ // ״̬ʹԶ̷֧ͱǩ汾;
+ 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
{
- _tprintf(_T("ǩ: %s (major=%u minor=%u)\n"), strLastTag.GetString(), nMajor, nMinor);
- }
+ if (!TryGetCurrentBranch(lpRepoPath, strBranch))
+ {
+ _tprintf(_T(": ȡǰ֧ʧ\n"));
+ return errorCodes.nBidErrorCode;
+ }
+ _tprintf(_T("ǰ֧: %s\n"), strBranch.GetString());
- UINT nYY = 0;
- UINT nMMDD = 0;
- UINT nId = 0;
- if (!ResolveFileVersionDateAndCommitCount(lpRepoPath, strBranch, nYY, nMMDD, nId))
- {
- _tprintf(_T(": ȡ֧ύϢʧ\n"));
- return errorCodes.nCommitErrorCode;
+ if (!TryGetBidFromBranch(strBranch, nBid))
+ {
+ _tprintf(_T(": ȡ֧ bid ʧ\n"));
+ return errorCodes.nBidErrorCode;
+ }
+ _tprintf(_T("֧ bid: %u\n"), nBid);
+
+ CString strLastTag;
+ if (!GetLastMajorMinorTag(lpRepoPath, strBranch, strLastTag, nMajor, nMinor))
+ {
+ nMajor = nDefaultMajor;
+ nMinor = nDefaultMinor;
+ _tprintf(_T("ʹĬϱǩ %u %u\n"), nMajor, nMinor);
+ }
+ else
+ {
+ _tprintf(_T("ǩ: %s (major=%u minor=%u)\n"), strLastTag.GetString(), nMajor, nMinor);
+ }
+
+ if (!ResolveFileVersionDateAndCommitCount(lpRepoPath, strBranch, nYY, nMMDD, nId))
+ {
+ _tprintf(_T(": ȡ֧ύϢʧ\n"));
+ 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);
strFileVersion.Format(_T("%u.%u.%u.%u"), nPid, nYY, nMMDD, nId);
_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 291dbd7..58bea86 100644
--- a/GitVer/GitVer_version.h
+++ b/GitVer/GitVer_version.h
@@ -5,12 +5,23 @@ struct VersionBuildErrorCodes
int nBidErrorCode;
int nTagErrorCode;
int nCommitErrorCode;
+ int nStatusErrorCode;
};
+BOOL TryGetGitStatus(LPCTSTR lpRepoPath, CString& strStatus);
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 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 ResolveTagFileVersionDateAndCommitCount(
+ LPCTSTR lpRepoPath,
+ const CString& strBranch,
+ const CString& strTag,
+ UINT& nYY,
+ UINT& nMMDD,
+ UINT& nId);
int BuildVersionsFromRepo(
LPCTSTR lpRepoPath,
UINT nPid,