1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 from translate.storage.versioncontrol import run_command
28 from translate.storage.versioncontrol import GenericRevisionControlSystem
29 import os
30
31
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
45 """git requires the git metadata directory for every operation
46 """
47 return os.path.join(self.root_dir, self.RCS_METADIR)
48
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
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
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
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
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
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
101 """Get a clean version of a file from the git repository"""
102
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