Skip to content

Commit cf4cf73

Browse files
committed
refactor(config): move the detached-head ConfigLoader fix to its own PR
Review feedback on #1532: the cached git-source recovery isn't part of the postgres sink. The change and its tests now live on fix/configloader-detached-head unchanged.
1 parent f22f537 commit cf4cf73

2 files changed

Lines changed: 15 additions & 98 deletions

File tree

src/config/ConfigLoader.ts

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -293,24 +293,22 @@ export class ConfigLoader extends EventEmitter {
293293

294294
console.log(`Using repository directory: ${repoDir}`);
295295

296-
const gitEnv = {
297-
// dont wait for credentials; the command should be sufficiently authed
298-
// https://git-scm.com/docs/git#Documentation/git.txt-codeGITTERMINALPROMPTcode
299-
GIT_TERMINAL_PROMPT: 'false',
300-
...process.env,
301-
...(source.auth?.type === 'ssh'
302-
? {
303-
GIT_SSH_COMMAND: `ssh -i ${source.auth.privateKeyPath}`,
304-
}
305-
: {}),
306-
};
307-
308296
// Clone or pull repository
309297
if (!fs.existsSync(repoDir)) {
310298
console.log(`Cloning repository ${source.repository} to ${repoDir}`);
311299
const execOptions = {
312300
cwd: process.cwd(),
313-
env: gitEnv,
301+
env: {
302+
// dont wait for credentials; the command should be sufficiently authed
303+
// https://git-scm.com/docs/git#Documentation/git.txt-codeGITTERMINALPROMPTcode
304+
GIT_TERMINAL_PROMPT: 'false',
305+
...process.env,
306+
...(source.auth?.type === 'ssh'
307+
? {
308+
GIT_SSH_COMMAND: `ssh -i ${source.auth.privateKeyPath}`,
309+
}
310+
: {}),
311+
},
314312
};
315313

316314
try {
@@ -320,20 +318,12 @@ export class ConfigLoader extends EventEmitter {
320318
handleErrorAndThrow(error, 'Failed to clone repository');
321319
}
322320
} else {
323-
console.log(`Fetching latest changes from ${source.repository}`);
321+
console.log(`Pulling latest changes from ${source.repository}`);
324322
try {
325-
if (source.branch) {
326-
await execFileAsync('git', ['fetch', '--all', '--prune'], { cwd: repoDir, env: gitEnv });
327-
console.log('Repository fetched successfully');
328-
} else {
329-
await execFileAsync('git', ['pull'], { cwd: repoDir, env: gitEnv });
330-
console.log('Repository pulled successfully');
331-
}
323+
await execFileAsync('git', ['pull'], { cwd: repoDir });
324+
console.log('Repository pulled successfully');
332325
} catch (error: unknown) {
333-
handleErrorAndThrow(
334-
error,
335-
source.branch ? 'Failed to fetch repository' : 'Failed to pull repository',
336-
);
326+
handleErrorAndThrow(error, 'Failed to pull repository');
337327
}
338328
}
339329

@@ -346,17 +336,6 @@ export class ConfigLoader extends EventEmitter {
346336
} catch (error: unknown) {
347337
handleErrorAndThrow(error, `Failed to checkout branch ${source.branch}`);
348338
}
349-
350-
console.log(`Pulling latest changes from ${source.branch}`);
351-
try {
352-
await execFileAsync('git', ['pull', '--ff-only', 'origin', source.branch], {
353-
cwd: repoDir,
354-
env: gitEnv,
355-
});
356-
console.log('Repository pulled successfully');
357-
} catch (error: unknown) {
358-
handleErrorAndThrow(error, 'Failed to pull repository');
359-
}
360339
}
361340

362341
// Read and parse config file

test/ConfigLoader.test.ts

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import { describe, it, beforeEach, afterEach, afterAll, expect, vi } from 'vitest';
1818
import fs from 'fs';
1919
import path from 'path';
20-
import { execFile } from 'child_process';
21-
import { promisify } from 'util';
2220
import { getConfigFile } from '../src/config/file';
2321
import {
2422
ConfigLoader,
@@ -35,8 +33,6 @@ import {
3533
} from '../src/config/types';
3634
import axios from 'axios';
3735

38-
const execFileAsync = promisify(execFile);
39-
4036
describe('ConfigLoader', () => {
4137
let configLoader: ConfigLoader;
4238
let tempDir: string;
@@ -475,64 +471,6 @@ describe('ConfigLoader', () => {
475471
expect(config).toHaveProperty('cookieSecret');
476472
}, 10000);
477473

478-
it('should recover a cached git repository that is on a detached HEAD', async () => {
479-
const source: GitSource = {
480-
type: 'git',
481-
repository: 'https://example.com/git-proxy-config-test.git',
482-
path: 'proxy.config.json',
483-
branch: 'main',
484-
enabled: true,
485-
};
486-
const remoteDir = path.resolve(tempDir, 'remote.git');
487-
const workDir = path.resolve(tempDir, 'work');
488-
const envPaths = (await import('env-paths')).default;
489-
const paths = envPaths('git-proxy', { suffix: '' });
490-
const repoDirName = Buffer.from(source.repository)
491-
.toString('base64')
492-
.replace(/[^a-zA-Z0-9]/g, '_');
493-
const repoDir = path.join(paths.cache, 'git-config-cache', repoDirName);
494-
495-
if (fs.existsSync(repoDir)) {
496-
fs.rmSync(repoDir, { recursive: true });
497-
}
498-
499-
await execFileAsync('git', ['init', '--bare', remoteDir]);
500-
await execFileAsync('git', ['init', '--initial-branch=main', workDir]);
501-
fs.writeFileSync(
502-
path.join(workDir, source.path),
503-
JSON.stringify({ proxyUrl: 'https://test.com', cookieSecret: 'from-cache' }),
504-
);
505-
await execFileAsync('git', ['add', source.path], { cwd: workDir });
506-
await execFileAsync(
507-
'git',
508-
[
509-
'-c',
510-
'user.email=test@example.com',
511-
'-c',
512-
'user.name=git-proxy test',
513-
'commit',
514-
'-m',
515-
'add config',
516-
],
517-
{ cwd: workDir },
518-
);
519-
await execFileAsync('git', ['remote', 'add', 'origin', remoteDir], { cwd: workDir });
520-
await execFileAsync('git', ['push', '-u', 'origin', 'main'], { cwd: workDir });
521-
await execFileAsync('git', ['clone', remoteDir, repoDir]);
522-
// The clone's default branch follows the host's `init.defaultBranch`, which
523-
// may differ from the pushed `main` (e.g. `master` on CI), leaving local
524-
// HEAD unborn. Detach at the pushed commit by SHA so HEAD is reliably
525-
// detached and the working tree is populated on every git version.
526-
const { stdout: headSha } = await execFileAsync('git', ['rev-parse', 'origin/main'], {
527-
cwd: repoDir,
528-
});
529-
await execFileAsync('git', ['checkout', headSha.trim()], { cwd: repoDir });
530-
531-
const config = await configLoader.loadFromSource(source);
532-
533-
expect(config.cookieSecret).toBe('from-cache');
534-
});
535-
536474
it('should throw error for invalid configuration file path (git)', async () => {
537475
const source: GitSource = {
538476
type: 'git',

0 commit comments

Comments
 (0)