-
Notifications
You must be signed in to change notification settings - Fork 10
/
git-autocommit.py
97 lines (75 loc) · 2.46 KB
/
git-autocommit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import functools
import tempfile
import os
from threading import Timer
import sublime
import sublime_plugin
import Git.git_commands
def is_auto_commit_file_exists(root, parent = ""):
if root == parent:
return False
marker_file = root + "/.sublime-text-git-autocommit"
if not os.path.exists(marker_file):
return is_auto_commit_file_exists(os.path.dirname(root), root)
else:
return True
def is_file_from_autocommit_list(view):
# Right now all files from folder with special "marker"-file considered to be auto-committed
# TODO: Maybe put a list of auto-committed files to the "marker"-file? One filename per line
return is_auto_commit_file_exists(os.path.realpath(os.path.dirname(view.file_name())))
'''
Inspired by:
https://github.com/kemayo/sublime-text-git
'''
class SilentAddWithCommitCommand(Git.git.GitTextCommand):
def __init__(self, view):
self.view = view
def run(self, edit):
if not is_file_from_autocommit_list(self.view):
return
self.run_command(['git', 'add', self.get_file_name()],
functools.partial(self.add_done, "Auto-committing \'" + os.path.basename(self.get_file_name()) + "\'"))
def add_done(self, message, result):
if result.strip():
sublime.error_message("Error adding file:\n" + result)
return
self.run_command(['git', 'commit', '-m', message],
callback=self.update_status)
def update_status(self, output, **kwargs):
sublime.status_message(output)
'''
Inspired by:
https://github.com/jamesfzhang/auto-save
'''
class AutoCommitListener(sublime_plugin.EventListener):
save_queue = [] # Save queue for on_modified events.
def on_modified(self, view):
if not is_file_from_autocommit_list(view):
return
# auto-commit on_modified after...
delay = 30 # seconds
'''
Must use this callback for ST2 compatibility
'''
def callback():
view.run_command("silent_add_with_commit")
'''
If the queue is longer than 1, pop the last item off,
Otherwise save and reset the queue.
'''
def debounce_save():
if len(self.save_queue) > 1:
self.save_queue.pop()
elif len(self.save_queue) == 0:
return
else:
sublime.set_timeout(callback, 0)
self.save_queue = []
if view.file_name():
self.save_queue.append(0) # Append to queue for every on_modified event.
Timer(delay, debounce_save).start() # Debounce save by the specified delay.
def on_post_save(self, view):
if not is_file_from_autocommit_list(view):
return
view.run_command("silent_add_with_commit")
self.save_queue = []