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

Commit 4aeda82

Browse files
Merge pull request #121 from github-for-unity/fixes/changes-view-refactoring
Refactoring and testing changes view tree model objects
2 parents 84ee60e + dc9ee44 commit 4aeda82

File tree

12 files changed

+996
-269
lines changed

12 files changed

+996
-269
lines changed

src/GitHub.Api/Git/GitObjectFactory.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ public GitObjectFactory(IEnvironment environment)
1414

1515
public GitStatusEntry CreateGitStatusEntry(string path, GitFileStatus status, string originalPath = null, bool staged = false)
1616
{
17-
var npath = new NPath(path).MakeAbsolute();
18-
var relativePath = npath.RelativeTo(environment.RepositoryPath);
19-
var projectPath = npath.RelativeTo(environment.UnityProjectPath);
17+
var absolutePath = new NPath(path).MakeAbsolute();
18+
var relativePath = absolutePath.RelativeTo(environment.RepositoryPath);
19+
var projectPath = absolutePath.RelativeTo(environment.UnityProjectPath);
2020

21-
return new GitStatusEntry(relativePath, npath, projectPath, status, originalPath?.ToNPath(), staged);
21+
return new GitStatusEntry(relativePath, absolutePath, projectPath, status, originalPath?.ToNPath(), staged);
2222
}
2323

2424
public GitLock CreateGitLock(string path, string user, int id)

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@
221221
<Compile Include="Extensions\UriExtensions.cs" />
222222
<Compile Include="Platform\Platform.cs" />
223223
<Compile Include="Git\GitCredentialManager.cs" />
224+
<Compile Include="UI\FileTreeNode.cs" />
225+
<Compile Include="UI\GitCommitTarget.cs" />
226+
<Compile Include="UI\TreeBuilder.cs" />
224227
</ItemGroup>
225228
<Choose>
226229
<When Condition="$(Buildtype) == 'Internal'">

src/GitHub.Api/GitHub.Api.csproj.DotSettings

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=platform/@EntryIndexedValue">True</s:Boolean>
1515
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=primitives/@EntryIndexedValue">True</s:Boolean>
1616
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=process/@EntryIndexedValue">True</s:Boolean>
17-
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=tasks/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
17+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=tasks/@EntryIndexedValue">True</s:Boolean>
18+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=ui/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/GitHub.Api/UI/FileTreeNode.cs

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace GitHub.Unity
5+
{
6+
enum CommitState
7+
{
8+
None,
9+
Some,
10+
All
11+
}
12+
13+
class FileTreeNode
14+
{
15+
private List<FileTreeNode> children;
16+
private string path;
17+
private CommitState state;
18+
19+
public object Icon;
20+
public string Label;
21+
public bool Open = true;
22+
public string RepositoryPath;
23+
public GitCommitTarget Target { get; set; }
24+
25+
public FileTreeNode()
26+
{
27+
children = new List<FileTreeNode>();
28+
}
29+
30+
public FileTreeNode(string path)
31+
{
32+
this.path = path ?? "";
33+
Label = this.path;
34+
children = new List<FileTreeNode>();
35+
}
36+
37+
public FileTreeNode Add(FileTreeNode child)
38+
{
39+
children.Add(child);
40+
return child;
41+
}
42+
43+
public CommitState State
44+
{
45+
get
46+
{
47+
if (children == null)
48+
return state;
49+
50+
var commitState = CommitState.None;
51+
if (Target != null)
52+
{
53+
commitState = Target.All ? CommitState.All : Target.Any ? CommitState.Some : CommitState.None;
54+
if (!children.Any())
55+
{
56+
state = commitState;
57+
return state;
58+
}
59+
}
60+
61+
var allCount = children.Count(c => c.State == CommitState.All);
62+
63+
if (allCount == children.Count && (commitState == CommitState.All || Target == null))
64+
{
65+
state = CommitState.All;
66+
return state;
67+
}
68+
69+
if (allCount > 0 || commitState == CommitState.Some)
70+
{
71+
state = CommitState.Some;
72+
return state;
73+
}
74+
75+
var someCount = children.Count(c => c.State == CommitState.Some);
76+
if (someCount > 0 || commitState == CommitState.Some)
77+
{
78+
state = CommitState.Some;
79+
return state;
80+
}
81+
state = CommitState.None;
82+
return state;
83+
}
84+
85+
set
86+
{
87+
if (value == state)
88+
{
89+
return;
90+
}
91+
92+
if (Target != null)
93+
{
94+
if (value == CommitState.None)
95+
{
96+
Target.Clear();
97+
}
98+
else if (value == CommitState.All)
99+
{
100+
Target.All = true;
101+
}
102+
}
103+
104+
state = value;
105+
106+
if (children == null)
107+
{
108+
return;
109+
}
110+
111+
for (var index = 0; index < children.Count; ++index)
112+
{
113+
children[index].State = value;
114+
}
115+
}
116+
}
117+
118+
public string Path
119+
{
120+
get { return path; }
121+
}
122+
123+
public IEnumerable<FileTreeNode> Children
124+
{
125+
get {
126+
if (children == null)
127+
children = new List<FileTreeNode>();
128+
return children;
129+
}
130+
}
131+
132+
private ILogging logger;
133+
protected ILogging Logger
134+
{
135+
get
136+
{
137+
if (logger == null)
138+
logger = Logging.GetLogger(GetType());
139+
return logger;
140+
}
141+
}
142+
}
143+
}

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/GitCommitTarget.cs renamed to src/GitHub.Api/UI/GitCommitTarget.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using System;
2-
using UnityEngine;
32

43
namespace GitHub.Unity
54
{
65
[Serializable]
76
class GitCommitTarget
87
{
9-
[SerializeField] public bool All = false;
8+
public bool All = false;
109

1110
public void Clear()
1211
{

src/GitHub.Api/UI/TreeBuilder.cs

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace GitHub.Unity
6+
{
7+
static class TreeBuilder
8+
{
9+
internal static void BuildChildNode(FileTreeNode parent, FileTreeNode node, List<string> foldedTreeEntries)
10+
{
11+
if (String.IsNullOrEmpty(node.Label))
12+
{
13+
// TODO: We should probably reassign this target onto the parent? Depends on how we want to handle .meta files for folders
14+
return;
15+
}
16+
17+
node.RepositoryPath = parent.RepositoryPath.ToNPath().Combine(node.Label);
18+
parent.Open = !foldedTreeEntries.Contains(parent.RepositoryPath);
19+
20+
// Is this node inside a folder?
21+
var nodePath = node.Label.ToNPath();
22+
if (nodePath.Elements.Count() > 1)
23+
{
24+
// Figure out what the root folder is and chop it from the path
25+
var root = nodePath.Elements.First();
26+
node.Label = new NPath("").Combine(nodePath.Elements.Skip(1).ToArray());
27+
28+
// Look for a branch matching our root in the existing children
29+
var found = false;
30+
foreach (var child in parent.Children)
31+
{
32+
// If we found the branch, continue building from that branch
33+
if (child.Label.Equals(root))
34+
{
35+
found = true;
36+
BuildChildNode(child, node, foldedTreeEntries);
37+
break;
38+
}
39+
}
40+
41+
// No existing branch - we will have to add a new one to build from
42+
if (!found)
43+
{
44+
var p = parent.RepositoryPath.ToNPath().Combine(root);
45+
BuildChildNode(parent.Add(new FileTreeNode(root) { RepositoryPath = p }), node, foldedTreeEntries);
46+
}
47+
}
48+
else if (nodePath.ExtensionWithDot == ".meta")
49+
{
50+
// Look for a branch matching our root in the existing children
51+
var found = false;
52+
foreach (var child in parent.Children)
53+
{
54+
// If we found the branch, continue building from that branch
55+
if (child.Label.Equals(nodePath.Parent.Combine(nodePath.FileNameWithoutExtension)))
56+
{
57+
found = true;
58+
BuildChildNode(child, node, foldedTreeEntries);
59+
break;
60+
}
61+
}
62+
if (!found)
63+
{
64+
parent.Add(node);
65+
}
66+
}
67+
// Not inside a folder - just add this node right here
68+
else
69+
{
70+
parent.Add(node);
71+
}
72+
}
73+
74+
internal static FileTreeNode BuildTreeRoot(IList<GitStatusEntry> newEntries, List<GitStatusEntry> gitStatusEntries, List<GitCommitTarget> gitCommitTargets, List<string> foldedTreeEntries, Func<string, object> iconLoaderFunc = null)
75+
{
76+
Guard.ArgumentNotNullOrEmpty(newEntries, "newEntries");
77+
78+
// Remove what got nuked
79+
for (var index = 0; index < gitStatusEntries.Count;)
80+
{
81+
if (!newEntries.Contains(gitStatusEntries[index]))
82+
{
83+
gitStatusEntries.RemoveAt(index);
84+
gitCommitTargets.RemoveAt(index);
85+
}
86+
else
87+
{
88+
index++;
89+
}
90+
}
91+
92+
// Remove folding state of nuked items
93+
for (var index = 0; index < foldedTreeEntries.Count;)
94+
{
95+
if (!newEntries.Any(e => e.Path.IndexOf(foldedTreeEntries[index]) == 0))
96+
{
97+
foldedTreeEntries.RemoveAt(index);
98+
}
99+
else
100+
{
101+
index++;
102+
}
103+
}
104+
105+
// Add new stuff
106+
for (var index = 0; index < newEntries.Count; ++index)
107+
{
108+
var entry = newEntries[index];
109+
if (!gitStatusEntries.Contains(entry))
110+
{
111+
gitStatusEntries.Add(entry);
112+
gitCommitTargets.Add(new GitCommitTarget());
113+
}
114+
}
115+
116+
// TODO: Filter .meta files - consider adding them as children of the asset or folder they're supporting
117+
// TODO: In stead of completely rebuilding the tree structure, figure out a way to migrate open/closed states from the old tree to the new
118+
// Build tree structure
119+
120+
var tree = new FileTreeNode(FileSystemHelpers.FindCommonPath(gitStatusEntries.Select(e => e.Path)));
121+
tree.RepositoryPath = tree.Path;
122+
123+
for (var index1 = 0; index1 < gitStatusEntries.Count; index1++)
124+
{
125+
var gitStatusEntry = gitStatusEntries[index1];
126+
var entryPath = gitStatusEntry.Path.ToNPath();
127+
if (entryPath.IsChildOf(tree.Path)) entryPath = entryPath.RelativeTo(tree.Path.ToNPath());
128+
129+
var node = new FileTreeNode(entryPath) { Target = gitCommitTargets[index1] };
130+
if (!String.IsNullOrEmpty(gitStatusEntry.ProjectPath))
131+
{
132+
node.Icon = iconLoaderFunc?.Invoke(gitStatusEntry.ProjectPath);
133+
}
134+
135+
BuildChildNode(tree, node, foldedTreeEntries);
136+
}
137+
138+
return tree;
139+
}
140+
}
141+
}

src/UnityExtension/Assets/Editor/GitHub.Unity/GitHub.Unity.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@
9797
<Compile Include="UI\BranchesView.cs" />
9898
<Compile Include="UI\ChangesetTreeView.cs" />
9999
<Compile Include="UI\ChangesView.cs" />
100-
<Compile Include="UI\GitCommitTarget.cs" />
101100
<Compile Include="UI\HistoryView.cs" />
102101
<Compile Include="UI\IView.cs" />
103102
<Compile Include="UI\SettingsView.cs" />
@@ -111,7 +110,7 @@
111110
</ItemGroup>
112111
<Choose>
113112
<When Condition="$(Buildtype) == 'Internal'">
114-
<ItemGroup >
113+
<ItemGroup>
115114
<Compile Include="$(SolutionDir)\script\src\MetricsInitialization.cs">
116115
<Link>Metrics\MetricsInitialization.cs</Link>
117116
</Compile>

0 commit comments

Comments
 (0)