Skip to content

Commit e8d959d

Browse files
committed
Added DiscardChanges and DiscardAllChanges menu items.
1 parent c335e15 commit e8d959d

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

Assets/Plugins/CandyCoded.GitStatus/Scripts/CustomEditor/Git.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Diagnostics;
66
using System.Linq;
7+
using Debug = UnityEngine.Debug;
78

89
namespace CandyCoded.GitStatus
910
{
@@ -62,6 +63,28 @@ public static string[] UntrackedFiles()
6263

6364
}
6465

66+
public static void DiscardChanges(string path)
67+
{
68+
69+
var process = Process.Start(new ProcessStartInfo
70+
{
71+
FileName = "/usr/local/bin/git",
72+
Arguments = $"checkout {path}",
73+
UseShellExecute = false,
74+
RedirectStandardOutput = true,
75+
RedirectStandardError = true,
76+
CreateNoWindow = true
77+
});
78+
79+
if (process.StandardError.ReadLine().StartsWith("error: pathspec"))
80+
{
81+
82+
Debug.LogError("File not tracked by git.");
83+
84+
}
85+
86+
}
87+
6588
}
6689

6790
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information.
2+
3+
#if UNITY_EDITOR
4+
using System;
5+
using System.IO;
6+
using UnityEditor;
7+
8+
namespace CandyCoded.GitStatus
9+
{
10+
11+
public static class GitMenuItems
12+
{
13+
14+
private const int PRIORITY = 5000;
15+
16+
private static string GetSelectedPath()
17+
{
18+
19+
return Path.Combine(Environment.CurrentDirectory, AssetDatabase.GetAssetPath(Selection.activeObject));
20+
21+
}
22+
23+
[MenuItem("Assets/Discard Changes", false, PRIORITY)]
24+
private static void DiscardChanges()
25+
{
26+
27+
Git.DiscardChanges(GetSelectedPath());
28+
29+
}
30+
31+
[MenuItem("Assets/Discard Changes", true, PRIORITY)]
32+
private static bool ValidateDiscardChanges()
33+
{
34+
35+
return Selection.activeObject && File.Exists(GetSelectedPath());
36+
37+
}
38+
39+
[MenuItem("Assets/Discard All Changes", false, PRIORITY)]
40+
private static void DiscardAllChanges()
41+
{
42+
43+
if (EditorUtility.DisplayDialog(
44+
"Discard all changes",
45+
$"Are you sure you want to discard all changes in {Selection.activeObject.name}?",
46+
"Yes",
47+
"Cancel"))
48+
{
49+
50+
Git.DiscardChanges(GetSelectedPath());
51+
52+
}
53+
54+
}
55+
56+
[MenuItem("Assets/Discard All Changes", true, PRIORITY)]
57+
private static bool ValidateDiscardAllChanges()
58+
{
59+
60+
return Selection.activeObject && Directory.Exists(GetSelectedPath());
61+
62+
}
63+
64+
}
65+
66+
}
67+
68+
#endif

Assets/Plugins/CandyCoded.GitStatus/Scripts/CustomEditor/GitMenuItems.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)