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
5 changes: 5 additions & 0 deletions .changeset/update-keep-ssl-out-of-stash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nostream": patch
---

`nostream update` no longer stashes the files under `nginx/ssl`. They were neither tracked nor ignored, so the stash took the certificates the nginx compose file mounts, private keys included, into a Git object any local account can read, and restoring them did not keep the owner-only mode they had. The directory is now in `.gitignore`, and the stash command leaves it out as well, because it runs before the pull that brings that rule to an older checkout.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ coverage-report/

# graphify code-graph output (regenerated, ~4 MB per run)
graphify-out/

# TLS certificates and private keys the nginx compose file mounts
nginx/ssl/
18 changes: 16 additions & 2 deletions src/cli/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import { runCommand, runCommandWithOutput } from '../utils/process'
import { runStart } from './start'
import { runStop } from './stop'

// `git stash -u` takes every untracked file that is not ignored. nginx/ssl holds the certificates the nginx compose file
// mounts, private keys included, and a stash keeps them as a Git object any local account can read. The directory is in
// .gitignore, but the stash runs before the pull that brings that rule to an older checkout, so the command leaves it
// out as well. The paths are anchored at the top of the repository so the command does not depend on where it runs.
export const STASH_ARGS = ['stash', 'push', '-u', '-m', 'nostream-cli-update', '--', ':/', ':(top,exclude)nginx/ssl']

// wasStashCreated() reads git's own message, which git translates, so the command runs in the C locale. In another one
// "nothing to stash" would read as a stash that was made, and the pop after the pull would restore an older stash or fail.
export const STASH_ENV = { LC_ALL: 'C' }

const wasStashCreated = (output: string): boolean => {
return !output.includes('No local changes to save')
}
Expand All @@ -17,9 +27,13 @@ export const runUpdate = async (passthrough: string[]): Promise<number> => {
return code
}

const stashResult = await runCommandWithOutput('git', ['stash', 'push', '-u', '-m', 'nostream-cli-update'])
const stashResult = await runCommandWithOutput('git', STASH_ARGS, { env: STASH_ENV })
if (!stashResult.ok) {
spinner.fail(stashResult.ok === false && stashResult.reason === 'not-found' ? 'Update failed: git is not installed' : 'Update failed while stashing local changes')
spinner.fail(
stashResult.ok === false && stashResult.reason === 'not-found'
? 'Update failed: git is not installed'
: 'Update failed while stashing local changes',
)
return 1
}
if (stashResult.code !== 0) {
Expand Down
117 changes: 104 additions & 13 deletions test/unit/cli/update.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { execFileSync } from 'node:child_process'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'

import { expect } from 'chai'
import sinon from 'sinon'

import { runUpdate } from '../../../src/cli/commands/update'
import { runUpdate, STASH_ARGS, STASH_ENV } from '../../../src/cli/commands/update'
import * as processUtils from '../../../src/cli/utils/process'
import * as startCommand from '../../../src/cli/commands/start'
import * as stopCommand from '../../../src/cli/commands/stop'
Expand All @@ -20,12 +25,7 @@ describe('runUpdate', () => {
stdout: 'Saved working directory and index state WIP on main: abc123',
stderr: '',
})
const runCommandStub = sinon
.stub(processUtils, 'runCommand')
.onFirstCall()
.resolves(1)
.onSecondCall()
.resolves(0)
const runCommandStub = sinon.stub(processUtils, 'runCommand').onFirstCall().resolves(1).onSecondCall().resolves(0)

const code = await runUpdate([])

Expand All @@ -44,12 +44,7 @@ describe('runUpdate', () => {
stdout: 'Saved working directory and index state WIP on main: abc123',
stderr: '',
})
const runCommandStub = sinon
.stub(processUtils, 'runCommand')
.onFirstCall()
.resolves(1)
.onSecondCall()
.resolves(2)
const runCommandStub = sinon.stub(processUtils, 'runCommand').onFirstCall().resolves(1).onSecondCall().resolves(2)

const code = await runUpdate([])

Expand All @@ -58,4 +53,100 @@ describe('runUpdate', () => {
expect(runCommandStub.secondCall.args).to.deep.equal(['git', ['stash', 'pop']])
expect(runStartStub.called).to.equal(false)
})

it('stashes with the arguments that leave nginx/ssl out, in the C locale', async () => {
sinon.stub(stopCommand, 'runStop').resolves(0)
sinon.stub(startCommand, 'runStart').resolves(0)
const runCommandWithOutputStub = sinon.stub(processUtils, 'runCommandWithOutput').resolves({
ok: true,
code: 0,
stdout: 'No local changes to save',
stderr: '',
})
sinon.stub(processUtils, 'runCommand').resolves(0)

await runUpdate([])

expect(runCommandWithOutputStub.firstCall.args).to.deep.equal(['git', STASH_ARGS, { env: STASH_ENV }])
expect(STASH_ENV).to.deep.equal({ LC_ALL: 'C' })
})

describe('the stash', () => {
let repo: string
// The fixture is its own repository: variables that point git at the caller's checkout are dropped, and neither the
// developer's global or system configuration (commit signing, hooks, templates) nor their locale applies to it.
const isolatedEnv = (): NodeJS.ProcessEnv => {
const env = Object.fromEntries(Object.entries(process.env).filter(([name]) => !name.startsWith('GIT_')))
return { ...env, GIT_CONFIG_GLOBAL: os.devNull, GIT_CONFIG_NOSYSTEM: '1', LC_ALL: 'C' }
}
const git = (...args: string[]) =>
execFileSync('git', ['-c', 'commit.gpgsign=false', '-c', `core.hooksPath=${os.devNull}`, ...args], {
cwd: repo,
encoding: 'utf-8',
env: isolatedEnv(),
})
const stashPaths = () =>
git('stash', 'show', '--include-untracked', '--name-only', 'stash@{0}').split('\n').filter(Boolean)

beforeEach(() => {
repo = fs.mkdtempSync(path.join(os.tmpdir(), 'nostream-update-'))
git('init', '-q')
git('config', 'user.email', 'test@example.com')
git('config', 'user.name', 'test')
fs.writeFileSync(path.join(repo, 'tracked'), 'tracked')
git('add', 'tracked')
git('commit', '-q', '-m', 'initial')
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
fs.mkdirSync(path.join(repo, 'nginx/ssl/live/relay.example'), { recursive: true })
fs.writeFileSync(path.join(repo, 'nginx/ssl/live/relay.example/privkey.pem'), 'PRIVATE KEY', { mode: 0o600 })
})

afterEach(() => {
fs.rmSync(repo, { recursive: true, force: true })
})

// An older checkout has no ignore rule for nginx/ssl until the pull that follows the stash.
it('keeps a private key under nginx/ssl out of the stash and in place', () => {
fs.writeFileSync(path.join(repo, 'notes.txt'), 'local')

git(...STASH_ARGS)

expect(stashPaths()).to.deep.equal(['notes.txt'])
const key = path.join(repo, 'nginx/ssl/live/relay.example/privkey.pem')
expect(fs.readFileSync(key, 'utf-8')).to.equal('PRIVATE KEY')
expect(fs.statSync(key).mode & 0o777).to.equal(0o600)
})

it('still stashes the other local files and restores them', () => {
fs.writeFileSync(path.join(repo, 'notes.txt'), 'local')
fs.writeFileSync(path.join(repo, 'tracked'), 'changed')

git(...STASH_ARGS)
expect(fs.existsSync(path.join(repo, 'notes.txt'))).to.equal(false)
git('stash', 'pop', '-q')

expect(fs.readFileSync(path.join(repo, 'notes.txt'), 'utf-8')).to.equal('local')
expect(fs.readFileSync(path.join(repo, 'tracked'), 'utf-8')).to.equal('changed')
})

it('leaves nothing to stash when nginx/ssl is the only local change', () => {
expect(git(...STASH_ARGS)).to.contain('No local changes to save')
})

it('does not depend on the directory it runs in', () => {
fs.mkdirSync(path.join(repo, 'sub'))
fs.writeFileSync(path.join(repo, 'notes.txt'), 'local')

execFileSync('git', [...STASH_ARGS], { cwd: path.join(repo, 'sub'), env: isolatedEnv() })

expect(stashPaths()).to.deep.equal(['notes.txt'])
})

it('is kept out of a plain stash by the project .gitignore', () => {
fs.copyFileSync(path.resolve(__dirname, '../../../.gitignore'), path.join(repo, '.gitignore'))
git('add', '.gitignore')
git('commit', '-q', '-m', 'ignore')

expect(git('stash', 'push', '-u', '-m', 'plain')).to.contain('No local changes to save')
})
})
})
Loading