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