Skip to content

Commit c0965a6

Browse files
committed
Merge pull request #430 from Jonnymcc/add-command-lock
Use threading lock object instead of commands_working counter
2 parents 0f7701a + d6fdeb8 commit c0965a6

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

git.py

+5-16
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,10 @@ def find_git():
134134
return git_path
135135
GIT = find_git()
136136

137-
commands_working = 0
138-
def are_commands_working():
139-
return commands_working != 0
140137

141138
class CommandThread(threading.Thread):
139+
command_lock = threading.Lock()
140+
142141
def __init__(self, command, on_done, working_dir="", fallback_encoding="", **kwargs):
143142
threading.Thread.__init__(self)
144143
self.command = command
@@ -156,12 +155,11 @@ def __init__(self, command, on_done, working_dir="", fallback_encoding="", **kwa
156155
self.kwargs = kwargs
157156

158157
def run(self):
159-
global commands_working
160158
# Ignore directories that no longer exist
161159
if not os.path.isdir(self.working_dir):
162160
return
163161

164-
commands_working = commands_working + 1
162+
self.command_lock.acquire()
165163
output = ''
166164
callback = self.on_done
167165
try:
@@ -200,7 +198,7 @@ def run(self):
200198
else:
201199
output = e.strerror
202200
finally:
203-
commands_working = commands_working - 1
201+
self.command_lock.release()
204202
main_thread(callback, output, **self.kwargs)
205203

206204
class GitScratchOutputCommand(sublime_plugin.TextCommand):
@@ -216,23 +214,14 @@ class GitCommand(object):
216214
may_change_files = False
217215

218216
def run_command(self, command, callback=None, show_status=True,
219-
filter_empty_args=True, no_save=False, wait_for_lock=True, **kwargs):
217+
filter_empty_args=True, no_save=False, **kwargs):
220218
if filter_empty_args:
221219
command = [arg for arg in command if arg]
222220
if 'working_dir' not in kwargs:
223221
kwargs['working_dir'] = self.get_working_dir()
224222
if 'fallback_encoding' not in kwargs and self.active_view() and self.active_view().settings().get('fallback_encoding'):
225223
kwargs['fallback_encoding'] = self.active_view().settings().get('fallback_encoding').rpartition('(')[2].rpartition(')')[0]
226224

227-
root = git_root(self.get_working_dir())
228-
if wait_for_lock and root and os.path.exists(os.path.join(root, '.git', 'index.lock')):
229-
print("waiting for index.lock", command)
230-
do_when(lambda: not os.path.exists(os.path.join(root, '.git', 'index.lock')),
231-
self.run_command, command, callback=callback,
232-
show_status=show_status, filter_empty_args=filter_empty_args,
233-
no_save=no_save, wait_for_lock=wait_for_lock, **kwargs)
234-
return
235-
236225
s = sublime.load_settings("Git.sublime-settings")
237226
if s.get('save_first') and self.active_view() and self.active_view().is_dirty() and not no_save:
238227
self.active_view().run_command('save')

statusbar.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import sublime
44
import sublime_plugin
5-
from .git import GitTextCommand, do_when, are_commands_working
5+
from .git import GitTextCommand
66

77

88
class GitBranchStatusListener(sublime_plugin.EventListener):
@@ -21,10 +21,7 @@ def run(self, view):
2121
else:
2222
self.view.set_status("git-branch", "")
2323
if (s.get("statusbar_status")):
24-
do_when(
25-
lambda: not are_commands_working(),
26-
self.run_command,
27-
['git', 'status', '--porcelain'], self.status_done, show_status=False, no_save=True)
24+
self.run_command(['git', 'status', '--porcelain'], self.status_done, show_status=False, no_save=True)
2825
else:
2926
self.view.set_status("git-status", "")
3027

0 commit comments

Comments
 (0)