3
3
using System . Diagnostics ;
4
4
using System . Globalization ;
5
5
using System . Linq ;
6
- using System . Threading . Tasks ;
7
6
8
7
namespace GitHub . Unity
9
8
{
10
9
[ DebuggerDisplay ( "{DebuggerDisplay,nq}" ) ]
11
10
class Repository : IRepository , IEquatable < Repository >
12
11
{
13
- private readonly IGitClient gitClient ;
14
- private readonly IRepositoryManager repositoryManager ;
12
+ private IRepositoryManager repositoryManager ;
13
+ private ConfigBranch ? currentBranch ;
14
+ private ConfigRemote ? currentRemote ;
15
+ private GitStatus currentStatus ;
15
16
16
17
public event Action < GitStatus > OnStatusUpdated ;
17
18
public event Action < string > OnActiveBranchChanged ;
@@ -22,54 +23,45 @@ class Repository : IRepository, IEquatable<Repository>
22
23
public event Action OnRepositoryInfoChanged ;
23
24
24
25
public IEnumerable < GitBranch > LocalBranches => repositoryManager . LocalBranches . Values . Select (
25
- x => new GitBranch ( x . Name , ( x . IsTracking ? ( x . Remote . Value . Name + "/" + x . Name ) : "[None]" ) , x . Name == CurrentBranch ) ) ;
26
+ x => new GitBranch ( x . Name , ( x . IsTracking ? ( x . Remote . Value . Name + "/" + x . Name ) : "[None]" ) , x . Name == CurrentBranch ? . Name ) ) ;
26
27
27
28
public IEnumerable < GitBranch > RemoteBranches => repositoryManager . RemoteBranches . Values . SelectMany (
28
29
x => x . Values ) . Select ( x => new GitBranch ( x . Remote . Value . Name + "/" + x . Name , "[None]" , false ) ) ;
29
30
30
31
/// <summary>
31
32
/// Initializes a new instance of the <see cref="Repository"/> class.
32
33
/// </summary>
33
- /// <param name="gitClient"></param>
34
34
/// <param name="repositoryManager"></param>
35
35
/// <param name="name">The repository name.</param>
36
36
/// <param name="cloneUrl">The repository's clone URL.</param>
37
37
/// <param name="localPath"></param>
38
- public Repository ( IGitClient gitClient , IRepositoryManager repositoryManager , NPath localPath )
38
+ public Repository ( string name , NPath localPath )
39
39
{
40
- Guard . ArgumentNotNull ( repositoryManager , nameof ( repositoryManager ) ) ;
40
+ Guard . ArgumentNotNullOrWhiteSpace ( name , nameof ( name ) ) ;
41
+ Guard . ArgumentNotNull ( localPath , nameof ( localPath ) ) ;
41
42
42
- this . gitClient = gitClient ;
43
- this . repositoryManager = repositoryManager ;
43
+ Name = name ;
44
44
LocalPath = localPath ;
45
- if ( repositoryManager . ActiveBranch . HasValue )
46
- RepositoryManager_OnActiveBranchChanged ( repositoryManager . ActiveBranch ? . Name ) ;
47
- if ( repositoryManager . ActiveRemote . HasValue )
48
- RepositoryManager_OnActiveRemoteChanged ( repositoryManager . ActiveRemote ) ;
49
- SetCloneUrl ( ) ;
45
+ this . User = new User ( ) ;
46
+ }
47
+
48
+ public void Initialize ( IRepositoryManager repositoryManager )
49
+ {
50
+ Guard . ArgumentNotNull ( repositoryManager , nameof ( repositoryManager ) ) ;
50
51
52
+ this . repositoryManager = repositoryManager ;
53
+ repositoryManager . OnLocalBranchListChanged += RepositoryManager_OnLocalBranchListChanged ;
54
+ repositoryManager . OnCommitChanged += RepositoryManager_OnHeadChanged ;
55
+ repositoryManager . OnLocksUpdated += RepositoryManager_OnLocksUpdated ;
51
56
repositoryManager . OnStatusUpdated += RepositoryManager_OnStatusUpdated ;
52
57
repositoryManager . OnActiveBranchChanged += RepositoryManager_OnActiveBranchChanged ;
53
58
repositoryManager . OnActiveRemoteChanged += RepositoryManager_OnActiveRemoteChanged ;
54
- repositoryManager . OnLocalBranchListChanged += RepositoryManager_OnLocalBranchListChanged ;
55
- repositoryManager . OnHeadChanged += RepositoryManager_OnHeadChanged ;
56
- repositoryManager . OnLocksUpdated += RepositoryManager_OnLocksUpdated ;
57
- repositoryManager . OnRemoteOrTrackingChanged += SetCloneUrl ;
58
- }
59
-
60
- public void Initialize ( )
61
- {
62
- User = new User ( ) ;
63
- gitClient . GetConfig ( "user.name" , GitConfigSource . User )
64
- . Then ( ( s , x ) => User . Name = x )
65
- . Then ( gitClient . GetConfig ( "user.email" , GitConfigSource . User ) )
66
- . Then ( ( s , x ) => User . Email = x )
67
- . Start ( ) ;
59
+ repositoryManager . OnGitUserLoaded += user => User = user ;
68
60
}
69
61
70
62
public void Refresh ( )
71
63
{
72
- repositoryManager . Refresh ( ) ;
64
+ repositoryManager ? . Refresh ( ) ;
73
65
}
74
66
75
67
public ITask SetupRemote ( string remote , string remoteUrl )
@@ -88,12 +80,12 @@ public ITask SetupRemote(string remote, string remoteUrl)
88
80
89
81
public ITask Pull ( )
90
82
{
91
- return repositoryManager . Pull ( CurrentRemote . Value . Name , CurrentBranch ) ;
83
+ return repositoryManager . Pull ( CurrentRemote . Value . Name , CurrentBranch ? . Name ) ;
92
84
}
93
85
94
86
public ITask Push ( )
95
87
{
96
- return repositoryManager . Push ( CurrentRemote . Value . Name , CurrentBranch ) ;
88
+ return repositoryManager . Push ( CurrentRemote . Value . Name , CurrentBranch ? . Name ) ;
97
89
}
98
90
99
91
public ITask Fetch ( )
@@ -131,17 +123,19 @@ private void SetCloneUrl()
131
123
OnRepositoryInfoChanged ? . Invoke ( ) ;
132
124
}
133
125
126
+ private void RepositoryManager_OnStatusUpdated ( GitStatus status )
127
+ {
128
+ CurrentStatus = status ;
129
+ }
130
+
134
131
private void RepositoryManager_OnActiveRemoteChanged ( ConfigRemote ? remote )
135
132
{
136
133
CurrentRemote = remote ;
137
- SetCloneUrl ( ) ;
138
- OnActiveRemoteChanged ? . Invoke ( CurrentRemote . HasValue ? CurrentRemote . Value . Name : null ) ;
139
134
}
140
135
141
- private void RepositoryManager_OnActiveBranchChanged ( string branch )
136
+ private void RepositoryManager_OnActiveBranchChanged ( ConfigBranch ? branch )
142
137
{
143
138
CurrentBranch = branch ;
144
- OnActiveBranchChanged ? . Invoke ( CurrentBranch ) ;
145
139
}
146
140
147
141
private void RepositoryManager_OnHeadChanged ( )
@@ -154,12 +148,6 @@ private void RepositoryManager_OnLocalBranchListChanged()
154
148
OnLocalBranchListChanged ? . Invoke ( ) ;
155
149
}
156
150
157
- private void RepositoryManager_OnStatusUpdated ( GitStatus status )
158
- {
159
- CurrentStatus = status ;
160
- OnStatusUpdated ? . Invoke ( CurrentStatus ) ;
161
- }
162
-
163
151
private void RepositoryManager_OnLocksUpdated ( IEnumerable < GitLock > locks )
164
152
{
165
153
CurrentLocks = locks ;
@@ -197,25 +185,51 @@ public bool Equals(IRepository other)
197
185
object . Equals ( LocalPath , other . LocalPath ) ;
198
186
}
199
187
200
- public override string ToString ( )
188
+ public ConfigBranch ? CurrentBranch
201
189
{
202
- return DebuggerDisplay ;
190
+ get { return currentBranch ; }
191
+ set
192
+ {
193
+ if ( currentBranch . HasValue != value . HasValue || ( currentBranch . HasValue && ! currentBranch . Value . Equals ( value . Value ) ) )
194
+ {
195
+ currentBranch = value ;
196
+ Logger . Trace ( "OnActiveBranchChanged: {0}" , value ? . ToString ( ) ?? "NULL" ) ;
197
+ OnActiveBranchChanged ? . Invoke ( CurrentBranch . HasValue ? CurrentBranch . Value . Name : null ) ;
198
+ }
199
+ }
203
200
}
204
-
205
201
/// <summary>
206
202
/// Gets the current branch of the repository.
207
203
/// </summary>
208
- public string CurrentBranch { get ; private set ; }
204
+
205
+ public string CurrentBranchName => currentBranch ? . Name ;
209
206
210
207
/// <summary>
211
208
/// Gets the current remote of the repository.
212
209
/// </summary>
213
- public ConfigRemote ? CurrentRemote { get ; private set ; }
210
+ public ConfigRemote ? CurrentRemote
211
+ {
212
+ get { return currentRemote ; }
213
+ set
214
+ {
215
+ if ( currentRemote . HasValue != value . HasValue || ( currentRemote . HasValue && ! currentRemote . Value . Equals ( value . Value ) ) )
216
+ {
217
+ currentRemote = value ;
218
+ SetCloneUrl ( ) ;
219
+ Logger . Trace ( "OnActiveRemoteChanged: {0}" , value ? . ToString ( ) ?? "NULL" ) ;
220
+ OnActiveRemoteChanged ? . Invoke ( CurrentRemote . HasValue ? CurrentRemote . Value . Name : null ) ;
221
+ }
222
+ }
223
+ }
214
224
215
- public string Name { get ; private set ; }
216
225
public UriString CloneUrl { get ; private set ; }
226
+
227
+ public string Name { get ; private set ; }
228
+
217
229
public NPath LocalPath { get ; private set ; }
230
+
218
231
public string Owner => CloneUrl ? . Owner ?? null ;
232
+
219
233
public bool IsGitHub { get { return HostAddress . IsGitHubDotCom ( CloneUrl ) ; } }
220
234
221
235
internal string DebuggerDisplay => String . Format (
@@ -227,10 +241,19 @@ public override string ToString()
227
241
CloneUrl ,
228
242
LocalPath ,
229
243
CurrentBranch ,
230
- CurrentRemote ? . Name
231
- ) ;
244
+ CurrentRemote ) ;
245
+
246
+ public GitStatus CurrentStatus
247
+ {
248
+ get { return currentStatus ; }
249
+ set
250
+ {
251
+ Logger . Trace ( "OnStatusUpdated: {0}" , value . ToString ( ) ) ;
252
+ currentStatus = value ;
253
+ OnStatusUpdated ? . Invoke ( CurrentStatus ) ;
254
+ }
255
+ }
232
256
233
- public GitStatus CurrentStatus { get ; private set ; }
234
257
public IUser User { get ; set ; }
235
258
public IEnumerable < GitLock > CurrentLocks { get ; private set ; }
236
259
protected static ILogging Logger { get ; } = Logging . GetLogger < Repository > ( ) ;
0 commit comments