-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheasy_commit.py
More file actions
165 lines (137 loc) · 6.64 KB
/
Copy patheasy_commit.py
File metadata and controls
165 lines (137 loc) · 6.64 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import sublime
import sublime_plugin
import subprocess
import threading
import os
class EasyCommitAllCommand(sublime_plugin.TextCommand):
def run(self, edit):
threading.Thread(target=self.easy_commit_all).start()
def easy_commit_all(self):
working_dir = self.view.window().extract_variables()['folder']
self.update_status('Performing fetch...')
if self.run_git_command(['git', 'fetch'], working_dir):
self.update_status('Fetch successfully completed!')
if self.run_git_command(['git', 'pull'], working_dir) and self.check_for_changes(working_dir):
if self.run_git_command(['git', 'add', '-A'], working_dir):
sublime.set_timeout(lambda: self.request_commit_message(working_dir), 0)
else:
self.display_error_message("Failed to add changes.")
else:
self.display_error_message("Failed to perform the pull.")
else:
self.display_error_message("Failed to perform the fetch.")
def request_commit_message(self, working_dir):
self.update_status("Awaiting commit message...")
self.view.window().show_input_panel(
"Commit Message:", "",
lambda s: self.commit(s, working_dir), None,
lambda: self.cancel_commit(working_dir))
def commit(self, message, working_dir):
if message:
self.update_status('Commit message received!')
if not self.run_git_command(['git', 'commit', '-m', message], working_dir):
self.display_error_message("Failed to perform the commit.")
return
self.update_status('Sending your changes...')
if not self.run_git_command(['git', 'push'], working_dir):
self.display_error_message("Failed to send changes.")
return
self.update_status('Success!')
sublime.set_timeout(self.erase_status, 5000)
else:
self.cancel_commit(working_dir)
def cancel_commit(self, working_dir):
self.update_status("Commit canceled.")
self.undo_add(working_dir)
sublime.set_timeout(self.erase_status, 5000)
def undo_add(self, working_dir):
self.run_git_command(['git', 'reset'], working_dir)
def check_for_changes(self, working_dir):
try:
output = subprocess.check_output(['git', 'status', '--porcelain'], cwd=working_dir)
return len(output.strip()) > 0
except subprocess.CalledProcessError as e:
self.display_error_message("Error checking for changes: " + str(e))
return False
def run_git_command(self, command, working_dir):
try:
subprocess.check_output(command, cwd=working_dir)
return True
except subprocess.CalledProcessError as e:
return False
def display_error_message(self, message):
sublime.error_message(message)
self.erase_status()
def update_status(self, message):
sublime.set_timeout(lambda: self.view.set_status('easy_commit', message), 0)
def erase_status(self):
sublime.set_timeout(lambda: self.view.erase_status('easy_commit'), 0)
class EasyCommitFileCommand(sublime_plugin.TextCommand):
def run(self, edit):
threading.Thread(target=self.easy_commit_file).start()
def easy_commit_file(self):
working_dir = self.view.window().extract_variables()['folder']
file_name = self.view.file_name()
if file_name:
self.update_status('Performing fetch...')
if self.run_git_command(['git', 'fetch'], working_dir):
self.update_status('Fetch successfully completed!')
if self.check_for_changes(working_dir): # Verifica por mudanças ou arquivos novos aqui
# Mudança específica: Adiciona o arquivo atualmente aberto
if not self.run_git_command(['git', 'add', file_name], working_dir):
self.display_error_message("Failed to add current file.")
return
# Solicita mensagem de commit depois de adicionar o arquivo
sublime.set_timeout(lambda: self.request_commit_message(working_dir), 0)
else:
self.display_error_message("No changes detected.")
else:
self.display_error_message("Failed to perform the fetch.")
else:
self.display_error_message("No file selected.")
def request_commit_message(self, working_dir):
self.update_status("Awaiting commit message...")
self.view.window().show_input_panel(
"Commit Message:", "",
lambda s: self.commit(s, working_dir), None,
lambda: self.cancel_commit(working_dir))
def commit(self, message, working_dir):
if message:
self.update_status('Commit message received!')
if not self.run_git_command(['git', 'commit', '-m', message], working_dir):
self.display_error_message("Failed to perform the commit.")
return
self.update_status('Sending your changes...')
if not self.run_git_command(['git', 'push'], working_dir):
self.display_error_message("Failed to send changes.")
return
self.update_status('Success!')
sublime.set_timeout(self.erase_status, 5000)
else:
self.cancel_commit(working_dir)
def cancel_commit(self, working_dir):
self.update_status("Commit canceled.")
self.undo_add(working_dir)
sublime.set_timeout(self.erase_status, 5000)
def undo_add(self, working_dir):
self.run_git_command(['git', 'reset'], working_dir)
def check_for_changes(self, working_dir):
try:
output = subprocess.check_output(['git', 'status', '--porcelain'], cwd=working_dir)
return len(output.strip()) > 0
except subprocess.CalledProcessError as e:
self.display_error_message("Error checking for changes: " + str(e))
return False
def run_git_command(self, command, working_dir):
try:
subprocess.check_output(command, cwd=working_dir)
return True
except subprocess.CalledProcessError as e:
return False
def display_error_message(self, message):
sublime.error_message(message)
self.erase_status()
def update_status(self, message):
sublime.set_timeout(lambda: self.view.set_status('easy_commit', message), 0)
def erase_status(self):
sublime.set_timeout(lambda: self.view.erase_status('easy_commit'), 0)