Permalink
Browse files

Add git commit capabilities back as requested

  • Loading branch information...
1 parent 4b4aa59 commit 00c9b005757f3b876460541d1eac0813d65aff25 @flavioamieiro committed Mar 15, 2010
Showing with 45 additions and 2 deletions.
  1. +45 −2 dojotools.py
View
@@ -26,6 +26,7 @@
import sys
import re
import subprocess
+from time import ctime
from optparse import OptionParser
import gtk
import gobject
@@ -59,7 +60,7 @@ def update(self):
class Monitor(object):
- def __init__(self, ui, directory, commands, patterns_file):
+ def __init__(self, ui, directory, commands, patterns_file, commit=False):
"""
'directory' is the directory to be watched for changes.
@@ -77,6 +78,7 @@ def __init__(self, ui, directory, commands, patterns_file):
self.commands = commands
self.patterns = self._get_patterns(patterns_file)
self.ui = ui
+ self.commit = commit
gobject.timeout_add(1000, self.check)
@@ -117,6 +119,23 @@ def _filter_files(self, files):
files = [f for f in files if not p.match(f)]
return files
+ def git_commit_all(self):
+ """
+ Adds all files and commits them using git
+ """
+ msg = ctime()
+ process = subprocess.Popen(
+ "git add . && git commit -m '%s'" % msg,
+ shell=True,
+ cwd=self.directory,
+ )
+
+ #if git returns 128 it means 'command not found' or 'not a git repo'
+ if process.wait() == 128:
+ error = ('Impossible to commit to repository. '
+ 'Make sure git is installed an this is a valid repository')
+ raise OSError(error)
+
def run_command(self, test_cmd):
"""
As the name says, runs a command and waits for it to finish
@@ -143,6 +162,11 @@ def check(self):
"""
m_time_list = []
for root, dirs, files in os.walk(self.directory):
+ # We must ignore all the files in .git directory because
+ # any commit changes them. Taking this directory into
+ # consideration would cause an infinite loop.
+ if '.git' in root:
+ continue
files = self._filter_files(files)
# Be careful. The += operator works as the extend method
# on mutable objects. For more information refer to
@@ -155,6 +179,8 @@ def check(self):
if new_sum != self.old_sum:
for command in self.commands:
self.run_command(command)
+ if self.commit:
+ self.git_commit_all()
self.old_sum = new_sum
# This method must return True so gobject.timeout_add runs it again
@@ -170,6 +196,17 @@ def parse_options():
to use quotes if you command has spaces in it.
""".replace(' ', '')
parser = OptionParser(usage, description=description)
+ parser.add_option(
+ '-c',
+ '--commit',
+ action='store_true',
+ dest = 'commit',
+ help = (
+ 'if this flag is used, a git commit will '
+ 'be issued whenever the files change'
+ ),
+ default = False,
+ )
parser.add_option(
'-d',
'--directory',
@@ -226,7 +263,13 @@ def parse_options():
timer = Timer(options.round_time)
ui = UserInterface(timer)
- monitor = Monitor(ui, options.directory, args, options.patterns_file)
+ monitor = Monitor(
+ ui,
+ options.directory,
+ args,
+ options.patterns_file,
+ options.commit,
+ )
gtk.main()

0 comments on commit 00c9b00

Please sign in to comment.