Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 5570e69

Browse files
Merge pull request #12 from github-for-unity/ui/i-made-a-huge-mistake
Adding functionality to revert a commit in HistoryView
2 parents 6483e01 + 5ec7d8b commit 5570e69

File tree

7 files changed

+75
-0
lines changed

7 files changed

+75
-0
lines changed

src/GitHub.Api/Git/GitClient.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ ITask<string> Pull(string remote, string branch,
3232
ITask<string> Push(string remote, string branch,
3333
IOutputProcessor<string> processor = null);
3434

35+
ITask<string> Revert(string changeset,
36+
IOutputProcessor<string> processor = null);
37+
3538
ITask<string> Fetch(string remote,
3639
IOutputProcessor<string> processor = null);
3740

@@ -198,6 +201,12 @@ public ITask<string> Push(string remote, string branch,
198201
.Configure(processManager);
199202
}
200203

204+
public ITask<string> Revert(string changeset, IOutputProcessor<string> processor = null)
205+
{
206+
return new GitRevertTask(changeset, cancellationToken, processor)
207+
.Configure(processManager);
208+
}
209+
201210
public ITask<string> Fetch(string remote,
202211
IOutputProcessor<string> processor = null)
203212
{

src/GitHub.Api/Git/IRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface IRepository : IEquatable<IRepository>
1212
ITask SetupRemote(string remoteName, string remoteUrl);
1313
ITask Pull();
1414
ITask Push();
15+
ITask Revert(string changeset);
1516
ITask ListLocks();
1617
ITask RequestLock(string file);
1718
ITask ReleaseLock(string file, bool force);

src/GitHub.Api/Git/Repository.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public ITask Push()
8888
return repositoryManager.Push(CurrentRemote.Value.Name, CurrentBranch);
8989
}
9090

91+
public ITask Revert(string changeset)
92+
{
93+
return repositoryManager.Revert(changeset);
94+
}
95+
9196
public ITask ListLocks()
9297
{
9398
return repositoryManager.ListLocks(false);

src/GitHub.Api/Git/RepositoryManager.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ interface IRepositoryManager : IDisposable
3434
ITask Fetch(string remote);
3535
ITask Pull(string remote, string branch);
3636
ITask Push(string remote, string branch);
37+
ITask Revert(string changeset);
3738
ITask RemoteAdd(string remote, string url);
3839
ITask RemoteRemove(string remote);
3940
ITask RemoteChange(string remote, string url);
@@ -234,6 +235,12 @@ public ITask Push(string remote, string branch)
234235
return HookupHandlers(task);
235236
}
236237

238+
public ITask Revert(string changeset)
239+
{
240+
var task = GitClient.Revert(changeset);
241+
return HookupHandlers(task);
242+
}
243+
237244
public ITask RemoteAdd(string remote, string url)
238245
{
239246
var task = GitClient.RemoteAdd(remote, url);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Threading;
2+
3+
namespace GitHub.Unity
4+
{
5+
class GitRevertTask : ProcessTask<string>
6+
{
7+
private readonly string arguments;
8+
9+
public GitRevertTask(string changeset,
10+
CancellationToken token, IOutputProcessor<string> processor = null)
11+
: base(token, processor ?? new SimpleOutputProcessor())
12+
{
13+
Guard.ArgumentNotNull(changeset, "changeset");
14+
15+
arguments = $"revert --no-edit {changeset}";
16+
}
17+
18+
public override string Name { get { return "git revert"; } }
19+
public override string ProcessArguments { get { return arguments; } }
20+
public override TaskAffinity Affinity { get { return TaskAffinity.Exclusive; } }
21+
}
22+
}

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
<Compile Include="Git\GitObjectFactory.cs" />
116116
<Compile Include="Git\GitStatusEntry.cs" />
117117
<Compile Include="Git\IGitObjectFactory.cs" />
118+
<Compile Include="Git\Tasks\GitRevertTask.cs" />
118119
<Compile Include="IO\FileSystemHelpers.cs" />
119120
<Compile Include="Application\IApplicationManager.cs" />
120121
<Compile Include="NewTaskSystem\ActionTask.cs" />

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,28 @@ private void CullHistory()
557557
EntryHeight + 1, 0, history.Count);
558558
}
559559

560+
private void RevertCommit()
561+
{
562+
var selection = history[selectionIndex];
563+
564+
var dialogTitle = "Revert commit";
565+
var dialogBody = string.Format(@"Are you sure you want to revert the following commit:""{0}""", selection.Summary);
566+
567+
if (EditorUtility.DisplayDialog(dialogTitle, dialogBody, "Revert", "Cancel"))
568+
{
569+
Repository
570+
.Revert(selection.CommitID)
571+
.FinallyInUI((success, e) => {
572+
if (!success)
573+
{
574+
EditorUtility.DisplayDialog(dialogTitle,
575+
"Error reverting commit: " + e.Message, Localization.Cancel);
576+
}
577+
})
578+
.Start();
579+
}
580+
}
581+
560582
private bool HistoryEntry(GitLogEntry entry, LogEntryState state, bool selected)
561583
{
562584
var entryRect = GUILayoutUtility.GetRect(Styles.HistoryEntryHeight, Styles.HistoryEntryHeight);
@@ -620,6 +642,14 @@ private bool HistoryEntry(GitLogEntry entry, LogEntryState state, bool selected)
620642
GUI.DrawTexture(normalIndicatorRect, Styles.DotIcon);
621643
}
622644
}
645+
else if (Event.current.type == EventType.ContextClick && entryRect.Contains(Event.current.mousePosition))
646+
{
647+
GenericMenu menu = new GenericMenu();
648+
menu.AddItem(new GUIContent("Revert"), false, RevertCommit);
649+
menu.ShowAsContext();
650+
651+
Event.current.Use();
652+
}
623653
else if (Event.current.type == EventType.MouseDown && entryRect.Contains(Event.current.mousePosition))
624654
{
625655
Event.current.Use();

0 commit comments

Comments
 (0)