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

Commit 03e6346

Browse files
Merge branch 'enhancements/changes-tree-view' into enhancements/removing-old-tree-code
# Conflicts: # src/GitHub.Api/GitHub.Api.csproj # src/tests/UnitTests/UnitTests.csproj
2 parents 0f5d2ab + 5f011ad commit 03e6346

File tree

10 files changed

+627
-226
lines changed

10 files changed

+627
-226
lines changed

src/GitHub.Api/Git/GitBranch.cs

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

33
namespace GitHub.Unity
44
{
5-
public interface ITreeData
6-
{
7-
string Name { get; }
8-
bool IsActive { get; }
9-
}
10-
115
[Serializable]
12-
public struct GitBranch : ITreeData
6+
public struct GitBranch
137
{
148
public static GitBranch Default = new GitBranch();
159

src/GitHub.Api/Git/GitStatusEntry.cs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,46 @@ namespace GitHub.Unity
55
[Serializable]
66
public struct GitStatusEntry
77
{
8-
public string Path;
9-
public string FullPath;
10-
public string ProjectPath;
11-
public string OriginalPath;
12-
public GitFileStatus Status;
13-
public bool Staged;
8+
public static GitStatusEntry Default = new GitStatusEntry();
9+
10+
public string path;
11+
public string fullPath;
12+
public string projectPath;
13+
public string originalPath;
14+
public GitFileStatus status;
15+
public bool staged;
1416

1517
public GitStatusEntry(string path, string fullPath, string projectPath,
1618
GitFileStatus status,
1719
string originalPath = null, bool staged = false)
1820
{
19-
Path = path;
20-
Status = status;
21-
FullPath = fullPath;
22-
ProjectPath = projectPath;
23-
OriginalPath = originalPath;
24-
Staged = staged;
21+
Guard.ArgumentNotNullOrWhiteSpace(path, "path");
22+
Guard.ArgumentNotNullOrWhiteSpace(fullPath, "fullPath");
23+
Guard.ArgumentNotNullOrWhiteSpace(projectPath, "projectPath");
24+
25+
this.path = path;
26+
this.status = status;
27+
this.fullPath = fullPath;
28+
this.projectPath = projectPath;
29+
this.originalPath = originalPath;
30+
this.staged = staged;
2531
}
2632

33+
public string Path => path;
34+
35+
public string FullPath => fullPath;
36+
37+
public string ProjectPath => projectPath;
38+
39+
public string OriginalPath => originalPath;
40+
41+
public GitFileStatus Status => status;
42+
43+
public bool Staged => staged;
44+
2745
public override string ToString()
2846
{
29-
return String.Format("Path:'{0}' Status:'{1}' FullPath:'{2}' ProjectPath:'{3}' OriginalPath:'{4}' Staged:'{5}'", Path, Status, FullPath, ProjectPath, OriginalPath, Staged);
47+
return $"Path:'{Path}' Status:'{Status}' FullPath:'{FullPath}' ProjectPath:'{ProjectPath}' OriginalPath:'{OriginalPath}' Staged:'{Staged}'";
3048
}
3149
}
3250
}

src/GitHub.Api/Git/TreeData.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
3+
namespace GitHub.Unity
4+
{
5+
public interface ITreeData
6+
{
7+
string Path { get; }
8+
bool IsActive { get; }
9+
string CustomStringTag { get;}
10+
int CustomIntTag { get; }
11+
}
12+
13+
[Serializable]
14+
public struct GitBranchTreeData : ITreeData
15+
{
16+
public static GitBranchTreeData Default = new GitBranchTreeData(Unity.GitBranch.Default);
17+
18+
public GitBranch GitBranch;
19+
20+
public GitBranchTreeData(GitBranch gitBranch)
21+
{
22+
GitBranch = gitBranch;
23+
}
24+
25+
public string Path => GitBranch.Name;
26+
public bool IsActive => GitBranch.IsActive;
27+
28+
public string CustomStringTag => null;
29+
30+
public int CustomIntTag => 0;
31+
}
32+
33+
[Serializable]
34+
public struct GitStatusEntryTreeData : ITreeData
35+
{
36+
public static GitStatusEntryTreeData Default = new GitStatusEntryTreeData(GitStatusEntry.Default);
37+
38+
public GitStatusEntry gitStatusEntry;
39+
40+
public GitStatusEntryTreeData(GitStatusEntry gitStatusEntry)
41+
{
42+
this.gitStatusEntry = gitStatusEntry;
43+
}
44+
45+
public string Path => gitStatusEntry.ProjectPath;
46+
public bool IsActive => false;
47+
public GitStatusEntry GitStatusEntry => gitStatusEntry;
48+
49+
public string CustomStringTag => gitStatusEntry.ProjectPath;
50+
51+
public int CustomIntTag => (int)gitStatusEntry.Status;
52+
}
53+
}

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<Compile Include="Git\Tasks\GitAheadBehindStatusTask.cs" />
108108
<Compile Include="Git\Tasks\GitLfsVersionTask.cs" />
109109
<Compile Include="Git\Tasks\GitVersionTask.cs" />
110+
<Compile Include="Git\TreeData.cs" />
110111
<Compile Include="Git\ValidateGitInstallResult.cs" />
111112
<Compile Include="Helpers\AssemblyResources.cs" />
112113
<Compile Include="Authentication\IKeychain.cs" />
@@ -235,6 +236,7 @@
235236
<Compile Include="Extensions\UriExtensions.cs" />
236237
<Compile Include="Platform\Platform.cs" />
237238
<Compile Include="Git\GitCredentialManager.cs" />
239+
<Compile Include="UI\TreeLoader.cs" />
238240
</ItemGroup>
239241
<Choose>
240242
<When Condition="$(Buildtype) == 'Internal'">
@@ -291,6 +293,7 @@
291293
<ItemGroup>
292294
<None Include="packages.config" />
293295
</ItemGroup>
296+
<ItemGroup />
294297
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
295298
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
296299
Other similar extension points exist, see Microsoft.Common.targets.

src/GitHub.Api/UI/TreeLoader.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace GitHub.Unity
6+
{
7+
public interface ITree
8+
{
9+
void AddNode(string path, string label, int level, bool isFolder, bool isActive, bool isHidden, bool isCollapsed, bool isSelected, int customIntTag = 0, string customStringTag = (string)null);
10+
void Clear();
11+
HashSet<string> GetCollapsedFolders();
12+
string Title { get; }
13+
bool DisplayRootNode { get; }
14+
bool IsCheckable { get; }
15+
string PathSeparator { get; }
16+
string SelectedNodePath { get; }
17+
}
18+
19+
public static class TreeLoader
20+
{
21+
public static void Load(ITree tree, IEnumerable<ITreeData> treeDatas)
22+
{
23+
var collapsedFolders = tree.GetCollapsedFolders();
24+
var selectedNodePath = tree.SelectedNodePath;
25+
26+
tree.Clear();
27+
28+
var displayRootLevel = tree.DisplayRootNode ? 1 : 0;
29+
30+
var isSelected = selectedNodePath != null && tree.Title == selectedNodePath;
31+
tree.AddNode(path: tree.Title, label: tree.Title, level: -1 + displayRootLevel, isFolder: true, isActive: false, isHidden: false, isCollapsed: false, isSelected: isSelected);
32+
33+
var hideChildren = false;
34+
var hideChildrenBelowLevel = 0;
35+
36+
var folders = new HashSet<string>();
37+
38+
foreach (var treeData in treeDatas)
39+
{
40+
var parts = treeData.Path.Split(new[] { tree.PathSeparator }, StringSplitOptions.None);
41+
for (int i = 0; i < parts.Length; i++)
42+
{
43+
var label = parts[i];
44+
var level = i + 1;
45+
var nodePath = String.Join(tree.PathSeparator, parts, 0, level);
46+
var isFolder = i < parts.Length - 1;
47+
var alreadyExists = folders.Contains(nodePath);
48+
if (!alreadyExists)
49+
{
50+
var nodeIsHidden = false;
51+
if (hideChildren)
52+
{
53+
if (level <= hideChildrenBelowLevel)
54+
{
55+
hideChildren = false;
56+
}
57+
else
58+
{
59+
nodeIsHidden = true;
60+
}
61+
}
62+
63+
var nodeIsCollapsed = false;
64+
string customStringTag = null;
65+
var customIntTag = 0;
66+
67+
if (isFolder)
68+
{
69+
folders.Add(nodePath);
70+
71+
if (collapsedFolders.Contains(nodePath))
72+
{
73+
nodeIsCollapsed = true;
74+
75+
if (!hideChildren)
76+
{
77+
hideChildren = true;
78+
hideChildrenBelowLevel = level;
79+
}
80+
}
81+
}
82+
else
83+
{
84+
customStringTag = treeData.CustomStringTag;
85+
customIntTag = treeData.CustomIntTag;
86+
}
87+
88+
isSelected = selectedNodePath != null && nodePath == selectedNodePath;
89+
tree.AddNode(path: nodePath, label: label, level: i + displayRootLevel, isFolder: isFolder, isActive: treeData.IsActive, isHidden: nodeIsHidden, isCollapsed: nodeIsCollapsed, isSelected: isSelected, customStringTag: customStringTag, customIntTag: customIntTag);
90+
}
91+
}
92+
}
93+
}
94+
}
95+
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,10 @@ private void BuildTree()
153153
if (treeLocals == null)
154154
{
155155
treeLocals = new BranchesTree();
156+
treeLocals.Title = LocalTitle;
156157

157158
treeRemotes = new BranchesTree();
159+
treeRemotes.Title = RemoteTitle;
158160
treeRemotes.IsRemote = true;
159161

160162
UpdateTreeIcons();
@@ -163,8 +165,8 @@ private void BuildTree()
163165
localBranches.Sort(CompareBranches);
164166
remoteBranches.Sort(CompareBranches);
165167

166-
treeLocals.Load(localBranches.Cast<ITreeData>(), LocalTitle);
167-
treeRemotes.Load(remoteBranches.Cast<ITreeData>(), RemoteTitle);
168+
TreeLoader.Load(treeLocals, localBranches.Select(branch => (ITreeData) new GitBranchTreeData(branch)));
169+
TreeLoader.Load(treeRemotes, remoteBranches.Select(branch => (ITreeData) new GitBranchTreeData(branch)));
168170
Redraw();
169171
}
170172

@@ -208,7 +210,7 @@ private void OnButtonBarGUI()
208210
{
209211
if (GUILayout.Button(DeleteBranchButton, EditorStyles.miniButton, GUILayout.ExpandWidth(false)))
210212
{
211-
DeleteLocalBranch(treeLocals.SelectedNode.Name);
213+
DeleteLocalBranch(treeLocals.SelectedNode.Path);
212214
}
213215
}
214216
EditorGUI.EndDisabledGroup();
@@ -278,7 +280,7 @@ private void OnButtonBarGUI()
278280
// Effectuate create
279281
if (createBranch)
280282
{
281-
GitClient.CreateBranch(newBranchName, treeLocals.SelectedNode.Name)
283+
GitClient.CreateBranch(newBranchName, treeLocals.SelectedNode.Path)
282284
.FinallyInUI((success, e) =>
283285
{
284286
if (success)
@@ -326,7 +328,7 @@ private void OnTreeGUI(Rect rect)
326328
if(node.IsActive)
327329
return;
328330

329-
SwitchBranch(node.Name);
331+
SwitchBranch(node.Path);
330332
},
331333
node => {
332334
if (node.IsFolder)
@@ -354,7 +356,7 @@ private void OnTreeGUI(Rect rect)
354356
if (node.IsFolder)
355357
return;
356358

357-
CheckoutRemoteBranch(node.Name);
359+
CheckoutRemoteBranch(node.Path);
358360
},
359361
node => {
360362
if (node.IsFolder)
@@ -391,11 +393,11 @@ private GenericMenu CreateContextMenuForLocalBranchNode(TreeNode node)
391393
else
392394
{
393395
genericMenu.AddItem(deleteGuiContent, false, () => {
394-
DeleteLocalBranch(node.Name);
396+
DeleteLocalBranch(node.Path);
395397
});
396398

397399
genericMenu.AddItem(switchGuiContent, false, () => {
398-
SwitchBranch(node.Name);
400+
SwitchBranch(node.Path);
399401
});
400402
}
401403

@@ -409,7 +411,7 @@ private GenericMenu CreateContextMenuForRemoteBranchNode(TreeNode node)
409411
var checkoutGuiContent = new GUIContent(CheckoutBranchContextMenuLabel);
410412

411413
genericMenu.AddItem(checkoutGuiContent, false, () => {
412-
CheckoutRemoteBranch(node.Name);
414+
CheckoutRemoteBranch(node.Path);
413415
});
414416

415417
return genericMenu;

0 commit comments

Comments
 (0)