Package translate :: Package storage :: Package versioncontrol :: Module git |
[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.versioncontrol.git

      1  #!/usr/bin/env python
      2  # -*- coding: utf-8 -*-
      3  #
      4  # Copyright 2004-2007 Zuza Software Foundation
      5  #
      6  # This file is part of translate.
      7  #
      8  # translate is free software; you can redistribute it and/or modify
      9  # it under the terms of the GNU General Public License as published by
     10  # the Free Software Foundation; either version 2 of the License, or
     11  # (at your option) any later version.
     12  #
     13  # translate is distributed in the hope that it will be useful,
     14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  # GNU General Public License for more details.
     17  #
     18  # You should have received a copy of the GNU General Public License
     19  # along with translate; if not, write to the Free Software
     20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
     21  #
     22  #
     23  # Requires: git
     24  #
     25
     26
     27  from translate.storage.versioncontrol import run_command
     28  from translate.storage.versioncontrol import GenericRevisionControlSystem
     29  import os
     30
     31
     32 -def is_available():
     33      """check if git is installed"""
     34      exitcode, output, error = run_command(["git", "--version"])
     35      return exitcode == 0
     36
     37
     38 -class git(GenericRevisionControlSystem):
     39      """Class to manage items under revision control of git."""
     40
     41      RCS_METADIR = ".git"
     42      SCAN_PARENTS = True
     43
     44 -    def _get_git_dir(self):
     45          """git requires the git metadata directory for every operation
     46          """
     47          return os.path.join(self.root_dir, self.RCS_METADIR)
     48
     49 -    def _get_git_command(self, args):
     50          """prepends generic git arguments to default ones
     51          """
     52          command = ["git", "--git-dir", self._get_git_dir()]
     53          command.extend(args)
     54          return command
     55
     56 -    def update(self, revision=None):
     57          """Does a clean update of the given path"""
     58          # git checkout
     59          command = self._get_git_command(["checkout", self.location_rel])
     60          exitcode, output_checkout, error = run_command(command, self.root_dir)
     61          if exitcode != 0:
     62              raise IOError("[GIT] checkout failed (%s): %s" % (command, error))
     63          # pull changes
     64          command = self._get_git_command(["pull"])
     65          exitcode, output_pull, error = run_command(command, self.root_dir)
     66          if exitcode != 0:
     67              raise IOError("[GIT] pull failed (%s): %s" % (command, error))
     68          return output_checkout + output_pull
     69
     70 -    def commit(self, message=None, author=None):
     71          """Commits the file and supplies the given commit message if present"""
     72          # add the file
     73          command = self._get_git_command(["add", self.location_rel])
     74          exitcode, output_add, error = run_command(command, self.root_dir)
     75          if exitcode != 0:
     76              raise IOError("[GIT] add of ('%s', '%s') failed: %s" \
     77                      % (self.root_dir, self.location_rel, error))
     78          # commit file
     79          command = self._get_git_command(["commit"])
     80          if message:
     81              command.extend(["-m", message])
     82          if author:
     83              command.extend(["--author", author])
     84          exitcode, output_commit, error = run_command(command, self.root_dir)
     85          if exitcode != 0:
     86              if len(error):
     87                  msg = error
     88              else:
     89                  msg = output_commit
     90              raise IOError("[GIT] commit of ('%s', '%s') failed: %s" \
     91                      % (self.root_dir, self.location_rel, msg))
     92          # push changes
     93          command = self._get_git_command(["push"])
     94          exitcode, output_push, error = run_command(command, self.root_dir)
     95          if exitcode != 0:
     96              raise IOError("[GIT] push of ('%s', '%s') failed: %s" \
     97                      % (self.root_dir, self.location_rel, error))
     98          return output_add + output_commit + output_push
     99
    100 -    def getcleanfile(self, revision=None):
    101          """Get a clean version of a file from the git repository"""
    102          # run git-show
    103          command = self._get_git_command(["show", "HEAD:%s" % self.location_rel])
    104          exitcode, output, error = run_command(command, self.root_dir)
    105          if exitcode != 0:
    106              raise IOError("[GIT] 'show' failed for ('%s', %s): %s" \
    107                      % (self.root_dir, self.location_rel, error))
    108          return output
    109