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

Commit 9d0a23b

Browse files
Merge branch 'fixes/application-cache-initial-times' into fixes/split-status-cache
# Conflicts: # src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs
2 parents 6a9b00f + d1f99de commit 9d0a23b

File tree

4 files changed

+73
-55
lines changed

4 files changed

+73
-55
lines changed

src/GitHub.Api/Git/GitConfig.cs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,56 @@ namespace GitHub.Unity
99
[Serializable]
1010
public struct ConfigRemote
1111
{
12-
public string Name;
13-
public string Url;
12+
public static ConfigRemote Default = new ConfigRemote(String.Empty, String.Empty);
13+
14+
public string name;
15+
public string url;
16+
17+
public ConfigRemote(string name, string url)
18+
{
19+
this.name = name;
20+
this.url = url;
21+
}
22+
23+
public string Name => name;
24+
25+
public string Url => url;
1426

1527
public override string ToString()
1628
{
17-
return String.Format("{{Remote {0} {1}}}", Name, Url);
29+
return $"{{Remote {Name} {Url}}}";
1830
}
1931
}
2032

2133
[Serializable]
2234
public struct ConfigBranch
2335
{
24-
public string Name;
25-
public ConfigRemote? Remote;
36+
public static ConfigBranch Default = new ConfigBranch(String.Empty);
37+
38+
public string name;
39+
public ConfigRemote remote;
40+
41+
public ConfigBranch(string name)
42+
{
43+
this.name = name;
44+
remote = ConfigRemote.Default;
45+
}
46+
47+
public ConfigBranch(string name, ConfigRemote? remote)
48+
{
49+
this.name = name;
50+
this.remote = remote ?? ConfigRemote.Default;
51+
}
52+
2653
public bool IsTracking => Remote.HasValue;
2754

55+
public string Name => name;
56+
57+
public ConfigRemote? Remote => Equals(remote, ConfigRemote.Default) ? (ConfigRemote?) null : remote;
58+
2859
public override string ToString()
2960
{
30-
return String.Format("{{Branch {0} {1}}}", Name, Remote?.ToString() ?? "Untracked");
61+
return $"{{Branch {Name} {Remote?.ToString() ?? "Untracked"}}}";
3162
}
3263
}
3364

@@ -74,11 +105,7 @@ public IEnumerable<ConfigBranch> GetBranches()
74105
return groups
75106
.Where(x => x.Key == "branch")
76107
.SelectMany(x => x.Value)
77-
.Select(x => new ConfigBranch
78-
{
79-
Name = x.Key,
80-
Remote = GetRemote(x.Value.TryGetString("remote"))
81-
});
108+
.Select(x => new ConfigBranch(x.Key, GetRemote(x.Value.TryGetString("remote"))));
82109
}
83110

84111
public IEnumerable<ConfigRemote> GetRemotes()
@@ -87,11 +114,7 @@ public IEnumerable<ConfigRemote> GetRemotes()
87114
.Where(x => x.Key == "remote")
88115
.SelectMany(x => x.Value)
89116
.Where(x => x.Value.TryGetString("url") != null)
90-
.Select(x => new ConfigRemote
91-
{
92-
Name = x.Key,
93-
Url = x.Value.TryGetString("url")
94-
});
117+
.Select(x => new ConfigRemote(x.Key, x.Value.TryGetString("url")));
95118
}
96119

97120
public ConfigRemote? GetRemote(string remote)
@@ -100,11 +123,7 @@ public IEnumerable<ConfigRemote> GetRemotes()
100123
.Where(x => x.Key == "remote")
101124
.SelectMany(x => x.Value)
102125
.Where(x => x.Key == remote && x.Value.TryGetString("url") != null)
103-
.Select(x => new ConfigRemote
104-
{
105-
Name = x.Key,
106-
Url = x.Value.GetString("url")
107-
} as ConfigRemote?)
126+
.Select(x => new ConfigRemote(x.Key,x.Value.GetString("url")) as ConfigRemote?)
108127
.FirstOrDefault();
109128
}
110129

@@ -114,11 +133,7 @@ public IEnumerable<ConfigRemote> GetRemotes()
114133
.Where(x => x.Key == "branch")
115134
.SelectMany(x => x.Value)
116135
.Where(x => x.Key == branch)
117-
.Select(x => new ConfigBranch
118-
{
119-
Name = x.Key,
120-
Remote = GetRemote(x.Value.TryGetString("remote"))
121-
} as ConfigBranch?)
136+
.Select(x => new ConfigBranch(x.Key,GetRemote(x.Value.TryGetString("remote"))) as ConfigBranch?)
122137
.FirstOrDefault();
123138
}
124139

src/GitHub.Api/Git/Repository.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ public void Initialize(IGitClient client)
677677
Logger.Trace("Initialize");
678678

679679
gitClient = client;
680+
cacheContainer.GitUserCache.ValidateData();
680681
}
681682

682683
public void SetNameAndEmail(string name, string email)
@@ -730,20 +731,23 @@ private void HandleUserCacheUpdatedEvent(CacheUpdateEvent cacheUpdateEvent)
730731

731732
private void UpdateUserAndEmail()
732733
{
733-
if (gitClient != null)
734-
{
735-
Logger.Trace("UpdateUserAndEmail");
734+
Logger.Trace("UpdateUserAndEmail");
736735

737-
gitClient.GetConfigUserAndEmail()
738-
.ThenInUI((success, value) =>
739-
{
740-
if (success)
741-
{
742-
Name = value.Name;
743-
Email = value.Email;
744-
}
745-
}).Start();
736+
if (gitClient == null)
737+
{
738+
Logger.Trace("GitClient is null");
739+
return;
746740
}
741+
742+
gitClient.GetConfigUserAndEmail()
743+
.ThenInUI((success, value) =>
744+
{
745+
if (success)
746+
{
747+
Name = value.Name;
748+
Email = value.Email;
749+
}
750+
}).Start();
747751
}
748752

749753
protected static ILogging Logger { get; } = Logging.GetLogger<User>();

src/GitHub.Api/Git/RepositoryManager.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Threading;
55
using System.Threading.Tasks;
6+
using Octokit;
67

78
namespace GitHub.Unity
89
{
@@ -409,7 +410,7 @@ private void GetCurrentBranchAndRemote(out ConfigBranch? branch, out ConfigRemot
409410

410411
if (!branch.HasValue)
411412
{
412-
branch = new ConfigBranch { Name = branchName };
413+
branch = new ConfigBranch(branchName);
413414
}
414415
}
415416

@@ -512,7 +513,7 @@ private void UpdateLocalBranches(Dictionary<string, ConfigBranch> branches, NPat
512513
configBranches.Where(x => x.Name == branchName).Select(x => x as ConfigBranch?).FirstOrDefault();
513514
if (!branch.HasValue)
514515
{
515-
branch = new ConfigBranch { Name = branchName };
516+
branch = new ConfigBranch(branchName);
516517
}
517518
branches.Add(branchName, branch.Value);
518519
}
@@ -541,7 +542,7 @@ private void UpdateRemoteBranches()
541542
.Select(x => x.RelativeTo(basedir))
542543
.Select(x => x.ToString(SlashMode.Forward)))
543544
{
544-
branchList.Add(branch, new ConfigBranch { Name = branch, Remote = remotes[remote] });
545+
branchList.Add(branch, new ConfigBranch(branch, remotes[remote]));
545546
}
546547

547548
remoteBranches.Add(remote, branchList);

src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationCache.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,10 @@ private bool ValidateInitialized()
148148
var notInitialized = ApplicationCache.Instance.FirstRunAt > InitializedAt;
149149
if (notInitialized)
150150
{
151-
Logger.Trace("Initialized");
152-
InitializedAt = DateTimeOffset.Now;
153-
Save(true);
151+
Logger.Trace("Not Initialized");
154152

155153
if (invalidOnFirstRun)
156154
{
157-
Logger.Trace("FirstRun Invalidation");
158155
InvalidateData();
159156
}
160157
}
@@ -166,11 +163,15 @@ public void InvalidateData()
166163
{
167164
Logger.Trace("Invalidate");
168165
CacheInvalidated.SafeInvoke();
169-
SaveData(DateTimeOffset.Now, false);
170166
}
171167

172168
protected void SaveData(DateTimeOffset now, bool isUpdated)
173169
{
170+
if (InitializedAt == DateTimeOffset.MinValue)
171+
{
172+
InitializedAt = now;
173+
}
174+
174175
if (isUpdated)
175176
{
176177
LastUpdatedAt = now;
@@ -498,9 +499,6 @@ public override TimeSpan DataTimeout
498499
[Location("cache/branches.yaml", LocationAttribute.Location.LibraryFolder)]
499500
sealed class BranchCache : ManagedCacheBase<BranchCache>, IBranchCache
500501
{
501-
public static readonly ConfigBranch DefaultConfigBranch = new ConfigBranch();
502-
public static readonly ConfigRemote DefaultConfigRemote = new ConfigRemote();
503-
504502
[SerializeField] private string lastUpdatedAtString = DateTimeOffset.MinValue.ToString(Constants.Iso8601Format);
505503
[SerializeField] private string lastVerifiedAtString = DateTimeOffset.MinValue.ToString(Constants.Iso8601Format);
506504
[SerializeField] private string initializedAtString = DateTimeOffset.MinValue.ToString(Constants.Iso8601Format);
@@ -524,7 +522,7 @@ public ConfigRemote? CurrentConfigRemote
524522
get
525523
{
526524
ValidateData();
527-
return gitConfigRemote.Equals(DefaultConfigRemote) ? (ConfigRemote?)null : gitConfigRemote;
525+
return gitConfigRemote.Equals(ConfigRemote.Default) ? (ConfigRemote?)null : gitConfigRemote;
528526
}
529527
set
530528
{
@@ -535,7 +533,7 @@ public ConfigRemote? CurrentConfigRemote
535533

536534
if (!Nullable.Equals(gitConfigRemote, value))
537535
{
538-
gitConfigRemote = value ?? DefaultConfigRemote;
536+
gitConfigRemote = value ?? ConfigRemote.Default;
539537
isUpdated = true;
540538
}
541539

@@ -548,7 +546,7 @@ public ConfigBranch? CurentConfigBranch
548546
get
549547
{
550548
ValidateData();
551-
return gitConfigBranch.Equals(DefaultConfigBranch) ? (ConfigBranch?)null : gitConfigBranch;
549+
return gitConfigBranch.Equals(ConfigBranch.Default) ? (ConfigBranch?)null : gitConfigBranch;
552550
}
553551
set
554552
{
@@ -559,7 +557,7 @@ public ConfigBranch? CurentConfigBranch
559557

560558
if (!Nullable.Equals(gitConfigBranch, value))
561559
{
562-
gitConfigBranch = value ?? DefaultConfigBranch;
560+
gitConfigBranch = value ?? ConfigBranch.Default;
563561
isUpdated = true;
564562
}
565563

@@ -674,7 +672,7 @@ public void AddLocalBranch(string branch)
674672
if (!LocalConfigBranches.ContainsKey(branch))
675673
{
676674
var now = DateTimeOffset.Now;
677-
LocalConfigBranches.Add(branch, new ConfigBranch { Name = branch });
675+
LocalConfigBranches.Add(branch, new ConfigBranch(branch));
678676
Logger.Trace("AddLocalBranch {0} branch:{1} ", now, branch);
679677
SaveData(now, true);
680678
}
@@ -692,7 +690,7 @@ public void AddRemoteBranch(string remote, string branch)
692690
if (!branchList.ContainsKey(branch))
693691
{
694692
var now = DateTimeOffset.Now;
695-
branchList.Add(branch, new ConfigBranch { Name = branch, Remote = ConfigRemotes[remote] });
693+
branchList.Add(branch, new ConfigBranch(branch,ConfigRemotes[remote]));
696694
Logger.Trace("AddRemoteBranch {0} remote:{1} branch:{2} ", now, remote, branch);
697695
SaveData(now, true);
698696
}

0 commit comments

Comments
 (0)