Fix checkout init for SHA-256 repositories

This commit is contained in:
Yashwanth Anantharaju
2026-05-21 14:31:18 -04:00
parent 900f2210b1
commit daa5732ee1
7 changed files with 445 additions and 27 deletions

133
dist/index.js vendored
View File

@@ -252,6 +252,11 @@ class GitAuthHelper {
}
});
}
configureSshCommand() {
return __awaiter(this, void 0, void 0, function* () {
yield this.configureSsh(false);
});
}
configureSubmoduleAuth() {
return __awaiter(this, void 0, void 0, function* () {
// Remove possible previous HTTPS instead of SSH
@@ -313,12 +318,18 @@ class GitAuthHelper {
}
});
}
removeSshCommand() {
return __awaiter(this, void 0, void 0, function* () {
yield this.removeSsh(false);
this.git.removeEnvironmentVariable('GIT_SSH_COMMAND');
});
}
/**
* Configures SSH authentication by writing the SSH key and known hosts,
* and setting up the GIT_SSH_COMMAND environment variable.
*/
configureSsh() {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter(this, arguments, void 0, function* (persistCredentials = this.settings.persistCredentials) {
if (!this.settings.sshKey) {
return;
}
@@ -368,7 +379,7 @@ class GitAuthHelper {
core.info(`Temporarily overriding GIT_SSH_COMMAND=${this.sshCommand}`);
this.git.setEnvironmentVariable('GIT_SSH_COMMAND', this.sshCommand);
// Configure core.sshCommand
if (this.settings.persistCredentials) {
if (persistCredentials) {
yield this.git.config(SSH_COMMAND_KEY, this.sshCommand);
}
});
@@ -454,7 +465,7 @@ class GitAuthHelper {
* known hosts files, and SSH command configurations.
*/
removeSsh() {
return __awaiter(this, void 0, void 0, function* () {
return __awaiter(this, arguments, void 0, function* (removeGitConfig = true) {
var _a, _b;
// SSH key
const keyPath = this.sshKeyPath || stateHelper.SshKeyPath;
@@ -480,10 +491,12 @@ class GitAuthHelper {
core.warning(`Failed to remove SSH known hosts '${knownHostsPath}'`);
}
}
// SSH command
core.info('Removing SSH command configuration');
yield this.removeGitConfig(SSH_COMMAND_KEY);
yield this.removeSubmoduleGitConfig(SSH_COMMAND_KEY);
if (removeGitConfig) {
// SSH command
core.info('Removing SSH command configuration');
yield this.removeGitConfig(SSH_COMMAND_KEY);
yield this.removeSubmoduleGitConfig(SSH_COMMAND_KEY);
}
});
}
/**
@@ -896,9 +909,14 @@ class GitCommandManager {
getWorkingDirectory() {
return this.workingDirectory;
}
init() {
init(objectFormat) {
return __awaiter(this, void 0, void 0, function* () {
yield this.execGit(['init', this.workingDirectory]);
const args = ['init'];
if (objectFormat === 'sha256') {
args.push('--object-format=sha256');
}
args.push(this.workingDirectory);
yield this.execGit(args);
});
}
isDetached() {
@@ -1056,6 +1074,52 @@ class GitCommandManager {
return stdout;
});
}
tryGetObjectFormat(repositoryUrl) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
let stderr = '';
const listeners = {
stderr: (data) => {
stderr += data.toString();
},
errline: (data) => {
stderr += data.toString();
}
};
const existingTracePacket = this.gitEnv['GIT_TRACE_PACKET'];
this.gitEnv['GIT_TRACE_PACKET'] = '1';
try {
const output = yield this.execGit([
'-c',
'protocol.version=2',
'ls-remote',
'--quiet',
'--exit-code',
'--symref',
repositoryUrl,
'HEAD'
], true, true, listeners);
if (output.exitCode !== 0) {
core.debug(`Unable to determine repository object format: git ls-remote exited with ${output.exitCode}`);
return { format: '', succeeded: false };
}
}
catch (err) {
core.debug(`Unable to determine repository object format: ${(_a = err === null || err === void 0 ? void 0 : err.message) !== null && _a !== void 0 ? _a : err}`);
return { format: '', succeeded: false };
}
finally {
if (existingTracePacket === undefined) {
delete this.gitEnv['GIT_TRACE_PACKET'];
}
else {
this.gitEnv['GIT_TRACE_PACKET'] = existingTracePacket;
}
}
const match = stderr.match(/object-format=(sha1|sha256)(?=\s|$)/);
return { format: match ? match[1] : '', succeeded: true };
});
}
tryGetConfigValues(configKey, globalConfig, configFile) {
return __awaiter(this, void 0, void 0, function* () {
const args = ['config'];
@@ -1449,6 +1513,10 @@ function getSource(settings) {
const git = yield getGitCommandManager(settings);
core.endGroup();
let authHelper = null;
let didStartConfigureAuth = false;
let didConfigureAuth = false;
let didConfigureSshCommand = false;
let didConfigureGlobalAuth = false;
try {
if (git) {
authHelper = gitAuthHelper.createAuthHelper(git, settings);
@@ -1465,6 +1533,20 @@ function getSource(settings) {
stateHelper.setSafeDirectory();
}
}
const configureGlobalAuth = () => __awaiter(this, void 0, void 0, function* () {
if (!authHelper || didConfigureGlobalAuth) {
return;
}
yield authHelper.configureGlobalAuth();
didConfigureGlobalAuth = true;
});
const configureSshCommand = () => __awaiter(this, void 0, void 0, function* () {
if (!authHelper || didConfigureSshCommand) {
return;
}
yield authHelper.configureSshCommand();
didConfigureSshCommand = true;
});
// Prepare existing directory, otherwise recreate
if (isExisting) {
yield gitDirectoryHelper.prepareExistingDirectory(git, settings.repositoryPath, repositoryUrl, settings.clean, settings.ref);
@@ -1486,8 +1568,27 @@ function getSource(settings) {
stateHelper.setRepositoryPath(settings.repositoryPath);
// Initialize the repository
if (!fsHelper.directoryExistsSync(path.join(settings.repositoryPath, '.git'))) {
core.startGroup('Determining repository object format');
let objectFormatResult = yield git.tryGetObjectFormat(repositoryUrl);
if (!objectFormatResult.succeeded) {
if (settings.sshKey) {
yield configureSshCommand();
}
else {
yield configureGlobalAuth();
}
objectFormatResult = yield git.tryGetObjectFormat(repositoryUrl);
}
if (!objectFormatResult.succeeded) {
throw new Error('Unable to determine repository object format');
}
const objectFormat = objectFormatResult.format;
if (objectFormat === 'sha256') {
core.info('Detected SHA-256 repository object format');
}
core.endGroup();
core.startGroup('Initializing the repository');
yield git.init();
yield git.init(objectFormat);
yield git.remoteAdd('origin', repositoryUrl);
core.endGroup();
}
@@ -1503,7 +1604,9 @@ function getSource(settings) {
}
// Configure auth
core.startGroup('Setting up auth');
didStartConfigureAuth = true;
yield authHelper.configureAuth();
didConfigureAuth = true;
core.endGroup();
// Determine the default branch
if (!settings.ref && !settings.commit) {
@@ -1599,7 +1702,7 @@ function getSource(settings) {
if (settings.submodules) {
// Temporarily override global config
core.startGroup('Setting up auth for fetching submodules');
yield authHelper.configureGlobalAuth();
yield configureGlobalAuth();
core.endGroup();
// Checkout submodules
core.startGroup('Fetching submodules');
@@ -1625,12 +1728,16 @@ function getSource(settings) {
finally {
// Remove auth
if (authHelper) {
if (!settings.persistCredentials) {
if (!settings.persistCredentials ||
(didStartConfigureAuth && !didConfigureAuth)) {
core.startGroup('Removing auth');
yield authHelper.removeAuth();
core.endGroup();
}
authHelper.removeGlobalConfig();
else if (didConfigureSshCommand && !didConfigureAuth) {
yield authHelper.removeSshCommand();
}
yield authHelper.removeGlobalConfig();
}
}
});