Last active
February 20, 2017 01:56
-
-
Save andyiac/4036864aadc75229d6b8be2ed195989a to your computer and use it in GitHub Desktop.
sublime git 自动 commit push 插件,需要提前安装 Git 插件 ,源于GitAutoCommit插件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import tempfile | |
import os | |
import glob | |
import urllib | |
from threading import Timer | |
import sublime | |
import sublime_plugin | |
import Git.git_commands | |
# 判断当前目录下 .sublime-text-git-autocommit 文件是否存在 | |
def is_auto_commit_file_exists(root, parent = ""): | |
print("======root====>>> " + root) | |
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 | |
# 判断当前目录下 或父 目录中 .sublime-text-git-autocommit 文件是否存在 | |
def is_auto_commit_file_exists_in_root(root, parent = ""): | |
print("======root====>>> " + root) | |
if root == parent: | |
return False | |
root_array = root.split('/') | |
for x in root_array: | |
marker_file = '/'.join(root_array) + "/.sublime-text-git-autocommit" | |
print("marker_file=====>>>" + marker_file) | |
if os.path.exists(marker_file): | |
return True | |
else: | |
root_array.pop() | |
# 判断是否有可用的网络连接 | |
def is_internet_on(): | |
try: | |
urllib.request.urlopen('http://www.baidu.com') | |
return True | |
except urllib.request.URLError: | |
return False | |
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()))) | |
return is_auto_commit_file_exists_in_root(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) | |
self.git_pull() | |
def git_pull(self): | |
if not is_internet_on(): | |
return | |
self.run_command(['git', 'pull', 'origin', 'master'], | |
callback=self.git_push) | |
def git_push(self, output): | |
self.run_command(['git', 'push', 'origin', 'master']) | |
''' | |
Inspired by: | |
https://github.com/jamesfzhang/auto-save | |
''' | |
class AutoCommitListener(sublime_plugin.EventListener): | |
save_queue = [] # Save queue for on_modified events. | |
saveCount = 0 | |
def on_post_save(self, view): | |
if not is_file_from_autocommit_list(view): | |
return | |
self.saveCount = self.saveCount + 1 | |
if self.saveCount % 9 == 0 : | |
print("------press save -----------" + self.saveCount) | |
view.run_command("silent_add_with_commit") | |
self.save_queue = [] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
脚本有待优化,坚听某一目录下文件变化会好一些