|
|
|
|
@@ -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<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)
|
|
|
|
|
{
|
|
|
|
|
nYY = 0;
|
|
|
|
|
@@ -282,7 +413,42 @@ int BuildVersionsFromRepo(
|
|
|
|
|
nDefaultMinor);
|
|
|
|
|
|
|
|
|
|
UINT nBid = 0;
|
|
|
|
|
CString strStatus;
|
|
|
|
|
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))
|
|
|
|
|
{
|
|
|
|
|
_tprintf(_T("错误: 获取当前分支失败\n"));
|
|
|
|
|
@@ -298,8 +464,6 @@ int BuildVersionsFromRepo(
|
|
|
|
|
_tprintf(_T("分支 bid: %u\n"), nBid);
|
|
|
|
|
|
|
|
|
|
CString strLastTag;
|
|
|
|
|
UINT nMajor = 0;
|
|
|
|
|
UINT nMinor = 0;
|
|
|
|
|
if (!GetLastMajorMinorTag(lpRepoPath, strBranch, strLastTag, nMajor, nMinor))
|
|
|
|
|
{
|
|
|
|
|
nMajor = nDefaultMajor;
|
|
|
|
|
@@ -311,18 +475,17 @@ int BuildVersionsFromRepo(
|
|
|
|
|
_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))
|
|
|
|
|
{
|
|
|
|
|
_tprintf(_T("错误: 获取分支提交信息失败\n"));
|
|
|
|
|
return errorCodes.nCommitErrorCode;
|
|
|
|
|
}
|
|
|
|
|
_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;
|
|
|
|
|
}
|