Skip to content

Commit 9b6f798

Browse files
committed
Fix project url detection when it's a git worktree, workspace, xcproject or playground
1 parent d171043 commit 9b6f798

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

Tool/Sources/Environment/Environment.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public enum Environment {
6969
return nil
7070
}
7171

72+
#warning("TODO: Use WorkspaceXcodeWindowInspector.extractProjectURL instead.")
7273
public static var guessProjectRootURLForFile: (_ fileURL: URL) async throws -> URL = {
7374
fileURL in
7475
var currentURL = fileURL
@@ -77,10 +78,19 @@ public enum Environment {
7778
while currentURL.pathComponents.count > 1 {
7879
defer { currentURL.deleteLastPathComponent() }
7980
guard FileManager.default.fileIsDirectory(atPath: currentURL.path) else { continue }
81+
guard currentURL.pathExtension != "xcodeproj" else { continue }
82+
guard currentURL.pathExtension != "xcworkspace" else { continue }
83+
guard currentURL.pathExtension != "playground" else { continue }
8084
if firstDirectoryURL == nil { firstDirectoryURL = currentURL }
8185
let gitURL = currentURL.appendingPathComponent(".git")
8286
if FileManager.default.fileIsDirectory(atPath: gitURL.path) {
8387
lastGitDirectoryURL = currentURL
88+
} else if let text = try? String(contentsOf: gitURL) {
89+
if !text.hasPrefix("gitdir: ../"), // it's not a sub module
90+
text.range(of: "/.git/worktrees/") != nil // it's a git worktree
91+
{
92+
lastGitDirectoryURL = currentURL
93+
}
8494
}
8595
}
8696

Tool/Sources/XcodeInspector/XcodeWindowInspector.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,19 @@ public final class WorkspaceXcodeWindowInspector: XcodeWindowInspector {
115115
while currentURL.pathComponents.count > 1 {
116116
defer { currentURL.deleteLastPathComponent() }
117117
guard FileManager.default.fileIsDirectory(atPath: currentURL.path) else { continue }
118+
guard currentURL.pathExtension != "xcodeproj" else { continue }
119+
guard currentURL.pathExtension != "xcworkspace" else { continue }
120+
guard currentURL.pathExtension != "playground" else { continue }
118121
if firstDirectoryURL == nil { firstDirectoryURL = currentURL }
119122
let gitURL = currentURL.appendingPathComponent(".git")
120123
if FileManager.default.fileIsDirectory(atPath: gitURL.path) {
121124
lastGitDirectoryURL = currentURL
125+
} else if let text = try? String(contentsOf: gitURL) {
126+
if !text.hasPrefix("gitdir: ../"), // it's not a sub module
127+
text.range(of: "/.git/worktrees/") != nil // it's a git worktree
128+
{
129+
lastGitDirectoryURL = currentURL
130+
}
122131
}
123132
}
124133

0 commit comments

Comments
 (0)