fix git support for v1.5.3 (or higher) by setting "--work-tree"
[translate_toolkit.git] / storage / versioncontrol / git.py
blob4e61874cd70d3d4c758c3a5e75d1d44a0bd8da10
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 v.1.5.3
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
31 class git(GenericRevisionControlSystem):
32 """Class to manage items under revision control of git."""
34 RCS_METADIR = ".git"
35 SCAN_PARENTS = True
37 def _get_git_dir(self):
38 """git requires the git metadata directory for every operation
39 """
40 import os
41 return os.path.join(self.root_dir, self.RCS_METADIR)
43 def _get_git_command(self, args):
44 """prepends generic git arguments to conrete ones
45 """
46 command = ["git", "--git-dir", self._get_git_dir(), "--work-tree", self.root_dir]
47 command.extend(args)
48 return command
50 def update(self, revision=None):
51 """Does a clean update of the given path"""
52 # git checkout
53 command = self._get_git_command(["checkout", self.location_rel])
54 exitcode, output_checkout, error = run_command(command)
55 if exitcode != 0:
56 raise IOError("[GIT] checkout failed (%s): %s" % (command, error))
57 # pull changes
58 command = self._get_git_command(["pull"])
59 exitcode, output_pull, error = run_command(command)
60 if exitcode != 0:
61 raise IOError("[GIT] pull failed (%s): %s" % (command, error))
62 return output_checkout + output_pull
64 def commit(self, message=None):
65 """Commits the file and supplies the given commit message if present"""
66 # add the file
67 command = self._get_git_command(["add", self.location_rel])
68 exitcode, output_add, error = run_command(command)
69 if exitcode != 0:
70 raise IOError("[GIT] add of ('%s', '%s') failed: %s" \
71 % (self.root_dir, self.location_rel, error))
72 # commit file
73 command = self._get_git_command(["commit"])
74 if message:
75 command.extend(["-m", message])
76 exitcode, output_commit, error = run_command(command)
77 if exitcode != 0:
78 if len(error):
79 msg = error
80 else:
81 msg = output_commit
82 raise IOError("[GIT] commit of ('%s', '%s') failed: %s" \
83 % (self.root_dir, self.location_rel, msg))
84 # push changes
85 command = self._get_git_command(["push"])
86 exitcode, output_push, error = run_command(command)
87 if exitcode != 0:
88 raise IOError("[GIT] push of ('%s', '%s') failed: %s" \
89 % (self.root_dir, self.location_rel, error))
90 return output_add + output_commit + output_push
92 def getcleanfile(self, revision=None):
93 """Get a clean version of a file from the git repository"""
94 # run git-show
95 command = self._get_git_command(["show", "HEAD:%s" % self.location_rel])
96 exitcode, output, error = run_command(command)
97 if exitcode != 0:
98 raise IOError("[GIT] 'show' failed for ('%s', %s): %s" \
99 % (self.root_dir, self.location_rel, error))
100 return output