@@ -508,11 +508,28 @@ public Repository(bool isBare, string path, string gitDir)
508
508
IsBare = isBare ;
509
509
FullPath = path ;
510
510
GitDir = gitDir ;
511
+
512
+ var commonDirFile = Path . Combine ( _gitDir , "commondir" ) ;
513
+ _isWorktree = _gitDir . Replace ( '\\ ' , '/' ) . IndexOf ( "/worktrees/" , StringComparison . Ordinal ) > 0 &&
514
+ File . Exists ( commonDirFile ) ;
515
+
516
+ if ( _isWorktree )
517
+ {
518
+ var commonDir = File . ReadAllText ( commonDirFile ) . Trim ( ) ;
519
+ if ( ! Path . IsPathRooted ( commonDir ) )
520
+ commonDir = new DirectoryInfo ( Path . Combine ( _gitDir , commonDir ) ) . FullName ;
521
+
522
+ _gitCommonDir = commonDir ;
523
+ }
524
+ else
525
+ {
526
+ _gitCommonDir = _gitDir ;
527
+ }
511
528
}
512
529
513
530
public void Open ( )
514
531
{
515
- var settingsFile = Path . Combine ( _gitDir , "sourcegit.settings" ) ;
532
+ var settingsFile = Path . Combine ( _gitCommonDir , "sourcegit.settings" ) ;
516
533
if ( File . Exists ( settingsFile ) )
517
534
{
518
535
try
@@ -532,16 +549,7 @@ public void Open()
532
549
533
550
try
534
551
{
535
- // For worktrees, we need to watch the $GIT_COMMON_DIR instead of the $GIT_DIR.
536
- var gitDirForWatcher = _gitDir ;
537
- if ( _gitDir . Replace ( '\\ ' , '/' ) . IndexOf ( "/worktrees/" , StringComparison . Ordinal ) > 0 )
538
- {
539
- var commonDir = new Commands . QueryGitCommonDir ( _fullpath ) . GetResult ( ) ;
540
- if ( ! string . IsNullOrEmpty ( commonDir ) )
541
- gitDirForWatcher = commonDir ;
542
- }
543
-
544
- _watcher = new Models . Watcher ( this , _fullpath , gitDirForWatcher ) ;
552
+ _watcher = new Models . Watcher ( this , _fullpath , _gitCommonDir ) ;
545
553
}
546
554
catch ( Exception ex )
547
555
{
@@ -570,16 +578,12 @@ public void Close()
570
578
SelectedView = null ; // Do NOT modify. Used to remove exists widgets for GC.Collect
571
579
Logs . Clear ( ) ;
572
580
573
- try
581
+ if ( ! _isWorktree )
574
582
{
575
583
_settings . LastCommitMessage = _workingCopy . CommitMessage ;
576
- using var stream = File . Create ( Path . Combine ( _gitDir , "sourcegit.settings" ) ) ;
584
+ using var stream = File . Create ( Path . Combine ( _gitCommonDir , "sourcegit.settings" ) ) ;
577
585
JsonSerializer . Serialize ( stream , _settings , JsonCodeGen . Default . RepositorySettings ) ;
578
586
}
579
- catch
580
- {
581
- // Ignore
582
- }
583
587
584
588
_autoFetchTimer . Dispose ( ) ;
585
589
_autoFetchTimer = null ;
@@ -2007,6 +2011,8 @@ private void AutoFetchInBackground(object sender)
2007
2011
2008
2012
private string _fullpath = string . Empty ;
2009
2013
private string _gitDir = string . Empty ;
2014
+ private string _gitCommonDir = string . Empty ;
2015
+ private bool _isWorktree = false ;
2010
2016
private Models . RepositorySettings _settings = null ;
2011
2017
private Models . FilterMode _historiesFilterMode = Models . FilterMode . None ;
2012
2018
private bool _hasAllowedSignersFile = false ;
0 commit comments