@@ -13,41 +13,79 @@ public string RemoteUrl
1313 get => _remoteUrl ;
1414 private set
1515 {
16+ if ( _remoteUrl == value )
17+ return ;
18+
1619 _remoteUrl = value ;
1720 RemoteUrlChanged ? . Invoke ( value ) ;
1821 }
1922 }
2023
24+ public string BranchName
25+ {
26+ get => _branchName ;
27+ private set
28+ {
29+ if ( _branchName == value )
30+ return ;
31+
32+ _branchName = value ;
33+ BranchNameChanged ? . Invoke ( value ) ;
34+ }
35+ }
36+
2137 public event Action < string > RemoteUrlChanged ;
38+ public event Action < string > BranchNameChanged ;
2239
2340 private VsObserver _vsObserver ;
41+ private Repository _lastRepository ;
42+
2443 private string _remoteUrl ;
44+ private string _branchName ;
2545
2646 public GitObserver ( VsObserver vsObserver )
2747 {
2848 _vsObserver = vsObserver ;
49+ OnWindowChanged ( vsObserver . DTE . ActiveWindow ) ;
2950 OnSolutionChanged ( vsObserver . DTE . Solution ) ;
3051 }
3152
53+ private void OnWindowChanged ( Window window )
54+ {
55+ UpdateBranchName ( ) ;
56+ }
57+
3258 private void OnSolutionChanged ( Solution solution )
3359 {
3460 Microsoft . VisualStudio . Shell . ThreadHelper . ThrowIfNotOnUIThread ( ) ;
3561
3662 string repositoryPath = solution . FullName ;
3763 if ( string . IsNullOrEmpty ( repositoryPath ) )
3864 {
39- RemoteUrl = string . Empty ;
65+ _lastRepository = null ;
4066 return ;
4167 }
4268
4369 string repositoryName = Path . GetDirectoryName ( repositoryPath ) ;
4470 if ( ! Repository . IsValid ( repositoryName ) )
71+ {
72+ _lastRepository = null ;
73+ return ;
74+ }
75+
76+ _lastRepository = new Repository ( repositoryName ) ;
77+ UpdateRemoteUrl ( ) ;
78+ }
79+
80+ private void UpdateRemoteUrl ( )
81+ {
82+ if ( _lastRepository == null )
4583 {
4684 RemoteUrl = string . Empty ;
4785 return ;
4886 }
4987
50- Remote firstRemote = new Repository ( repositoryName ) . Network . Remotes . FirstOrDefault ( ) ;
88+ Remote firstRemote = _lastRepository . Network . Remotes . FirstOrDefault ( ) ;
5189 if ( firstRemote == null )
5290 {
5391 RemoteUrl = string . Empty ;
@@ -57,13 +95,26 @@ private void OnSolutionChanged(Solution solution)
5795 RemoteUrl = firstRemote . Url ;
5896 }
5997
98+ private void UpdateBranchName ( )
99+ {
100+ if ( _lastRepository == null )
101+ {
102+ BranchName = string . Empty ;
103+ return ;
104+ }
105+
106+ BranchName = _lastRepository . Head . FriendlyName ;
107+ }
108+
60109 public void Observe ( )
61110 {
111+ _vsObserver . WindowChanged += OnWindowChanged ;
62112 _vsObserver . SolutionChanged += OnSolutionChanged ;
63113 }
64114
65115 public void Unobserve ( )
66116 {
117+ _vsObserver . WindowChanged -= OnWindowChanged ;
67118 _vsObserver . SolutionChanged -= OnSolutionChanged ;
68119 }
69120 }
0 commit comments