Skip to content

Commit ca3e9b1

Browse files
committed
no git roots allowed in parent of homedir
1 parent 14d64e2 commit ca3e9b1

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

npm-app/src/menu.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818

1919
import { codebuffConfigFile } from 'common/json-config/constants'
2020
import { Formatter } from 'picocolors/types'
21-
import { getProjectRoot, isSubdir } from './project-files'
21+
import { getProjectRoot, isValidProjectRoot } from './project-files'
2222

2323
export interface CommandInfo {
2424
commandText: string // e.g., 'type "login"', 'type "diff" or "d"', 'hit ESC key or Ctrl-C'
@@ -219,7 +219,7 @@ export function displayGreeting(costMode: CostMode, username: string | null) {
219219
console.log(`${costModeDescription[costMode]}`)
220220
}
221221

222-
if (isSubdir(getProjectRoot(), os.homedir())) {
222+
if (!isValidProjectRoot(getProjectRoot())) {
223223
console.info(
224224
`Welcome! Codebuff is your AI pair programmer that edits your codebase through natural conversation.
225225

npm-app/src/project-files.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ export function getCurrentChatDir(): string {
7373
return dir
7474
}
7575

76+
export function isValidProjectRoot(dir: string): boolean {
77+
return !isSubdir(dir, os.homedir())
78+
}
79+
7680
const execAsync = promisify(exec)
7781

7882
let projectRoot: string
@@ -155,6 +159,9 @@ export function toAbsolutePath(filepath: string, projectRoot: string): string {
155159
return path.normalize(path.resolve(projectRoot, filepath))
156160
}
157161

162+
/**
163+
* Returns true if the `toPath` is a subdirectory of `fromPath`.
164+
*/
158165
export function isSubdir(fromPath: string, toPath: string) {
159166
const resolvedFrom = path.resolve(fromPath)
160167
const resolvedTo = path.resolve(toPath)

npm-app/src/utils/git.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs, { existsSync, statSync } from 'fs'
33
import gitUrlParse from 'git-url-parse'
44
import { getConfig, listFiles, log, ReadCommitResult } from 'isomorphic-git'
55
import path from 'path'
6-
import { getWorkingDirectory } from '../project-files'
6+
import { getWorkingDirectory, isValidProjectRoot } from '../project-files'
77
import { logger } from './logger'
88

99
/**
@@ -38,7 +38,7 @@ export function findGitRoot(startDir: string): string | null {
3838
while (currentDir !== path.parse(currentDir).root) {
3939
const gitDir = path.join(currentDir, '.git')
4040
if (existsSync(gitDir) && statSync(gitDir).isDirectory()) {
41-
return currentDir
41+
return isValidProjectRoot(currentDir) ? currentDir : null
4242
}
4343
currentDir = path.dirname(currentDir)
4444
}

0 commit comments

Comments
 (0)