# HG changeset patch # User Ludovic Chabant # Date 1456639252 28800 # Node ID 9f391ab1b4e0c6aa2371c494497c63c901439d7e # Parent a77b4656c602223200085f60a53b964bf933a6b0 admin: Add support for Git source-control. diff -r a77b4656c602 -r 9f391ab1b4e0 foodtruck/scm/git.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/foodtruck/scm/git.py Sat Feb 27 22:00:52 2016 -0800 @@ -0,0 +1,67 @@ +import os +import logging +import tempfile +import subprocess +from .base import SourceControl, RepoStatus, _s + + +logger = logging.getLogger(__name__) + + +class GitSourceControl(SourceControl): + def __init__(self, root_dir, cfg): + super(GitSourceControl, self).__init__(root_dir, cfg) + self.git = cfg.get('exe', 'git') + + def getStatus(self): + res = RepoStatus() + st_out = self._run('status', '-s') + print(st_out) + for line in st_out.split('\n'): + print(line) + if not line: + continue + if line.startswith('?? '): + path = line[3:].strip() + if path[-1] == '/': + import glob + res.new_files += [ + f for f in glob.glob(path + '**', recursive=True) + if f[-1] != '/'] + else: + res.new_files.append(path) + elif line.startswith(' M '): + res.edited_files.append(path[3:]) + print(res.__dict__) + return res + + def _doCommit(self, paths, message, author): + self._run('add', *paths) + + # Create a temp file with the commit message. + f, temp = tempfile.mkstemp() + with os.fdopen(f, 'w') as fd: + fd.write(message) + + # Commit and clean up the temp file. + try: + commit_args = list(paths) + ['-F', temp] + if author: + commit_args += ['--author="%s"' % author] + self._run('commit', *commit_args) + finally: + os.remove(temp) + + def _run(self, cmd, *args, **kwargs): + exe = [self.git] + exe.append(cmd) + exe += args + + logger.debug("Running Git: " + str(exe)) + proc = subprocess.Popen( + exe, stdout=subprocess.PIPE, cwd=self.root_dir) + out, _ = proc.communicate() + + encoded_out = _s(out) + return encoded_out +