@@ -20,6 +20,8 @@ import {
2020 PullErrorReason ,
2121 PushError ,
2222 PushErrorReason ,
23+ ResetError ,
24+ ResetErrorReason ,
2325 StashPushError ,
2426 StashPushErrorReason ,
2527 TagError ,
@@ -73,42 +75,47 @@ const textDecoder = new TextDecoder('utf8');
7375const rootSha = '4b825dc642cb6eb9a060e54bf8d69288fbee4904' ;
7476
7577export const GitErrors = {
78+ alreadyCheckedOut : / a l r e a d y c h e c k e d o u t / i,
79+ alreadyExists : / a l r e a d y e x i s t s / i,
80+ ambiguousArgument : / f a t a l : \s * a m b i g u o u s a r g u m e n t [ ' " ] .+ [ ' " ] : u n k n o w n r e v i s i o n o r p a t h n o t i n t h e w o r k i n g t r e e / i,
7681 badRevision : / b a d r e v i s i o n ' ( .* ?) ' / i,
7782 cantLockRef : / c a n n o t l o c k r e f | u n a b l e t o u p d a t e l o c a l r e f / i,
7883 changesWouldBeOverwritten :
7984 / Y o u r l o c a l c h a n g e s t o t h e f o l l o w i n g f i l e s w o u l d b e o v e r w r i t t e n | Y o u r l o c a l c h a n g e s w o u l d b e o v e r w r i t t e n / i,
8085 commitChangesFirst : / P l e a s e , c o m m i t y o u r c h a n g e s b e f o r e y o u c a n / i,
8186 conflict : / ^ C O N F L I C T \( [ ^ ) ] + \) : \b / m,
87+ detachedHead : / Y o u a r e i n ' d e t a c h e d H E A D ' s t a t e / i,
88+ entryNotUpToDate : / e r r o r : \s * E n t r y [ ' " ] .+ [ ' " ] n o t u p t o d a t e \. C a n n o t m e r g e \. / i,
8289 failedToDeleteDirectoryNotEmpty : / f a i l e d t o d e l e t e ' ( .* ?) ' : D i r e c t o r y n o t e m p t y / i,
90+ invalidLineCount : / f i l e .+ ? h a s o n l y \d + l i n e s / i,
8391 invalidObjectName : / i n v a l i d o b j e c t n a m e : ( .* ) \s / i,
8492 invalidObjectNameList : / c o u l d n o t o p e n o b j e c t n a m e l i s t : ( .* ) \s / i,
93+ invalidTagName : / i n v a l i d t a g n a m e / i,
94+ mainWorkingTree : / i s a m a i n w o r k i n g t r e e / i,
8595 noFastForward : / \( n o n - f a s t - f o r w a r d \) / i,
8696 noMergeBase : / n o m e r g e b a s e / i,
8797 noRemoteRepositorySpecified : / N o r e m o t e r e p o s i t o r y s p e c i f i e d \. / i,
98+ noUpstream : / ^ f a t a l : T h e c u r r e n t b r a n c h .* h a s n o u p s t r e a m b r a n c h / i,
8899 notAValidObjectName : / N o t a v a l i d o b j e c t n a m e / i,
89100 notAWorkingTree : / ' ( .* ?) ' i s n o t a w o r k i n g t r e e / i,
90101 noUserNameConfigured : / P l e a s e t e l l m e w h o y o u a r e \. / i,
91- invalidLineCount : / f i l e .+ ? h a s o n l y \d + l i n e s / i,
92- uncommittedChanges : / c o n t a i n s m o d i f i e d o r u n t r a c k e d f i l e s / i,
93- alreadyExists : / a l r e a d y e x i s t s / i,
94- alreadyCheckedOut : / a l r e a d y c h e c k e d o u t / i,
95- mainWorkingTree : / i s a m a i n w o r k i n g t r e e / i,
96- noUpstream : / ^ f a t a l : T h e c u r r e n t b r a n c h .* h a s n o u p s t r e a m b r a n c h / i,
97102 noPausedOperation :
98103 / n o m e r g e (?: i n p r o g r e s s | t o a b o r t ) | n o c h e r r y - p i c k (?: o r r e v e r t ) ? i n p r o g r e s s | n o r e b a s e i n p r o g r e s s / i,
99104 permissionDenied : / P e r m i s s i o n .* d e n i e d / i,
100105 pushRejected : / ^ e r r o r : f a i l e d t o p u s h s o m e r e f s t o \b / m,
101106 rebaseMultipleBranches : / c a n n o t r e b a s e o n t o m u l t i p l e b r a n c h e s / i,
107+ refLocked : / f a t a l : \s * c a n n o t l o c k r e f [ ' " ] .+ [ ' " ] : u n a b l e t o c r e a t e f i l e / i,
102108 remoteAhead : / r e j e c t e d b e c a u s e t h e r e m o t e c o n t a i n s w o r k / i,
103109 remoteConnection : / C o u l d n o t r e a d f r o m r e m o t e r e p o s i t o r y / i,
104- tagConflict : / ! \[ r e j e c t e d \] .* \( w o u l d c l o b b e r e x i s t i n g t a g \) / m,
105- unmergedFiles : / i s n o t p o s s i b l e b e c a u s e y o u h a v e u n m e r g e d f i l e s | Y o u h a v e u n m e r g e d f i l e s / i,
106- unstagedChanges : / Y o u h a v e u n s t a g e d c h a n g e s / i,
110+ remoteRejected : / r e j e c t e d b e c a u s e t h e r e m o t e c o n t a i n s w o r k / i,
107111 tagAlreadyExists : / t a g .* a l r e a d y e x i s t s / i,
112+ tagConflict : / ! \[ r e j e c t e d \] .* \( w o u l d c l o b b e r e x i s t i n g t a g \) / m,
108113 tagNotFound : / t a g .* n o t f o u n d / i,
109- invalidTagName : / i n v a l i d t a g n a m e / i,
110- remoteRejected : / r e j e c t e d b e c a u s e t h e r e m o t e c o n t a i n s w o r k / i,
114+ uncommittedChanges : / c o n t a i n s m o d i f i e d o r u n t r a c k e d f i l e s / i,
115+ unmergedChanges : / e r r o r : \s * y o u n e e d t o r e s o l v e y o u r c u r r e n t i n d e x f i r s t / i,
116+ unmergedFiles : / i s n o t p o s s i b l e b e c a u s e y o u h a v e u n m e r g e d f i l e s | Y o u h a v e u n m e r g e d f i l e s / i,
111117 unresolvedConflicts : / Y o u m u s t e d i t a l l m e r g e c o n f l i c t s | R e s o l v e a l l c o n f l i c t s / i,
118+ unstagedChanges : / Y o u h a v e u n s t a g e d c h a n g e s / i,
112119} ;
113120
114121const GitWarnings = {
@@ -177,6 +184,16 @@ const tagErrorAndReason: [RegExp, TagErrorReason][] = [
177184 [ GitErrors . remoteRejected , TagErrorReason . RemoteRejected ] ,
178185] ;
179186
187+ const resetErrorAndReason : [ RegExp , ResetErrorReason ] [ ] = [
188+ [ GitErrors . ambiguousArgument , ResetErrorReason . AmbiguousArgument ] ,
189+ [ GitErrors . changesWouldBeOverwritten , ResetErrorReason . ChangesWouldBeOverwritten ] ,
190+ [ GitErrors . detachedHead , ResetErrorReason . DetachedHead ] ,
191+ [ GitErrors . entryNotUpToDate , ResetErrorReason . EntryNotUpToDate ] ,
192+ [ GitErrors . permissionDenied , ResetErrorReason . PermissionDenied ] ,
193+ [ GitErrors . refLocked , ResetErrorReason . RefLocked ] ,
194+ [ GitErrors . unmergedChanges , ResetErrorReason . UnmergedChanges ] ,
195+ ] ;
196+
180197export class Git {
181198 /** Map of running git commands -- avoids running duplicate overlaping commands */
182199 private readonly pendingCommands = new Map < string , Promise < string | Buffer > > ( ) ;
@@ -1627,8 +1644,33 @@ export class Git {
16271644 return this . exec < string > ( { cwd : repoPath } , 'remote' , 'get-url' , remote ) ;
16281645 }
16291646
1630- reset ( repoPath : string | undefined , pathspecs : string [ ] ) {
1631- return this . exec < string > ( { cwd : repoPath } , 'reset' , '-q' , '--' , ...pathspecs ) ;
1647+ async reset (
1648+ repoPath : string ,
1649+ pathspecs : string [ ] ,
1650+ options ?: { hard ?: boolean ; soft ?: never ; ref ?: string } | { soft ?: boolean ; hard ?: never ; ref ?: string } ,
1651+ ) : Promise < void > {
1652+ try {
1653+ const flags = [ ] ;
1654+ if ( options ?. hard ) {
1655+ flags . push ( '--hard' ) ;
1656+ } else if ( options ?. soft ) {
1657+ flags . push ( '--soft' ) ;
1658+ }
1659+
1660+ if ( options ?. ref ) {
1661+ flags . push ( options . ref ) ;
1662+ }
1663+ await this . exec < string > ( { cwd : repoPath } , 'reset' , '-q' , ...flags , '--' , ...pathspecs ) ;
1664+ } catch ( ex ) {
1665+ const msg : string = ex ?. toString ( ) ?? '' ;
1666+ for ( const [ error , reason ] of resetErrorAndReason ) {
1667+ if ( error . test ( msg ) || error . test ( ex . stderr ?? '' ) ) {
1668+ throw new ResetError ( reason , ex ) ;
1669+ }
1670+ }
1671+
1672+ throw new ResetError ( ResetErrorReason . Other , ex ) ;
1673+ }
16321674 }
16331675
16341676 async rev_list (
0 commit comments