11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
34using System . Threading . Tasks ;
45using Avalonia . Threading ;
56using CommunityToolkit . Mvvm . ComponentModel ;
@@ -14,6 +15,11 @@ public bool IsLoading
1415 private set => SetProperty ( ref _isLoading , value ) ;
1516 }
1617
18+ public bool CanResetFiles
19+ {
20+ get => _canResetFiles ;
21+ }
22+
1723 public string BaseName
1824 {
1925 get => _baseName ;
@@ -81,9 +87,10 @@ public DiffContext DiffContext
8187 private set => SetProperty ( ref _diffContext , value ) ;
8288 }
8389
84- public Compare ( string repo , object based , object to )
90+ public Compare ( Repository repo , object based , object to )
8591 {
86- _repo = repo ;
92+ _repo = repo . FullPath ;
93+ _canResetFiles = ! repo . IsBare ;
8794 _based = GetSHA ( based ) ;
8895 _to = GetSHA ( to ) ;
8996 _baseName = GetName ( based ) ;
@@ -134,6 +141,121 @@ public void OpenInExternalDiffTool(Models.Change change)
134141 new Commands . DiffTool ( _repo , new Models . DiffOption ( _based , _to , change ) ) . Open ( ) ;
135142 }
136143
144+ public async Task ResetToLeftAsync ( Models . Change change )
145+ {
146+ if ( ! _canResetFiles )
147+ return ;
148+
149+ if ( change . Index == Models . ChangeState . Added )
150+ {
151+ var fullpath = Native . OS . GetAbsPath ( _repo , change . Path ) ;
152+ if ( File . Exists ( fullpath ) )
153+ await new Commands . Remove ( _repo , [ change . Path ] ) . ExecAsync ( ) ;
154+ }
155+ else if ( change . Index == Models . ChangeState . Renamed )
156+ {
157+ var renamed = Native . OS . GetAbsPath ( _repo , change . Path ) ;
158+ if ( File . Exists ( renamed ) )
159+ await new Commands . Remove ( _repo , [ change . Path ] ) . ExecAsync ( ) ;
160+
161+ await new Commands . Checkout ( _repo ) . FileWithRevisionAsync ( change . OriginalPath , _baseHead . SHA ) ;
162+ }
163+ else
164+ {
165+ await new Commands . Checkout ( _repo ) . FileWithRevisionAsync ( change . Path , _baseHead . SHA ) ;
166+ }
167+ }
168+
169+ public async Task ResetToRightAsync ( Models . Change change )
170+ {
171+ if ( change . Index == Models . ChangeState . Deleted )
172+ {
173+ var fullpath = Native . OS . GetAbsPath ( _repo , change . Path ) ;
174+ if ( File . Exists ( fullpath ) )
175+ await new Commands . Remove ( _repo , [ change . Path ] ) . ExecAsync ( ) ;
176+ }
177+ else if ( change . Index == Models . ChangeState . Renamed )
178+ {
179+ var old = Native . OS . GetAbsPath ( _repo , change . OriginalPath ) ;
180+ if ( File . Exists ( old ) )
181+ await new Commands . Remove ( _repo , [ change . OriginalPath ] ) . ExecAsync ( ) ;
182+
183+ await new Commands . Checkout ( _repo ) . FileWithRevisionAsync ( change . Path , ToHead . SHA ) ;
184+ }
185+ else
186+ {
187+ await new Commands . Checkout ( _repo ) . FileWithRevisionAsync ( change . Path , ToHead . SHA ) ;
188+ }
189+ }
190+
191+ public async Task ResetMultipleToLeftAsync ( List < Models . Change > changes )
192+ {
193+ var checkouts = new List < string > ( ) ;
194+ var removes = new List < string > ( ) ;
195+
196+ foreach ( var c in changes )
197+ {
198+ if ( c . Index == Models . ChangeState . Added )
199+ {
200+ var fullpath = Native . OS . GetAbsPath ( _repo , c . Path ) ;
201+ if ( File . Exists ( fullpath ) )
202+ removes . Add ( c . Path ) ;
203+ }
204+ else if ( c . Index == Models . ChangeState . Renamed )
205+ {
206+ var old = Native . OS . GetAbsPath ( _repo , c . Path ) ;
207+ if ( File . Exists ( old ) )
208+ removes . Add ( c . Path ) ;
209+
210+ checkouts . Add ( c . OriginalPath ) ;
211+ }
212+ else
213+ {
214+ checkouts . Add ( c . Path ) ;
215+ }
216+ }
217+
218+ if ( removes . Count > 0 )
219+ await new Commands . Remove ( _repo , removes ) . ExecAsync ( ) ;
220+
221+ if ( checkouts . Count > 0 )
222+ await new Commands . Checkout ( _repo ) . MultipleFilesWithRevisionAsync ( checkouts , _baseHead . SHA ) ;
223+ }
224+
225+ public async Task ResetMultipleToRightAsync ( List < Models . Change > changes )
226+ {
227+ var checkouts = new List < string > ( ) ;
228+ var removes = new List < string > ( ) ;
229+
230+ foreach ( var c in changes )
231+ {
232+ if ( c . Index == Models . ChangeState . Deleted )
233+ {
234+ var fullpath = Native . OS . GetAbsPath ( _repo , c . Path ) ;
235+ if ( File . Exists ( fullpath ) )
236+ removes . Add ( c . Path ) ;
237+ }
238+ else if ( c . Index == Models . ChangeState . Renamed )
239+ {
240+ var renamed = Native . OS . GetAbsPath ( _repo , c . OriginalPath ) ;
241+ if ( File . Exists ( renamed ) )
242+ removes . Add ( c . OriginalPath ) ;
243+
244+ checkouts . Add ( c . Path ) ;
245+ }
246+ else
247+ {
248+ checkouts . Add ( c . Path ) ;
249+ }
250+ }
251+
252+ if ( removes . Count > 0 )
253+ await new Commands . Remove ( _repo , removes ) . ExecAsync ( ) ;
254+
255+ if ( checkouts . Count > 0 )
256+ await new Commands . Checkout ( _repo ) . MultipleFilesWithRevisionAsync ( checkouts , _toHead . SHA ) ;
257+ }
258+
137259 public async Task SaveChangesAsPatchAsync ( List < Models . Change > changes , string saveTo )
138260 {
139261 var succ = await Commands . SaveChangesAsPatch . ProcessRevisionCompareChangesAsync ( _repo , changes , _based , _to , saveTo ) ;
@@ -241,6 +363,7 @@ private string GetSHA(object obj)
241363
242364 private string _repo ;
243365 private bool _isLoading = true ;
366+ private bool _canResetFiles = false ;
244367 private string _based = string . Empty ;
245368 private string _to = string . Empty ;
246369 private string _baseName = string . Empty ;
0 commit comments