add "is_available" function to all version control modules
[translate_toolkit.git] / storage / versioncontrol / git.py
blobd59e83277fc4f71c69e4652191ac47d02d745afe
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.
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.
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.
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
23 # Requires: git v1.5.3 (or higher)
24 # For git v1.5.2 take a look at http://bugs.locamotion.org/show_bug.cgi?id=347
28 from translate.storage.versioncontrol import run_command
29 from translate.storage.versioncontrol import GenericRevisionControlSystem
32 def is_available():
33 """check if git is installed"""
34 exitcode, output_revert, error = run_command(["git", "--version"])
35 return exitcode == 0
38 class git(GenericRevisionControlSystem):
39 """Class to manage items under revision control of git."""
41 RCS_METADIR = ".git"
42 SCAN_PARENTS = True
44 def _get_git_dir(self):
45 """git requires the git metadata directory for every operation
46 """
47 import os
48 return os.path.join(self.root_dir, self.RCS_METADIR)
50 def _get_git_command(self, args):
51 """prepends generic git arguments to conrete ones
52 """
53 command = ["git", "--git-dir", self._get_git_dir(), "--work-tree", self.root_dir]
54 command.extend(args)
55 return command
57 def update(self, revision=None):
58 """Does a clean update of the given path"""
59 # git checkout
60 command = self._get_git_command(["checkout", self.location_rel])
61 exitcode, output_checkout, error = run_command(command)
62 if exitcode != 0:
63 raise IOError("[GIT] checkout failed (%s): %s" % (command, error))
64 # pull changes
65 command = self._get_git_command(["pull"])
66 exitcode, output_pull, error = run_command(command)
67 if exitcode != 0:
68 raise IOError("[GIT] pull failed (%s): %s" % (command, error))
69 return output_checkout + output_pull
71 def commit(self, message=None):
72 """Commits the file and supplies the given commit message if present"""
73 # add the file
74 command = self._get_git_command(["add", self.location_rel])
75 exitcode, output_add, error = run_command(command)
76 if exitcode != 0:
77 raise IOError("[GIT] add of ('%s', '%s') failed: %s" \
78 % (self.root_dir, self.location_rel, error))
79 # commit file
80 command = self._get_git_command(["commit"])
81 if message:
82 command.extend(["-m", message])
83 exitcode, output_commit, error = run_command(command)
84 if exitcode != 0:
85 if len(error):
86 msg = error
87 else:
88 msg = output_commit
89 raise IOError("[GIT] commit of ('%s', '%s') failed: %s" \
90 % (self.root_dir, self.location_rel, msg))
91 # push changes
92 command = self._get_git_command(["push"])
93 exitcode, output_push, error = run_command(command)
94 if exitcode != 0:
95 raise IOError("[GIT] push of ('%s', '%s') failed: %s" \
96 % (self.root_dir, self.location_rel, error))
97 return output_add + output_commit + output_push
99 def getcleanfile(self, revision=None):
100 """Get a clean version of a file from the git repository"""
101 # run git-show
102 command = self._get_git_command(["show", "HEAD:%s" % self.location_rel])
103 exitcode, output, error = run_command(command)
104 if exitcode != 0:
105 raise IOError("[GIT] 'show' failed for ('%s', %s): %s" \
106 % (self.root_dir, self.location_rel, error))
107 return output