8
8
namespace GitHub . Unity
9
9
{
10
10
[ DebuggerDisplay ( "{DebuggerDisplay,nq}" ) ]
11
- class Repository : IRepository , IEquatable < Repository >
11
+ class Repository : IEquatable < Repository > , IRepository
12
12
{
13
- private IRepositoryManager repositoryManager ;
14
13
private ConfigBranch ? currentBranch ;
14
+ private IList < GitLock > currentLocks ;
15
15
private ConfigRemote ? currentRemote ;
16
16
private GitStatus currentStatus ;
17
17
private Dictionary < string , ConfigBranch > localBranches = new Dictionary < string , ConfigBranch > ( ) ;
18
- private IEnumerable < GitLock > locks ;
19
18
private Dictionary < string , Dictionary < string , ConfigBranch > > remoteBranches = new Dictionary < string , Dictionary < string , ConfigBranch > > ( ) ;
20
19
private Dictionary < string , ConfigRemote > remotes ;
21
-
22
- public event Action < GitStatus > OnStatusChanged ;
20
+ private IRepositoryManager repositoryManager ;
23
21
public event Action < string > OnCurrentBranchChanged ;
24
22
public event Action < string > OnCurrentRemoteChanged ;
23
+ public event Action OnLocalBranchChanged ;
25
24
public event Action OnLocalBranchListChanged ;
26
- public event Action OnRemoteBranchListChanged ;
27
- public event Action OnHeadChanged ;
28
25
public event Action < IEnumerable < GitLock > > OnLocksChanged ;
26
+ public event Action OnRemoteBranchListChanged ;
29
27
public event Action OnRepositoryInfoChanged ;
30
28
31
- public IEnumerable < GitBranch > LocalBranches => localBranches . Values . Select (
32
- x => new GitBranch ( x . Name , ( x . IsTracking ? ( x . Remote . Value . Name + "/" + x . Name ) : "[None]" ) , x . Name == CurrentBranch ? . Name ) ) ;
33
-
34
- public IEnumerable < GitBranch > RemoteBranches => remoteBranches . Values . SelectMany (
35
- x => x . Values ) . Select ( x => new GitBranch ( x . Remote . Value . Name + "/" + x . Name , "[None]" , false ) ) ;
29
+ public event Action < GitStatus > OnStatusChanged ;
36
30
37
31
/// <summary>
38
32
/// Initializes a new instance of the <see cref="Repository"/> class.
39
33
/// </summary>
40
- /// <param name="repositoryManager"></param>
41
34
/// <param name="name">The repository name.</param>
42
- /// <param name="cloneUrl">The repository's clone URL.</param>
43
35
/// <param name="localPath"></param>
44
36
public Repository ( string name , NPath localPath )
45
37
{
@@ -57,13 +49,13 @@ public void Initialize(IRepositoryManager repositoryManager)
57
49
58
50
this . repositoryManager = repositoryManager ;
59
51
60
- repositoryManager . OnCurrentBranchUpdated += RepositoryManager_OnCurrentBranchUpdated ;
61
- repositoryManager . OnCurrentRemoteUpdated += RepositoryManager_OnCurrentRemoteUpdated ;
62
- repositoryManager . OnStatusUpdated += RepositoryManager_OnStatusUpdated ;
63
- repositoryManager . OnLocksUpdated += RepositoryManager_OnLocksUpdated ;
64
- repositoryManager . OnLocalBranchListUpdated += RepositoryManager_OnLocalBranchListUpdated ;
65
- repositoryManager . OnRemoteBranchListUpdated += RepositoryManager_OnRemoteBranchListUpdated ;
66
- repositoryManager . OnLocalBranchUpdated += RepositoryManager_OnLocalBranchUpdated ;
52
+ repositoryManager . OnCurrentBranchUpdated += branch => CurrentBranch = branch ;
53
+ repositoryManager . OnCurrentRemoteUpdated += remote => CurrentRemote = remote ;
54
+ repositoryManager . OnStatusUpdated += status => CurrentStatus = status ;
55
+ repositoryManager . OnLocksUpdated += locks => CurrentLocks = locks ;
56
+ repositoryManager . OnLocalBranchListUpdated += OnRepositoryManager_OnLocalBranchListUpdated ;
57
+ repositoryManager . OnRemoteBranchListUpdated += OnRepositoryManager_OnRemoteBranchListUpdated ;
58
+ repositoryManager . OnLocalBranchUpdated += OnRepositoryManager_OnLocalBranchUpdated ;
67
59
repositoryManager . OnLocalBranchAdded += RepositoryManager_OnLocalBranchAdded ;
68
60
repositoryManager . OnLocalBranchRemoved += RepositoryManager_OnLocalBranchRemoved ;
69
61
repositoryManager . OnRemoteBranchAdded += RepositoryManager_OnRemoteBranchAdded ;
@@ -122,7 +114,7 @@ public ITask Fetch()
122
114
{
123
115
return repositoryManager . Fetch ( CurrentRemote . Value . Name ) ;
124
116
}
125
-
117
+
126
118
public ITask Revert ( string changeset )
127
119
{
128
120
return repositoryManager . Revert ( changeset ) ;
@@ -145,65 +137,109 @@ public ITask ReleaseLock(string file, bool force)
145
137
return repositoryManager . UnlockFile ( file , force ) ;
146
138
}
147
139
148
- private void UpdateRepositoryInfo ( )
140
+ /// <summary>
141
+ /// Note: We don't consider CloneUrl a part of the hash code because it can change during the lifetime
142
+ /// of a repository. Equals takes care of any hash collisions because of this
143
+ /// </summary>
144
+ /// <returns></returns>
145
+ public override int GetHashCode ( )
149
146
{
150
- if ( CurrentRemote . HasValue )
151
- {
152
- CloneUrl = new UriString ( CurrentRemote . Value . Url ) ;
153
- Name = CloneUrl . RepositoryName ;
154
- Logger . Trace ( "CloneUrl: {0}" , CloneUrl . ToString ( ) ) ;
155
- }
156
- else
157
- {
158
- CloneUrl = null ;
159
- Name = LocalPath . FileName ;
160
- Logger . Trace ( "CloneUrl: [NULL]" ) ;
161
- }
162
-
163
- OnRepositoryInfoChanged ? . Invoke ( ) ;
147
+ return LocalPath . GetHashCode ( ) ;
164
148
}
165
149
166
- private void RepositoryManager_OnStatusUpdated ( GitStatus status )
150
+ public override bool Equals ( object obj )
167
151
{
168
- CurrentStatus = status ;
152
+ if ( ReferenceEquals ( this , obj ) )
153
+ return true ;
154
+ var other = obj as Repository ;
155
+ return Equals ( other ) ;
169
156
}
170
157
171
- private void RepositoryManager_OnLocksUpdated ( IEnumerable < GitLock > locks )
158
+ public bool Equals ( Repository other )
172
159
{
173
- CurrentLocks = locks ;
174
- OnLocksChanged ? . Invoke ( CurrentLocks ) ;
160
+ return Equals ( ( IRepository ) other ) ;
175
161
}
176
162
177
- private void RepositoryManager_OnCurrentBranchUpdated ( ConfigBranch ? branch )
163
+ public bool Equals ( IRepository other )
178
164
{
179
- CurrentBranch = branch ;
165
+ if ( ReferenceEquals ( this , other ) )
166
+ return true ;
167
+ return other != null &&
168
+ object . Equals ( LocalPath , other . LocalPath ) ;
180
169
}
181
170
182
- private void RepositoryManager_OnCurrentRemoteUpdated ( ConfigRemote ? remote )
171
+ private void OnRepositoryManager_OnLocalBranchUpdated ( string name )
183
172
{
184
- CurrentRemote = remote ;
173
+ if ( name == currentBranch ? . Name )
174
+ {
175
+ OnLocalBranchChanged ? . Invoke ( ) ;
176
+ Refresh ( ) ;
177
+ }
185
178
}
186
179
187
- private void RepositoryManager_OnRemoteBranchListUpdated ( Dictionary < string , Dictionary < string , ConfigBranch > > branches )
180
+ private void OnRepositoryManager_OnRemoteBranchListUpdated ( Dictionary < string , ConfigRemote > updatedRemotes , Dictionary < string , Dictionary < string , ConfigBranch > > branches )
188
181
{
189
- Logger . Trace ( "RemoteBranchListUpdated" ) ;
182
+ remotes = updatedRemotes ;
183
+
184
+ Remotes = remotes . Select ( pair => new GitRemote {
185
+ Name = pair . Value . Name ,
186
+ Url = pair . Value . Url
187
+ } ) . ToArray ( ) ;
190
188
191
189
remoteBranches = branches ;
192
- remotes = branches
193
- . Where ( pair => pair . Value . Any ( ) )
194
- . ToDictionary ( pair => pair . Key , pair => pair . Value . First ( ) . Value . Remote . Value ) ;
195
190
191
+ RemoteBranches = remoteBranches . Values
192
+ . SelectMany ( x => x . Values )
193
+ . Select ( x =>
194
+ {
195
+ var name = x . Remote . Value . Name + "/" + x . Name ;
196
+ return new GitBranch ( name , "[None]" , false ) ;
197
+ } ) . ToArray ( ) ;
198
+
199
+
200
+ Logger . Trace ( "OnRemoteBranchListChanged" ) ;
196
201
OnRemoteBranchListChanged ? . Invoke ( ) ;
197
202
}
198
203
199
- private void RepositoryManager_OnLocalBranchListUpdated ( Dictionary < string , ConfigBranch > branches )
204
+ private void OnRepositoryManager_OnLocalBranchListUpdated ( Dictionary < string , ConfigBranch > branches )
200
205
{
201
- Logger . Trace ( "LocalBranchListUpdated" ) ;
202
-
203
206
localBranches = branches ;
207
+
208
+ LocalBranches = localBranches . Values . Select ( x =>
209
+ {
210
+ var argName = x . Name ;
211
+
212
+ var tracking = x . IsTracking
213
+ ? x . Remote . Value . Name + "/" + argName
214
+ : "[None]" ;
215
+
216
+ var active = argName == CurrentBranch ? . Name ;
217
+
218
+ return new GitBranch ( argName , tracking , active ) ;
219
+ } ) . ToArray ( ) ;
220
+
221
+ Logger . Trace ( "OnLocalBranchListChanged" ) ;
204
222
OnLocalBranchListChanged ? . Invoke ( ) ;
205
223
}
206
224
225
+ private void UpdateRepositoryInfo ( )
226
+ {
227
+ if ( CurrentRemote . HasValue )
228
+ {
229
+ CloneUrl = new UriString ( CurrentRemote . Value . Url ) ;
230
+ Name = CloneUrl . RepositoryName ;
231
+ Logger . Trace ( "CloneUrl: {0}" , CloneUrl . ToString ( ) ) ;
232
+ }
233
+ else
234
+ {
235
+ CloneUrl = null ;
236
+ Name = LocalPath . FileName ;
237
+ Logger . Trace ( "CloneUrl: [NULL]" ) ;
238
+ }
239
+
240
+ OnRepositoryInfoChanged ? . Invoke ( ) ;
241
+ }
242
+
207
243
private void RepositoryManager_OnLocalBranchRemoved ( string name )
208
244
{
209
245
if ( localBranches . ContainsKey ( name ) )
@@ -235,16 +271,6 @@ private void RepositoryManager_OnLocalBranchAdded(string name)
235
271
}
236
272
}
237
273
238
- private void RepositoryManager_OnLocalBranchUpdated ( string name )
239
- {
240
- if ( name == currentBranch ? . Name )
241
- {
242
- // commit of current branch changed, trigger OnHeadChanged
243
- OnHeadChanged ? . Invoke ( ) ;
244
- repositoryManager . Refresh ( ) ;
245
- }
246
- }
247
-
248
274
private void RepositoryManager_OnRemoteBranchAdded ( string remote , string name )
249
275
{
250
276
Dictionary < string , ConfigBranch > branchList ;
@@ -265,7 +291,7 @@ private void RepositoryManager_OnRemoteBranchAdded(string remote, string name)
265
291
Logger . Warning ( "Remote {0} is not found" , remote ) ;
266
292
}
267
293
}
268
-
294
+
269
295
private void RepositoryManager_OnRemoteBranchRemoved ( string remote , string name )
270
296
{
271
297
Dictionary < string , ConfigBranch > branchList ;
@@ -287,36 +313,11 @@ private void RepositoryManager_OnRemoteBranchRemoved(string remote, string name)
287
313
}
288
314
}
289
315
290
- /// <summary>
291
- /// Note: We don't consider CloneUrl a part of the hash code because it can change during the lifetime
292
- /// of a repository. Equals takes care of any hash collisions because of this
293
- /// </summary>
294
- /// <returns></returns>
295
- public override int GetHashCode ( )
296
- {
297
- return LocalPath . GetHashCode ( ) ;
298
- }
316
+ public IList < GitRemote > Remotes { get ; private set ; }
299
317
300
- public override bool Equals ( object obj )
301
- {
302
- if ( ReferenceEquals ( this , obj ) )
303
- return true ;
304
- var other = obj as Repository ;
305
- return Equals ( other ) ;
306
- }
318
+ public IList < GitBranch > LocalBranches { get ; private set ; }
307
319
308
- public bool Equals ( Repository other )
309
- {
310
- return ( Equals ( ( IRepository ) other ) ) ;
311
- }
312
-
313
- public bool Equals ( IRepository other )
314
- {
315
- if ( ReferenceEquals ( this , other ) )
316
- return true ;
317
- return other != null &&
318
- object . Equals ( LocalPath , other . LocalPath ) ;
319
- }
320
+ public IList < GitBranch > RemoteBranches { get ; private set ; }
320
321
321
322
public ConfigBranch ? CurrentBranch
322
323
{
@@ -331,6 +332,7 @@ public ConfigBranch? CurrentBranch
331
332
}
332
333
}
333
334
}
335
+
334
336
/// <summary>
335
337
/// Gets the current branch of the repository.
336
338
/// </summary>
@@ -381,14 +383,25 @@ public GitStatus CurrentStatus
381
383
get { return currentStatus ; }
382
384
set
383
385
{
384
- Logger . Trace ( "OnStatusUpdated: {0}" , value . ToString ( ) ) ;
385
386
currentStatus = value ;
386
- OnStatusChanged ? . Invoke ( CurrentStatus ) ;
387
+ Logger . Trace ( "OnStatusChanged: {0}" , value . ToString ( ) ) ;
388
+ OnStatusChanged ? . Invoke ( value ) ;
387
389
}
388
390
}
389
391
390
392
public IUser User { get ; set ; }
391
- public IEnumerable < GitLock > CurrentLocks { get ; private set ; }
393
+
394
+ public IList < GitLock > CurrentLocks
395
+ {
396
+ get { return currentLocks ; }
397
+ private set
398
+ {
399
+ Logger . Trace ( "OnLocksChanged: {0}" , value . ToString ( ) ) ;
400
+ currentLocks = value ;
401
+ OnLocksChanged ? . Invoke ( value ) ;
402
+ }
403
+ }
404
+
392
405
protected static ILogging Logger { get ; } = Logging . GetLogger < Repository > ( ) ;
393
406
}
394
407
@@ -401,12 +414,12 @@ public interface IUser
401
414
[ Serializable ]
402
415
class User : IUser
403
416
{
404
- public string Name { get ; set ; }
405
- public string Email { get ; set ; }
406
-
407
417
public override string ToString ( )
408
418
{
409
419
return String . Format ( "Name: {0} Email: {1}" , Name , Email ) ;
410
420
}
421
+
422
+ public string Name { get ; set ; }
423
+ public string Email { get ; set ; }
411
424
}
412
425
}
0 commit comments