Skip to content

Commit deafd13

Browse files
BetoHerbasisraelantezana
authored andcommitted
fix commitId tracking logic to ensure correct SHA after amend
1 parent 1a2e5e5 commit deafd13

1 file changed

Lines changed: 24 additions & 18 deletions

File tree

script/commitScript.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,44 @@ import fs from 'fs';
44
import path from 'path';
55
import { getLastTestId } from './id_execution_tests_commit.js';
66

7-
const getLatestCommitId = () => {
8-
const latestCommit = execSync('git rev-parse HEAD').toString().trim();
9-
return latestCommit;
10-
};
11-
127
const getLatestCommitName = () => {
138
const commitName = execSync('git log -1 --pretty=%B').toString().trim();
149
return commitName;
1510
};
1611

17-
const updateJsonFile = (commitId, commitName) => {
12+
const updateJsonFile = () => {
1813
const __filename = fileURLToPath(import.meta.url);
1914
const __dirname = path.dirname(__filename);
2015
const filePath = path.join(__dirname, 'tdd_log.json');
2116
const commitTimestamp = Date.now();
17+
const commitName = getLatestCommitName();
2218
let testId = getLastTestId(filePath);
23-
const data = { commitId, commitName, commitTimestamp, testId };
19+
20+
let existingData = [];
2421
try {
2522
const fileData = fs.readFileSync(filePath, 'utf8');
26-
const existingData = JSON.parse(fileData);
27-
existingData.push(data);
28-
fs.writeFileSync(filePath, JSON.stringify(existingData, null, 2));
23+
existingData = JSON.parse(fileData);
2924
} catch (err) {
30-
if (err.code === 'ENOENT') {
31-
fs.writeFileSync(filePath, JSON.stringify([data], null, 2));
32-
} else {
33-
console.error('Error updating JSON file:', err);
25+
if (err.code !== 'ENOENT') {
26+
console.error('Error reading JSON file:', err);
27+
return;
3428
}
3529
}
36-
};
3730

38-
const latestCommitId = getLatestCommitId();
39-
const latestCommitName = getLatestCommitName();
31+
const headIndex = existingData.findIndex(entry => entry.commitId === 'HEAD');
32+
if (headIndex > -1) {
33+
try {
34+
const realSha = execSync('git rev-parse HEAD~1').toString().trim();
35+
existingData[headIndex].commitId = realSha;
36+
} catch (error) {
37+
console.error('Could not get previous commit SHA:', error);
38+
}
39+
}
40+
41+
const data = { commitId: 'HEAD', commitName, commitTimestamp, testId };
42+
existingData.push(data);
43+
44+
fs.writeFileSync(filePath, JSON.stringify(existingData, null, 2));
45+
};
4046

41-
updateJsonFile(latestCommitId, latestCommitName);
47+
updateJsonFile();

0 commit comments

Comments
 (0)