2 # -*- coding: utf-8 -*-
4 # Copyright 2004-2007 Zuza Software Foundation
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
33 """check if git is installed"""
34 exitcode
, output_revert
, error
= run_command(["git", "--version"])
38 class git(GenericRevisionControlSystem
):
39 """Class to manage items under revision control of git."""
44 def _get_git_dir(self
):
45 """git requires the git metadata directory for every operation
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
53 command
= ["git", "--git-dir", self
._get
_git
_dir
(), "--work-tree", self
.root_dir
]
57 def update(self
, revision
=None):
58 """Does a clean update of the given path"""
60 command
= self
._get
_git
_command
(["checkout", self
.location_rel
])
61 exitcode
, output_checkout
, error
= run_command(command
)
63 raise IOError("[GIT] checkout failed (%s): %s" % (command
, error
))
65 command
= self
._get
_git
_command
(["pull"])
66 exitcode
, output_pull
, error
= run_command(command
)
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"""
74 command
= self
._get
_git
_command
(["add", self
.location_rel
])
75 exitcode
, output_add
, error
= run_command(command
)
77 raise IOError("[GIT] add of ('%s', '%s') failed: %s" \
78 % (self
.root_dir
, self
.location_rel
, error
))
80 command
= self
._get
_git
_command
(["commit"])
82 command
.extend(["-m", message
])
83 exitcode
, output_commit
, error
= run_command(command
)
89 raise IOError("[GIT] commit of ('%s', '%s') failed: %s" \
90 % (self
.root_dir
, self
.location_rel
, msg
))
92 command
= self
._get
_git
_command
(["push"])
93 exitcode
, output_push
, error
= run_command(command
)
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"""
102 command
= self
._get
_git
_command
(["show", "HEAD:%s" % self
.location_rel
])
103 exitcode
, output
, error
= run_command(command
)
105 raise IOError("[GIT] 'show' failed for ('%s', %s): %s" \
106 % (self
.root_dir
, self
.location_rel
, error
))