33using System . Diagnostics ;
44using System . Globalization ;
55using System . Linq ;
6- using System . Threading . Tasks ;
76
87namespace GitHub . Unity
98{
109 [ DebuggerDisplay ( "{DebuggerDisplay,nq}" ) ]
1110 class Repository : IRepository , IEquatable < Repository >
1211 {
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 ;
1516
1617 public event Action < GitStatus > OnStatusUpdated ;
1718 public event Action < string > OnActiveBranchChanged ;
@@ -22,54 +23,45 @@ class Repository : IRepository, IEquatable<Repository>
2223 public event Action OnRepositoryInfoChanged ;
2324
2425 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 ) ) ;
2627
2728 public IEnumerable < GitBranch > RemoteBranches => repositoryManager . RemoteBranches . Values . SelectMany (
2829 x => x . Values ) . Select ( x => new GitBranch ( x . Remote . Value . Name + "/" + x . Name , "[None]" , false ) ) ;
2930
3031 /// <summary>
3132 /// Initializes a new instance of the <see cref="Repository"/> class.
3233 /// </summary>
33- /// <param name="gitClient"></param>
3434 /// <param name="repositoryManager"></param>
3535 /// <param name="name">The repository name.</param>
3636 /// <param name="cloneUrl">The repository's clone URL.</param>
3737 /// <param name="localPath"></param>
38- public Repository ( IGitClient gitClient , IRepositoryManager repositoryManager , NPath localPath )
38+ public Repository ( string name , NPath localPath )
3939 {
40- Guard . ArgumentNotNull ( repositoryManager , nameof ( repositoryManager ) ) ;
40+ Guard . ArgumentNotNullOrWhiteSpace ( name , nameof ( name ) ) ;
41+ Guard . ArgumentNotNull ( localPath , nameof ( localPath ) ) ;
4142
42- this . gitClient = gitClient ;
43- this . repositoryManager = repositoryManager ;
43+ Name = name ;
4444 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 ) ) ;
5051
52+ this . repositoryManager = repositoryManager ;
53+ repositoryManager . OnLocalBranchListChanged += RepositoryManager_OnLocalBranchListChanged ;
54+ repositoryManager . OnCommitChanged += RepositoryManager_OnHeadChanged ;
55+ repositoryManager . OnLocksUpdated += RepositoryManager_OnLocksUpdated ;
5156 repositoryManager . OnStatusUpdated += RepositoryManager_OnStatusUpdated ;
5257 repositoryManager . OnActiveBranchChanged += RepositoryManager_OnActiveBranchChanged ;
5358 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 ;
6860 }
6961
7062 public void Refresh ( )
7163 {
72- repositoryManager . Refresh ( ) ;
64+ repositoryManager ? . Refresh ( ) ;
7365 }
7466
7567 public ITask SetupRemote ( string remote , string remoteUrl )
@@ -88,12 +80,12 @@ public ITask SetupRemote(string remote, string remoteUrl)
8880
8981 public ITask Pull ( )
9082 {
91- return repositoryManager . Pull ( CurrentRemote . Value . Name , CurrentBranch ) ;
83+ return repositoryManager . Pull ( CurrentRemote . Value . Name , CurrentBranch ? . Name ) ;
9284 }
9385
9486 public ITask Push ( )
9587 {
96- return repositoryManager . Push ( CurrentRemote . Value . Name , CurrentBranch ) ;
88+ return repositoryManager . Push ( CurrentRemote . Value . Name , CurrentBranch ? . Name ) ;
9789 }
9890
9991 public ITask Fetch ( )
@@ -131,17 +123,19 @@ private void SetCloneUrl()
131123 OnRepositoryInfoChanged ? . Invoke ( ) ;
132124 }
133125
126+ private void RepositoryManager_OnStatusUpdated ( GitStatus status )
127+ {
128+ CurrentStatus = status ;
129+ }
130+
134131 private void RepositoryManager_OnActiveRemoteChanged ( ConfigRemote ? remote )
135132 {
136133 CurrentRemote = remote ;
137- SetCloneUrl ( ) ;
138- OnActiveRemoteChanged ? . Invoke ( CurrentRemote . HasValue ? CurrentRemote . Value . Name : null ) ;
139134 }
140135
141- private void RepositoryManager_OnActiveBranchChanged ( string branch )
136+ private void RepositoryManager_OnActiveBranchChanged ( ConfigBranch ? branch )
142137 {
143138 CurrentBranch = branch ;
144- OnActiveBranchChanged ? . Invoke ( CurrentBranch ) ;
145139 }
146140
147141 private void RepositoryManager_OnHeadChanged ( )
@@ -154,12 +148,6 @@ private void RepositoryManager_OnLocalBranchListChanged()
154148 OnLocalBranchListChanged ? . Invoke ( ) ;
155149 }
156150
157- private void RepositoryManager_OnStatusUpdated ( GitStatus status )
158- {
159- CurrentStatus = status ;
160- OnStatusUpdated ? . Invoke ( CurrentStatus ) ;
161- }
162-
163151 private void RepositoryManager_OnLocksUpdated ( IEnumerable < GitLock > locks )
164152 {
165153 CurrentLocks = locks ;
@@ -197,25 +185,51 @@ public bool Equals(IRepository other)
197185 object . Equals ( LocalPath , other . LocalPath ) ;
198186 }
199187
200- public override string ToString ( )
188+ public ConfigBranch ? CurrentBranch
201189 {
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+ }
203200 }
204-
205201 /// <summary>
206202 /// Gets the current branch of the repository.
207203 /// </summary>
208- public string CurrentBranch { get ; private set ; }
204+
205+ public string CurrentBranchName => currentBranch ? . Name ;
209206
210207 /// <summary>
211208 /// Gets the current remote of the repository.
212209 /// </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+ }
214224
215- public string Name { get ; private set ; }
216225 public UriString CloneUrl { get ; private set ; }
226+
227+ public string Name { get ; private set ; }
228+
217229 public NPath LocalPath { get ; private set ; }
230+
218231 public string Owner => CloneUrl ? . Owner ?? null ;
232+
219233 public bool IsGitHub { get { return HostAddress . IsGitHubDotCom ( CloneUrl ) ; } }
220234
221235 internal string DebuggerDisplay => String . Format (
@@ -227,10 +241,19 @@ public override string ToString()
227241 CloneUrl ,
228242 LocalPath ,
229243 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+ }
232256
233- public GitStatus CurrentStatus { get ; private set ; }
234257 public IUser User { get ; set ; }
235258 public IEnumerable < GitLock > CurrentLocks { get ; private set ; }
236259 protected static ILogging Logger { get ; } = Logging . GetLogger < Repository > ( ) ;
0 commit comments