forked from desktop-plus/desktop-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworktree.ts
More file actions
195 lines (165 loc) · 4.74 KB
/
Copy pathworktree.ts
File metadata and controls
195 lines (165 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import * as Path from 'path'
import * as Fs from 'fs'
import type { Repository } from '../../models/repository'
import type { WorktreeEntry, WorktreeType } from '../../models/worktree'
import { git } from './core'
import { normalizePath } from '../helpers/path'
function getDotGitPath(repositoryPath: string): string {
return Path.join(repositoryPath, '.git')
}
export interface IWorktreePathInfo {
readonly isLinkedWorktree: boolean
readonly mainWorktreePath: string | null
}
export function parseWorktreePorcelainOutput(
stdout: string
): ReadonlyArray<WorktreeEntry> {
if (stdout.trim().length === 0) {
return []
}
const blocks = stdout.trim().split('\n\n')
const entries: WorktreeEntry[] = []
for (let i = 0; i < blocks.length; i++) {
const lines = blocks[i].split('\n')
let path = ''
let head = ''
let branch: string | null = null
let isDetached = false
let isLocked = false
let isPrunable = false
for (const line of lines) {
if (line.startsWith('worktree ')) {
path = line.substring('worktree '.length)
} else if (line.startsWith('HEAD ')) {
head = line.substring('HEAD '.length)
} else if (line.startsWith('branch ')) {
branch = line.substring('branch '.length)
} else if (line === 'detached') {
isDetached = true
} else if (line === 'locked' || line.startsWith('locked ')) {
isLocked = true
} else if (line === 'prunable' || line.startsWith('prunable ')) {
isPrunable = true
}
}
const type: WorktreeType = i === 0 ? 'main' : 'linked'
entries.push({ path, head, branch, isDetached, type, isLocked, isPrunable })
}
return entries
}
export async function listWorktrees(
repository: Repository
): Promise<ReadonlyArray<WorktreeEntry>> {
const result = await git(
['worktree', 'list', '--porcelain'],
repository.path,
'listWorktrees'
)
return parseWorktreePorcelainOutput(result.stdout)
}
export async function addWorktree(
repository: Repository,
path: string,
options: {
readonly branch?: string
readonly createBranch?: string
readonly detach?: boolean
readonly commitish?: string
} = {}
): Promise<void> {
const args = ['worktree', 'add']
if (options.detach) {
args.push('--detach')
}
if (options.createBranch) {
args.push('-b', options.createBranch)
}
args.push(path)
if (options.branch) {
args.push(options.branch)
} else if (options.commitish) {
args.push(options.commitish)
}
await git(args, repository.path, 'addWorktree')
}
export async function removeWorktree(
repository: Repository,
path: string,
force: boolean = false
): Promise<void> {
const args = ['worktree', 'remove']
if (force) {
args.push('--force')
}
args.push(path)
await git(args, repository.path, 'removeWorktree')
}
export async function pruneWorktrees(repository: Repository): Promise<void> {
await git(['worktree', 'prune'], repository.path, 'pruneWorktrees')
}
export async function moveWorktree(
repository: Repository,
oldPath: string,
newPath: string
): Promise<void> {
await git(
['worktree', 'move', oldPath, newPath],
repository.path,
'moveWorktree'
)
}
export async function isLinkedWorktree(
repository: Repository
): Promise<boolean> {
const worktrees = await listWorktrees(repository)
const repoPath = normalizePath(repository.path)
return worktrees.some(
wt => wt.type === 'linked' && normalizePath(wt.path) === repoPath
)
}
export async function getMainWorktreePath(
repository: Repository
): Promise<string | null> {
const worktrees = await listWorktrees(repository)
const main = worktrees.find(wt => wt.type === 'main')
return main?.path ?? null
}
export function getWorktreePathInfoSync(
repositoryPath: string
): IWorktreePathInfo | null {
try {
const dotGit = getDotGitPath(repositoryPath)
// eslint-disable-next-line no-sync
const stats = Fs.statSync(dotGit)
if (stats.isDirectory()) {
return { isLinkedWorktree: false, mainWorktreePath: repositoryPath }
}
if (!stats.isFile()) {
return null
}
// eslint-disable-next-line no-sync
const contents = Fs.readFileSync(dotGit, 'utf8').trim()
if (!contents.startsWith('gitdir: ')) {
return null
}
const gitDirPath = Path.resolve(
repositoryPath,
contents.substring('gitdir: '.length)
)
// eslint-disable-next-line no-sync
const commondir = Fs.readFileSync(
Path.join(gitDirPath, 'commondir'),
'utf8'
).trim()
if (commondir.length === 0) {
return null
}
const commonGitDir = Path.resolve(gitDirPath, commondir)
return {
isLinkedWorktree: true,
mainWorktreePath: Path.dirname(commonGitDir),
}
} catch {
return null
}
}