forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiscard.cs
More file actions
94 lines (88 loc) · 3.61 KB
/
Copy pathDiscard.cs
File metadata and controls
94 lines (88 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace SourceGit.Commands
{
public static class Discard
{
/// <summary>
/// Discard all local changes (unstaged & staged)
/// </summary>
public static async Task AllAsync(string repo, bool includeUntracked, bool includeIgnored, Models.ICommandLog log)
{
if (includeUntracked)
{
// Untracked paths that contains `.git` file (detached submodule) must be removed manually.
var changes = await new QueryLocalChanges(repo).GetResultAsync().ConfigureAwait(false);
try
{
foreach (var c in changes)
{
if (c.WorkTree == Models.ChangeState.Untracked ||
c.WorkTree == Models.ChangeState.Added ||
c.Index == Models.ChangeState.Added ||
c.Index == Models.ChangeState.Renamed)
{
var fullPath = Path.Combine(repo, c.Path);
if (Directory.Exists(fullPath))
Directory.Delete(fullPath, true);
}
}
}
catch (Exception e)
{
App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}");
}
if (includeIgnored)
await new Clean(repo, Models.CleanMode.All).Use(log).ExecAsync().ConfigureAwait(false);
else
await new Clean(repo, Models.CleanMode.OnlyUntrackedFiles).Use(log).ExecAsync().ConfigureAwait(false);
}
else if (includeIgnored)
{
await new Clean(repo, Models.CleanMode.OnlyIgnoredFiles).Use(log).ExecAsync().ConfigureAwait(false);
}
await new Reset(repo, "HEAD", "--hard").Use(log).ExecAsync().ConfigureAwait(false);
}
/// <summary>
/// Discard selected changes (only unstaged).
/// </summary>
/// <param name="repo"></param>
/// <param name="changes"></param>
/// <param name="log"></param>
public static async Task ChangesAsync(string repo, List<Models.Change> changes, Models.ICommandLog log)
{
var restores = new List<string>();
try
{
foreach (var c in changes)
{
if (c.WorkTree == Models.ChangeState.Untracked || c.WorkTree == Models.ChangeState.Added)
{
var fullPath = Path.Combine(repo, c.Path);
if (Directory.Exists(fullPath))
Directory.Delete(fullPath, true);
else
File.Delete(fullPath);
}
else
{
restores.Add(c.Path);
}
}
}
catch (Exception e)
{
App.RaiseException(repo, $"Failed to discard changes. Reason: {e.Message}");
}
if (restores.Count > 0)
{
var pathSpecFile = Path.GetTempFileName();
await File.WriteAllLinesAsync(pathSpecFile, restores).ConfigureAwait(false);
await new Restore(repo, pathSpecFile, false).Use(log).ExecAsync().ConfigureAwait(false);
File.Delete(pathSpecFile);
}
}
}
}