Skip to content

Commit 7a2bcd3

Browse files
Phil Stainerphilstainer
authored andcommitted
fix: check for worktree repo
1 parent 1ec2bce commit 7a2bcd3

File tree

8 files changed

+39
-33
lines changed

8 files changed

+39
-33
lines changed

src/commands/add.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
fetch,
55
getRemoteBranches,
66
removeLocalBranchesThatDoNotExistOnRemoteRepository,
7-
throwIfNotRepository,
7+
throwIfNotInWorktreeRepository,
88
} from '../helpers/git';
99
import {
1010
getUniqueWorktreeName,
@@ -21,7 +21,7 @@ const createWorktreeOption = 'Create new worktree';
2121

2222
export const add = async () => {
2323
try {
24-
await throwIfNotRepository();
24+
await throwIfNotInWorktreeRepository();
2525

2626
await fetch();
2727

src/commands/list.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { throwIfNotRepository } from '#/helpers/git';
1+
import { throwIfNotInWorktreeRepository } from '#/helpers/git';
22
import settings from '../config/settings';
33
import { raiseIssue, showUserMessage } from '../helpers/vscode';
44
import { getWorktrees } from '../helpers/worktree/getWorktrees';
@@ -7,7 +7,7 @@ import { shouldMoveIntoWorktree } from '../helpers/worktree/shouldMoveIntoWorktr
77

88
export const list = async () => {
99
try {
10-
await throwIfNotRepository();
10+
await throwIfNotInWorktreeRepository();
1111

1212
const worktrees = await getWorktrees();
1313
if (!worktrees.length)

src/commands/publish.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ import {
33
getRemoteBranches,
44
getRemoteOrigin,
55
pushNewBranchToRemote,
6-
throwIfNotRepository,
6+
throwIfNotInWorktreeRepository,
77
} from '../helpers/git';
88
import { raiseIssue, showUserMessage } from '../helpers/vscode';
99
import { getWorktrees } from '../helpers/worktree/getWorktrees';
1010
import { selectWorktree } from '../helpers/worktree/selectWorktree';
1111

1212
export const publish = async () => {
1313
try {
14-
await throwIfNotRepository();
14+
await throwIfNotInWorktreeRepository();
1515

1616
const hasRemote = await getRemoteOrigin();
1717
if (!hasRemote)

src/commands/remove.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { removeBranch, throwIfNotRepository } from '#/helpers/git';
1+
import { removeBranch, throwIfNotInWorktreeRepository } from '#/helpers/git';
22
import settings from '../config/settings';
33
import { raiseIssue, showUserMessage } from '../helpers/vscode';
44
import { getWorktrees } from '../helpers/worktree/getWorktrees';
@@ -8,7 +8,7 @@ import { selectWorktree } from '../helpers/worktree/selectWorktree';
88

99
export const remove = async () => {
1010
try {
11-
await throwIfNotRepository();
11+
await throwIfNotInWorktreeRepository();
1212

1313
const worktrees = await getWorktrees();
1414
if (!worktrees.length)

src/commands/rename.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
fetch,
66
getRemoteOrigin,
77
pushNewBranchToRemote,
8-
throwIfNotRepository,
8+
throwIfNotInWorktreeRepository,
99
} from '../helpers/git';
1010
import {
1111
getUniqueWorktreeName,
@@ -19,7 +19,7 @@ import { shouldMoveIntoWorktree } from '../helpers/worktree/shouldMoveIntoWorktr
1919

2020
export const rename = async () => {
2121
try {
22-
await throwIfNotRepository();
22+
await throwIfNotInWorktreeRepository();
2323

2424
const worktrees = await getWorktrees();
2525
if (!worktrees.length)

src/helpers/general.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { loggingOptions, loggingOptionValue } from '../config/constants';
1010
import { globalState } from '../extension';
1111
import { isExistingDirectory } from './file';
1212

13+
import type { ExecOptions } from 'child_process';
14+
1315
const exec = util.promisify(require('child_process').exec);
1416

1517
export const getCurrentPath = () => workspace.rootPath;
@@ -21,7 +23,10 @@ export const getCurrentDirectory = () => {
2123
};
2224
export const getWorkspaceFilePath = () => workspace.workspaceFile;
2325

24-
export const executeCommand = async (command: string, options?: any) => {
26+
export const executeCommand = async (
27+
command: string,
28+
options?: ExecOptions
29+
) => {
2530
let execOptions = options;
2631

2732
if (!options) {

src/helpers/git.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,6 @@ import { executeCommand } from './general';
66
import { removeNewLine } from './string';
77
import { getWorktrees } from './worktree/getWorktrees';
88

9-
export const isGitRepository = async () => {
10-
try {
11-
const command = 'git rev-parse --is-inside-work-tree';
12-
await executeCommand(command);
13-
14-
return true;
15-
} catch (e: any) {
16-
return false;
17-
}
18-
};
19-
209
export const getRemoteOrigin = async () => {
2110
const command = 'git remote';
2211

@@ -30,27 +19,39 @@ export const getRemoteOrigin = async () => {
3019
}
3120
};
3221

33-
export const throwIfNotRepository = async () => {
34-
const isRepo = await isGitRepository();
35-
36-
if (isRepo) return;
22+
export const isInsideWorkTree = async () => {
23+
try {
24+
const command = 'git rev-parse --is-inside-work-tree';
25+
await executeCommand(command);
3726

38-
throw new Error('This is not a git repository.');
27+
return true;
28+
} catch (e: any) {
29+
return false;
30+
}
3931
};
4032

41-
export const isBareRepository = async (path: string) => {
33+
export const isInsideBareRepository = async (path?: string) => {
4234
try {
43-
const command = `git -C ${path} rev-parse --is-bare-repository`;
44-
const { stdout } = await executeCommand(command);
35+
const command = `git rev-parse --is-bare-repository`;
36+
const { stdout } = await executeCommand(command, { cwd: path });
4537

4638
const result = removeNewLine(stdout);
4739

4840
return result === 'true';
4941
} catch (e: any) {
50-
throw Error(e);
42+
return false;
5143
}
5244
};
5345

46+
export const throwIfNotInWorktreeRepository = async () => {
47+
const isWorktree = await isInsideWorkTree();
48+
const isBareRepo = await isInsideBareRepository();
49+
50+
if (isWorktree || isBareRepo) return;
51+
52+
throw new Error('You are not in a worktree or bare repo');
53+
};
54+
5455
const hasBareRepository = async () => {
5556
const worktrees = await getWorktrees(true);
5657
const hasBareRepo = worktrees.find((wt) => wt.worktree === BARE_REPOSITORY);

src/helpers/worktree/calculateNewWorktreePath.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import settings from '#/src/config/settings';
22
import { isAbsolute, resolve } from 'path';
33
import { executeCommand } from '../general';
4-
import { isBareRepository } from '../git';
4+
import { isInsideBareRepository } from '../git';
55
import { removeNewLine } from '../string';
66

77
const baseDirectory = settings.baseDirectory;
@@ -13,7 +13,7 @@ export const calculateNewWorktreePath = async (branch: string) => {
1313

1414
const path = removeNewLine(stdout);
1515

16-
const isBare = await isBareRepository(path);
16+
const isBare = await isInsideBareRepository(path);
1717
if (!isBare) throw new Error('Bare repository not found');
1818

1919
const resolvedPath = isAbsolute(baseDirectory)

0 commit comments

Comments
 (0)