Repository: CodeEditApp/CodeEdit
File: CodeEdit/Features/CEWorkspace/Models/CEWorkspaceFileManager.swift (Swift)

Code before:
    /// - Parameter events: An array of events that occurred.
    private 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 parent = "/" + event.path.split(separator: "/").dropLast().joined(separator: "/")
                guard let parentItem = self.getFile(parent) else {
                    continue
                }

                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:
                    try? self.rebuildFiles(fromItem: parentItem)
                    files.insert(parentItem)
                }
            }
            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

Review comment (bug):
```suggestion
                        self.logger.error(
                            "Failed to rebuild files for event: \(event.eventType.rawValue), path: \(event.path, privacy: .sensitive)"
                            )
```

Code after:
    /// 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.
    private 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 parent = "/" + event.path.split(separator: "/").dropLast().joined(separator: "/")
                guard let parentItem = self.getFile(parent) else {
                    continue
                }

                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:
                    do {
                        try self.rebuildFiles(fromItem: parentItem)
                    } 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(parentItem)
                }
            }
            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"
        })