1
1
import type { Denops } from "jsr:@denops/std@^7.0.0" ;
2
+ import * as fn from "jsr:@denops/std@^7.0.0/function" ;
2
3
import * as batch from "jsr:@denops/std@^7.0.0/batch" ;
3
4
import * as helper from "jsr:@denops/std@^7.0.0/helper" ;
5
+ import { join } from "jsr:@std/path@^1.0.0/join" ;
6
+ import { dirname } from "jsr:@std/path@^1.0.0/dirname" ;
4
7
import { define , GatherCandidates , Range } from "./core.ts" ;
8
+ import { revParse } from "../git/finder.ts" ;
5
9
6
10
export type Candidate = { target ?: string } ;
7
11
@@ -42,12 +46,14 @@ async function doNew(
42
46
force : boolean ,
43
47
gatherCandidates : GatherCandidates < Candidate > ,
44
48
) : Promise < void > {
49
+ const cwd = await fn . getcwd ( denops ) ;
50
+ const root = await findRoot ( cwd ) ;
45
51
const xs = await gatherCandidates ( denops , bufnr , range ) ;
46
52
const x = xs . at ( 0 ) ;
47
53
const target = x ?. target ?? "HEAD" ;
48
54
const worktreePath = await helper . input ( denops , {
49
55
prompt : `Worktree path (for ${ target } ): ` ,
50
- text : `.worktrees/${ target } ` ,
56
+ text : join ( root , `.worktrees/${ target } ` ) ,
51
57
} ) ;
52
58
await denops . cmd ( 'redraw | echo ""' ) ;
53
59
if ( ! worktreePath ) {
@@ -69,9 +75,11 @@ async function doNewOrphan(
69
75
_range : Range ,
70
76
_gatherCandidates : GatherCandidates < unknown > ,
71
77
) : Promise < void > {
78
+ const cwd = await fn . getcwd ( denops ) ;
79
+ const root = await findRoot ( cwd ) ;
72
80
const worktreePath = await helper . input ( denops , {
73
81
prompt : "Worktree path (orphan): " ,
74
- text : `.worktrees/orphan` ,
82
+ text : join ( root , `.worktrees/orphan` ) ,
75
83
} ) ;
76
84
await denops . cmd ( 'redraw | echo ""' ) ;
77
85
if ( ! worktreePath ) {
@@ -85,3 +93,14 @@ async function doNewOrphan(
85
93
worktreePath ,
86
94
] ) ;
87
95
}
96
+
97
+ async function findRoot (
98
+ cwd : string ,
99
+ ) : Promise < string > {
100
+ try {
101
+ const gitdir = await revParse ( cwd , [ "--git-common-dir" ] ) ;
102
+ return dirname ( gitdir ) ;
103
+ } catch {
104
+ return "" ;
105
+ }
106
+ }
0 commit comments