|
1 | 1 | import contextlib |
2 | 2 | import logging |
3 | 3 | import uuid |
4 | | -from pathlib import Path |
| 4 | +from pathlib import Path, PurePath |
5 | 5 | from threading import Lock as ThreadingLock |
6 | 6 |
|
7 | 7 | import sublime |
8 | 8 | import sublime_plugin |
9 | 9 |
|
10 | 10 | from .actions import SshKeepaliveThread |
11 | 11 | from .project_data import SshSession, remove_from_project_folders, update_window_status |
| 12 | +from .paths import mounts_path |
12 | 13 | from .ssh_utils import ssh_disconnect, umount_sshfs |
13 | 14 |
|
14 | 15 | _logger = logging.getLogger(__package__) |
|
18 | 19 | _ka_threads_lock = ThreadingLock() |
19 | 20 |
|
20 | 21 |
|
| 22 | +def disable_git_gutter_for_view(view: sublime.View) -> None: |
| 23 | + """ |
| 24 | + This function disables GitGutter from view, if it contains a file relative to SSHubl mount paths |
| 25 | + folder (which is very likely supposed to be a remote file mounted over SSHFS). |
| 26 | + This is to prevent Sublime's Git integration to keep opening file descriptors which mess with |
| 27 | + unmounting operations. |
| 28 | + It requires GitGutter v1.7.5+. |
| 29 | + """ |
| 30 | + view_file_name = view.file_name() |
| 31 | + if view_file_name is None: |
| 32 | + # file doesn't exist on disk |
| 33 | + return |
| 34 | + |
| 35 | + view_file_name_path = PurePath(view_file_name) |
| 36 | + # Python < 3.9, `PurePath.is_relative_to` doesn't exist |
| 37 | + try: |
| 38 | + view_file_name_path.relative_to(mounts_path) |
| 39 | + except ValueError: |
| 40 | + # file isn't relative to SSHFS mount points |
| 41 | + return |
| 42 | + |
| 43 | + _logger.debug( |
| 44 | + "%s is likely a remote file, disabling GitGutter from view %d...", |
| 45 | + view_file_name_path, |
| 46 | + view.id(), |
| 47 | + ) |
| 48 | + |
| 49 | + view.settings().update( |
| 50 | + { |
| 51 | + "git_gutter_enable": False, |
| 52 | + } |
| 53 | + ) |
| 54 | + |
| 55 | + |
21 | 56 | def start_ka_thread_if_needed(window: sublime.Window) -> None: |
22 | 57 | """ |
23 | 58 | This function starts a new `SshKeepaliveThread` for passed `window`, only if there isn't any at |
@@ -66,6 +101,7 @@ def on_pre_close_window(self, window): |
66 | 101 |
|
67 | 102 | class ViewEventListener(sublime_plugin.ViewEventListener): |
68 | 103 | def on_load_async(self): |
| 104 | + disable_git_gutter_for_view(self.view) |
69 | 105 | update_window_status(self.view.window()) |
70 | 106 |
|
71 | 107 |
|
|
0 commit comments