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

Commit afec12d

Browse files
committed
File History
1 parent 5ae520f commit afec12d

File tree

11 files changed

+359
-10
lines changed

11 files changed

+359
-10
lines changed

src/GitHub.Api/Application/ApiClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,13 @@ private GitHubUser GetValidatedGitHubUser(Connection keychainConnection, IKeycha
283283
}
284284
}
285285

286-
class GitHubUser
286+
public class GitHubUser
287287
{
288288
public string Name { get; set; }
289289
public string Login { get; set; }
290290
}
291291

292-
class GitHubRepository
292+
public class GitHubRepository
293293
{
294294
public string Name { get; set; }
295295
public string CloneUrl { get; set; }

src/GitHub.Api/Application/IApiClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace GitHub.Unity
44
{
5-
interface IApiClient
5+
public interface IApiClient
66
{
77
HostAddress HostAddress { get; }
88
UriString OriginalUrl { get; }
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace GitHub.Unity
22
{
3-
class Organization
3+
public class Organization
44
{
55
public string Name { get; set; }
66
public string Login { get; set; }
77
}
8-
}
8+
}

src/GitHub.Api/Git/GitClient.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ public interface IGitClient
199199
/// <returns>String output of git command</returns>
200200
ITask<string> DiscardAll(IOutputProcessor<string> processor = null);
201201

202+
/// <summary>
203+
/// Executes at least one `git checkout` command to checkout files at the given changeset
204+
/// </summary>
205+
/// <param name="changeset">The md5 of the changeset</param>
206+
/// <param name="files">The files to check out</param>
207+
/// <param name="processor">A custom output processor instance</param>
208+
/// <returns>String output of git command</returns>
209+
ITask<string> CheckoutVersion(string changeset, IEnumerable<string> files, IOutputProcessor<string> processor = null);
210+
202211
/// <summary>
203212
/// Executes at least one `git reset HEAD` command to remove files from the git index.
204213
/// </summary>
@@ -241,6 +250,13 @@ public interface IGitClient
241250
/// <returns><see cref="List&lt;T&gt;"/> of <see cref="GitLogEntry"/> output</returns>
242251
ITask<List<GitLogEntry>> Log(BaseOutputListProcessor<GitLogEntry> processor = null);
243252

253+
/// <summary>
254+
/// Executes `git log -- <file>` to get the history of a specific file.
255+
/// </summary>
256+
/// <param name="processor">A custom output processor instance</param>
257+
/// <returns><see cref="List&lt;T&gt;"/> of <see cref="GitLogEntry"/> output</returns>
258+
ITask<List<GitLogEntry>> LogFile(NPath file, BaseOutputListProcessor<GitLogEntry> processor = null);
259+
244260
/// <summary>
245261
/// Executes `git --version` to get the git version.
246262
/// </summary>
@@ -332,6 +348,17 @@ public ITask<List<GitLogEntry>> Log(BaseOutputListProcessor<GitLogEntry> process
332348
.Then((success, list) => success ? list : new List<GitLogEntry>());
333349
}
334350

351+
///<inheritdoc/>
352+
public ITask<List<GitLogEntry>> LogFile(NPath file, BaseOutputListProcessor<GitLogEntry> processor = null)
353+
{
354+
return new GitLogTask(file, new GitObjectFactory(environment), cancellationToken, processor)
355+
.Configure(processManager)
356+
.Catch(exception => exception is ProcessException &&
357+
exception.Message.StartsWith("fatal: your current branch") &&
358+
exception.Message.EndsWith("does not have any commits yet"))
359+
.Then((success, list) => success ? list : new List<GitLogEntry>());
360+
}
361+
335362
///<inheritdoc/>
336363
public ITask<TheVersion> Version(IOutputProcessor<TheVersion> processor = null)
337364
{
@@ -549,6 +576,13 @@ public ITask<string> DiscardAll(IOutputProcessor<string> processor = null)
549576
.Configure(processManager);
550577
}
551578

579+
///<inheritdoc/>
580+
public ITask<string> CheckoutVersion(string changeset, IEnumerable<string> files, IOutputProcessor<string> processor = null)
581+
{
582+
return new GitCheckoutTask(changeset, files, cancellationToken, processor)
583+
.Configure(processManager);
584+
}
585+
552586
///<inheritdoc/>
553587
public ITask<string> Remove(IList<string> files,
554588
IOutputProcessor<string> processor = null)

src/GitHub.Api/Git/Tasks/GitCheckoutTask.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,29 @@ public GitCheckoutTask(CancellationToken token,
3030
arguments = "checkout -- .";
3131
}
3232

33+
public GitCheckoutTask(
34+
string changeset,
35+
IEnumerable<string> files,
36+
CancellationToken token,
37+
IOutputProcessor<string> processor = null) : base(token, processor ?? new SimpleOutputProcessor())
38+
{
39+
Guard.ArgumentNotNull(files, "files");
40+
Name = TaskName;
41+
42+
arguments = "checkout ";
43+
arguments += changeset;
44+
arguments += " -- ";
45+
46+
foreach (var file in files)
47+
{
48+
arguments += " \"" + file.ToNPath().ToString(SlashMode.Forward) + "\"";
49+
}
50+
51+
Message = "Checking out files at rev " + changeset.Substring(0, 7);
52+
}
53+
3354
public override string ProcessArguments { get { return arguments; } }
3455
public override TaskAffinity Affinity { get { return TaskAffinity.Exclusive; } }
35-
public override string Message { get; set; } = "Checking out branch...";
56+
public override string Message { get; set; } = "Checking out files...";
3657
}
3758
}

src/GitHub.Api/Git/Tasks/GitLogTask.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,31 @@ namespace GitHub.Unity
55
class GitLogTask : ProcessTaskWithListOutput<GitLogEntry>
66
{
77
private const string TaskName = "git log";
8+
private const string baseArguments = @"-c i18n.logoutputencoding=utf8 -c core.quotepath=false log --pretty=format:""%H%n%P%n%aN%n%aE%n%aI%n%cN%n%cE%n%cI%n%B---GHUBODYEND---"" --name-status";
9+
private readonly string arguments;
810

911
public GitLogTask(IGitObjectFactory gitObjectFactory,
1012
CancellationToken token, BaseOutputListProcessor<GitLogEntry> processor = null)
1113
: base(token, processor ?? new LogEntryOutputProcessor(gitObjectFactory))
1214
{
1315
Name = TaskName;
16+
arguments = baseArguments;
17+
}
18+
19+
public GitLogTask(NPath file,
20+
IGitObjectFactory gitObjectFactory,
21+
CancellationToken token, BaseOutputListProcessor<GitLogEntry> processor = null)
22+
: base(token, processor ?? new LogEntryOutputProcessor(gitObjectFactory))
23+
{
24+
Name = TaskName;
25+
arguments = baseArguments;
26+
arguments += " -- ";
27+
arguments += " \"" + file.ToString(SlashMode.Forward) + "\"";
1428
}
1529

1630
public override string ProcessArguments
1731
{
18-
get { return @"-c i18n.logoutputencoding=utf8 -c core.quotepath=false log --pretty=format:""%H%n%P%n%aN%n%aE%n%aI%n%cN%n%cE%n%cI%n%B---GHUBODYEND---"" --name-status"; }
32+
get { return arguments; }
1933
}
2034
public override string Message { get; set; } = "Loading the history...";
2135
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace GitHub.Unity
99
{
10-
abstract class BaseWindow : EditorWindow, IView
10+
public abstract class BaseWindow : EditorWindow, IView
1111
{
1212
[NonSerialized] private bool initialized = false;
1313
[NonSerialized] private IUser cachedUser;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEditor;
5+
6+
namespace GitHub.Unity
7+
{
8+
public class ContextMenu
9+
{
10+
[MenuItem("Assets/Git/History", false)]
11+
private static void GitFileHistory()
12+
{
13+
if (Selection.assetGUIDs != null)
14+
{
15+
int maxWindowsToOpen = 10;
16+
int windowsOpened = 0;
17+
foreach(var guid in Selection.assetGUIDs)
18+
{
19+
var assetPath = AssetDatabase.GUIDToAssetPath(guid);
20+
FileHistoryWindow.OpenWindow(assetPath);
21+
windowsOpened++;
22+
if (windowsOpened >= maxWindowsToOpen)
23+
{
24+
break;
25+
}
26+
}
27+
}
28+
}
29+
30+
[MenuItem("Assets/Git/History", true)]
31+
private static bool GitFileHistoryValidation()
32+
{
33+
return Selection.assetGUIDs != null && Selection.assetGUIDs.Length > 0;
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)