1+ import * as Path from 'path'
2+
13import {
24 isRepositoryWithGitHubRepository ,
35 hasDefaultRemoteUrl ,
68import { RepoType } from '../../models/github-repository'
79import { IMenuItem } from '../../lib/menu-item'
810import { Repositoryish } from './group-repositories'
11+ import { WorktreeEntry } from '../../models/worktree'
912import { clipboard } from 'electron'
1013import {
1114 RevealInFileManagerLabel ,
@@ -19,10 +22,10 @@ interface IRepositoryListItemContextMenuConfig {
1922 externalEditorLabel : string | undefined
2023 askForConfirmationOnRemoveRepository : boolean
2124 onViewOnGitHub : ( repository : Repositoryish ) => void
22- onOpenInNewWindow ?: ( repository : Repositoryish ) => void
23- onOpenInShell : ( repository : Repositoryish ) => void
24- onShowRepository : ( repository : Repositoryish ) => void
25- onOpenInExternalEditor : ( repository : Repositoryish ) => void
25+ onOpenInNewWindow ?: ( repository : Repositoryish , path ?: string ) => void
26+ onOpenInShell : ( repository : Repositoryish , path ?: string ) => void
27+ onShowRepository : ( repository : Repositoryish , path ?: string ) => void
28+ onOpenInExternalEditor : ( repository : Repositoryish , path ?: string ) => void
2629 onRemoveRepository : ( repository : Repositoryish ) => void
2730 onChangeRepositoryAlias : ( repository : Repository ) => void
2831 onRemoveRepositoryAlias : ( repository : Repository ) => void
@@ -34,6 +37,7 @@ interface IRepositoryListItemContextMenuConfig {
3437 onUnpinRepository ?: ( repository : Repository ) => void
3538 onCreateWorktree ?: ( repository : Repository ) => void
3639 onShowWorktrees ?: ( repository : Repository ) => void
40+ worktreePath ?: string
3741}
3842
3943export const generateRepositoryListContextMenu = (
@@ -83,23 +87,25 @@ export const generateRepositoryListContextMenu = (
8387 label : __DARWIN__
8488 ? 'Open Repository in New Window'
8589 : 'Open repository in new window' ,
86- action : ( ) => config . onOpenInNewWindow ?.( repository ) ,
90+ action : ( ) =>
91+ config . onOpenInNewWindow ?.( repository , config . worktreePath ) ,
8792 } ,
8893 ]
8994 : [ ] ) ,
9095 {
9196 label : openInShell ,
92- action : ( ) => config . onOpenInShell ( repository ) ,
97+ action : ( ) => config . onOpenInShell ( repository , config . worktreePath ) ,
9398 enabled : ! missing ,
9499 } ,
95100 {
96101 label : RevealInFileManagerLabel ,
97- action : ( ) => config . onShowRepository ( repository ) ,
102+ action : ( ) => config . onShowRepository ( repository , config . worktreePath ) ,
98103 enabled : ! missing ,
99104 } ,
100105 {
101106 label : openInExternalEditor ,
102- action : ( ) => config . onOpenInExternalEditor ( repository ) ,
107+ action : ( ) =>
108+ config . onOpenInExternalEditor ( repository , config . worktreePath ) ,
103109 enabled : ! missing ,
104110 } ,
105111 { type : 'separator' } ,
@@ -112,6 +118,95 @@ export const generateRepositoryListContextMenu = (
112118 return items
113119}
114120
121+ interface IWorktreeListItemContextMenuConfig {
122+ repository : Repository
123+ worktree : WorktreeEntry
124+ shellLabel : string | undefined
125+ externalEditorLabel : string | undefined
126+ onCreateWorktree : ( repository : Repository ) => void
127+ onRenameWorktree : ( repository : Repository , worktreePath : string ) => void
128+ onDeleteWorktree : ( repository : Repository , worktreePath : string ) => void
129+ onViewOnGitHub : ( repository : Repositoryish ) => void
130+ onOpenWorktreeInNewWindow : (
131+ repository : Repository ,
132+ worktreePath : string
133+ ) => void
134+ onOpenInShell : ( repository : Repositoryish , path ?: string ) => void
135+ onShowRepository : ( repository : Repositoryish , path ?: string ) => void
136+ onOpenInExternalEditor : ( repository : Repositoryish , path ?: string ) => void
137+ onCopyWorktreePath : ( path : string ) => void
138+ }
139+
140+ export const generateWorktreeListItemContextMenu = (
141+ config : IWorktreeListItemContextMenuConfig
142+ ) : ReadonlyArray < IMenuItem > => {
143+ const { repository, worktree } = config
144+ const path = worktree . path
145+ const name = Path . basename ( path )
146+ const isGitHub = isRepositoryWithGitHubRepository ( repository )
147+ const hasOriginUrl = hasDefaultRemoteUrl ( repository )
148+ const canModify = ! worktree . isLocked
149+ const openInExternalEditor = config . externalEditorLabel
150+ ? `Open in ${ config . externalEditorLabel } `
151+ : DefaultEditorLabel
152+ const openInShell = config . shellLabel
153+ ? `Open in ${ config . shellLabel } `
154+ : DefaultShellLabel
155+
156+ return [
157+ {
158+ label : __DARWIN__ ? 'New Worktree…' : 'New worktree…' ,
159+ action : ( ) => config . onCreateWorktree ( repository ) ,
160+ } ,
161+ {
162+ label : __DARWIN__ ? 'Rename Worktree…' : 'Rename worktree…' ,
163+ action : ( ) => config . onRenameWorktree ( repository , path ) ,
164+ enabled : canModify ,
165+ } ,
166+ { type : 'separator' } ,
167+ {
168+ label : __DARWIN__ ? 'Copy Worktree Name' : 'Copy worktree name' ,
169+ action : ( ) => clipboard . writeText ( name ) ,
170+ } ,
171+ {
172+ label : __DARWIN__ ? 'Copy Worktree Path' : 'Copy worktree path' ,
173+ action : ( ) => config . onCopyWorktreePath ( path ) ,
174+ } ,
175+ { type : 'separator' } ,
176+ {
177+ label : getViewOnBrowserLabel (
178+ isGitHub ? repository . gitHubRepository . type : null
179+ ) ,
180+ action : ( ) => config . onViewOnGitHub ( repository ) ,
181+ enabled : isGitHub || hasOriginUrl ,
182+ } ,
183+ {
184+ label : __DARWIN__
185+ ? 'Open Worktree in New Window'
186+ : 'Open worktree in new window' ,
187+ action : ( ) => config . onOpenWorktreeInNewWindow ( repository , path ) ,
188+ } ,
189+ {
190+ label : openInShell ,
191+ action : ( ) => config . onOpenInShell ( repository , path ) ,
192+ } ,
193+ {
194+ label : RevealInFileManagerLabel ,
195+ action : ( ) => config . onShowRepository ( repository , path ) ,
196+ } ,
197+ {
198+ label : openInExternalEditor ,
199+ action : ( ) => config . onOpenInExternalEditor ( repository , path ) ,
200+ } ,
201+ { type : 'separator' } ,
202+ {
203+ label : __DARWIN__ ? 'Delete Worktree…' : 'Delete worktree…' ,
204+ action : ( ) => config . onDeleteWorktree ( repository , path ) ,
205+ enabled : canModify ,
206+ } ,
207+ ]
208+ }
209+
115210function getViewOnBrowserLabel ( repoType : RepoType | null ) {
116211 switch ( repoType ) {
117212 case 'github' :
0 commit comments