agentHost: Avoid warning for missing remote HEAD

Probe the optional origin/HEAD symbolic ref in quiet mode so repositories without it do not produce recurring agent host warnings. Add an integration test covering the expected missing-ref case.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Christof Marti
2026-08-19 09:48:00 +02:00
co-authored by Copilot
parent d11fd428e3
commit e4960d8bf9
2 changed files with 25 additions and 4 deletions
@@ -59,7 +59,7 @@ export class AgentHostGitService implements IAgentHostGitService {
async getDefaultBranch(workingDirectory: URI): Promise<IDefaultBranch | undefined> {
// Try to read the default branch from the remote HEAD reference
const remoteRef = (await this._runGit(workingDirectory, ['symbolic-ref', 'refs/remotes/origin/HEAD']))?.trim();
const remoteRef = (await this._runGit(workingDirectory, ['symbolic-ref', '--quiet', 'refs/remotes/origin/HEAD']))?.trim();
if (remoteRef) {
if (!remoteRef.startsWith('refs/remotes/origin/')) {
return { name: remoteRef, startPoint: remoteRef };
@@ -29,8 +29,15 @@ import { DiskFileSystemProvider } from '../../../files/node/diskFileSystemProvid
import { DisposableStore } from '../../../../base/common/lifecycle.js';
import { AgentHostGitService } from '../../node/agentHostGitService.js';
function createGitService(disposables: Pick<DisposableStore, 'add'>): AgentHostGitService {
const logService = new NullLogService();
class TestLogService extends NullLogService {
readonly warnings: string[] = [];
override warn(message: string): void {
this.warnings.push(message);
}
}
function createGitService(disposables: Pick<DisposableStore, 'add'>, logService: NullLogService = new NullLogService()): AgentHostGitService {
const fileService = disposables.add(new FileService(logService));
disposables.add(fileService.registerProvider(Schemas.file, disposables.add(new DiskFileSystemProvider(logService))));
const env: Partial<INativeEnvironmentService> = { tmpDir: URI.file(tmpdir()) };
@@ -54,10 +61,12 @@ suite('AgentHostGitService - getSessionGitState (real git)', () => {
let tmpRoot: string | undefined;
let svc: AgentHostGitService | undefined;
let logService: TestLogService;
setup(() => {
tmpRoot = undefined;
svc = createGitService(disposables);
logService = new TestLogService();
svc = createGitService(disposables, logService);
});
teardown(() => {
@@ -150,6 +159,18 @@ suite('AgentHostGitService - getSessionGitState (real git)', () => {
});
});
(hasGit ? test : test.skip)('does not warn when the default remote-tracking ref is missing', async () => {
const dir = initRepo();
assert.deepStrictEqual({
defaultBranch: await svc!.getDefaultBranch(URI.file(dir)),
warnings: logService.warnings,
}, {
defaultBranch: undefined,
warnings: [],
});
});
(hasGit ? test : test.skip)('falls back to the local branch when the default remote-tracking ref is missing', async () => {
const dir = initRepo();
cp.execFileSync('git', ['symbolic-ref', 'refs/remotes/origin/HEAD', 'refs/remotes/origin/main'], { cwd: dir, stdio: 'pipe' });