Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit b9358e2

Browse files
Add support for worktree to RepositoryWatcher
1 parent eac5791 commit b9358e2

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

src/GitHub.Api/Events/RepositoryWatcher.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class RepositoryWatcher : IRepositoryWatcher
3030
private readonly NPath[] ignoredPaths;
3131
private readonly ManualResetEventSlim pauseEvent;
3232
private NativeInterface nativeInterface;
33+
private NativeInterface worktreeNativeInterface;
3334
private bool running;
3435
private int lastCountOfProcessedEvents = 0;
3536
private bool processingEvents;
@@ -64,6 +65,11 @@ public void Initialize()
6465
try
6566
{
6667
nativeInterface = new NativeInterface(pathsRepositoryPath);
68+
69+
if (paths.IsWorktree)
70+
{
71+
worktreeNativeInterface = new NativeInterface(paths.WorktreeDotGitPath);
72+
}
6773
}
6874
catch (Exception ex)
6975
{
@@ -80,6 +86,18 @@ public void Start()
8086
}
8187

8288
Logger.Trace("Watching Path: \"{0}\"", paths.RepositoryPath.ToString());
89+
90+
if (paths.IsWorktree)
91+
{
92+
if (worktreeNativeInterface == null)
93+
{
94+
Logger.Warning("Worktree NativeInterface is null");
95+
throw new InvalidOperationException("Worktree NativeInterface is null");
96+
}
97+
98+
Logger.Trace("Watching Additional Path for Worktree: \"{0}\"", paths.WorktreeDotGitPath);
99+
}
100+
83101
running = true;
84102
pauseEvent.Reset();
85103
Task.Factory.StartNew(WatcherLoop, cancellationToken, TaskCreationOptions.None, TaskScheduler.Default);
@@ -131,6 +149,15 @@ public int CheckAndProcessEvents()
131149
processedEventCount = ProcessEvents(fileEvents);
132150
}
133151

152+
if (worktreeNativeInterface != null)
153+
{
154+
fileEvents = worktreeNativeInterface.GetEvents();
155+
if (fileEvents.Length > 0)
156+
{
157+
processedEventCount = processedEventCount + ProcessEvents(fileEvents);
158+
}
159+
}
160+
134161
lastCountOfProcessedEvents = processedEventCount;
135162
processingEvents = false;
136163
signalProcessingEventsDone.Set();
@@ -158,7 +185,7 @@ private int ProcessEvents(Event[] fileEvents)
158185
var fileA = eventDirectory.Combine(fileEvent.FileA);
159186

160187
// handling events in .git/*
161-
if (fileA.IsChildOf(paths.DotGitPath))
188+
if (fileA.IsChildOf(paths.DotGitPath) || (paths.WorktreeDotGitPath.IsInitialized && fileA.IsChildOf(paths.WorktreeDotGitPath)))
162189
{
163190
if (!events.Contains(EventType.ConfigChanged) && fileA.Equals(paths.DotGitConfig))
164191
{

src/GitHub.Api/Git/RepositoryManager.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,19 @@ interface IRepositoryPathConfiguration
6060
NPath DotGitIndex { get; }
6161
NPath DotGitHead { get; }
6262
NPath DotGitConfig { get; }
63+
NPath WorktreeDotGitPath { get; }
64+
bool IsWorktree { get; }
6365
}
6466

6567
class RepositoryPathConfiguration : IRepositoryPathConfiguration
6668
{
6769
public RepositoryPathConfiguration(NPath repositoryPath)
6870
{
6971
RepositoryPath = repositoryPath;
72+
WorktreeDotGitPath = NPath.Default;
7073

7174
DotGitPath = repositoryPath.Combine(".git");
72-
NPath CommonPath;
75+
NPath commonPath;
7376
if (DotGitPath.FileExists())
7477
{
7578
DotGitPath =
@@ -79,30 +82,35 @@ public RepositoryPathConfiguration(NPath repositoryPath)
7982
.First();
8083
if (DotGitPath.Combine("commondir").FileExists())
8184
{
82-
CommonPath = DotGitPath.Combine("commondir").ReadAllLines()
85+
commonPath = DotGitPath.Combine("commondir").ReadAllLines()
8386
.Select(x => x.Trim().ToNPath())
8487
.First();
85-
CommonPath = DotGitPath.Combine(CommonPath);
88+
commonPath = DotGitPath.Combine(commonPath);
89+
90+
IsWorktree = true;
91+
WorktreeDotGitPath = commonPath;
8692
}
8793
else
8894
{
89-
CommonPath = DotGitPath;
95+
commonPath = DotGitPath;
9096
}
9197
}
9298
else
9399
{
94-
CommonPath = DotGitPath;
100+
commonPath = DotGitPath;
95101
}
96102

97-
BranchesPath = CommonPath.Combine("refs", "heads");
98-
RemotesPath = CommonPath.Combine("refs", "remotes");
103+
BranchesPath = commonPath.Combine("refs", "heads");
104+
RemotesPath = commonPath.Combine("refs", "remotes");
99105
DotGitIndex = DotGitPath.Combine("index");
100106
DotGitHead = DotGitPath.Combine("HEAD");
101-
DotGitConfig = CommonPath.Combine("config");
107+
DotGitConfig = commonPath.Combine("config");
102108
DotGitCommitEditMsg = DotGitPath.Combine("COMMIT_EDITMSG");
103109
}
104110

111+
public bool IsWorktree { get; }
105112
public NPath RepositoryPath { get; }
113+
public NPath WorktreeDotGitPath { get; }
106114
public NPath DotGitPath { get; }
107115
public NPath BranchesPath { get; }
108116
public NPath RemotesPath { get; }

0 commit comments

Comments
 (0)