|
7 | 7 |
|
8 | 8 | using Avalonia; |
9 | 9 | using Avalonia.Threading; |
10 | | - |
11 | 10 | using CommunityToolkit.Mvvm.ComponentModel; |
12 | 11 |
|
13 | 12 | namespace SourceGit.ViewModels |
@@ -254,48 +253,154 @@ public async Task ResetToThisRevisionAsync(string path) |
254 | 253 | log.Complete(); |
255 | 254 | } |
256 | 255 |
|
| 256 | + public async Task ResetToThisRevisionAsync(Models.Change change) |
| 257 | + { |
| 258 | + var log = _repo.CreateLog($"Reset File to '{_commit.SHA}'"); |
| 259 | + |
| 260 | + if (change.Index == Models.ChangeState.Deleted) |
| 261 | + { |
| 262 | + var fullpath = Native.OS.GetAbsPath(_repo.FullPath, change.Path); |
| 263 | + if (File.Exists(fullpath)) |
| 264 | + await new Commands.Remove(_repo.FullPath, [change.Path]) |
| 265 | + .Use(log) |
| 266 | + .ExecAsync(); |
| 267 | + } |
| 268 | + else if (change.Index == Models.ChangeState.Renamed) |
| 269 | + { |
| 270 | + var old = Native.OS.GetAbsPath(_repo.FullPath, change.OriginalPath); |
| 271 | + if (File.Exists(old)) |
| 272 | + await new Commands.Remove(_repo.FullPath, [change.OriginalPath]) |
| 273 | + .Use(log) |
| 274 | + .ExecAsync(); |
| 275 | + |
| 276 | + await new Commands.Checkout(_repo.FullPath) |
| 277 | + .Use(log) |
| 278 | + .FileWithRevisionAsync(change.Path, _commit.SHA); |
| 279 | + } |
| 280 | + else |
| 281 | + { |
| 282 | + await new Commands.Checkout(_repo.FullPath) |
| 283 | + .Use(log) |
| 284 | + .FileWithRevisionAsync(change.Path, _commit.SHA); |
| 285 | + } |
| 286 | + |
| 287 | + log.Complete(); |
| 288 | + } |
| 289 | + |
257 | 290 | public async Task ResetToParentRevisionAsync(Models.Change change) |
258 | 291 | { |
259 | 292 | var log = _repo.CreateLog($"Reset File to '{_commit.SHA}~1'"); |
260 | 293 |
|
261 | | - if (change.Index == Models.ChangeState.Renamed) |
262 | | - await new Commands.Checkout(_repo.FullPath).Use(log).FileWithRevisionAsync(change.OriginalPath, $"{_commit.SHA}~1"); |
| 294 | + if (change.Index == Models.ChangeState.Added) |
| 295 | + { |
| 296 | + var fullpath = Native.OS.GetAbsPath(_repo.FullPath, change.Path); |
| 297 | + if (File.Exists(fullpath)) |
| 298 | + await new Commands.Remove(_repo.FullPath, [change.Path]) |
| 299 | + .Use(log) |
| 300 | + .ExecAsync(); |
| 301 | + } |
| 302 | + else if (change.Index == Models.ChangeState.Renamed) |
| 303 | + { |
| 304 | + var renamed = Native.OS.GetAbsPath(_repo.FullPath, change.Path); |
| 305 | + if (File.Exists(renamed)) |
| 306 | + await new Commands.Remove(_repo.FullPath, [change.Path]) |
| 307 | + .Use(log) |
| 308 | + .ExecAsync(); |
| 309 | + |
| 310 | + await new Commands.Checkout(_repo.FullPath) |
| 311 | + .Use(log) |
| 312 | + .FileWithRevisionAsync(change.OriginalPath, _commit.SHA); |
| 313 | + } |
| 314 | + else |
| 315 | + { |
| 316 | + await new Commands.Checkout(_repo.FullPath) |
| 317 | + .Use(log) |
| 318 | + .FileWithRevisionAsync(change.Path, _commit.SHA); |
| 319 | + } |
263 | 320 |
|
264 | | - await new Commands.Checkout(_repo.FullPath).Use(log).FileWithRevisionAsync(change.Path, $"{_commit.SHA}~1"); |
265 | 321 | log.Complete(); |
266 | 322 | } |
267 | 323 |
|
268 | 324 | public async Task ResetMultipleToThisRevisionAsync(List<Models.Change> changes) |
269 | 325 | { |
270 | | - var files = new List<string>(); |
| 326 | + var checkouts = new List<string>(); |
| 327 | + var removes = new List<string>(); |
| 328 | + |
271 | 329 | foreach (var c in changes) |
272 | | - files.Add(c.Path); |
| 330 | + { |
| 331 | + if (c.Index == Models.ChangeState.Deleted) |
| 332 | + { |
| 333 | + var fullpath = Native.OS.GetAbsPath(_repo.FullPath, c.Path); |
| 334 | + if (File.Exists(fullpath)) |
| 335 | + removes.Add(c.Path); |
| 336 | + } |
| 337 | + else if (c.Index == Models.ChangeState.Renamed) |
| 338 | + { |
| 339 | + var old = Native.OS.GetAbsPath(_repo.FullPath, c.OriginalPath); |
| 340 | + if (File.Exists(old)) |
| 341 | + removes.Add(c.OriginalPath); |
| 342 | + |
| 343 | + checkouts.Add(c.Path); |
| 344 | + } |
| 345 | + else |
| 346 | + { |
| 347 | + checkouts.Add(c.Path); |
| 348 | + } |
| 349 | + } |
273 | 350 |
|
274 | 351 | var log = _repo.CreateLog($"Reset Files to '{_commit.SHA}'"); |
275 | | - await new Commands.Checkout(_repo.FullPath).Use(log).MultipleFilesWithRevisionAsync(files, _commit.SHA); |
| 352 | + |
| 353 | + if (removes.Count > 0) |
| 354 | + await new Commands.Remove(_repo.FullPath, removes) |
| 355 | + .Use(log) |
| 356 | + .ExecAsync(); |
| 357 | + |
| 358 | + if (checkouts.Count > 0) |
| 359 | + await new Commands.Checkout(_repo.FullPath) |
| 360 | + .Use(log) |
| 361 | + .MultipleFilesWithRevisionAsync(checkouts, _commit.SHA); |
| 362 | + |
276 | 363 | log.Complete(); |
277 | 364 | } |
278 | 365 |
|
279 | 366 | public async Task ResetMultipleToParentRevisionAsync(List<Models.Change> changes) |
280 | 367 | { |
281 | | - var renamed = new List<string>(); |
282 | | - var modified = new List<string>(); |
| 368 | + var checkouts = new List<string>(); |
| 369 | + var removes = new List<string>(); |
283 | 370 |
|
284 | 371 | foreach (var c in changes) |
285 | 372 | { |
286 | | - if (c.Index == Models.ChangeState.Renamed) |
287 | | - renamed.Add(c.OriginalPath); |
| 373 | + if (c.Index == Models.ChangeState.Added) |
| 374 | + { |
| 375 | + var fullpath = Native.OS.GetAbsPath(_repo.FullPath, c.Path); |
| 376 | + if (File.Exists(fullpath)) |
| 377 | + removes.Add(c.Path); |
| 378 | + } |
| 379 | + else if (c.Index == Models.ChangeState.Renamed) |
| 380 | + { |
| 381 | + var renamed = Native.OS.GetAbsPath(_repo.FullPath, c.Path); |
| 382 | + if (File.Exists(renamed)) |
| 383 | + removes.Add(c.Path); |
| 384 | + |
| 385 | + checkouts.Add(c.OriginalPath); |
| 386 | + } |
288 | 387 | else |
289 | | - modified.Add(c.Path); |
| 388 | + { |
| 389 | + checkouts.Add(c.Path); |
| 390 | + } |
290 | 391 | } |
291 | 392 |
|
292 | 393 | var log = _repo.CreateLog($"Reset Files to '{_commit.SHA}~1'"); |
293 | 394 |
|
294 | | - if (modified.Count > 0) |
295 | | - await new Commands.Checkout(_repo.FullPath).Use(log).MultipleFilesWithRevisionAsync(modified, $"{_commit.SHA}~1"); |
| 395 | + if (removes.Count > 0) |
| 396 | + await new Commands.Remove(_repo.FullPath, removes) |
| 397 | + .Use(log) |
| 398 | + .ExecAsync(); |
296 | 399 |
|
297 | | - if (renamed.Count > 0) |
298 | | - await new Commands.Checkout(_repo.FullPath).Use(log).MultipleFilesWithRevisionAsync(renamed, $"{_commit.SHA}~1"); |
| 400 | + if (checkouts.Count > 0) |
| 401 | + await new Commands.Checkout(_repo.FullPath) |
| 402 | + .Use(log) |
| 403 | + .MultipleFilesWithRevisionAsync(checkouts, $"{_commit.SHA}~1"); |
299 | 404 |
|
300 | 405 | log.Complete(); |
301 | 406 | } |
|
0 commit comments