-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix for symlinks bug #1840
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix for symlinks bug #1840
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
33a8dae
Added check for folder symlink on the file manager
armartinez c4decd7
Created new directory events file manager extension, added label to s…
armartinez 0bb6029
Merge branch 'main' into fix-symlinks
armartinez 42ef16b
Updated CodeFileDocument url to linked url when opening files
armartinez 7c3b429
Fix for the loop when saving symlinks
armartinez 147a43b
Merge branch 'main' into fix-symlinks
armartinez 4aa40de
Renamed linkedURL, fixed typo and updated isSymbolicLink property
armartinez b26307a
Restored named computed property
armartinez 6937ffa
Merge branch 'main' into fix-symlinks
armartinez 0f7afc8
Fixed typos
armartinez 9155c39
Update CodeEdit/Features/Editor/Views/EditorAreaFileView.swift
tom-ludwig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFileManager+DirectoryEvents.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
// | ||
// CEWorkspaceFileManager+DirectoryEvents.swift | ||
// CodeEdit | ||
// | ||
// Created by Axel Martinez on 5/8/24. | ||
// | ||
|
||
import Foundation | ||
|
||
/// This extension handles the file system events triggered by changes in the root folder. | ||
extension CEWorkspaceFileManager { | ||
/// Called by `fsEventStream` when an event occurs. | ||
/// | ||
/// This method may be called on a background thread, but all work done by this function will be queued on the main | ||
/// thread. | ||
/// - Parameter events: An array of events that occurred. | ||
func fileSystemEventReceived(events: [DirectoryEventStream.Event]) { | ||
DispatchQueue.main.async { | ||
var files: Set<CEWorkspaceFile> = [] | ||
for event in events { | ||
// Event returns file/folder that was changed, but in tree we need to update it's parent | ||
let parentUrl = "/" + event.path.split(separator: "/").dropLast().joined(separator: "/") | ||
// Find all folders pointing to the parent's file url. | ||
let fileItems = self.flattenedFileItems.filter({ | ||
$0.value.resolvedURL.path == parentUrl | ||
}).map { $0.value } | ||
|
||
switch event.eventType { | ||
case .changeInDirectory, .itemChangedOwner, .itemModified: | ||
// Can be ignored for now, these I think not related to tree changes | ||
continue | ||
case .rootChanged: | ||
// TODO: Handle workspace root changing. | ||
continue | ||
case .itemCreated, .itemCloned, .itemRemoved, .itemRenamed: | ||
for fileItem in fileItems { | ||
do { | ||
try self.rebuildFiles(fromItem: fileItem) | ||
} catch { | ||
// swiftlint:disable:next line_length | ||
self.logger.error("Failed to rebuild files for event: \(event.eventType.rawValue), path: \(event.path, privacy: .sensitive)") | ||
} | ||
files.insert(fileItem) | ||
} | ||
} | ||
} | ||
if !files.isEmpty { | ||
self.notifyObservers(updatedItems: files) | ||
} | ||
|
||
self.handleGitEvents(events: events) | ||
} | ||
} | ||
|
||
func handleGitEvents(events: [DirectoryEventStream.Event]) { | ||
// Changes excluding .git folder | ||
let notGitChanges = events.filter({ !$0.path.contains(".git/") }) | ||
|
||
// .git folder was changed | ||
let gitFolderChange = events.first(where: { | ||
$0.path == "\(self.folderUrl.relativePath)/.git" | ||
}) | ||
|
||
// Change made to git index file, staged/unstaged files | ||
let gitIndexChange = events.first(where: { | ||
$0.path == "\(self.folderUrl.relativePath)/.git/index" | ||
}) | ||
|
||
// Change made to git stash | ||
let gitStashChange = events.first(where: { | ||
$0.path == "\(self.folderUrl.relativePath)/.git/refs/stash" | ||
}) | ||
|
||
// Changes made to git branches | ||
let gitBranchChange = events.first(where: { | ||
$0.path.contains("\(self.folderUrl.relativePath)/.git/refs/heads") | ||
}) | ||
|
||
// Changes made to git HEAD - current branch changed | ||
let gitHeadChange = events.first(where: { | ||
$0.path.contains("\(self.folderUrl.relativePath)/.git/HEAD") | ||
}) | ||
|
||
// Change made to remotes by looking at .git/config | ||
let gitConfigChange = events.first(where: { | ||
$0.path == "\(self.folderUrl.relativePath)/.git/config" | ||
}) | ||
|
||
// If changes were made to project OR files were staged, refresh changes | ||
if !notGitChanges.isEmpty || gitIndexChange != nil { | ||
Task { | ||
await self.sourceControlManager?.refreshAllChangedFiles() | ||
} | ||
} | ||
|
||
// If changes were stashed, refresh stashed entries | ||
if gitStashChange != nil { | ||
Task { | ||
try await self.sourceControlManager?.refreshStashEntries() | ||
} | ||
} | ||
|
||
// If branches were added or removed, refresh branches | ||
if gitBranchChange != nil { | ||
Task { | ||
await self.sourceControlManager?.refreshBranches() | ||
} | ||
} | ||
|
||
// If HEAD was changed, refresh the current branch | ||
if gitHeadChange != nil { | ||
Task { | ||
await self.sourceControlManager?.refreshCurrentBranch() | ||
} | ||
} | ||
|
||
// If git config changed, refresh remotes | ||
if gitConfigChange != nil { | ||
Task { | ||
try await self.sourceControlManager?.refreshRemotes() | ||
} | ||
} | ||
|
||
// If .git folder was added or removed, check if repository is valid | ||
if gitFolderChange != nil { | ||
Task { | ||
try await self.sourceControlManager?.validate() | ||
} | ||
} | ||
} | ||
|
||
/// Creates or deletes children of the ``CEWorkspaceFile`` so that they are accurate with the file system, | ||
/// instead of creating an entirely new ``CEWorkspaceFile``. Can optionally run a deep rebuild. | ||
/// | ||
/// This method will return immediately if the given file item is not a directory. | ||
/// This will also only rebuild *already cached* directories. | ||
/// - Parameters: | ||
/// - fileItem: The ``CEWorkspaceFile`` to correct the children of | ||
/// - deep: Set to `true` if this should perform the rebuild recursively. | ||
func rebuildFiles(fromItem fileItem: CEWorkspaceFile, deep: Bool = false) throws { | ||
// Do not index directories that are not already loaded. | ||
guard childrenMap[fileItem.id] != nil else { return } | ||
|
||
// get the actual directory children | ||
let directoryContentsUrls = try fileManager.contentsOfDirectory( | ||
at: fileItem.resolvedURL, | ||
includingPropertiesForKeys: nil | ||
) | ||
|
||
// test for deleted children, and remove them from the index | ||
// Folders may or may not have slash at the end, this will normalize check | ||
let directoryContentsUrlsRelativePaths = directoryContentsUrls.map({ $0.relativePath }) | ||
for (idx, oldURL) in (childrenMap[fileItem.id] ?? []).map({ URL(filePath: $0) }).enumerated().reversed() | ||
where !directoryContentsUrlsRelativePaths.contains(oldURL.relativePath) { | ||
flattenedFileItems.removeValue(forKey: oldURL.relativePath) | ||
childrenMap[fileItem.id]?.remove(at: idx) | ||
} | ||
|
||
// test for new children, and index them | ||
for newContent in directoryContentsUrls { | ||
// if the child has already been indexed, continue to the next item. | ||
guard !ignoredFilesAndFolders.contains(newContent.lastPathComponent) && | ||
!(childrenMap[fileItem.id]?.contains(newContent.relativePath) ?? true) else { continue } | ||
|
||
if fileManager.fileExists(atPath: newContent.path) { | ||
let newFileItem = createChild(newContent, forParent: fileItem) | ||
flattenedFileItems[newFileItem.id] = newFileItem | ||
childrenMap[fileItem.id]?.append(newFileItem.id) | ||
} | ||
} | ||
|
||
childrenMap[fileItem.id] = childrenMap[fileItem.id]? | ||
.map { URL(filePath: $0) } | ||
.sortItems(foldersOnTop: true) | ||
.map { $0.relativePath } | ||
|
||
if deep && childrenMap[fileItem.id] != nil { | ||
for child in (childrenMap[fileItem.id] ?? []).compactMap({ flattenedFileItems[$0] }) { | ||
try rebuildFiles(fromItem: child) | ||
} | ||
} | ||
} | ||
|
||
/// Notify observers that an update occurred in the watched files. | ||
func notifyObservers(updatedItems: Set<CEWorkspaceFile>) { | ||
observers.allObjects.reversed().forEach { delegate in | ||
guard let delegate = delegate as? CEWorkspaceFileManagerObserver else { | ||
observers.remove(delegate) | ||
return | ||
} | ||
delegate.fileManagerUpdated(updatedItems: updatedItems) | ||
} | ||
} | ||
|
||
/// Add an observer for file system events. | ||
/// - Parameter observer: The observer to add. | ||
func addObserver(_ observer: CEWorkspaceFileManagerObserver) { | ||
observers.add(observer as AnyObject) | ||
} | ||
|
||
/// Remove an observer for file system events. | ||
/// - Parameter observer: The observer to remove. | ||
func removeObserver(_ observer: CEWorkspaceFileManagerObserver) { | ||
observers.remove(observer as AnyObject) | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.