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,43 @@ 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 ( ) ;
50
-
51
- repositoryManager . OnStatusUpdated += RepositoryManager_OnStatusUpdated ;
52
- repositoryManager . OnActiveBranchChanged += RepositoryManager_OnActiveBranchChanged ;
53
- repositoryManager . OnActiveRemoteChanged += RepositoryManager_OnActiveRemoteChanged ;
54
- repositoryManager . OnLocalBranchListChanged += RepositoryManager_OnLocalBranchListChanged ;
55
- repositoryManager . OnHeadChanged += RepositoryManager_OnHeadChanged ;
56
- repositoryManager . OnLocksUpdated += RepositoryManager_OnLocksUpdated ;
57
- repositoryManager . OnRemoteOrTrackingChanged += SetCloneUrl ;
58
45
}
59
46
60
- public void Initialize ( )
47
+ public void Initialize ( IRepositoryManager repositoryManager )
61
48
{
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 ( ) ;
49
+ Guard . ArgumentNotNull ( repositoryManager , nameof ( repositoryManager ) ) ;
50
+
51
+ this . repositoryManager = repositoryManager ;
52
+ repositoryManager . OnLocalBranchListChanged += RepositoryManager_OnLocalBranchListChanged ;
53
+ repositoryManager . OnCommitChanged += RepositoryManager_OnHeadChanged ;
54
+ repositoryManager . OnLocksUpdated += RepositoryManager_OnLocksUpdated ;
55
+ repositoryManager . OnStatusUpdated += status => CurrentStatus = status ;
56
+ repositoryManager . OnActiveBranchChanged += branch => CurrentBranch = branch ;
57
+ repositoryManager . OnActiveRemoteChanged += remote => CurrentRemote = remote ;
68
58
}
69
59
70
60
public void Refresh ( )
71
61
{
72
- repositoryManager . Refresh ( ) ;
62
+ repositoryManager ? . Refresh ( ) ;
73
63
}
74
64
75
65
public ITask SetupRemote ( string remote , string remoteUrl )
@@ -88,12 +78,12 @@ public ITask SetupRemote(string remote, string remoteUrl)
88
78
89
79
public ITask Pull ( )
90
80
{
91
- return repositoryManager . Pull ( CurrentRemote . Value . Name , CurrentBranch ) ;
81
+ return repositoryManager . Pull ( CurrentRemote . Value . Name , CurrentBranch ? . Name ) ;
92
82
}
93
83
94
84
public ITask Push ( )
95
85
{
96
- return repositoryManager . Push ( CurrentRemote . Value . Name , CurrentBranch ) ;
86
+ return repositoryManager . Push ( CurrentRemote . Value . Name , CurrentBranch ? . Name ) ;
97
87
}
98
88
99
89
public ITask Fetch ( )
@@ -154,12 +144,6 @@ private void RepositoryManager_OnLocalBranchListChanged()
154
144
OnLocalBranchListChanged ? . Invoke ( ) ;
155
145
}
156
146
157
- private void RepositoryManager_OnStatusUpdated ( GitStatus status )
158
- {
159
- CurrentStatus = status ;
160
- OnStatusUpdated ? . Invoke ( CurrentStatus ) ;
161
- }
162
-
163
147
private void RepositoryManager_OnLocksUpdated ( IEnumerable < GitLock > locks )
164
148
{
165
149
CurrentLocks = locks ;
@@ -197,23 +181,55 @@ public bool Equals(IRepository other)
197
181
object . Equals ( LocalPath , other . LocalPath ) ;
198
182
}
199
183
200
- public override string ToString ( )
184
+ public ConfigBranch ? CurrentBranch
201
185
{
202
- return DebuggerDisplay ;
186
+ get { return currentBranch ; }
187
+ set
188
+ {
189
+ if ( currentBranch . HasValue != value . HasValue || ( currentBranch . HasValue && ! currentBranch . Value . Equals ( value . Value ) ) )
190
+ {
191
+ currentBranch = value ;
192
+ Logger . Trace ( "OnActiveBranchChanged: {0}" , value ? . ToString ( ) ?? "NULL" ) ;
193
+ OnActiveBranchChanged ? . Invoke ( currentBranch ? . Name ) ;
194
+ }
195
+ }
203
196
}
204
-
205
197
/// <summary>
206
198
/// Gets the current branch of the repository.
207
199
/// </summary>
208
- public string CurrentBranch { get ; private set ; }
200
+
201
+ public string CurrentBranchName => currentBranch ? . Name ;
209
202
210
203
/// <summary>
211
204
/// Gets the current remote of the repository.
212
205
/// </summary>
213
- public ConfigRemote ? CurrentRemote { get ; private set ; }
206
+ public ConfigRemote ? CurrentRemote
207
+ {
208
+ get { return currentRemote ; }
209
+ set
210
+ {
211
+ if ( currentRemote . HasValue != value . HasValue || ( currentRemote . HasValue && ! currentRemote . Value . Equals ( value . Value ) ) )
212
+ {
213
+ currentRemote = value ;
214
+ Logger . Trace ( "OnActiveRemoteChanged: {0}" , value ? . ToString ( ) ?? "NULL" ) ;
215
+ OnActiveRemoteChanged ? . Invoke ( value ? . Name ) ;
216
+ }
217
+ }
218
+ }
219
+
220
+ public UriString CloneUrl
221
+ {
222
+ get
223
+ {
224
+ if ( CurrentRemote . HasValue && CurrentRemote . Value . Url != null )
225
+ return new UriString ( CurrentRemote . Value . Url ) . ToRepositoryUrl ( ) ;
226
+
227
+ return null ;
228
+ }
229
+ }
230
+
214
231
215
232
public string Name { get ; private set ; }
216
- public UriString CloneUrl { get ; private set ; }
217
233
public NPath LocalPath { get ; private set ; }
218
234
public string Owner => CloneUrl ? . Owner ?? null ;
219
235
public bool IsGitHub { get { return HostAddress . IsGitHubDotCom ( CloneUrl ) ; } }
@@ -226,11 +242,19 @@ public override string ToString()
226
242
Name ,
227
243
CloneUrl ,
228
244
LocalPath ,
229
- CurrentBranch ,
230
- CurrentRemote ? . Name
231
- ) ;
245
+ GetHashCode ( ) ) ;
246
+
247
+ public GitStatus CurrentStatus
248
+ {
249
+ get { return currentStatus ; }
250
+ set
251
+ {
252
+ Logger . Trace ( "OnStatusUpdated: {0}" , value . ToString ( ) ) ;
253
+ currentStatus = value ;
254
+ OnStatusUpdated ? . Invoke ( value ) ;
255
+ }
256
+ }
232
257
233
- public GitStatus CurrentStatus { get ; private set ; }
234
258
public IUser User { get ; set ; }
235
259
public IEnumerable < GitLock > CurrentLocks { get ; private set ; }
236
260
protected static ILogging Logger { get ; } = Logging . GetLogger < Repository > ( ) ;
0 commit comments