@@ -14,6 +14,7 @@ import {
1414 WorktreeDeleteError ,
1515 WorktreeDeleteErrorReason ,
1616} from '../../git/errors' ;
17+ import type { GitDiff } from '../../git/models/diff' ;
1718import type { GitBranchReference , GitReference } from '../../git/models/reference' ;
1819import type { Repository } from '../../git/models/repository' ;
1920import { uncommitted , uncommittedStaged } from '../../git/models/revision' ;
@@ -37,6 +38,7 @@ import { configuration } from '../../system/-webview/configuration';
3738import { isDescendant } from '../../system/-webview/path' ;
3839import { revealInFileExplorer } from '../../system/-webview/vscode' ;
3940import { getWorkspaceFriendlyPath , openWorkspace } from '../../system/-webview/vscode/workspaces' ;
41+ import { Logger } from '../../system/logger' ;
4042import { basename } from '../../system/path' ;
4143import type { Deferred } from '../../system/promise' ;
4244import { pluralize , truncateLeft } from '../../system/string' ;
@@ -1194,29 +1196,43 @@ export class WorktreeGitCommand extends QuickCommand<State> {
11941196 const sourceSvc = this . container . git . getRepositoryService ( state . source ?. uri ?? state . repo . uri ) ;
11951197
11961198 if ( ! state . changes . contents || ! state . changes . baseSha ) {
1199+ let diff : GitDiff | undefined ;
11971200 let untrackedPaths : string [ ] | undefined ;
1198- if ( state . changes . type !== 'index' ) {
1199- // stage any untracked files
1200- const status = await sourceSvc . status . getStatus ( ) ;
1201- untrackedPaths = status ?. untrackedChanges . map ( f => f . path ) ;
1201+ try {
1202+ if ( state . changes . type !== 'index' ) {
1203+ // stage any untracked files to include them in the diff
1204+ const status = await sourceSvc . status . getStatus ( ) ;
1205+
1206+ untrackedPaths = status ?. untrackedChanges . map ( f => f . path ) ;
1207+ if ( untrackedPaths ?. length ) {
1208+ try {
1209+ await sourceSvc . staging ?. stageFiles ( untrackedPaths ) ;
1210+ } catch ( ex ) {
1211+ Logger . error (
1212+ ex ,
1213+ `Failed to stage (${ untrackedPaths . length } ) untracked files for copying changes` ,
1214+ ) ;
1215+ }
1216+ }
1217+ }
1218+
1219+ diff = await sourceSvc . diff . getDiff ?.(
1220+ state . changes . type === 'index' ? uncommittedStaged : uncommitted ,
1221+ 'HEAD' ,
1222+ ) ;
1223+ } finally {
12021224 if ( untrackedPaths ?. length ) {
12031225 try {
1204- await sourceSvc . staging ?. stageFiles ( untrackedPaths ) ;
1205- } catch { }
1226+ await sourceSvc . staging ?. unstageFiles ( untrackedPaths ) ;
1227+ } catch ( ex ) {
1228+ Logger . error (
1229+ ex ,
1230+ `Failed to unstage (${ untrackedPaths . length } ) untracked files for copying changes` ,
1231+ ) ;
1232+ }
12061233 }
12071234 }
12081235
1209- const diff = await sourceSvc . diff . getDiff ?.(
1210- state . changes . type === 'index' ? uncommittedStaged : uncommitted ,
1211- 'HEAD' ,
1212- ) ;
1213-
1214- if ( untrackedPaths ?. length ) {
1215- try {
1216- await sourceSvc . staging ?. unstageFiles ( untrackedPaths ) ;
1217- } catch { }
1218- }
1219-
12201236 if ( ! diff ?. contents ) {
12211237 void window . showErrorMessage ( `No changes to copy` ) ;
12221238
0 commit comments