1818#import " PBSourceViewGitStashItem.h"
1919#import " PBSidebarTableViewCell.h"
2020#import " PBGitRef.h"
21+ #import " GitX-Swift.h"
2122
2223#define PBSidebarCellIdentifier @" PBSidebarCellIdentifier"
2324
@@ -42,6 +43,9 @@ - (PBSourceViewItem *)itemForRev:(PBGitRevSpecifier *)rev;
4243- (void )removeRevSpec : (PBGitRevSpecifier *)rev ;
4344- (void )updateActionMenu ;
4445- (void )updateRemoteControls ;
46+ - (void )reloadSidebarAfterReferencesChange ;
47+
48+ @property (nonatomic ) PBGitRevSpecifier *lastKnownHeadRef;
4549@end
4650
4751@implementation PBGitSidebarController
@@ -69,6 +73,7 @@ - (void)awakeFromNib
6973 [self populateList ];
7074
7175 PBGitRepository *repository = self.repository ;
76+ self.lastKnownHeadRef = repository.headRef ;
7277
7378 [repository addObserver: self
7479 keyPath: @" currentBranch"
@@ -99,6 +104,14 @@ - (void)awakeFromNib
99104 }
100105 }];
101106
107+ [repository addObserver: self
108+ keyPath: @" refs"
109+ options: 0
110+ block: ^(MAKVONotification *notification) {
111+ PBGitSidebarController *observer = notification.observer ;
112+ [observer reloadSidebarAfterReferencesChange ];
113+ }];
114+
102115 [repository addObserver: self
103116 keyPath: @" stashes"
104117 options: 0
@@ -188,20 +201,56 @@ - (PBSourceViewItem *)addRevSpec:(PBGitRevSpecifier *)rev
188201 return item;
189202
190203 if (![rev isSimpleRef ]) {
191- [others addChild: [PBSourceViewItem itemWithRevSpec: rev]];
204+ item = [PBSourceViewItem itemWithRevSpec: rev];
205+ [others addChild: item];
192206 return item;
193207 }
194208
195209 NSArray *pathComponents = [[rev simpleRef ] componentsSeparatedByString: @" /" ];
196- if ([pathComponents count ] < 2 )
197- [branches addChild: [PBSourceViewItem itemWithRevSpec: rev]];
210+ if ([pathComponents count ] < 2 ) {
211+ item = [PBSourceViewItem itemWithRevSpec: rev];
212+ [branches addChild: item];
213+ }
198214 else if ([[pathComponents objectAtIndex: 1 ] isEqualToString: @" heads" ])
199215 [branches addRev: rev toPath: [pathComponents subarrayWithRange: NSMakeRange (2 , [pathComponents count ] - 2 )]];
200216 else if ([[rev simpleRef ] hasPrefix: @" refs/tags/" ])
201217 [tags addRev: rev toPath: [pathComponents subarrayWithRange: NSMakeRange (2 , [pathComponents count ] - 2 )]];
202218 else if ([[rev simpleRef ] hasPrefix: @" refs/remotes/" ])
203219 [remotes addRev: rev toPath: [pathComponents subarrayWithRange: NSMakeRange (2 , [pathComponents count ] - 2 )]];
204- return item;
220+ return item ?: [self itemForRev: rev];
221+ }
222+
223+ - (void )reloadSidebarAfterReferencesChange
224+ {
225+ BOOL stageSelected = [PBGitDefaults showStageView ];
226+ PBGitRevSpecifier *viewedRev = self.repository .currentBranch ;
227+ PBGitRevSpecifier *newHead = self.repository .headRef ;
228+ BOOL followHead = [PBHistoryRefreshSelectionPolicy shouldFollowCheckedOutBranchWithStageSelected: stageSelected
229+ viewedRef: viewedRev.simpleRef
230+ previousHeadRef: self .lastKnownHeadRef.simpleRef];
231+ if (followHead && newHead && ![viewedRev isEqual: newHead]) {
232+ self.repository .currentBranch = newHead;
233+ viewedRev = newHead;
234+ }
235+
236+ [sourceView reloadData ];
237+ [sourceView expandItem: items.firstObject];
238+ [sourceView expandItem: branches expandChildren: YES ];
239+ [sourceView expandItem: remotes];
240+ if (stageSelected) {
241+ [self selectStage ];
242+ } else {
243+ PBSourceViewItem *item = [self itemForRev: viewedRev];
244+ if (item) {
245+ [sourceView PBExpandItem: item expandParents: YES ];
246+ [sourceView selectRowIndexes: [NSIndexSet indexSetWithIndex: [sourceView rowForItem: item]] byExtendingSelection: NO ];
247+ }
248+ }
249+
250+ NSLog (@" [GitX] Refreshed sidebar refs: HEAD %@ -> %@ , followed=%@ , stage=%@ " ,
251+ self.lastKnownHeadRef .simpleRef ?: @" (none)" , newHead.simpleRef ?: @" (none)" ,
252+ followHead ? @" yes" : @" no" , stageSelected ? @" yes" : @" no" );
253+ self.lastKnownHeadRef = newHead;
205254}
206255
207256- (void )removeRevSpec : (PBGitRevSpecifier *)rev
0 commit comments