diff --git a/dist/index.js b/dist/index.js index fff6634..34fcf64 100644 --- a/dist/index.js +++ b/dist/index.js @@ -34361,11 +34361,15 @@ function revisionOfLastVersionBump({ gitRepo, versionFilePath, productVersionMac // Get the blame information for the version file const blameInfo = yield git.raw(['blame', versionFilePath]); // Use regex to find the line containing the PRODUCT_VERSION macro - const versionRegex = new RegExp(`^([a-f0-9]+).+${productVersionMacroName}.*\\(\\d+\\)`, 'm'); + const versionRegex = new RegExp(`^(\\^?[a-f0-9]+).+${productVersionMacroName}.*\\(\\d+\\)`, 'm'); const match = versionRegex.exec(blameInfo); if (match) { const commitHash = match[1]; - return commitHash; + // If the commit hash starts with '^', it represents an initial commit + // In this case, we remove the '^' before returning the hash + // Otherwise, we return the hash as is + // In both cases, we only return the first 7 characters of the hash + return commitHash.startsWith('^') ? commitHash.substring(1, 8) : commitHash.substring(0, 7); } else { throw new Error(`Could not find the ${productVersionMacroName} line in the blame information.`); @@ -34430,7 +34434,7 @@ function mostRecentRevisionInFolder({ gitRepo, folderPath }) { if (!log.latest) { throw new Error('No latest revision found'); } - return log.latest.hash.substring(0, 8); + return log.latest.hash.substring(0, 7); } catch (error) { throw new Error(`Error getting the latest Git revision for folder "${folderPath}": ${error}`); diff --git a/src/git.test.ts b/src/git.test.ts index fd959cb..8af43ed 100644 --- a/src/git.test.ts +++ b/src/git.test.ts @@ -42,7 +42,8 @@ describe('revisionOfLastVersionBump', () => { productVersionMacroName: productVersionMacroName }); - expect(result).toEqual(commitHash); + expect(result).toHaveLength(7); + expect(result).toEqual(commitHash.substring(0, 7)); expect(gitMock).toHaveBeenCalledWith(gitRepo); expect(rawMock).toHaveBeenCalledWith(['blame', versionFilePath]); }); @@ -62,6 +63,41 @@ describe('revisionOfLastVersionBump', () => { `Could not find the ${productVersionMacroName} line in the blame information.` ); }); + + test('should handle the first commit of a file', async () => { + const gitRepo = '/path/to/repo'; + const versionFilePath = '/path/to/version/file/application.cpp'; + const productVersionMacroName = 'PRODUCT_VERSION'; + + // Git blame will return a commit hash starting with ^ for the first commit of a file + rawMock.mockResolvedValue(createBlameInfoMock('^1234abc', productVersionMacroName)); + + const result = await revisionOfLastVersionBump({ + gitRepo, + versionFilePath, + productVersionMacroName + }); + + expect(result).toHaveLength(7); + expect(result).toEqual('1234abc'); + }); + + test('should return the first 7 characters of the commit hash', async () => { + const commitHash = 'a1b2c3d4e5f6'; + const gitRepo = '/path/to/repo'; + const versionFilePath = '/path/to/version/file/application.cpp'; + const productVersionMacroName = 'PRODUCT_VERSION'; + + rawMock.mockResolvedValue(createBlameInfoMock(commitHash, productVersionMacroName)); + + const result = await revisionOfLastVersionBump({ + gitRepo, + versionFilePath, + productVersionMacroName + }); + + expect(result).toEqual(commitHash.substring(0, 7)); + }); }); describe('currentFirmwareVersion', () => { @@ -277,7 +313,8 @@ describe('mostRecentRevisionInFolder', () => { const result = await mostRecentRevisionInFolder({ gitRepo: gitRepo, folderPath: folderPath }); expect(logMock).toHaveBeenCalledWith({ file: folderPath }); - expect(result).toBe(latestHash.substring(0, 8)); + expect(result).toHaveLength(7); + expect(result).toBe(latestHash.substring(0, 7)); }); test('should throw an error if no latest revision is found', async () => { @@ -304,6 +341,20 @@ describe('mostRecentRevisionInFolder', () => { ); }); + test('should return the first 7 characters of the commit hash', async () => { + const gitRepo = '/path/to/repo'; + const folderPath = '/path/to/repo/some/folder'; + const latestHash = '123456789abcdef'; + // Set up the mock to return the latest Git revision + logMock.mockResolvedValue({ + latest: { + hash: latestHash + } + }); + const result = await mostRecentRevisionInFolder({ gitRepo: gitRepo, folderPath: folderPath }); + expect(result).toBe(latestHash.substring(0, 7)); + }); + }); describe('hasFullHistory', () => { const gitRepo = '.'; // Use the current directory as the git repository for testing diff --git a/src/git.ts b/src/git.ts index d35a3e3..4e6bd8b 100644 --- a/src/git.ts +++ b/src/git.ts @@ -60,12 +60,16 @@ export async function revisionOfLastVersionBump( const blameInfo = await git.raw(['blame', versionFilePath]); // Use regex to find the line containing the PRODUCT_VERSION macro - const versionRegex = new RegExp(`^([a-f0-9]+).+${productVersionMacroName}.*\\(\\d+\\)`, 'm'); + const versionRegex = new RegExp(`^(\\^?[a-f0-9]+).+${productVersionMacroName}.*\\(\\d+\\)`, 'm'); const match = versionRegex.exec(blameInfo); - if (match) { const commitHash = match[1]; - return commitHash; + + // If the commit hash starts with '^', it represents an initial commit + // In this case, we remove the '^' before returning the hash + // Otherwise, we return the hash as is + // In both cases, we only return the first 7 characters of the hash + return commitHash.startsWith('^') ? commitHash.substring(1, 8) : commitHash.substring(0, 7); } else { throw new Error(`Could not find the ${productVersionMacroName} line in the blame information.`); } @@ -133,7 +137,7 @@ export async function mostRecentRevisionInFolder( if (!log.latest) { throw new Error('No latest revision found'); } - return log.latest.hash.substring(0, 8); + return log.latest.hash.substring(0, 7); } catch (error) { throw new Error(`Error getting the latest Git revision for folder "${folderPath}": ${error}`); }