Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/handlers/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,13 @@ function tryAutoInitRepo(repoAbs, log) {
} else {
mkdirSync(repoAbs, { recursive: true });
}
const result = spawnSync('git', ['init', repoAbs], {
// Pin the initial branch to `main` regardless of the server's
// `init.defaultBranch` config. `receive.denyCurrentBranch
// updateInstead` only extracts the working tree when the push
// targets the branch HEAD points at, so a deterministic default
// keeps `git push pod HEAD:main` working on every deployment. See
// #471.
const result = spawnSync('git', ['init', '-b', 'main', repoAbs], {
stdio: ['ignore', 'pipe', 'pipe']
});
if (result.status !== 0) {
Expand Down
17 changes: 16 additions & 1 deletion test/git-auto-init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { createServer } from '../src/server.js';
import { createServer as createNetServer } from 'net';
import fs from 'fs-extra';
import path from 'path';
import { existsSync, statSync, writeFileSync } from 'fs';
import { existsSync, statSync, writeFileSync, readFileSync } from 'fs';

const TEST_HOST = 'localhost';
const DATA_DIR = './test-data-git-auto-init';
Expand Down Expand Up @@ -77,6 +77,21 @@ describe('Git auto-init on first push', () => {
assert.ok(existsSync(path.join(dotGit, 'refs')), '.git/refs must exist');
});

it('pins the initial branch to `main` regardless of server config', async () => {
// `updateInstead` only extracts the working tree when the push
// targets the branch HEAD points at. If `git init` honoured the
// server's `init.defaultBranch`, a server configured for e.g.
// `gh-pages` would silently fail to extract files pushed to `main`.
// Pin the auto-init'd HEAD so `git push pod HEAD:main` works
// everywhere. See #471.
const repoPath = 'public/apps/penny';
const headPath = path.resolve(DATA_DIR, repoPath, '.git', 'HEAD');
assert.ok(existsSync(headPath), 'prereq: repo from earlier test still exists');
const head = readFileSync(headPath, 'utf8').trim();
assert.strictEqual(head, 'ref: refs/heads/main',
`HEAD must point at refs/heads/main, got: ${head}`);
});

it('auto-inits a regular repo when the target directory exists but is empty', async () => {
const repoPath = 'public/empty-dir';
await fs.ensureDir(path.resolve(DATA_DIR, repoPath));
Expand Down