@@ -12,7 +12,7 @@ import type { GitDiff } from '../git/models/diff';
1212import type { Repository } from '../git/models/repository' ;
1313import { uncommitted , uncommittedStaged } from '../git/models/revision' ;
1414import { splitCommitMessage } from '../git/utils/commit.utils' ;
15- import { isSha , shortenRevision } from '../git/utils/revision.utils' ;
15+ import { isSha , isUncommitted , isUncommittedStaged , shortenRevision } from '../git/utils/revision.utils' ;
1616import { showPatchesView } from '../plus/drafts/actions' ;
1717import type { ProviderAuth } from '../plus/drafts/draftsService' ;
1818import type { Draft , LocalDraft } from '../plus/drafts/models/drafts' ;
@@ -30,6 +30,7 @@ import {
3030 isCommandContextViewNodeHasComparison ,
3131 isCommandContextViewNodeHasFileCommit ,
3232 isCommandContextViewNodeHasFileRefs ,
33+ isCommandContextViewNodeHasRefFile ,
3334} from './commandContext.utils' ;
3435
3536export interface CreatePatchCommandArgs {
@@ -83,7 +84,6 @@ abstract class CreatePatchCommandBase extends GlCommandBase {
8384 args = {
8485 repoPath : repo ?. path ,
8586 to : to ,
86- from : 'HEAD' ,
8787 uris : [ ...map ( uris , u => Uri . parse ( u ) ) ] ,
8888 title : to === uncommittedStaged ? 'Staged Changes' : 'Uncommitted Changes' ,
8989 includeUntracked : includeUntracked ? true : undefined ,
@@ -98,25 +98,33 @@ abstract class CreatePatchCommandBase extends GlCommandBase {
9898 args = {
9999 repoPath : repo ?. path ,
100100 to : to ,
101- from : 'HEAD' ,
102101 title : to === uncommittedStaged ? 'Staged Changes' : 'Uncommitted Changes' ,
103102 } ;
104103 } else if ( context . type === 'viewItem' ) {
105104 if ( isCommandContextViewNodeHasCommit ( context ) ) {
106105 const { commit } = context . node ;
107- if ( commit . message == null ) {
108- await commit . ensureFullDetails ( ) ;
109- }
106+ if ( commit . isUncommitted ) {
107+ const to = commit . isUncommittedStaged ? uncommittedStaged : uncommitted ;
108+ args = {
109+ repoPath : context . node . commit . repoPath ,
110+ to : to ,
111+ title : to === uncommittedStaged ? 'Staged Changes' : 'Uncommitted Changes' ,
112+ } ;
113+ } else {
114+ if ( commit . message == null ) {
115+ await commit . ensureFullDetails ( ) ;
116+ }
110117
111- const { summary : title , body : description } = splitCommitMessage ( commit . message ) ;
118+ const { summary : title , body : description } = splitCommitMessage ( commit . message ) ;
112119
113- args = {
114- repoPath : context . node . commit . repoPath ,
115- to : context . node . commit . ref ,
116- from : `${ context . node . commit . ref } ^` ,
117- title : title ,
118- description : description ,
119- } ;
120+ args = {
121+ repoPath : context . node . commit . repoPath ,
122+ to : context . node . commit . ref ,
123+ from : `${ context . node . commit . ref } ^` ,
124+ title : title ,
125+ description : description ,
126+ } ;
127+ }
120128 if ( isCommandContextViewNodeHasFileCommit ( context ) ) {
121129 args . uris = [ context . node . uri ] ;
122130 }
@@ -136,16 +144,53 @@ abstract class CreatePatchCommandBase extends GlCommandBase {
136144 from : context . node . ref1 ,
137145 uris : [ context . node . uri ] ,
138146 } ;
139- }
140- } else if ( context . type === 'viewItems' ) {
141- if ( isViewRefFileNode ( context . node ) ) {
147+ } else if ( context . node . is ( 'uncommitted-files' ) ) {
142148 args = {
143149 repoPath : context . node . repoPath ,
144- to : context . node . ref . sha ,
145- from : `${ context . node . ref . sha } ^` ,
146- uris : [ context . node . uri ] ,
147- title : `Changes (partial) in ${ shortenRevision ( context . node . ref . sha ) } ` ,
150+ to : uncommitted ,
151+ from : 'HEAD' ,
152+ title : 'Uncommitted Changes' ,
148153 } ;
154+ } else if ( isCommandContextViewNodeHasRefFile ( context ) ) {
155+ if ( isUncommitted ( context . node . ref . ref ) ) {
156+ const to = isUncommittedStaged ( context . node . ref . ref ) ? uncommittedStaged : uncommitted ;
157+ args = {
158+ repoPath : context . node . repoPath ,
159+ to : to ,
160+ from : context . node . is ( 'uncommitted-file' ) ? 'HEAD' : undefined ,
161+ uris : [ context . node . uri ] ,
162+ title : to === uncommittedStaged ? 'Staged Changes' : 'Uncommitted Changes' ,
163+ } ;
164+ } else {
165+ args = {
166+ repoPath : context . node . repoPath ,
167+ to : context . node . ref . sha ,
168+ from : `${ context . node . ref . sha } ^` ,
169+ uris : [ context . node . uri ] ,
170+ title : `Changes (partial) in ${ shortenRevision ( context . node . ref . sha ) } ` ,
171+ } ;
172+ }
173+ }
174+ } else if ( context . type === 'viewItems' ) {
175+ if ( isViewRefFileNode ( context . node ) ) {
176+ if ( isUncommitted ( context . node . ref . ref ) ) {
177+ const to = isUncommittedStaged ( context . node . ref . ref ) ? uncommittedStaged : uncommitted ;
178+ args = {
179+ repoPath : context . node . repoPath ,
180+ to : to ,
181+ from : context . node . is ( 'uncommitted-file' ) ? 'HEAD' : undefined ,
182+ uris : [ context . node . uri ] ,
183+ title : to === uncommittedStaged ? 'Staged Changes' : 'Uncommitted Changes' ,
184+ } ;
185+ } else {
186+ args = {
187+ repoPath : context . node . repoPath ,
188+ to : context . node . ref . sha ,
189+ from : `${ context . node . ref . sha } ^` ,
190+ uris : [ context . node . uri ] ,
191+ title : `Changes (partial) in ${ shortenRevision ( context . node . ref . sha ) } ` ,
192+ } ;
193+ }
149194
150195 for ( const node of context . nodes ) {
151196 if ( isViewRefFileNode ( node ) && node !== context . node && node . ref . sha === args . to ) {
@@ -160,34 +205,34 @@ abstract class CreatePatchCommandBase extends GlCommandBase {
160205 }
161206
162207 protected async getDiff ( title : string , args ?: CreatePatchCommandArgs ) : Promise < GitDiff | undefined > {
163- let repo ;
208+ let git ;
164209 if ( args ?. repoPath != null ) {
165- repo = this . container . git . getRepository ( args . repoPath ) ;
210+ git =
211+ this . container . git . getRepository ( args . repoPath ) ?. git ??
212+ this . container . git . getRepositoryService ( args . repoPath ) ;
166213 }
167- repo ??= await getRepositoryOrShowPicker ( this . container , title ) ;
168- if ( repo == null ) return ;
214+ git ??= ( await getRepositoryOrShowPicker ( this . container , title , undefined , args ?. repoPath ) ) ?. git ;
215+ if ( git == null ) return ;
169216
170217 let untrackedPaths : string [ ] | undefined ;
171218 try {
172219 if ( args ?. to === uncommitted ) {
173220 // stage any untracked files to include them in the diff
174- untrackedPaths = ( await repo . git . status ?. getUntrackedFiles ( ) ) ?. map ( f => f . path ) ;
221+ untrackedPaths = ( await git . status ?. getUntrackedFiles ( ) ) ?. map ( f => f . path ) ;
175222 if ( untrackedPaths ?. length ) {
176223 try {
177- await repo . git . staging ?. stageFiles ( untrackedPaths ) ;
224+ await git . staging ?. stageFiles ( untrackedPaths ) ;
178225 } catch ( ex ) {
179226 Logger . error ( ex , `Failed to stage (${ untrackedPaths . length } ) untracked files for patch` ) ;
180227 }
181228 }
182229 }
183230
184- return await repo . git . diff . getDiff ?.( args ?. to ?? uncommitted , args ?. from ?? 'HEAD' , {
185- uris : args ?. uris ,
186- } ) ;
231+ return await git . diff . getDiff ?.( args ?. to ?? uncommitted , args ?. from , { uris : args ?. uris } ) ;
187232 } finally {
188233 if ( untrackedPaths ?. length ) {
189234 try {
190- await repo . git . staging ?. unstageFiles ( untrackedPaths ) ;
235+ await git . staging ?. unstageFiles ( untrackedPaths ) ;
191236 } catch ( ex ) {
192237 Logger . error ( ex , `Failed to unstage (${ untrackedPaths . length } ) untracked files for patch` ) ;
193238 }
0 commit comments